开发者

convert boost::fusion::set to boost::fusion::map by using boost::fusion::fold

I have a fusion set and would like to convert it into a fusion map.

#include <cstdlib>
#include <iostream>

#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/include/make_map.hpp>
#include <boost/fusion/include/push_back.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/fusion/container/set.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/at_key.hpp>

struct node_base
{
    int get() {return 123;}
};
struct node_a : public node_base
{
node_a(){std::cout << "node_a::Ctor"<< std::endl;}
};
struct node_b : public node_base
{
node_b(){std::cout << "node_b::Ctor"<< std::endl;}
};
struct node_c : public node_base
{
node_c(){std::cout << "node_c::Ctor"<< std::endl;}
};

typedef ::boost::fusion::set<node_a,node_b,node_c> my_fusion_set;
my_fusion_set my_set;

struct push_back_map
{
    typedef ::boost::fusion::map<> result_type;

    template<class NODE>
    ::boost::fusion::map<> operator() (const ::boost::fusion::map<> & m, const NODE & n)
    {
    return ::boost::fusion::push_back(
        m
        , ::boost::fusion::make_pair<NODE>(n)
        );
    }
};

::boost::fusion::map<> my_map =
::boost::fusion::fold(
    my_set
    , ::boost::fusion::map<>()
    , push_back_map()
    );

struct print_map
{
    template<class P>
    void operator()(P & p) const
    {
    std::cout << p.second.get() << std::endl;
    }
};

int main()
{
    ::boost::fusion::for_each(my_map, print_map());
    std::cout << ::boost::fusion::at_key<node_a>(my_set).get() << std::endl;
    //std::cout << ::boost::fusion::at_key<node_a>(my_map).seco开发者_如何学编程nd.get() << std::endl;
    system("pause");
    return 0;
}

This code compiles. But it couldn't print out the results of my_map. my_set is constructed successfully. my_map is simply converted from a my_set with each element's original type as the key and its instance object from default constructor as the value. I am wondering if my_map is created successfully from fusion::fold. my_set is able to query by calling fusion::at_key<> on it, but my_map doesn't work for at_key<>. Thanks!


I couldn't think of a concise way to create fusion::map from fusion::set with fold. So this answer won't resolve your question directly. I post this in case this would give you some hint.
How about preparing a type list(as my_types in the following), and creating fusion::set and fusion::map from that list instead of creating fusion::map from fusion::set directly?
For example:

namespace bf = boost::fusion;
namespace bm = boost::mpl;

typedef bm::vector<node_a,node_b,node_c> my_types;
typedef bm::fold<
          my_types
        , bm::vector<>
        , bm::push_back< bm::_1, bf::pair< bm::_2, bm::_2 > >
        >::type my_map_types;

int main() {
    bf::result_of::as_set<my_types>::type my_set;
    bf::result_of::as_map<my_map_types>::type my_map;
    std::cout << bf::at_key<node_a>(my_set).get() << std::endl;
    std::cout << bf::at_key<node_a>(my_map).get() << std::endl;
}

Here is a test on ideone. Incidentally, if key-type and value-type in your fusion::map are always identical, fusion::set will meet that purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜