Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] [units] class_id and class_name to describe quantity dimension in serilization archive
From: Alfredo Correa (correaa_at_[hidden])
Date: 2011-09-24 18:08:26


Hi Robert,

On Sat, Sep 24, 2011 at 10:08 AM, Robert Ramey <ramey_at_[hidden]> wrote:
>> Current, serializing quantity<si::length> q produces:
>>
>> <q class_id="0" tracking_level="1" version="0" object_id="_0">
>>     <value>5</value>
>> </q>
>>
>> Which doesn't give any information about the quantity at hand, not
>> even for the human reading it. So, I tried to use
>> BOOST_CLASS_EXPORT_GUID to register a name for the serialized class,
>> but it is ignored.
> This is a feature of the xml_?archive implementation.  In general, the
> archive
> implementations include the minimum information required to re-constitute
> the original data - and no more.
>

Ah! that may also explain why in all the examples I see of
BOOST_CLASS_EXPORT, it only worked (saved a string with the name of
the class) for derived classes saves from their base class.

Good. Having settled on that, I think it would be cool to have the
*option* to save the class name (or other tag information) with the
object. Just a suggestion for Boost.Serialization.XML

I think of the quantity example because all quantities saved to
xml_archive look exactly the same regardless of whether they are the
same dimension or not, so it exists the possibility that
quantity<si::length> is saved, but then read as quantity<si::time>.

Now about a workaround from the point of view of Boost.Units, suppose
I want to ensure that additional information (spurious for
reconstruction but useful as a check and human reader) with the
current implementation. Currently the serialization of
boost.unit.quantity is:

// boost/units/io.hpp
template<class Archive,class Unit,class Y>
inline void serialize(Archive& ar,boost::units::quantity<Unit,Y>&
q,const unsigned int /*version*/){
    ar & boost::serialization::make_nvp("value", q.value); //basically
this in the code
}

if changed to

template<class Archive,class Unit,class Y>
inline void serialize(Archive& ar,boost::units::quantity<Unit,Y>&
q,const unsigned int /*version*/){
    ar & boost::serialization::make_nvp("value", q.value); //basically
this in the code
#ifdef BOOST_UNIT_SERIALIZATION_ADD_INFO
    std::string unit_info = boost::to_string(Unit());
    ar & boost::serialization::make_nvp("unit", unit_info );
#ifdef BOOST_UNIT_SERIALIZATION_CHECK_INFO
    assert( unit_info == boost::to_string(Unit()); // or throw
SOMETHING (see below)?
#endif
#endif
}

then I expect the output to be:

<q class_id="0" tracking_level="1" version="0" object_id="_0">
     <value>5</value>
     <unit>m</unit>
</q>

Which provides information to the human and and also can be used do a
basic check at reading the archive. The logic would be that nobody
ever will want to read a saved quantity of a given system and
dimension as another quantity in anothe system or dimension. In this
case this is a suggestion for the Boost.Units serialization
implementation. If double check is not desired the human can still
benefit, as it can if the tag had the following info <q unit = "m"
class_id="0" tracking_level="1" version="0" object_id="_0">

BTW, Robert, what would be the canonical exception to throw from the
assert line in the proposed code (which I plan to implement
internally)? something that can by useful for the error handling of
boost.serialization?

(I now understand that boost.serialization was not designed with this
in mind, but I need to do something like this to add this little piece
of information to the xml.)

Thanks,
Alfredo


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