fusion tuple to pod object
Hi there! 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? Greetings, Sebastian Weber
Sebastian, preprocessor can not do parsing of C++ tokens for you, so it can not strip template paramters. You approach should be reversed: SOME_MAGIC_DEF_MACRO(myVec, type_seq) should take a boost preprocessor sequence and genrate a template class instantiation and your pod type. Macro input would be: SOME_MAGIC_DEF_MACRO(myVec, ((int)(char)(double)(size_t)(whatever_type))) This macro expansion should result in: fusion::vector<int,char,double,size_t,whatever_type> myVec; and struct myVec_pod { int v1; double v2; ... }; BOOST_PP_SEQ_FOR_EACH or BOOST_PP_SEQ_FOLD_LEFT are your friends. With Kind Regards, Ovanes On Fri, Apr 4, 2008 at 1:34 PM, Sebastian Weber < sebastian.weber@physik.tu-darmstadt.de> wrote:
Hi there!
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?
Greetings,
Sebastian Weber
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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
Whow! This looks cool. Could have never thought of this solution. Thanks a lot for this code snippet. Sebastian On Fri, 2008-04-04 at 10:40 -0700, Steven Watanabe wrote:
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
einfaches Textdokument attachment (scratch.cpp) #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 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Ovanes Markarian
-
Sebastian Weber
-
Steven Watanabe