Serialization of an object with multiple formats

It is possible to define more than one serialization format for a data structure with the serialization library? If not, has someone some good purposes to make it to? Thanks, Vidal

Please expand your question - its not clear how what you want is different from being able to support arbitrary archive types. - which is the current situation. Robert Ramey Vidal Roca wrote:
It is possible to define more than one serialization format for a data structure with the serialization library? If not, has someone some good purposes to make it to? Thanks, Vidal

On Tuesday, June 20, 2006 at 12:50:37 (+0200) Vidal Roca writes:
It is possible to define more than one serialization format for a data structure with the serialization library? If not, has someone some good purposes to make it to?
How do you mean? There are several archive formats: binary, text,
xml. See:
#include

I solved this problem like this. Not sure if it's the best approach,
but it worked for me.
class Mesh
{
int data;
};
class BinaryMesh : public Mesh
{
template<class Archive>
void serialize(Archive& ar, const unsigned int version);
}
class XMLMesh : public Mesh
{
template<class Archive>
void serialize(Archive& ar, const unsigned int version);
}
class TextMesh : public Mesh
{
template<class Archive>
void serialize(Archive& ar, const unsigned int version);
}
On 6/21/06, Bill Lear
On Tuesday, June 20, 2006 at 12:50:37 (+0200) Vidal Roca writes:
It is possible to define more than one serialization format for a data structure with the serialization library? If not, has someone some good purposes to make it to?

Sorry for the unclear question.
As Bill Lear clarified, I mean the following (not archive formats):
class Foo {
template<class Archive>
void serialize(Archive& ar, const unsigned int version);
template<class Archive>
void serialize_II(Archive& ar, const unsigned int version);
};
where serialize_II reads and writes the data structure in a different manner
than serialize.
It could be possible using versioning; but is there any other possibility?
Thanks,
Vidal
"Vidal Roca"
It is possible to define more than one serialization format for a data structure with the serialization library? If not, has someone some good purposes to make it to? Thanks, Vidal

On Thursday, June 22, 2006 at 10:55:01 (+0200) Vidal Roca writes:
Sorry for the unclear question. As Bill Lear clarified, I mean the following (not archive formats):
class Foo { template<class Archive> void serialize(Archive& ar, const unsigned int version);
template<class Archive> void serialize_II(Archive& ar, const unsigned int version); };
where serialize_II reads and writes the data structure in a different manner than serialize.
It could be possible using versioning; but is there any other possibility?
Well, what you really want is runtime dispatch, then. Sounds like you need a flag of some sort in your class, and then maybe if you want to go overboard, a factory method, a set of derived classes, etc. But, basically, using a simple flag: class Foo { enum { Format1, Format2, Format3, ...}; int format; // ... template <class Archive> void serialize1(Archive& ar, const unsigned int version) { // ... } template <class Archive> void serialize(Archive& ar, const unsigned int version) { switch (format) { case Format1: serialize1(ar, version); break; case Format2: serialize2(ar, version); break; case Format3: serialize3(ar, version); break; default: // ... barf } } }; Build your runtime dispatch however you like. Here it's a class variable, but could be a global variable, method, etc. Not sure this is what you want. Bill

a) you shouldn't want to do this b) you shouldn't have to do this If you want to do this anyway, try class Foo { void serialize(boost::archive::binary_iarchive & ar, const unsigned int version); void serialize(boost::archive::text_iarchive & ar, const unsigned int version); void serialize(boost::archive::binary_oarchive & ar, const unsigned int version); void serialize(boost::archive::text_oarchive & ar, const unsigned int version); }; Rober Ramey

Robert: right, I don't want to do/have neither a) nor b).
The solution that Bill proposes does indeed what I need. However I wanted to
avoid these kind of "global" flags and to know if there are
already some built solutions wihtin the library itself.
Here is my concrete case:
struct parkinglot {
// static data
...
// simulation data
...
template <class Archive>
void serialize1(Archive& ar, const unsigned int version) {
// static data
// ...
}
template <class Archive>
void serialize2(Archive& ar, const unsigned int version) {
// simulation data
// ...
}
};
I want to serialize static data indendently of the simulation data and vice
versa.
Of course, the best solution would be a redesign of this data structure.
But the question is: does the library offer a solution to declare more than
one form of serialization for the same type of Archive?
Thanks,
Vidal
"Robert Ramey"
a) you shouldn't want to do this b) you shouldn't have to do this
If you want to do this anyway, try
class Foo { void serialize(boost::archive::binary_iarchive & ar, const unsigned int version); void serialize(boost::archive::text_iarchive & ar, const unsigned int version); void serialize(boost::archive::binary_oarchive & ar, const unsigned int version); void serialize(boost::archive::text_oarchive & ar, const unsigned int version); };
Rober Ramey

What's the matter with my propose solution below of using the archive type as the "flag"? If necessary you could define a special type of archive - confguration_data_archive and a simulation_data_archive. These would be "boiler plate" code modeled on the code from text_oarchive.hpp (or binary or) which would have the effect of creating a special archive types. These types can be used to dispatch the appropriate serializer. class Foo { void serialize(confguration_data_iarchive & ar, const unsigned int version); void serialize(confguration_data_oarchive & ar, const unsigned int version); void serialize(simulation_data_iarchive & ar, const unsigned int version); void serialize(simulation_data_oarchive & ar, const unsigned int version); }; or if you want to get fancier you can use MPL type techiniques to make it slicker. Robert Ramey

ok, I see.
thanks to all,
Vidal
"Robert Ramey"
What's the matter with my propose solution below of using the archive type as the "flag"?
If necessary you could define a special type of archive - confguration_data_archive and a simulation_data_archive. These would be "boiler plate" code modeled on the code from text_oarchive.hpp (or binary or) which would have the effect of creating a special archive types. These types can be used to dispatch the appropriate serializer.
class Foo { void serialize(confguration_data_iarchive & ar, const unsigned int version); void serialize(confguration_data_oarchive & ar, const unsigned int version); void serialize(simulation_data_iarchive & ar, const unsigned int version); void serialize(simulation_data_oarchive & ar, const unsigned int version); };
or if you want to get fancier you can use MPL type techiniques to make it slicker.
Robert Ramey
participants (4)
-
Bill Lear
-
Robert Ramey
-
Stan Vasilyev
-
Vidal Roca