I am working on a data aggregator implementation with nested lambda expressions. I am not very experienced with lambda functions yet and I am not 100% sure whether my implementation idea is implementable.
The Problem: I have a Multivalue class which has a vector as a private member. The data type of this private vector can be defined via a template parameter of the Multivalue class. I want to offer different aggregation functions (sum, harmonic mean, average, ...) for the data in the vector. BUT: if the vector data type is a tuple, the aggregation functions should also be available for the components of the tuple. The following link shows my first try, which is not working yet. I hope someone can explain to me what the problem is. I think there is a problem with the nested lambda expressions, I use:
#include <iostream>
#include <vector>
#include <functional>
#include <tuple>
using namespace std;
template<typename ... TTypes>
std::ostream& operator<<(std::ostream& out, const std::tuple<TTypes...>& value) { return out; }
/** our general abstract aggregation object */
template<typename T, typename C>
class AbstractAggregator {
public:
AbstractAggregator() { }
AbstractAggregator(const std::vector<C> *data, std::function<const T&(const C&)> access) :
data(data), access(access) { }
T sum() const {
T sum;
for (auto &i : *data)
sum += access(i);
return sum;
}
protected:
const std::vector<C> *data;
std::function<const T&(const C&)> access;
};
/** concrete aggregation implementation for types like int, float, double .. */
template<typename T, typename C>
class Aggregator : public AbstractAggregator<T, C> {
public:
Aggregator() { }
Aggregator(const std::vector<C> *data, std::function<const T&(const C&)> access) : AbstractAggregator<T, C>(data, access) { }
};
/** aggregator implementation for tuple (with subaggregators for each component */
template<typename ... TTypes, typename C>
class Aggregator<std::tuple<TTypes...>, C> : public AbstractAggregator<std::tuple<TTypes...>, C> {
public:
Aggregator() { }
Aggregator(const std::vector<C> *data, std::function<const std::tuple<TTypes...>&(const C&)> access) : AbstractAggregator<std::tuple<TTypes...>, C>(data, access) {
initSubAggregators<sizeof...(TTypes), TTypes...>(access);
}
std::tuple<Aggregator<TTypes, C>...> subaggregators;
private:
template<int N>
void initSubAggregators(std::function<const std::tuple<TTypes...>&(const C&)> access) { }
template<int N, typename THead, typename... TTail>
void initSubAggregators(std::function<const std::tuple<TTypes...>&(const C&)> access) {
constexpr int I = N - sizeof...(TTail) - 1;
std::get<I>(subaggregators) = Aggregator<THead, C>(AbstractAggregator<std::tuple<TTypes...>, C>::data, [&](const C &value) { return std::get<I>(access(value)); });
initSubAggregators<N, TTail...>(access);
}
};
namespace std {
template<size_t I, typename ... TTypes, typename C>
auto get(Aggregator<std::tuple<TTypes...>, C>& k) -> decltype(std::get<I>(k.subaggregators)) {
return std::get<I>(k.subaggregators);
}
template<size_t I, typename ... TTypes, typename C>
auto get(const Aggregator<std::tuple<TTypes...>, C>& k) -> decltype(std::get<I>(k.subaggregators)) {
return std::get<I>(k.subaggregators);
}
}
/** multivalue attribute implementation (inherits corresponding aggregation object) */
template<typename T>
class Multivalue : public Aggregator<T, T> {
public:
Multivalue() : Aggregator<T, T>(&data, [](const T &value) { return value; }) { }
void append(const T& item) { data.push_back(item); }
private:
std::vector<T> data;
};
int main() {
Multivalue<std::tuple<std::tuple<uint32_t, uint32_t>, float, double>> mv;
mv.append(std::make_tuple(std::make_tuple(13, 12), 2.5, 3.5));
mv.append(std::make_tuple(std::make_tuple(1, 7), 2.55123, 1.5));
mv.append(std::make_tuple(std::make_tuple(5, 3), 2.312, 1.8));
auto &a1 = std::get<2>(mv);
auto &a2 = std::get<1>(mv);
auto &a3 = std::get<0>(std::get<0>(mv));
auto &a4 = std::get<1>(std::get<0>(mv));
std::cout << "Sum 1: " << a1.sum() << std::endl;
std::cout << "Sum 2: " << a2.sum() << std::endl;
std::cout << "Sum 3: " << a3.sum() << std::endl;
std::cout << "Sum 4: " << a4.sum() << std::endl;
return 0;
}
Moo