Boost logo

Boost Users :

From: Markus Werle (numerical.simulation_at_[hidden])
Date: 2006-10-18 09:05:25


Robert Ramey <ramey <at> rrsd.com> writes:

> Markus Werle wrote:
> > Hi!
> >
> > I cannot figure out how to make some class behave like a primitive
> > and let boost::serialization rely on ostream/istream operators
> > for xml input. This makes sense to me for e.g. String representations
> > other than std::string.
> > It seems it does not suffice to define the iostream operators.
>
> "primitive" types are those supported explicitly by the
> class basic_?_?primitive with a function save/load
> in this class.
>
> There would be a number of ways of doing what you want depending
> on the different usage scenarios.
>
> The easiest would be just to define non-untrusive serialize templates for
> these classes.

Unfortunately this approach still requires an extra nvp for xml files, so
for the code below we obtain e.g.

<SomeTag><value>SomeText</value><SomeTag>

The extra tag is what I would like to avoid.

template<class Archive>
inline void save(Archive & ar,
CString const & S,
unsigned int const /* file_version */)
{
        using boost::serialization::make_nvp;
        std::string const s(S);
        
        ar << make_nvp("value", s); // how to avoid nvp here?
}

template<class Archive>
inline void load(Archive & ar,
                 CString & S,
                 unsigned int const /* file_version */)
{
        using boost::serialization::make_nvp;

        std::string s;
        ar >> make_nvp("value", s);
        S = s.c_str();
}

template<class Archive>
inline void serialize(Archive & ar,
                CString & S,
                const unsigned int file_version)
{
    boost::serialization::split_free(ar, S, file_version);
}

> If you want limited functionality (e.g. no tracking,
> versioning)
> then assign a lower implementation level - but one higher than primitive.

I was not aware of the side effect of limited functionality.
This is probably not what I want ... unsure.
Can you provide code which avoids the extra tag in xml files and
does the same job as for std::string?

> Implemenation can be anything you want. You might just want to lift
> the implementation for std::string from basic??primitive. or not.
>
> primitive is used for bypassing the whole serialization system itself so
> it presumes direct support in the archive class. If you really want to
> use primitive, be prepared to make your own archive and or
> derive from an existing archive - and now your new type will only
> work on your own archive .

I wished the UI of boost::serialization could provide some mechanism
that allows the plug in of types for which a string representation can be
generated and evaluated, or in other words: if the following functions are
available:

std::istream & operator>>(std::istream &is, MyType & T);
std::ostream & operator<<(std::ostream &os, MyType const & T);

then a single MACRO line should suffice to tell the lib to use these.
At least the requirement of an nvp should be removed from the lib then.
But maybe I overlooked something here ...
Again: is binary object a way to get this?

Markus


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net