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

funtxt_serializer.h

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

Generated on Thu Jun 16 16:18:12 2005 for s11n by  doxygen 1.4.3-20050530