From: stephan b. <st...@wa...> - 2003-09-07 15:50:57
|
It's not yet done in a 100% libfun-compatible way, but i can now de/seria= lize=20 arbitrary trees to fun-text format. i'm still experimenting with the API,= but=20 here's what i've done so far... i can currently deser arbitrary fun-txt files and then ser them to either= txt=20 or xml. One major caveat is that it doesn't have a real classloader yet - it reli= es on=20 statically-registered factories to load the serialized classes. The API g= ives=20 a way around this, but you must then know what types of class you must=20 instantiate (or live with base-class behaviour): template <typename BaseClass> bool deserialize( BaseClass & tgt ) const; will deserialize the SerNode into tgt by calling tgt's deserialize( SerNo= de &=20 ). This, of course, allows you to deserialize anything into anything, but= i=20 rather like that idea. e.g. FooClass * foo =3D new FooClass(); if( ! mynode.deserialize( *foo ) ) { // bad foo! delete( foo ); foo =3D 0; } The default way to deserialize a SerNode is: FooClass * foo =3D mynode.deserialize(); this assumes, however, that mynode.implClass() can be loaded and is of a = type=20 compatible with FooClass. If not deserialize() returns null. The=20 deserialization is recursive, of course. Since SerNode's deserialize() is virtual, adding true classloader support= =20 could be done by subclassing the default node type. Significant changes from the current model: - SerNode (SerialTree's counterpart) can now be separated from the=20 instantiation process by using it's deserialize<>( T & ) instead of=20 deserialize(). This allows you to potentially deserialize nodes for which= you=20 do not have the listed implementation class. - All save support is moved into an abstract SerNodeSerializer interface = i=20 currently have two working implementations: SerNodeSerializerFunTxt and=20 SerNodeSerializerXML. - load support is handled via dom-building lex parsers (SerNode trees) an= d is=20 in the SerNodeSerializer interface. i don't yet have the xml reader worki= ng,=20 but it shouldn't be terribly difficult to do. i haven't yet settled on an= =20 interface for wrapping these parsers within the SerNodeSerializer interfa= ce.=20 The fact that it's lex will be as deeply hidden as possible, of course. - SerNodeSerializer (the load/save interface) works only with i/o streams= ,=20 which also makes working with strings possible via stringstream. There is= no=20 loadFrom/saveToXXXFile() support. You do this instead: ofstream somefile( "outfile" ); bool workie =3D SerNodeSerializerFunTxt::save( mysertree, somefile ); somefile.close(); ofstream infile( "outfile" ); SerNode newtree; workie =3D SerNodeSerializerFunTxt::load( newtree, infile ); infile.close(); (There is also a more verbose, but more generic, approach.) Of course, there will be convenience functions to do those. PS: the name SerNode will change to S11nNode, i think. PS2: all of the above is subject to change at any time. --=20 ----- st...@wa... http://qub.sourceforge.net http://libfunutil.sourceforge.net =20 http://toc.sourceforge.net http://countermoves.sourceforge.net http://stephan.rootonfire.org |