访问boost融合映射字段名称

访问boost融合映射字段名称

本文介绍了访问boost融合映射字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用一些boost融合的东西来写一个常规的c结构到文件。 XML文件似乎是捕获数据并使其与其他工具兼容或手动可编辑的好方法。它似乎我几乎有它,但一些根本似乎失踪。
我使用的东西非常类似于boost :: fusion快速入门页面:。作为一个旁注,我已经彻底地看了这里和boost的文档,但没有人似乎正在访问字段名称。

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 << '>'
            ;
    }
};

我想使用它如下:

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;

我知道没有成员,但它似乎应该是为了访问字段名称!代码我已经与x.second工作正常,但然后没有完成我需要的是获取字段名称。
我怎么能实现这个呢?
谢谢!

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!

推荐答案

#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);
}

这篇关于访问boost融合映射字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:09