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 oth
#include
#include
#include
#include
#include
#include
#include
#include
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
struct XmlFieldNamePrinter
{
XmlFieldNamePrinter(const Sequence& seq):seq_(seq){}
const Sequence& seq_;
template
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::call();
std::cout
<< '<' << field_name << '>'
<< fusion::at(seq_)
<< "" << field_name << '>'
;
}
};
template
void printXml(Sequence const& v)
{
typedef mpl::range_c::value > Indices;
fusion::for_each(Indices(), XmlFieldNamePrinter(v));
}
int main()
{
myStructType saveMe = { 3.4, 5.6, 'g', 9};
printXml(saveMe);
}