deserializer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_DESERIALIZER_H
30 #define CXXTOOLS_DESERIALIZER_H
31 
33 #include <cxxtools/composer.h>
34 #include <cxxtools/config.h>
35 #include <stack>
36 
37 namespace cxxtools
38 {
43  {
44  public:
45 #ifdef HAVE_LONG_LONG
46  typedef long long int_type;
47 #else
48  typedef long int_type;
49 #endif
50 #ifdef HAVE_UNSIGNED_LONG_LONG
51  typedef unsigned long long unsigned_type;
52 #else
53  typedef unsigned long unsigned_type;
54 #endif
55 
57  { }
58 
59  virtual ~Deserializer()
60  { }
61 
67  template <typename T>
68  void deserialize(T& type) const
69  {
70  if (current() == 0)
71  throw SerializationError("no data was processed");
72 
73  *current() >>= type;
74  }
75 
76  template <typename T>
77  void deserialize(T& type, const std::string& name) const
78  {
79  const SerializationInfo* p;
80 
81  if (current() == 0 || (p = current()->findMember(name)) == 0)
82  throw SerializationMemberNotFound(name);
83 
84  *p >>= type;
85  }
86 
88  { return _si; }
89 
90  const SerializationInfo& si() const
91  { return _si; }
92 
93  void begin();
94 
95  void clear();
96 
98  { return _current.empty() ? 0 : _current.top(); }
99 
100  const SerializationInfo* current() const
101  { return _current.empty() ? 0 : _current.top(); }
102 
104  { current()->setCategory(category); }
105 
106  void setName(const std::string& name)
107  { current()->setName(name); }
108 
109  void setTypeName(const std::string& type)
110  { current()->setTypeName(type); }
111 
112  void setValue(const String& value)
113  { current()->setValue(value); }
114 
115  void setValue(const std::string& value)
116  { current()->setValue(value); }
117 
118  void setValue(const char* value)
119  { current()->setValue(value); }
120 
121  void setValue(bool value)
122  { current()->setValue(value); }
123 
124  void setValue(int_type value)
125  { current()->setValue(value); }
126 
128  { current()->setValue(value); }
129 
130  void setValue(long double value)
131  { current()->setValue(value); }
132 
133  void setNull()
134  { current()->setNull(); }
135 
136  void beginMember(const std::string& name, const std::string& type, SerializationInfo::Category category);
137 
138  void leaveMember();
139 
140  private:
141  SerializationInfo _si;
142  std::stack<SerializationInfo*> _current;
143  };
144 
145 }
146 
147 #endif // CXXTOOLS_DESERIALIZER_H