c++ - Fusion vector projection -
i have fusion vector elements have several member data of different types , create new fusion vector(s) project specific data member(s). i've been looking time no progress.
#include <iostream> #include <string> #include <boost/fusion/adapted/boost_tuple.hpp> #include <boost/fusion/include/fold.hpp> #include <boost/fusion/include/sequence.hpp> #include <boost/fusion/algorithm.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/fusion/include/make_vector.hpp> #include <boost/bind.hpp> namespace phx = boost::phoenix; namespace fusion = boost::fusion; using namespace phx::arg_names; struct mprint { template<typename t> void operator()(t& t) const { std::cout << t << std::endl; } }; struct tstruct { std::string val1_; double val2_; int val3_; bool val4_; tstruct( const std::string &val1, double val2, int val3, bool val4 ) : val1_(val1), val2_(val2), val3_(val3), val4_(val4) { } }; template <typename sequence> void viewval1( const std::string &dummy,const sequence& args ) { fusion::for_each( boost::tie(phx::bind(&tstruct::val1_, arg1)(args)), mprint() ); } template <typename sequence> void viewval2( const std::string &dummy,const sequence& args ) { //fusion::for_each( boost::tie(phx::bind(&tstruct::val2_, arg1)(args)), mprint() ); } int main( int argc, char* argv[] ) { auto mdata = fusion::make_vector( tstruct( "test1", 2.3, 1, true ), tstruct( "test2", 3.3, 2, false ), tstruct( "test3", 4.3, 3, true ) ); viewval1( "dummy", mdata ); viewval2( "dummy", mdata ); return 0; }
i think (meaning i'm not sure) need use fusion::transform_view
:
#include <iostream> #include <string> #include <boost/fusion/adapted/boost_tuple.hpp> #include <boost/fusion/include/fold.hpp> #include <boost/fusion/include/sequence.hpp> #include <boost/fusion/algorithm.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/fusion/include/make_vector.hpp> #include <boost/bind.hpp> namespace phx = boost::phoenix; namespace fusion = boost::fusion; using namespace phx::arg_names; struct mprint { template<typename t> void operator()(const t& t) const { std::cout << t << std::endl; } }; struct get_val1 { template <typename t> std::string operator()(const t& t) const { return t.val1_; } }; struct tstruct { std::string val1_; double val2_; int val3_; bool val4_; tstruct( const std::string &val1, double val2, int val3, bool val4 ) : val1_(val1), val2_(val2), val3_(val3), val4_(val4) { } }; template <typename sequence> void viewval1( const std::string &dummy, sequence& args ) { typedef fusion::transform_view<sequence, get_val1> project; fusion::for_each(project(args,get_val1()), mprint() ); } template <typename sequence> void viewval2( const std::string &dummy, sequence& args ) { typedef fusion::transform_view<sequence,decltype(phx::bind(&tstruct::val2_,arg1))> project; fusion::for_each( project(args,phx::bind(&tstruct::val2_, arg1)), mprint() ); } int main( int argc, char* argv[] ) { auto mdata = fusion::make_vector( tstruct( "test1", 2.3, 1, true ), tstruct( "test2", 3.3, 2, false ), tstruct( "test3", 4.3, 3, true ) ); viewval1( "dummy", mdata ); viewval2( "dummy", mdata ); return 0; }
Comments
Post a Comment