From: stephan b. <sg...@us...> - 2004-12-27 18:06:57
|
Update of /cvsroot/pclasses/pclasses2/src/s11n/io In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26406/src/s11n/io Modified Files: data_node_io.h Log Message: Re-added a couple helper classes i removed when porting from the s11n.net tree - parens_serializer uses them :/. Index: data_node_io.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/s11n/io/data_node_io.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- data_node_io.h 26 Dec 2004 14:35:13 -0000 1.4 +++ data_node_io.h 27 Dec 2004 18:06:46 -0000 1.5 @@ -26,6 +26,7 @@ #include <pclasses/Phoenix.h> // phoenix class #include <pclasses/Plugin/Plugin.h> #include <pclasses/System/SharedLib.h> // openSharedLib() +#include <pclasses/Util/StringTool.h> #include <pclasses/s11n/s11n_debuggering_macros.h> // COUT/CERR #include <pclasses/s11n/classload.h> // classloader() #include <pclasses/s11n/data_node_serialize.h> // unfortunately dep @@ -434,6 +435,136 @@ return load_serializable<NodeT,SerializableT>( is ); } + + /** + A helper for serializing properties to a + stream. Intended for use by Serializers, not + Serializables. + + NodeType is the container type used for data + serialization (e.g. s11n::data_node). + + */ + template <typename NodeType> + class key_value_serializer + { + public: + typedef NodeType node_type; + typedef typename node_type::value_type pair_type; + + typedef std::map<std::string,std::string> entity_translation_map; + + /** + map: needed so we can do entity translation + in a unified manner here. It must outlive + this object. Pass it 0 for no translation. + Translations are only applied on VALUES, + not KEYS. + + prefix: inserted before each property. + + separator: inserted between the key and value. + + suffix: appended after each entry. + + + */ + key_value_serializer( const entity_translation_map & map, + std::ostream & dest, + const std::string & prefix, + const std::string & separator, + const std::string & suffix + ) + : m_pre(prefix), m_sep(separator), m_suf(suffix), m_os(dest), m_map(&map) + { + } + + /** + Sends the following formatted string to os: + + {prefix}{src.first}{separator}{src.second}{suffix} + + */ + void operator()( const pair_type & src ) const + { + using ::P::StringTool; + static const std::string errval = ""; + std::string key = StringTool::to( src.first ); + std::string val = StringTool::to( src.second ); + // should we xlate the key as well? + if( this->m_map ) + { + StringTool::translateEntities( val, *(this->m_map) ); + } + this->m_os << this->m_pre; + this->m_os << key; + this->m_os << this->m_sep; + this->m_os << val; + this->m_os << this->m_suf; + } + private: + std::string m_pre; + std::string m_sep; + std::string m_suf; + std::ostream & m_os; + const entity_translation_map * m_map; + }; + + + /** + A helper functor to loop over serializable children + of a node from within a Serializer implementation. + + Designed for use with std::for_each(). + + SerializerT must be compatible with + <code>data_node_serializer<></code>. + + */ + template <typename SerializerT> + struct node_child_simple_formatter + { + typedef SerializerT serializer_type; +// typedef typename SerializerT::node_type node_type; + /** + Preconditions: + + - Ser must be valid references. + + - Both ser and os must outlive this + object. More correctly, this object's + operator() must not be called after either + ser or os are destroyed. + + */ + node_child_simple_formatter( serializer_type & ser, std::ostream & os, + const std::string & prefix = "", const std::string & suffix = "\n" ) + : m_ser(ser), m_os(&os), m_pre(prefix), m_suf(suffix) + { + } + + /** + Serializes src into this object's target + container, using this object's serializer. + */ + template <typename NodeType> + bool operator()( const NodeType * src ) const + { + if( ! src ) return false; + if( ! this->m_os ) return false; + if( ! this->m_pre.empty() ) *(this->m_os) << this->m_pre; + bool b = this->m_ser.serialize( *src, *(this->m_os) ); + if( ! this->m_suf.empty() ) *(this->m_os) << this->m_suf; + return b; + } + + private: + serializer_type & m_ser; + std::ostream * m_os; + std::string m_pre; + std::string m_suf; + }; + } // namespace io |