Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

c++ - Accessing boost fusion map field name

I've been trying to use some of the boost fusion stuff to write a regular c struct to file. An XML file seems a good way to capture the data and make it compatible with other tools or hand editable. It seems like I almost have it but something fundamental seems to be missing. I'm using something pretty similar to what's on the boost::fusion quick start page: http://www.boost.org/doc/libs/1_54_0/libs/fusion/doc/html/fusion/quick_start.html. As a side note I have thoroughly looked here and on boost's documentation but no one seems to be accessing the field name.

struct print_xml
{
    template <typename T>
    void operator()(T const& x) const
    {
        std::cout
            << '<' << x.first << '>'
            << x
            << "</" << x.first << '>'
            ;
    }
};

I want to use it as follows:

BOOST_FUSION_ADAPT_STRUCT(
    myStructType,
    (double, val1)
    (double, val2)
    (char, letter)
    (int, number)
    )    
myStructType saveMe = { 3.4, 5.6, 'g', 9};
for_each(saveMe, print_xml());

Other times I defined the struct as follows, but still no luck:

namespace fields{
    struct val1;
    struct val2;
    struct letter;
    struct number;
}

typedef fusion::map<
    fusion::pair<fields::val1, double>,
    fusion::pair<fields::val2, double>,
    fusion::pair<fields::letter, char>,
    fusion::pair<fields::number, int> > myStructType;

I know there is no member first, but it really seems like there should be in order to access the field name! The code I have works fine with x.second but then doesn't accomplish what I need which is to get the field name. How else might I accomplish this? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#include <iostream>
#include <string>

#include <boost/mpl/range_c.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/zip.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/mpl.hpp>

namespace fusion=boost::fusion;
namespace mpl=boost::mpl;

struct  myStructType
{
    double val1;
    double val2;
    char letter;
    int number;
}; 

BOOST_FUSION_ADAPT_STRUCT(
    myStructType,
    (double, val1)
    (double, val2)
    (char, letter)
    (int, number)
)   



template <typename Sequence>
struct XmlFieldNamePrinter
{
    XmlFieldNamePrinter(const Sequence& seq):seq_(seq){}
    const Sequence& seq_;
    template <typename Index>
    void operator() (Index idx) const
    {
        //use `Index::value` instead of `idx` if your compiler fails with it
        std::string field_name = fusion::extension::struct_member_name<Sequence,idx>::call();

        std::cout
            << '<' << field_name << '>'
            << fusion::at<Index>(seq_)
            << "</" << field_name << '>'
            ;
    }
};
template<typename Sequence>
void printXml(Sequence const& v)
{
    typedef mpl::range_c<unsigned, 0, fusion::result_of::size<Sequence>::value > Indices; 
    fusion::for_each(Indices(), XmlFieldNamePrinter<Sequence>(v));
}

int main()
{
    myStructType saveMe = { 3.4, 5.6, 'g', 9};
    printXml(saveMe);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...