Serializer.h
1 /*
2  * Copyright (C) 2008 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_Serializer_h
30 #define Pt_Serializer_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/Formatter.h>
34 #include <Pt/Decomposer.h>
35 #include <Pt/SerializationContext.h>
36 #include <Pt/Types.h>
37 #include <vector>
38 
39 namespace Pt {
40 
45 class PT_API Serializer
46 {
47  public:
50  Serializer();
51 
54  virtual ~Serializer();
55 
58  SerializationContext* context();
59 
62  void reset(SerializationContext* context);
63 
66  Formatter* formatter();
67 
70  void setFormatter(Formatter& formatter);
71 
74  void clear();
75 
85  template <typename T>
86  void begin(const T& type, const char* name)
87  {
88  void* m = this->allocate( sizeof(BasicDecomposer<T>) );
89 
90  BasicDecomposer<T>* dec = 0;
91  try
92  {
93  dec = new (m) BasicDecomposer<T>(_context);
94  dec->begin(type, name);
95  _stack.push_back(dec);
96  }
97  catch(...)
98  {
99  if(dec)
100  dec->~BasicDecomposer<T>();
101 
102  this->deallocate(m);
103  throw;
104  }
105  }
106 
114  bool advance();
115 
122  void finish();
123 
124  private:
126  void* allocate(std::size_t n);
127 
129  void deallocate(void* p);
130 
131  private:
132  SerializationContext* _context;
133  Formatter* _formatter;
134  std::vector<Decomposer*> _stack;
135  Decomposer* _current;
136  Pt::varint_t _r0; // allocator
137 };
138 
139 } // namespace Pt
140 
141 #endif
Manages the decomposition of types during serialization.
Definition: Decomposer.h:107
Serializes a set of types.
Definition: Serializer.h:45
void begin(const T &type, const char *name)
Begins serialization of an object.
Definition: Serializer.h:86
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
Support for serialization to different formats.
Definition: Formatter.h:45
Manages the decomposition of types during serialization.
Definition: Decomposer.h:43