
AMDG Ovanes Markarian wrote:
The fusion library seems to be the choice for handling tuple-like data in C++. However, for some reasons I would like to be able to transform a fusion vector into a pod object. Something like:
fusion::vector<int, double, float> myVec;
SOME_MAGIC_ADAPTER_MACRO(myVec)
The macro or whatever should then turn the vector into
struct myVec_pod { int v1; double v2; float v3; };
or similar. Is this possible?
Try the attached. In Christ, Steven Watanabe #ifndef BOOST_PP_IS_ITERATING #include <boost/fusion/sequence/intrinsic/at.hpp> #include <boost/fusion/sequence/intrinsic/value_at.hpp> #include <boost/fusion/sequence/intrinsic/size.hpp> #include <boost/fusion/container/vector.hpp> #include <boost/preprocessor/repetition/repeat.hpp> #include <boost/preprocessor/repetition/enum.hpp> #include <boost/preprocessor/cat.hpp> #include <iostream> struct pod_vector_tag {}; template<int N> struct make_pod_vector_impl; template<class Vector> struct make_pod_vector : make_pod_vector_impl<(boost::fusion::result_of::size<Vector>::value)>::template apply<Vector> {}; #define BOOST_PP_FILENAME_1 "scratch.cpp" #define BOOST_PP_ITERATION_LIMITS (0, 50) #include BOOST_PP_ITERATE() int main() { boost::fusion::vector2<int, char> vec(3, 'd'); make_pod_vector<boost::fusion::vector2<int, char> >::type pod_vec = make_pod_vector<boost::fusion::vector2<int, char> >::call(vec); std::cout << pod_vec.value0 << ' ' << pod_vec.value1 << std::endl; } #else #define n BOOST_PP_ITERATION() #define POD_VECTOR_ELEMENT(z, n, data) \ typedef typename boost::fusion::result_of::value_at_c<data, n>::type BOOST_PP_CAT(type, n); \ BOOST_PP_CAT(type, n) BOOST_PP_CAT(value, n); #define AT(z, n, data) \ boost::fusion::at_c<n>(data) template<class Vector> struct BOOST_PP_CAT(pod_vector, n) { typedef pod_vector_tag fusion_tag; BOOST_PP_REPEAT_1(n, POD_VECTOR_ELEMENT, Vector) }; template<> struct make_pod_vector_impl<n> { template<class Vector> struct apply { typedef BOOST_PP_CAT(pod_vector, n)<Vector> type; static type call(const Vector& vec) { type result = { BOOST_PP_ENUM(n, AT, vec) }; return(result); } }; }; #undef n #endif