安德烈斯给出了一个很好的答案。我原始代码中的问题是“for_each”只接受序列类型。当编译器为一个 int 计算 T 时,它传递给“for_each”一个 int 参数,因此它失败了。Adries 解决方案背后的想法是将“for_each”隐藏在特定于序列的类(下面的 DecImplSeq_s)中,并为非序列字段提供替代类(DecImplVoid_s)。然后创建一个门面类来划分序列和非序列字段的解码(DecCalc_s)。
通用标题与下面的第一个示例一起显示 Adres 的想法。
/* compile with g++ 4.4.6: g++ -I boost_1_35_0 test.cpp */
#include <typeinfo>
#include <string>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/lexical_cast.hpp>
#include <cxxabi.h>
#include <stdio.h>
using namespace boost::fusion;
解决方案的通用代码直接来自 Adres 的示例:
template <typename T2> struct Dec_s;
struct AppendToTextBox {
template <typename T> void operator()(T& t) const {
//decode T and t as the original code here...
Dec_s<T>::decode(t);
}
};
template <typename T2> struct DecImplSeq_s {
typedef DecImplSeq_s<T2> type;
static void decode(T2 & f) { for_each(f, AppendToTextBox()); };
};
template <typename T2> struct DecImplVoid_s {
typedef DecImplVoid_s<T2> type;
static void decode(T2 & f) { };
};
template <typename T2> struct DecCalc_s {
typedef typename
boost::mpl::eval_if< traits::is_sequence<T2>, DecImplSeq_s<T2>, DecImplVoid_s<T2> >
::type type;
};
template <typename T2> struct Dec_s : public DecCalc_s<T2>::type { };
以下是如何使用上面的通用代码:
struct Foo_s { int i; char k[100]; };
struct Bar_s { int v; Foo_s w; };
BOOST_FUSION_ADAPT_STRUCT( Foo_s, (int, i) (char, k[100]) )
BOOST_FUSION_ADAPT_STRUCT( Bar_s, (int, v) (Foo_s, w) )
int main(int argc, char *argv[]) {
Bar_s f = { 2, { 3, "abcd" } };
Dec_s<Bar_s>::decode(f);
return 0;
}
另一种不使用高级增强技巧更直接的解决方案,您可以为每个原始类型实现一个专门的解码器类,而无需使用“eval_if”。要使用此解决方案,您需要对结构中的每个原始类型进行专门化。
struct Foo_s { int i; char k[100]; };
BOOST_FUSION_ADAPT_STRUCT( Foo_s, (int, i) (char, k[100]) )
struct Bar_s { int v; Foo_s w; };
BOOST_FUSION_ADAPT_STRUCT( Bar_s, (int, v) (Foo_s, w) )
template <typename T2> struct Dec_s { static void decode(T2 & f); };
struct AppendToTextBox {
template <typename T>
void operator()(T& t) const {
//decode T and t as the original code here...
Dec_s<T>::decode(t);
}
};
template <typename T2> void Dec_s<T2>::decode(T2 & f) {
for_each(f, AppendToTextBox());
};
template<> void Dec_s<int >::decode(int & f) {};
template<> void Dec_s<char>::decode(char & f) {};
int main(int argc, char *argv[]) {
Bar_s f = { 2, { 3, "abcd" } };
Dec_s<Bar_s>::decode(f);
return 0;
}
经过一些渐进的探索,这里有一个完整的例子。它使用更新的 boost 功能,但不使用早期的 boost 版本(如 1.35.0)构建。它适用于 boost 1.47.0 和 1.51.0。
通用标题部分:
#include <typeinfo>
#include <string>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/type_traits.hpp> // is_array, is_class, remove_bounds
#include <boost/lexical_cast.hpp>
#include <cxxabi.h>
#include <stdio.h>
extern int dec_indents; /* 0, 4, 8, ... */
struct NL {
static void print() { printf("\n");
for (int i=0; i<dec_indents; i++) printf(" ");
}
};
using namespace boost::fusion;
然后是输出格式的通用解码器:
template <typename T2> struct Dec_s;
template <typename S, typename N> struct Comma {
static inline void comma() { printf(" , "); }
};
template <typename S> struct Comma<S, typename
boost::mpl::prior<typename boost::fusion::result_of::size<S>::type >::type> {
static inline void comma() {}
};
template <typename S, typename N> struct DecImplSeqItr_s {
typedef typename boost::fusion::result_of::value_at<S, N>::type current_t;
typedef typename boost::mpl::next<N>::type next_t;
typedef boost::fusion::extension::struct_member_name<S, N::value> name_t;
static inline void decode(S& s) {
printf(" \"%s\" = ", name_t::call() );
Dec_s<current_t>::decode(boost::fusion::at<N>(s));
Comma<S, N>::comma(); // Insert comma or not
DecImplSeqItr_s<S, next_t>::decode(s);
}
};
template <typename S>
struct DecImplSeqItr_s<S, typename boost::fusion::result_of::size<S>::type > {
static inline void decode(S& s) { }
};
template <typename S>
struct DecImplSeqStart_s:DecImplSeqItr_s<S, boost::mpl::int_<0> > {};
template <typename S> struct DecImplSeq_s {
typedef DecImplSeq_s<S> type;
static void decode(S & s) {
printf(" struct start --- { --- ");
dec_indents += 4;
NL::print();
DecImplSeqStart_s<S>::decode(s);
dec_indents -= 4;
NL::print();
printf(" struct done --- } --- ");
NL::print();
};
};
template <typename T2> struct DecImplArray_s {
typedef DecImplArray_s<T2> type;
typedef typename boost::remove_bounds<T2>::type slice_t;
static const size_t size = sizeof(T2) / sizeof(slice_t);
static inline void decode(T2 & t) {
printf(" array start --- [ --- ");
dec_indents += 4;
NL::print();
for(size_t idx=0; idx<size; idx++) {
Dec_s<slice_t>::decode(t[idx]);
if (idx < size-1) {
NL::print(); printf(" , ");
}
}
dec_indents -= 4;
NL::print();
printf(" array done --- ] --- \n");
NL::print();
}
};
template <typename T2> struct DecImplVoid_s {
typedef DecImplVoid_s<T2> type;
static void decode(T2 & t) {
int status = 0;
const char *realname = abi::__cxa_demangle(typeid(t).name(),0,0,&status);
printf(" type %s", realname);
NL::print();
};
};
template <typename T2> struct DecCalc_s {
typedef
typename boost::mpl::eval_if< traits::is_sequence<T2>, DecImplSeq_s<T2>,
typename boost::mpl::eval_if< boost::is_array<T2>,
boost::mpl::identity< DecImplArray_s<T2> >,
DecImplVoid_s<T2> > >
::type type;
};
template <typename T2> struct Dec_s : public DecCalc_s<T2>::type { };
要使用这个通用解码器,您可以将其放入 .h 文件中,并使用以下 .c 代码:
/* compile with g++ 4.5.1: g++ -I boost_1_47_0 test.cpp */
#include "common_decoder.h"
using namespace boost::fusion;
int dec_indents=0;
struct Foo_s { int i; typedef char j_t[10]; Foo_s::j_t j; };
BOOST_FUSION_ADAPT_STRUCT( Foo_s, (int, i) (Foo_s::j_t, j) )
struct Bar_s { int v; typedef Foo_s w_t[2]; Bar_s::w_t w; };
BOOST_FUSION_ADAPT_STRUCT( Bar_s, (int, v) (Bar_s::w_t, w) )
int main(int argc, char *argv[]) {
Bar_s f = { 2, {{ 3, "abcd" },{ 4, "defg" }} };
Dec_s<Bar_s>::decode(f);
return 0;
}