Hi every one,
I'm just wondering if any one as never think about using Boost serialization
framework to write/read OSG files ?
I'm working on this question, and until now I'm rather amazed:
- Sure, on one side you need to have a Boost dependency somewhere,
- And I haven't done any performance test yet but I'm pretty sure the io
operations will be slower than forIVE files.
But if read/write speed is not that critical to you this kind of framework
has very interesting feature:
1) You can use a single function to handle read and write process per class
2) This single function is used to write Text, Binary or even XML files (so
no duplication of code like for .osg vs .ive)
3) The code to do this become so simple it's almost frightening (cf example)
(in fact it hides all the reading/writing details...)
4) You can extend OSG with your own classes even more easily.
If someone already worked on this subject, did you end up with an unsolvable
problem ?
Here is an example of the code I use to serialize osg::Geometry:
#include <osg/Geometry>
#define TARGET osg::Geometry
BOOST_CLASS_EXPORT(TARGET)
namespace boost {
namespace serialization {
OSG_REF_TEMPLATE(TARGET);
template <>
void access::destroy(const TARGET* t)
{
// Do nothing: protected destructor;
}
template<class Archive>
void serialize(Archive & ar, TARGET::ArrayData& obj, const unsigned int
version)
{
NVP("Array",obj.array);
NVP("Indices",obj.indices);
NVP("Binding",obj.binding);
NVP("Normalize",obj.normalize);
};
template<class Archive>
void serialize(Archive & ar, TARGET& obj, const unsigned int version)
{
SERIALIZE_BASE(osg::Drawable);
NVP("PrimitiveSetList",obj.getPrimitiveSetList());
NVP("VertexData",obj.getVertexData());
NVP("NormalData",obj.getNormalData());
NVP("ColorData",obj.getColorData());
NVP("SecondaryColorData",obj.getSecondaryColorData());
NVP("FogCoordData",obj.getFogCoordData());
NVP("TexCoordArrayList",obj.getTexCoordArrayList());
NVP("VertexAttribArrayList",obj.getVertexAttribArrayList());
SERIALIZE_MEMBER(bool,FastPathHint);
}
} // namespace serialization
} // namespace boost
#undef TARGET
With the following macros:
#define OSG_REF_TEMPLATE(name) template<class Archive> \
void serialize(Archive & ar, osg::ref_ptr<name>& ref, const unsigned int
version) \
{ \
name* obj = ref.get(); \
ar & obj; \
ref = obj; \
}
#define SERIALIZE_MEMBER(type,name) { \
type member = obj.get ## name(); \
ar & boost::serialization::make_nvp( # name, member); \
if(Archive::is_loading()) \
obj.set ## name(member); \
}
#define NVP(t,o) ar & boost::serialization::make_nvp(t,o)
#define SERIALIZE_BASE(name) ar & boost::serialization::base_object<
name >(obj)
... Compare this with the content of Geometry.cpp in the osg or ive plugin
and you will probably be surprised... : the Boost serialization framework
handle automatically STL containers, derived classes, pointer references
etc...
Any way, I keep working on this project until I have a full
"ReaderWriter"... I'll tell you later if it's a so interesting feature or
not :-).
Manu.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org