Deserializer.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_Deserializer_h
30 #define Pt_Deserializer_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/Composer.h>
34 #include <Pt/Formatter.h>
35 #include <Pt/NonCopyable.h>
36 #include <Pt/SerializationContext.h>
37 
38 namespace Pt {
39 
44 class PT_API Deserializer : private NonCopyable
45 {
46  public:
49  Deserializer();
50 
53  virtual ~Deserializer();
54 
57  SerializationContext* context();
58 
61  void reset(SerializationContext* context);
62 
65  Formatter* formatter();
66 
69  void setFormatter(Formatter& formatter);
70 
73  void clear();
74 
81  template <typename T>
82  void begin(T& t)
83  {
84  if( ! _fmt)
85  return;
86 
87  // allocate() also destructs _current
88  void* m = this->allocate( sizeof(BasicComposer<T>) );
89 
90  BasicComposer<T>* comp = 0;
91  try
92  {
93  comp = new (m) BasicComposer<T>(_context);
94  comp->begin(t);
95  _current = comp;
96  }
97  catch(...)
98  {
99  if(comp)
100  comp->~BasicComposer<T>();
101 
102  this->deallocate(m);
103  throw;
104  }
105 
106  _fmt->beginParse(*_current);
107  }
108 
117  bool advance();
118 
125  void finish();
126 
133  void fixup();
134 
135  private:
137  void* allocate(std::size_t n);
138 
140  void deallocate(void* p);
141 
142  private:
143  SerializationContext* _context;
144  Formatter* _fmt;
145  Composer* _current;
146  void* _mem;
147  std::size_t _memsize;
148  Pt::varint_t _r0; // allocator
149 };
150 
151 } // namespace Pt
152 
153 #endif
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Composes types during serialization.
Definition: Composer.h:42
Deserializes a set of types.
Definition: Deserializer.h:44
void begin(T &t)
Starts parsing of an object.
Definition: Deserializer.h:82
Manages the composition of types during serialization.
Definition: Composer.h:268
Context for the serialization of types.
Definition: SerializationContext.h:20
Support for serialization to different formats.
Definition: Formatter.h:45
void begin(T &type)
Begin composing a type.
Definition: Composer.h:279