From: stephan b. <sg...@us...> - 2004-12-26 09:15:45
|
Update of /cvsroot/pclasses/pclasses2/src/s11n In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16835/src/s11n Modified Files: SIO.cpp SIO.h Log Message: More tweaks and fixes. Almost essentially complete. Index: SIO.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/s11n/SIO.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- SIO.h 26 Dec 2004 07:52:07 -0000 1.1 +++ SIO.h 26 Dec 2004 09:15:27 -0000 1.2 @@ -4,7 +4,7 @@ //////////////////////////////////////////////////////////////////////// // SIO.h: // Author: stephan beal <st...@s1...> -// License: Public Domain +// License: LGPL, copyright(c) 2004, 2005 stephan beal // // This header is intended to provide the standard P::s11n interface, // in particular the client-side interface. @@ -41,7 +41,8 @@ typedef ::P::s11n::s11n_node S11nNode; /** - NodeTraits are used to get information from S11nNode. + NodeTraits are used to view and manipulate data in S11nNode + objects. */ typedef ::P::s11n::node_traits<S11nNode> NodeTraits; @@ -74,7 +75,7 @@ } /** - See ::P::s11n::deserialize(). + See ::P::s11n::deserialize(). */ template <typename DeserializableT> bool deserialize( const S11nNode & src, DeserializableT & target ) @@ -136,6 +137,80 @@ return ::P::s11n::io::load_serializable<S11nNode,SerializableType>( src ); } + /** + See ::P::s11n::io::create_serializer(). + + In short: it looks at the given file to determine what + Serializer should be able to read it. + + Once IOManager support is completed this will also support + URL-based lookups. + */ + SerializerInterface * serializerFor( const std::string & url ); + + /** + Saves src to dest using the default + serializerClass(). Returns true on success, false on error. + */ + bool save( const S11nNode & src, const std::string & dest ); + + /** + Saves src to dest using the default + serializerClass(). Returns true on success, false on error. + */ + bool save( const S11nNode & src, std::ostream & dest ); + + /** + Uses SerializerPluginManager to load the Serializer + set via serializerClass() + + The caller owns the returned pointer, which may be 0. + */ + SerializerInterface * createSerializer(); + /** + Uses SerializerPluginManager to load a Serializer. + + The caller owns the returned pointer, which may be 0. + */ + SerializerInterface * createSerializer( const std::string & ); + + /** + Uses serialize(src,node) to serialize src then calls + save(n,dest). Returns true on success, false on error. + */ + template <typename SerializableT> + bool save( const SerializableT & src, std::ostream & dest ) + { + S11nNode n; + return (serialize( n, src ) && save( n, dest )); + } + + /** + Serializes src to a S11nNode && returns serialize(node,url). + + Returns true on success, false on error. + */ + template <typename SerializableT> + bool save( const SerializableT & src, const std::string & url ) + { + S11nNode n; + if( ! serialize( n, src ) ) return false; + return serialize( n, url ); + } + + /** + Serializes src to a S11nNode && returns serialize(node,dest). + + Returns true on success, false on error. + */ + template <typename SerializableT> + bool save( const SerializableT & src, const std::ostream & dest ) + { + S11nNode n; + return serialize( n, src ) && save( n, dest ); + } + + } // namespace SIO } // namespace P Index: SIO.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/s11n/SIO.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- SIO.cpp 26 Dec 2004 07:52:07 -0000 1.1 +++ SIO.cpp 26 Dec 2004 09:15:27 -0000 1.2 @@ -1,5 +1,13 @@ +//////////////////////////////////////////////////////////////////////// +// SIO.cpp - implementation code for SIO.h +// author: st...@s1... +// License: LGPL, copyright(c) 2004, 2005 stephan beal +//////////////////////////////////////////////////////////////////////// #include "SIO.h" +#include <pclasses/s11n/s11n_debuggering_macros.h> +#include <pclasses/pclasses-config.h> // PCLASSES_HAVE_LIBEXPAT +#define SIO_KEY_DEFAULT_SERIALIZER "/P/SIO/DefaultSerializer" namespace P { /** @@ -7,6 +15,57 @@ namespace SIO { + std::string m_serializer_class; + + void SIO_init() + { + m_serializer_class = SIO_KEY_DEFAULT_SERIALIZER; +#if PCLASSES_HAVE_LIBEXPAT + // @fixme: be more dynamic here, look poking around + // libdir/pclasses/*_serializer.so. + SerializerPluginManager::instance().alias( SIO_KEY_DEFAULT_SERIALIZER, "expat" ); +#endif + } + int SIO_init_placeholder = (SIO_init(),0); + + + SerializerInterface * serializerFor( const std::string & url ) + { + // @fixme: be more dynamic here: + // todo: port in IManager support from "original" ps11n + // once IOM is re-implemented in P2. + return ::P::s11n::io::create_serializer<S11nNode>( m_serializer_class ); + } + + SerializerInterface * createSerializer( const std::string & c ) + { + return SerializerPluginManager::instance().create( c ); + } + + SerializerInterface * createSerializer() + { + return SerializerPluginManager::instance().create( m_serializer_class ); + } + + bool save( const S11nNode & src, std::ostream & dest ) + { + typedef std::auto_ptr<SerializerInterface> SAP; + SAP ser = SAP(createSerializer()); + if( ! ser.get() ) + { + CERR << "Could not load Serializer!\n"; + return false; + } + return ser->serialize( src, dest ); + } + + bool save( const S11nNode & src, const std::string & dest ) + { + std::ofstream os(dest.c_str()); + if( ! os ) return false; + return save( src, os ); + } + S11nNode * loadNode( std::istream & src ) { @@ -19,4 +78,27 @@ return ::P::s11n::io::load_node<S11nNode>( src ); } + /** + Sets the current Serializer class used by createSerializer(). + Pass it a class name of a SerializerInterface type. + */ + void serializerClass( const std::string & ); + + /** + Gets the name of the current default Serializer type. + */ + std::string serializerClass(); + + void serializerClass( const std::string & c ) + { + m_serializer_class = c; + } + + std::string serializerClass() + { + return m_serializer_class; + } + + + }} // P::SIO |