Decomposer.h
1 /*
2  * Copyright (C) 2008-2013 by Marc Boris Duerner
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 Pt_Decomposer_h
30 #define Pt_Decomposer_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/Formatter.h>
34 #include <Pt/SerializationInfo.h>
35 #include <Pt/SerializationContext.h>
36 
37 namespace Pt {
38 
44 {
45  public:
47  virtual ~Decomposer()
48  {}
49 
52  { _parent = parent; }
53 
55  Decomposer* parent() const
56  { return _parent; }
57 
60  void format(Formatter& formatter)
61  { onFormat(formatter); }
62 
65  void beginFormat(Formatter& formatter)
66  { onBeginFormat(formatter); }
67 
71  { return onAdvanceFormat(formatter); }
72 
73  protected:
77  : _parent(0)
78  {}
79 
82  virtual void onFormat(Formatter& formatter)
83  {
84  onBeginFormat(formatter);
85 
86  while( onAdvanceFormat(formatter) != _parent )
87  ;
88  }
89 
92  virtual void onBeginFormat(Formatter& formatter) = 0;
93 
96  virtual Decomposer* onAdvanceFormat(Formatter& formatter) = 0;
97 
98  private:
99  Decomposer* _parent;
100 };
101 
106 template <typename T>
108 {
109  public:
113  : _type(0)
114  , _si(context)
115  , _current(0)
116  { }
117 
118  // TODO: pass instance name to format()/onFormat() and
119  // beginFormat()/onBeginFormat()
120 
123  void begin(const T& type, const char* name)
124  {
125  if(_type)
126  {
127  _si.clear();
129  _current = 0;
130  }
131 
132  _type = &type;
133  _si.setName(name);
134 
135  Pt::SerializationContext* ctx = _si.context();
136  if( ctx && ctx->isReferencing() )
137  {
138  *ctx << Pt::save() <<= type;
139  }
140  }
141 
142  protected:
143  // inherit docs
144  void onFormat(Formatter& formatter)
145  {
146  _si << Pt::save() <<= *_type;
147  _si.format(formatter);
148  }
149 
150  // inherit docs
151  void onBeginFormat(Formatter& formatter)
152  {
153  _si << Pt::save() <<= *_type;
154  _current = &_si;
155 
156  _it = _si.beginFormat(formatter);
157  }
158 
159  // inherit docs
161  {
162  if( _it == _current->end() )
163  {
164  _current->endFormat(formatter);
165 
166  if( _current->sibling() )
167  {
168  _current = _current->sibling();
169  _it = _current->beginFormat(formatter);
170  }
171  else
172  {
173  _current = _current->parent();
174  if(_current)
175  _it = _current->end();
176  }
177 
178  if(_current != 0 )
179  return this;
180 
181  _si.clear();
182  _type = 0;
183  return parent();
184  }
185 
186  SerializationInfo::Iterator it = _it->beginFormat(formatter);
187  if( it != _it->end() )
188  {
189  _current = &(*_it);
190  _it = it;
191  }
192  else
193  {
194  _it->endFormat(formatter);
195  ++_it;
196  }
197 
198  return this;
199  }
200 
201  private:
202  const T* _type;
203  SerializationInfo _si;
204  SerializationInfo* _current;
206 };
207 
208 } // namespace Pt
209 
210 #endif
Manages the decomposition of types during serialization.
Definition: Decomposer.h:107
void format(Formatter &formatter)
Format the type completely.
Definition: Decomposer.h:60
virtual void onBeginFormat(Formatter &formatter)=0
Begin formatting the type.
virtual Decomposer * onAdvanceFormat(Formatter &formatter)=0
Advance formatting the type.
SerializationInfo * parent()
Returns the parent node.
Definition: SerializationInfo.h:206
Iterator end()
Returns an iterator to the end of child elements.
Definition: SerializationInfo.h:822
void setName(const std::string &name)
Sets the instance name.
void endFormat(Formatter &formatter)
End formatting.
Decomposer * parent() const
Returns the parent.
Definition: Decomposer.h:55
BasicDecomposer(SerializationContext *context=0)
Construct with context.
Definition: Decomposer.h:112
void onFormat(Formatter &formatter)
Format the type completely.
Definition: Decomposer.h:144
bool isReferencing() const
Returns true if references are recorded.
Definition: SerializationContext.h:35
Iterator beginFormat(Formatter &formatter)
Begin formatting.
void setParent(Decomposer *parent)
Sets the parent.
Definition: Decomposer.h:51
void onBeginFormat(Formatter &formatter)
Begin formatting the type.
Definition: Decomposer.h:151
Decomposer()
Default constructor.
Definition: Decomposer.h:76
void clear()
Clears all content.
Decomposer * advanceFormat(Formatter &formatter)
Advance formatting the type.
Definition: Decomposer.h:70
void begin(const T &type, const char *name)
Begin decomposing a type.
Definition: Decomposer.h:123
Context for the serialization of types.
Definition: SerializationContext.h:20
void beginFormat(Formatter &formatter)
Begin formatting the type.
Definition: Decomposer.h:65
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:58
Support for serialization to different formats.
Definition: Formatter.h:45
Manages the decomposition of types during serialization.
Definition: Decomposer.h:43
void format(Formatter &formatter) const
Format complete value or all members.
Decomposer * onAdvanceFormat(Formatter &formatter)
Advance formatting the type.
Definition: Decomposer.h:160
Forward Iterator for child elements.
Definition: SerializationInfo.h:733
virtual void onFormat(Formatter &formatter)
Format the type completely.
Definition: Decomposer.h:82
SerializationContext * context() const
Returns the used context.
Definition: SerializationInfo.h:185
virtual ~Decomposer()
Destructor.
Definition: Decomposer.h:47