root/trunk/libfreebob/src/bebob/bebob_serialize.h

Revision 361, 3.1 kB (checked in by wagi, 17 years ago)

serializing interface extended. write and read except a 'string' as argument

Line 
1 /* bebob_serialize.h
2  * Copyright (C) 2006 by Daniel Wagner
3  *
4  * This file is part of FreeBoB.
5  *
6  * FreeBoB is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBoB is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBoB; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #ifndef bebob_serialize_h
22 #define bebob_serialize_h
23
24 #include <libxml++/libxml++.h>
25
26 #include <iostream>
27
28 namespace BeBoB {
29     class IOSerialize {
30     public:
31         IOSerialize() {}
32         virtual ~IOSerialize() {}
33
34         virtual bool write( const char* pMemberName,
35                             long long value ) = 0;
36         virtual bool write( const char* pMemberName,
37                             Glib::ustring str) = 0;
38
39         template <typename T>  bool write( const char* pMemberName, T value );
40     };
41
42     class IODeserialize {
43     public:
44         IODeserialize() {}
45         virtual ~IODeserialize() {}
46
47         virtual bool read( const char* pMemberName,
48                            long long& value ) = 0;
49         virtual bool read( const char* pMemberName,
50                            Glib::ustring& str ) = 0;
51
52         template <typename T> bool read( const char* pMemberName, T& value );
53     };
54
55     class XMLSerialize: public IOSerialize {
56     public:
57         XMLSerialize( const char* pFileName );
58         virtual ~XMLSerialize();
59
60         virtual bool write( const char* pMemberName,
61                             long long value );
62         virtual bool write( const char* pMemberName,
63                             Glib::ustring str);
64     private:
65         Glib::ustring    m_filepath;
66         xmlpp::Document  m_doc;
67
68         xmlpp::Node* getNodePath( xmlpp::Node* pRootNode, std::vector<std::string>& tokens );
69     };
70
71     class XMLDeserialize: public IODeserialize {
72     public:
73         XMLDeserialize( const char* pFileName );
74         virtual ~XMLDeserialize();
75
76         virtual bool read( const char* pMemberName,
77                            long long& value );
78         virtual bool read( const char* pMemberName,
79                            Glib::ustring& str );
80     private:
81         Glib::ustring    m_filepath;
82         xmlpp::DomParser m_parser;
83     };
84
85
86     //////////////////////////////////////////
87
88     template <typename T> bool IOSerialize::write( const char* pMemberName, T value )
89     {
90         return write( pMemberName, static_cast<long long>( value ) );
91     }
92
93     template <typename T> bool IODeserialize::read( const char* pMemberName, T& value )
94     {
95         long long tmp;
96         bool result = read( pMemberName, tmp );
97         value = static_cast<T>( tmp );
98         return result;
99     }
100 }
101
102 #endif
Note: See TracBrowser for help on using the browser.