Reputation: 834
I have the following code:
template <typename Type, typename Handler>
struct InternalColumn
{
column(Type* data, Handler h): data_(data), h_(h) {}
// overloaded functions for data binding ( int, char, long..)
template<typename SourceData>
void bindData(SourceData src){...}
...
Type* data_; // pointer to external data
Handler h_; // for some reasons
};
template <typename Columns, typename Handler>
writeDataFunc(Columns&& cols, Handler h)
{
// here i need to create here some tuples InternalColumn<T,H> vector
// vector<InternalColumn<T,H>> vt; how can I create it from incoming vector?
// then in loop - internCol.bind(col);
//
// write data
//
// call handler h(cols);
}
// i also have stuff like this:
template<typename T, typename H>
auto makeColumn(T* data, H h)
{
return column<T, H>(data, h);
}
main()
{
int i= 0;
char c[32] = {0};
long l = 0;
writeDataFunc(boost::fusion::vector_tie(i,c,l), [&](auto& col)
{
// working with i, c, l variables
});
}
So my problem is in writeDataFunc
.
I can't create InternalColumns outside and pass them to vector_tie
.
It should be POD types.
My question is: Is it possible to create an InternalColumn vector (boost, vector of tuples) in writeDataFunc?
Please any thoughts or if I was unclear.
Upvotes: 1
Views: 320
Reputation: 1113
You cannot create a std::vector of InternalColumn unless they have the same type T, so I guess you wanted to create fusion vectors. I used a std::tuple instead, which is very similar. The solution below works as long as the InternalColumn types are different (because I use the type as key for std::get). To generalize it one can create a std::index_sequence and access the tuple by index
#include <tuple>
template <typename Type, typename Handler>
struct InternalColumn
{
InternalColumn(Type& data, Handler h): data_(data), h_(h) {}
// overloaded functions for data binding ( int, char, long..)
template<typename SourceData>
void bindData(SourceData src){}
Type& data_; // pointer to external data
Handler h_; // for some reasons
};
template<typename Lambda, typename Tuple, int ID=0>
void for_each_tuple(Lambda&& l, Tuple& t){
l(std::get<ID>(t));
if constexpr(ID < std::tuple_size<Tuple>::value-1)
for_each_tuple<Lambda, Tuple, ID+1>(l, t);
}
template <template <typename ...> class Tuple, typename Handler, typename ... Types>
int writeDataFunc(Tuple<Types ...>&& cols, Handler h)
{
Tuple<InternalColumn<Types, Handler> ...> vt{InternalColumn{std::get<Types>(cols), h} ...};
for_each_tuple(h, vt);
}
// i also have stuff like this:
template<typename T, typename H>
auto makeColumn(T* data, H h)
{
return InternalColumn<T, H>(data, h);
}
main()
{
int i= 0;
char c[32] = {0};
long l = 0;
writeDataFunc(std::make_tuple(i,c,l), [&](auto& col)
{
// working with i, c, l variables
});
}
Upvotes: 1