c ++ / boost fusion handle父类

时间:2015-11-15 20:33:18

标签: c++ c++11 serialization boost boost-fusion

假设我有这样的类层次结构:

enum class Type { DUMMY };
struct Base {
  int a;
  explicit Base(int a) : a(a) {}
  virtual ~Base() {}
  virtual Type type() = 0;
};

struct Foo1 : public Base {
  double b;
  Foo1(int a, double b) : Base{a}, b(b) {}
  Type type() override { return Type::DUMMY; }
};

所有派生自Base使用单一继承而未定义 任何virtual方法,但覆盖type()方法除外。

我希望从Base派生每个元信息到序列化和调试输出。正如我所看到的那样,促进融合是我想要的:

#include <iostream>
#include <string>

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

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

enum class Type { DUMMY };
struct Base {
  int a;
  explicit Base(int a) : a(a) {}
  virtual ~Base() {}
  virtual Type type() = 0;
};

struct Foo1 : public Base {
  double b;
  Foo1(int a, double b) : Base{a}, b(b) {}
  Type type() override { return Type::DUMMY; }
};

BOOST_FUSION_ADAPT_STRUCT(Foo1, (double, b))

template <typename Sequence> struct XmlFieldNamePrinter {
  XmlFieldNamePrinter(const Sequence &seq) : seq_(seq) {}
  const Sequence &seq_;
  template <typename Index> void operator()(Index idx) const {

    std::string field_name =
        fusion::extension::struct_member_name<Sequence, idx>::call();

    std::cout << '<' << field_name << '>' << fusion::at<Index>(seq_) << "</"
              << field_name << ">\n";
    ;
  }
};
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() {
  Foo1 saveMe = {3, 3.4};
  printXml(saveMe);
}

但是如何处理Base数据成员? 我不希望将他们的描述包含在BOOST_FUSION_ADAPT_STRUCT(Foo1中, 像这样:

BOOST_FUSION_ADAPT_STRUCT(Foo1,
              (int, a)
              (double, b))

因为我必须为从Base继承的每个结构执行此操作, 所以我更喜欢与此类似的语法(当然没有编译):

BOOST_FUSION_ADAPT_STRUCT(Base, (int, a))

BOOST_FUSION_ADAPT_STRUCT(Foo1,
              (Base, __parent__)
              (double, b))

如何实现类似的语法?

1 个答案:

答案 0 :(得分:3)

您需要单独处理Fusion序列,例如使用SFINAE:

template <typename Index, typename IsSeq = IsSeq<Index> >
typename boost::disable_if<IsSeq, void>::type operator()(Index idx) const {
    std::string field_name = fusion::extension::struct_member_name<Sequence, idx>::call();

    std::cout << '<' << field_name << '>' << fusion::at<Index>(seq_) << "</" << field_name << ">\n";
}

template <typename Index, typename IsSeq = IsSeq<Index> >
typename boost::enable_if<IsSeq, void>::type operator()(Index idx) const {
    std::string field_name = fusion::extension::struct_member_name<Sequence, idx>::call();

    std::cout << "<" << field_name << ">\n\t";

    typedef typename IsSeq::sequence_type SubSeq;
    typedef mpl::range_c<unsigned, 0, fusion::result_of::size<SubSeq>::value> SubIndices;
    fusion::for_each(SubIndices(), XmlFieldNamePrinter<SubSeq>(fusion::at<Index>(seq_)));

    std::cout << "</" << field_name << ">\n";
}

IsSeq<Index>辅助特征的实现如下。现在你可以使用:

BOOST_FUSION_ADAPT_STRUCT(Base, a)
BOOST_FUSION_ADAPT_STRUCT(Foo1, base, b)

得到

<base>
    <a>3</a>
</base>
<b>3.4</b>

现场演示

<强> Live On Coliru

#include <iostream>
#include <string>

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

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

enum class Type { DUMMY };
struct Base {
    int a;
    explicit Base(int a) : a(a) {}
    virtual ~Base() {}
    virtual Type type() = 0;

    Base &base = *this;
};

struct Foo1 : public Base {
    double b;
    Foo1(int a, double b) : Base{ a }, b(b) {}
    Type type() override { return Type::DUMMY; }
};

BOOST_FUSION_ADAPT_STRUCT(Base, a)
BOOST_FUSION_ADAPT_STRUCT(Foo1, base, b)

template <typename Sequence> struct XmlFieldNamePrinter {
    XmlFieldNamePrinter(const Sequence &seq) : seq_(seq) {}
    const Sequence &seq_;

    template <typename Index,
             typename T = typename fusion::result_of::at_c<Sequence, Index::value>::type,
             typename BareT = typename boost::remove_reference<T>::type
        >
    struct IsSeq : mpl::bool_<fusion::traits::is_sequence<BareT>::value> {
        typedef BareT sequence_type; // if true_
    };

    template <typename Index, typename IsSeq = IsSeq<Index> >
    typename boost::disable_if<IsSeq, void>::type operator()(Index idx) const {
        std::string field_name = fusion::extension::struct_member_name<Sequence, idx>::call();

        std::cout << '<' << field_name << '>' << fusion::at<Index>(seq_) << "</" << field_name << ">\n";
    }

    template <typename Index, typename IsSeq = IsSeq<Index> >
    typename boost::enable_if<IsSeq, void>::type operator()(Index idx) const {
        std::string field_name = fusion::extension::struct_member_name<Sequence, idx>::call();

        std::cout << "<" << field_name << ">\n\t";

        typedef typename IsSeq::sequence_type SubSeq;
        typedef mpl::range_c<unsigned, 0, fusion::result_of::size<SubSeq>::value> SubIndices;
        fusion::for_each(SubIndices(), XmlFieldNamePrinter<SubSeq>(fusion::at<Index>(seq_)));

        std::cout << "</" << field_name << ">\n";
    }
};

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() {
    Foo1 saveMe = { 3, 3.4 };
    printXml(saveMe);
}