Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

funtxt_serializer.hpp

Go to the documentation of this file.
00001 #ifndef funtxt_SERIALIZER_H_INCLUDED
00002 #define funtxt_SERIALIZER_H_INCLUDED 1
00003 
00004 ////////////////////////////////////////////////////////////////////////
00005 // data_node_serializers.hpp: some file parsers for the s11n framework
00006 //
00007 // License: Public Domain
00008 // Author: stephan@s11n.net
00009 ////////////////////////////////////////////////////////////////////////
00010 
00011 #include <s11n.net/s11n/io/data_node_format.hpp>
00012 #include <s11n.net/s11n/traits.hpp> // node_traits
00013 
00014 #define MAGIC_COOKIE_FUNTXT "#SerialTree 1"
00015 
00016 #define INDENT(LEVEL,ECHO) indent = ""; for( size_t i = 0; i < depth + LEVEL; i++ ) { indent += '\t'; if(ECHO) dest << '\t'; }
00017 
00018 namespace s11n {
00019     namespace io {
00020                 namespace sharing {
00021                         /**
00022                            Sharing context used by funtxt_serializer.
00023                          */
00024                         struct funtxt_sharing_context {};
00025 
00026                 }
00027 
00028                 /**
00029                    The entity translations map used by funtxt_serializer.
00030                  */
00031                 entity_translation_map & funtxt_serializer_translations();
00032 
00033 
00034                 /**
00035                    De/serializes objects from/to this class' config-file-like grammar.
00036                 */
00037                 template <typename NodeType>
00038                 class funtxt_serializer : public tree_builder_lexer<NodeType,sharing::funtxt_sharing_context>
00039                 {
00040                 public:
00041                         typedef NodeType node_type;
00042 
00043                         typedef funtxt_serializer<node_type> this_type; // convenience typedef
00044                         typedef tree_builder_lexer<node_type,sharing::funtxt_sharing_context> parent_type; // convenience typedef
00045 
00046                         funtxt_serializer() : parent_type( "funtxt_data_nodeFlexLexer" ), m_depth(0)
00047                         {
00048                                 this->magic_cookie( MAGIC_COOKIE_FUNTXT );
00049                         }
00050 
00051                         virtual ~funtxt_serializer() {}
00052 
00053                         /**
00054                            Reimplemented to return this type's entity
00055                            translation map.
00056                          */
00057                         virtual const entity_translation_map & entity_translations() const
00058                         {
00059                                 return funtxt_serializer_translations();
00060                         }
00061 
00062 
00063                         /**
00064                            Writes src out to dest.
00065                         */
00066                         virtual bool serialize( const node_type & src, std::ostream & dest )
00067                         {
00068                                 typedef ::s11n::node_traits<node_type> NT;
00069                                 size_t depth = this->m_depth++;
00070                                 if ( 0 == depth )
00071                                 {
00072                                         dest << this->magic_cookie() << '\n';
00073                                 }
00074 
00075                                 std::string nname = NT::name(src);
00076                                 std::string impl = NT::class_name(src);
00077                                 std::string indent;
00078                                 std::string quote =
00079                                         (std::string::npos != impl.find('<'))
00080                                         ? "\""
00081                                         : "";
00082 
00083                                 dest << nname << " class=" << quote << impl << quote <<"\n";
00084                                 INDENT(0,1);
00085                                 dest <<"{\n";
00086                                 std::string propval;
00087                                 std::string key;
00088 
00089                                 INDENT(1,0);
00090                                 std::for_each(NT::properties(src).begin(),
00091                                               NT::properties(src).end(),
00092                                               key_value_serializer<node_type>(
00093                                                                               &(this->entity_translations()),
00094                                                                               dest,
00095                                                                               indent,
00096                                                                               " ",
00097                                                                               "\n" )
00098                                               );
00099 
00100                                 INDENT(1,0);
00101                                 std::for_each( NT::children(src).begin(),
00102                                                NT::children(src).end(),
00103                                                node_child_simple_formatter<this_type>( *this,
00104                                                                                        dest,
00105                                                                                        indent,
00106                                                                                        "" )
00107                                                );
00108 
00109                                 INDENT(0,1);
00110                                 dest << "}\n";
00111                                 if( 0 == depth )
00112                                 {
00113                                         dest.flush();
00114                                         // if we don't do this then the client is possibly forced to flush() the stream :/
00115                                 }
00116                                 --this->m_depth;
00117                                 return true;
00118                         }
00119 
00120                 private:
00121                         size_t m_depth;
00122                 };
00123     } // namespace io
00124 } // namespace s11n
00125 #undef INDENT
00126 #endif // funtxt_SERIALIZER_H_INCLUDED

Generated on Sun Dec 25 20:34:56 2005 for libs11n-1.2.3 by  doxygen 1.4.4