From: <sg...@us...> - 2003-10-14 03:37:48
|
Update of /cvsroot/libfunutil/libfunutil/lib/s11n In directory sc8-pr-cvs1:/tmp/cvs-serv9525/lib/s11n Added Files: FileLoader.cpp FileLoader.h Log Message: this object is of arguable utility. TODO: re-evaluate it's purpose in life. --- NEW FILE: FileLoader.cpp --- // // Author: stephan beal <sg...@us...> // License: Public Domain // #include <s11n/FileLoader.h> #include <s11n/flex_lexers.h> // FlexTreeBuilder #include <s11n/s11n-macros.h> // BROKEN() namespace s11n { s11n_node * FileLoader::loadNode( const std::string & key ) const { std::string path = this->PathFinder::find( key ); if( path.empty() ) return NULL; FlexTreeBuilder * bob = FlexTreeBuilder::loadBuilder( path ); if( ! bob ) return NULL; s11n_node * node = 0; if( bob->builder() ) { node = bob->root_node(); bob->builder()->autoDelete( false ); // we take control of node. } delete( bob ); return node; } } // namespace s11n --- NEW FILE: FileLoader.h --- // // Author: stephan beal <sg...@us...> // License: Public Domain // #ifndef S11NFILELOADER_H_INCLUDED #define S11NFILELOADER_H_INCLUDED 1 #include <string> #include <toolbox/PathFinder.h> #include <s11n/s11n_node.h> namespace s11n { /** A classloader for loading Serializable classes from files, plus a convenience class for loading files from arbitrary data types, without having to know the type nor if it is compressed or not. */ class FileLoader : public toolbox::PathFinder { public: typedef FileLoader ThisType; // typedef toolbox::DLLLoader ParentClass; // typedef ParentClass::value_type value_type; // typedef ParentClass::key_type key_type; FileLoader(){}; virtual ~ FileLoader(){}; /** First tries to load key via the ancestor's method(s), and returns that pointer if it works. Secondly it tries to find file key in this->path() and return a new value_type * if it can deserialize an object of that type from that file. The caller owns the returned pointer, which may be NULL. Note that the input file may be in any format internally supported by the library. */ template <typename SerializableType> SerializableType * load( const std::string & filename ) const { typedef SerializableType ST; s11n_node * node = FileLoader::loadNode( key ); if( node ) { ST * ch = 0; /** Grief!!!! gcc 3.3 refuses to compile this: ch = node->deserialize<ST>(); error is: parse error before `;' token but it allows the exact same code in some other contexts!!! A workaround is to qualify s11n_node's class, but that breaks polymorphism!!! deserialize() is a template func, so it can't be virtual, but that's beside the point! */ ch = node->s11n_node::deserialize<ST>(); delete( node ); } return ch; } /** Tries to load the root s11n_node from the input file. The caller owns the pointer. This is similar to load(), but the returned object is not deserialized (indeed, it is not Serializable). This function supports all current Serializer data formats, uncompressed or compressed (assuming the suitable decompressor is enabled in your library). */ s11n_node * loadNode( const std::string & ) const; }; // class FileLoader }; // namespace s11n #endif // S11NFILELOADER_H_INCLUDED |