CppCMS
serialization_classes.h
1 //
3 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
4 //
5 // See accompanying file COPYING.TXT file for licensing details.
6 //
8 #ifndef CPPCMS_SERIALIZATION_CLASSES_H
9 #define CPPCMS_SERIALIZATION_CLASSES_H
10 
11 #include <cppcms/defs.h>
12 #include <booster/copy_ptr.h>
13 #include <string>
14 #include <booster/backtrace.h>
15 
16 #include <booster/traits/enable_if.h>
17 #include <booster/traits/is_base_of.h>
18 
19 namespace cppcms {
20 
21  #ifdef CPPCMS_DOXYGEN_DOCS
22  template<typename Object>
28  {
32  static void save(Object const &d,archive &a);
36  static void load(Object &d,archive &a);
37  };
38 
39  #else
40 
41  template<typename Object,typename Enable = void>
42  struct archive_traits;
43  #endif
44 
45 
50  public:
51  archive_error(std::string const &e) : booster::runtime_error("cppcms::archive_error: " + e)
52  {
53  }
54  };
55 
56 
61  class CPPCMS_API archive {
62  public:
63 
67  void reserve(size_t size);
68 
72  void write_chunk(void const *begin,size_t len);
73 
77  void read_chunk(void *begin,size_t len);
78 
82  size_t next_chunk_size();
83 
87  bool eof();
88 
89 
93  std::string read_chunk_as_string();
94 
98  typedef enum {
99  save_to_archive,
100  load_from_archive
101  } mode_type;
102 
106  void mode(mode_type m);
107 
111  mode_type mode();
112 
116  void reset();
117 
121  std::string str();
122 
126  void str(std::string const &str);
127 
131  archive();
132 
136  ~archive();
137 
141  archive(archive const &);
142 
146  archive const &operator=(archive const &);
147 
148  private:
149  std::string buffer_;
150  size_t ptr_;
151  mode_type mode_;
152 
153  struct _data;
155 
156  };
157 
162  template<typename Archivable>
163  archive & operator &(archive &a,Archivable &object)
164  {
165  if(a.mode() == archive::save_to_archive)
167  else
169  return a;
170  }
171 
175  template<typename Archivable>
176  archive & operator <<(archive &a,Archivable const &object)
177  {
179  return a;
180  }
181 
185  template<typename Archivable>
186  archive & operator >>(archive &a,Archivable &object)
187  {
189  return a;
190  }
191 
192 
193 
194 
199  public:
203  virtual void load(archive &serialized_object) = 0;
207  virtual void save(archive &serialized_object) const = 0;
208 
209  virtual ~serializable_base()
210  {
211  }
212  };
213 
218  public:
219 
240 
241  virtual void serialize(archive &a) = 0;
245  virtual void load(archive &a)
246  {
247  serialize(a);
248  }
252  virtual void save(archive &a) const
253  {
254  const_cast<serializable *>(this)->serialize(a);
255  }
256  };
257 
258 
259  #ifdef CPPCMS_DOXYGEN_DOCS
260 
294 
295  template<typename Object>
300  static void load(std::string const &serialized_object,Object &real_object);
301 
305  static void save(Object const &real_object,std::string &serialized_object);
306  };
307 
308  #else
309 
310  template<typename Object,typename Enable = void>
311  struct serialization_traits;
312 
317  template<typename D>
318  struct serialization_traits<D,typename booster::enable_if<booster::is_base_of<serializable_base,D> >::type> {
322  static void load(std::string const &serialized_object,serializable_base &real_object)
323  {
324  archive a;
325  a.str(serialized_object);
326  real_object.load(a);
327  }
331  static void save(serializable_base const &real_object,std::string &serialized_object)
332  {
333  archive a;
334  real_object.save(a);
335  serialized_object = a.str();
336  }
337  };
338 
339 
340  #endif
341 
342 
343 }
344 #endif
Abstract class for serialization object.
Definition: serialization_classes.h:217
Error thrown in case of serialization error.
Definition: serialization_classes.h:49
Special traits class that describes how to serialize various objects that are not defived from serial...
Definition: serialization_classes.h:27
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
void mode(mode_type m)
mode_type
Definition: serialization_classes.h:98
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
archive & operator>>(archive &a, Archivable &object)
Definition: serialization_classes.h:186
archive & operator&(archive &a, Archivable &object)
Definition: serialization_classes.h:163
static void load(Object &d, archive &a)
virtual void load(archive &serialized_object)=0
static void save(Object const &d, archive &a)
virtual void load(archive &a)
Definition: serialization_classes.h:245
virtual void save(archive &serialized_object) const =0
std::string str()
virtual void save(archive &a) const
Definition: serialization_classes.h:252
Base abstract class for object that can be serialized into std::string.
Definition: serialization_classes.h:198
archive & operator<<(archive &a, Archivable const &object)
Definition: serialization_classes.h:176
This is the traits class for serialization traits.
Definition: serialization_classes.h:296
Class that represents a binary archive that can be stored in persistent storage or transfered...
Definition: serialization_classes.h:61