Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Build error serializing struct to XML using Boost

I’m trying to serialize some structs to XML using Boost. I can’t change the structs, so I’m trying to do it non invasively.

Following the simple "non intrusive version" example in the docs I managed to get flat text serialization to work. However when I try to extend it to XML by looking at the XML example I’m unable to build and get errors from within the Boost libraries. I find only one hit on the error message and I’m unable to see how to apply it to my situation to solve it. Looking generally for other posts and examples, I only see the referenced ones that don’t involve putting code inside the structs.

The code and error are below. Is anyone familiar enough with this to point out what I’m doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The code is

#include <iostream>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/config.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

void boostTest();

int main()
{
    boostTest();
}

struct s_FileInfo
{
    //const char* comment;
    int test;
    int motionCases; //number of motion cases
};

void boostTest()
{
    struct s_FileInfo test = { 3,2 };
    std::ofstream ofs("c:\\sandpit\\test.xml", std::ofstream::out); 
    boost::archive::xml_oarchive oa(ofs);
    oa << BOOST_SERIALIZATION_NVP(test);
    ofs.close();
}

namespace boost {
    namespace serialization {

        template<class Archive>
        void serialize(Archive& ar, s_FileInfo& g, const unsigned int version)
        {
            ar& g.test;
            ar& g.motionCases;
        }

    } // namespace serialization
} // namespace 

The errors I’m getting on attempting to build are

'mpl_assertion_in_line_6': const object must be initialized
'int boost::mpl::assertion_failed<false>(boost::mpl::assert<false>::type)': cannot convert argument 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type'

Both are in boost\archive\basic_xml_oarchive.hpp

>Solution :

The assert says it all:

// If your program fails to compile here, its most likely due to
// not specifying an nvp wrapper around the variable to
// be serialized.
BOOST_MPL_ASSERT((serialization::is_wrapper< T >));

So, let’s add that

Live On Coliru

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <fstream>

void boostTest();
int main() { boostTest(); }

struct s_FileInfo {
    // const char* comment;
    int test;
    int motionCases; // number of motion cases
};

void boostTest() {
    s_FileInfo    test {3, 2};
    std::ofstream ofs("test.xml");
    {
        boost::archive::xml_oarchive oa(ofs);
        oa << BOOST_SERIALIZATION_NVP(test);
    }
}

namespace boost::serialization {
    template <class Ar> void serialize(Ar& ar, s_FileInfo& g, unsigned /*unused*/) {
        ar& BOOST_SERIALIZATION_NVP(g.test);
        ar& BOOST_SERIALIZATION_NVP(g.motionCases);
    }
} // namespace boost::serialization

Creates test.xml:

<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="19">
<test class_id="0" tracking_level="0" version="0">
    <g.test>3</g.test>
    <g.motionCases>2</g.motionCases>
</test>
</boost_serialization>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading