Boost.Fusion


[]#include[][] <boost/spirit/fusion/algorithm/for_each.hpp>[]
[]#include[][] <boost/mpl/for_each.hpp>[]

[]#include[][] <boost/mpl/vector_c.hpp>[]
[]#include[][] <boost/mpl/vector.hpp>[]
[]#include[][] <boost/spirit/fusion/sequence/tuple.hpp>[]
[]#include[][] <boost/spirit/fusion/sequence/generate.hpp>[]
[]#include[][] <boost/xpressive/detail/fusion/sequence/cons.hpp>[]
[]#include[][] <boost/tuple/tuple.hpp>[]

[]#include[][] <iostream>[]

[]// See: libs/spirit/fusion/test/for_each_tests.cpp[]

[]namespace[] []pst[] {

[]namespace[] []fusion[] = []boost[]::[]fusion[];
[]namespace[] []mpl[] = []boost[]::[]mpl[];

[]// FunctionObject types[]
[]template[]< []class[] []Out[] >
[]struct[] []print[]
{
[]Out[]& []out[];
[]explicit[] []print[]([]Out[]& []out_[]) : []out[]([]out_[]) { }

[]template[]< []class[] []T[] >
[]void[] []operator[]()([]T[] []const[]& []v[]) []const[] { []out[] << []L[][]"[ "[] << []v[] << []L[][]" ] "[]; }
};

[]template[]< []class[] []Out[] >
[]print[]<[]Out[]> []make_print[]([]Out[]& []out_[]) { []return[] []print[]<[]Out[]>([]out_[]); }

[]struct[] []increment[]
{
[]template[]< []class[] []T[] >
[]void[] []operator[]()([]T[]& []v[]) []const[] { ++[]v[]; }
};

[]// helpers[]
[]template[]< []class[] []FusionSequence[] >
[]void[] []print_for_each[]([]FusionSequence[] []const[]& []seq[])
{
[]fusion[]::[]for_each[]([]seq[], []make_print[]([]std[]::[]wcout[]));
[]std[]::[]wcout[] << []std[]::[]endl[];
}

[]template[]< []class[] []FusionSequence[] >
[]void[] []increment_for_each[]([]FusionSequence[]& []seq[])
{
[]fusion[]::[]for_each[]([]seq[], []increment[]());
}

[]// main[]
[]void[] []test_main[]()
{
{
[]std[]::[]wcout[] << []L[][]"MPLSequence is also FusionSequence..."[] << []std[]::[]endl[];
[]typedef[] []mpl[]::[]vector_c[]<[]int[], 2, 3, 4, 5, 6> []mpl_seq_t[];
[]mpl_seq_t[] []ms[];
[]// IntegralConstant has an automatic conversion to[]
[]// a wrapped integral type(int, etc).[]
[]print_for_each[]([]ms[]);
[]// The conversion makes mpl::for_each work.[]
[]mpl[]::[]for_each[]<[]mpl_seq_t[]>([]make_print[]([]std[]::[]wcout[])); []std[]::[]wcout[] << []std[]::[]endl[];
}

{
[]std[]::[]wcout[] << []L[][]"Spirit has the tuple..."[] << []std[]::[]endl[];
[]typedef[] []fusion[]::[]tuple[]<[]int[], []wchar_t[], []double[], []wchar_t[] []const[]*> []tuple_t[];
[]tuple_t[] []t[](1, []L[][]'x'[], 3.3, []L[][]"Ruby"[]);
[]print_for_each[]([]t[]);
[]increment_for_each[]([]t[]); []print_for_each[]([]t[]);
}

{ []// boost::tuple is *NOT* a FusionSequence.[]
[]typedef[] []boost[]::[]tuple[]<[]int[], []wchar_t[], []double[], []wchar_t[] []const[]*> []boost_tuple_t[];
[]boost_tuple_t[] []bt[](1, []L[][]'x'[], 3.3, []L[][]"Ruby"[]);
[]// print_for_each(bt); error![]
}

{
[]std[]::[]wcout[] << []L[][]"Xpressive has a simple FusionSequence, cons..."[] << []std[]::[]endl[];
[]typedef[] []fusion[]::[]cons[]<[]int[],
[]fusion[]::[]cons[]<[]wchar_t[],
[]fusion[]::[]cons[]<[]double[],
[]fusion[]::[]cons[]<[]wchar_t[] []const[][5]> > > > []cons_t[];
[]cons_t[] []c[] =
[]fusion[]::[]make_cons[](1,
[]fusion[]::[]make_cons[]([]L[][]'x'[],
[]fusion[]::[]make_cons[](3.3,
[]fusion[]::[]make_cons[]([]L[][]"Ruby"[]))));
[]print_for_each[]([]c[]);
}

{
[]std[]::[]wcout[] << []L[][]"This is the *FUSION*..."[] << []std[]::[]endl[];
[]typedef[] []mpl[]::[]vector[]<[]int[], []wchar_t[], []double[], []wchar_t[] []const[]*> []mpl_seq_t[];
[]typedef[] []fusion[]::[]meta[]::[]generate[]<[]mpl_seq_t[]>::[]type[] []tuple_t[];
[]tuple_t[] []t[](1, []L[][]'x'[], 3.3, []L[][]"Ruby"[]);
[]print_for_each[]([]t[]);
}
}
} []// namespace pst[]

[]int[] []main[]()
{
[]pst[]::[]test_main[]();
[]std[]::[]wcin[].[]ignore[](); []return[] 0;
}