Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
serial.h
1 /*
2  * serial module interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2006, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_SERIAL2_TYPE_H
22 #define ELM_SERIAL2_TYPE_H
23 
24 #include <elm/rtti.h>
25 #include <elm/meta.h>
26 #include <elm/type_info.h>
27 #include <elm/serial2/Serializer.h>
28 #include <elm/serial2/Unserializer.h>
29 
30 
31 namespace elm { namespace serial2 {
32 
33 // for ascending compatibility only
34 template <class T>
35 class Base {
36 public:
37  inline Base(T *p): ptr(p) { }
38  inline Base(const T *p): ptr(const_cast<T *>(p)) { }
39  T *ptr;
40 };
41 
42 
43 // AbstractClass class
45 public:
47  : rtti::AbstractClass(name, base) { }
48 
49  virtual bool isSerial(void) const { return true; }
50  virtual const Serializable& asSerial(void) const { return *this; }
51  virtual const rtti::Type& type(void) const { return *this; }
52 };
53 
54 // SerializableClass class
55 template <class T, class B = void>
56 class Class: public AbstractClass {
57 public:
58  inline Class(CString name, const rtti::AbstractClass& base = type_of<B>().asClass())
59  : AbstractClass(name, base) { };
60 
61  void *instantiate() const override { return new T(); }
62  void free(void *obj) const override { delete static_cast<T *>(obj); }
63 
64  // Serializable interface
65  void serialize(elm::serial2::Serializer& ser, const void *data) const override {
66  __serialize(ser, *static_cast<const T *>(data));
67  }
68 
69  void unserialize(Unserializer& uns, void *data) const override {
70  __unserialize(uns, *static_cast<T *>(data));
71  }
72 };
73 
74 
75 template <class T>
76 class Enum: public rtti::Enum {
77 public:
78  Enum(const make& make): rtti::Enum(make) { }
79  Enum(cstring name, const Value values[]): rtti::Enum(name, values) { }
80 
81  // Serialize interface
82  virtual void *instantiate(void) const { return new T(T(0)); }
83  virtual void serialize(serial2::Serializer& ser, const void *data) const { ser.onEnum(data, int(*static_cast<const T *>(data)), *this); }
84  virtual void unserialize(serial2::Unserializer& uns, void *data) const { *static_cast<T *>(data) = T(uns.onEnum(*this)); }
85 };
86 
87 
88 
89 // Class information
90 template <class T>
91 inline void __serialize_body(Serializer& s, const T *v) {
92  __serialize_body(s, static_cast<const typename T::__base *>(v));
93  v->__visit(s);
94 }
95 template <> inline void __serialize_body(Serializer& s, const void *v) { }
96 
97 template <class T>
98 inline void __unserialize_body(Unserializer& s, T *v) {
99  __unserialize_body(s, static_cast<typename T::__base *>(v));
100  v->__visit(s);
101 }
102 template <> inline void __unserialize_body(Unserializer& s, void *v) { }
103 
104 template <class T> using supports_complete =
105  decltype(meta::declval<T>().__serial_complete());
107  do_complete(T& v) { v.__serial_complete(); }
109  do_complete(T& v) { }
110 
111 template <class T> struct from_class {
112  static inline void serialize(Serializer& s, const T& v) {
113  s.beginObject(type_of<T>(), &v);
114  __serialize_body(s, &v);
115  s.endObject(type_of<T>(), &v);
116  }
117  static inline void unserialize(Unserializer& s, T& v) {
118  s.beginObject(type_of<T>(), &v);
119  __unserialize_body(s, &v);
120  do_complete(v);
121  s.endObject(type_of<T>(), &v);
122  }
123 };
124 
125 
126 // Type information
127 template <class T> struct from_type {
128  static inline void serialize(Serializer& s, const T& v) { s.onValue(v); }
129  static inline void unserialize(Unserializer& s, T& v) { s.onValue(v); }
130 };
131 
132 
133 // Enum information
134 template <class T> struct from_enum {
135  static inline void serialize(Serializer& s, const T& v);
136  static inline void unserialize(Unserializer& s, T& v);
137 };
138 
139 // serialization
140 template <class T>
141 inline void __serialize(Serializer& s, T *v) {
142  s.onPointer(type_of<T>(), v);
143 }
144 template <class T>
145 inline void __serialize(Serializer& s, const T *v) {
146  s.onPointer(type_of<T>(), v);
147 }
148 template <class T>
149 inline void __serialize(Serializer& s, const T& v) {
152  >::serialize(s, v);
153 }
154 template <class T>
155 inline Serializer& operator&(Serializer& serializer, const T& data) {
156  __serialize(serializer, data);
157  return serializer;
158 }
159 template <class T>
160 inline Serializer& operator<<(Serializer& serializer, const T& data) {
161  __serialize(serializer, data);
162  return serializer;
163 }
164 
165 
166 // unserialization
167 template <class T>
168 inline void __unserialize(Unserializer& s, T *&v) {
169  s.onPointer(type_of<T>(), (void **)&v);
170 }
171 template <class T>
172 inline void __unserialize(Unserializer& s, const T *&v) {
173  s.onPointer(type_of<T>(), (void **)&v);
174 }
175 template <class T>
176 inline void __unserialize(Unserializer& s, T& v) {
179  >::unserialize(s, v);
180 }
181 template <class T>
182 inline Unserializer& operator&(Unserializer& serializer, T& data) {
183  __unserialize(serializer, data);
184  return serializer;
185 }
186 template <class T>
188  __unserialize(s, field);
189  return s;
190 }
191 template <class T>
193  __unserialize(s, field);
194  return s;
195 }
196 template <class T>
197 inline Unserializer& operator>>(Unserializer& serializer, T& data) {
198  __unserialize(serializer, data);
199  return serializer;
200 }
201 template <class T>
203  __unserialize(s, field);
204  return s;
205 }
206 template <class T>
208  __unserialize(s, field);
209  return s;
210 }
211 
212 
213 // ExternalSolver class
215 public:
216  static ExternalSolver null;
217  virtual ~ExternalSolver(void);
218  void *solve(string ref);
219  string ref(void *object);
220 };
221 
222 template <class T>
223 inline void from_enum<T>::serialize(Serializer& s, const T& v) {
224  s.onEnum(&v, v, type_of<T>());
225 }
226 
227 template <class T>
229  v = (T)s.onEnum(type_of<T>());
230 }
231 
232 
233 // Field inlines
234 template <class T>
235 inline void __serialize(Serializer& s, const Field<const T>& field) {
236  s.beginField(field.name());
237  __serialize(s, field.value());
238  s.endField();
239 }
240 template <class T>
242  __serialize(s, (const Field<const T>&)field);
243 }
244 
245 template <class T>
246 inline void __serialize(Serializer& s, const Field<T>& field) {
247  s.beginField(field.name());
248  __serialize(s, field.value());
249  s.endField();
250 }
251 template <class T>
252 inline void __serialize(Serializer& s, const DefaultField<T>& field) {
253  __serialize(s, (const Field<T>&)field);
254 }
255 
256 template <class T>
257 inline void __unserialize(Unserializer& s, const Field<T>& field) {
258  if(s.beginField(field.name())) {
259  __unserialize(s, field.value());
260  s.endField();
261  }
262 }
263 
264 template <class T>
266  if(s.beginField(field.name())) {
267  __unserialize(s, field.value());
268  s.endField();
269  }
270  else
271  field.value() = field.defaultValue();
272 }
273 
274 
275 // for ascending compatibility
276 template <class T>
278  { __unserialize_body(s, base.ptr); return s; }
279 
280 template <class T>
282  { __unserialize_body(s, base.ptr); return s; }
283 
284 template <class T>
286  { __serialize_body(s, base.ptr); return s; }
287 
288 template <class T>
290  { __serialize_body(s, base.ptr); return s; }
291 
292 } } // elm::serial2
293 
294 #endif // ELM_SERIAL2_TYPE_H
elm::serial2::AbstractClass::isSerial
virtual bool isSerial(void) const
Definition: serial.h:49
elm::serial2::Enum
Definition: serial.h:76
elm::serial2::Unserializer::beginObject
virtual void beginObject(const rtti::Type &clazz, void *object)=0
elm::serial2::from_enum::serialize
static void serialize(Serializer &s, const T &v)
Definition: serial.h:223
elm::serial2::__unserialize
void __unserialize(Unserializer &s, const ArrayField< T > &field)
Definition: ArrayField.h:62
elm::rtti::Type::name
string name(void) const
Definition: Type.h:91
elm::_if
Definition: meta.h:43
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::serial2::do_complete
meta::enable_if< meta::is_supported< T, supports_complete >::_ >::_ do_complete(T &v)
Definition: serial.h:107
elm::serial2::supports_complete
decltype(meta::declval< T >().__serial_complete()) supports_complete
Definition: serial.h:105
elm::serial2::Serializer::onValue
virtual void onValue(const bool &v)=0
elm::rtti::Enum::values
Iter values(void) const
Definition: Enum.h:62
elm::serial2::Unserializer::beginField
virtual bool beginField(CString name)=0
elm::serial2::Enum::serialize
virtual void serialize(serial2::Serializer &ser, const void *data) const
Definition: serial.h:83
elm::meta::enable_if
Definition: meta.h:35
elm::serial2::from_class::serialize
static void serialize(Serializer &s, const T &v)
Definition: serial.h:112
elm::serial2::ExternalSolver::ref
string ref(void *object)
Definition: serial2_serial.cpp:493
elm::serial2::Enum::Enum
Enum(const make &make)
Definition: serial.h:78
elm::serial2::Unserializer::endObject
virtual void endObject(const rtti::Type &clazz, void *object)=0
elm::serial2::Serializer::beginObject
virtual void beginObject(const rtti::Type &clazz, const void *object)=0
elm::io::base
IntFormat base(int base, IntFormat fmt)
Definition: Output.h:256
elm::serial2::AbstractClass
Definition: serial.h:44
elm::serial2::from_type::unserialize
static void unserialize(Unserializer &s, T &v)
Definition: serial.h:129
elm::serial2::Serializer::endObject
virtual void endObject(const rtti::Type &clazz, const void *object)=0
elm::serial2::Unserializer::onEnum
virtual int onEnum(const rtti::Type &clazz)=0
elm::rtti::Enum
Definition: Enum.h:31
elm::serial2::__serialize
void __serialize(Serializer &s, const ArrayField< T > &field)
Definition: ArrayField.h:50
elm::rtti::AbstractClass::asClass
virtual const AbstractClass & asClass(void) const
Definition: rtti.cpp:927
elm::rtti::Serializable
Definition: Type.h:50
elm::Field::value
V & value(void) const
Definition: rtti.h:39
elm::serial2::Serializer::onEnum
virtual void onEnum(const void *address, int value, const rtti::Type &clazz)=0
elm::serial2::Serializer::endField
virtual void endField(void)=0
elm::serial2::Class
Definition: serial.h:56
elm::serial2::operator>>
Unserializer & operator>>(Unserializer &serializer, T &data)
Definition: serial.h:197
elm::rtti::Enum::Value
Definition: Enum.h:34
elm::serial2::__serialize_body
void __serialize_body(Serializer &s, const T *v)
Definition: serial.h:91
elm::serial2::from_enum::unserialize
static void unserialize(Unserializer &s, T &v)
Definition: serial.h:228
elm::serial2::from_class::unserialize
static void unserialize(Unserializer &s, T &v)
Definition: serial.h:117
elm::serial2::Unserializer::onPointer
virtual void onPointer(const rtti::Type &clazz, void **object)=0
elm::CString
Definition: CString.h:17
elm::serial2::operator<<
Serializer & operator<<(Serializer &serializer, const T &data)
Definition: serial.h:160
elm::serial2::ExternalSolver::~ExternalSolver
virtual ~ExternalSolver(void)
Definition: serial2_serial.cpp:474
elm::serial2::Enum::unserialize
virtual void unserialize(serial2::Unserializer &uns, void *data) const
Definition: serial.h:84
elm::serial2::from_class
Definition: serial.h:111
elm::_
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:232
elm::serial2::Base::Base
Base(T *p)
Definition: serial.h:37
elm::serial2::ExternalSolver
Definition: serial.h:214
elm::Field< T >
elm::serial2::Base::Base
Base(const T *p)
Definition: serial.h:38
elm
Definition: adapter.h:26
elm::serial2::Serializer
Definition: Serializer.h:36
elm::serial2::Class::instantiate
void * instantiate() const override
Definition: serial.h:61
elm::serial2::Unserializer::onValue
virtual void onValue(bool &v)=0
elm::serial2::__unserialize_body
void __unserialize_body(Unserializer &s, T *v)
Definition: serial.h:98
elm::serial2::AbstractClass::type
virtual const rtti::Type & type(void) const
Definition: serial.h:51
elm::DefaultField
Definition: rtti.h:49
elm::serial2::from_type::serialize
static void serialize(Serializer &s, const T &v)
Definition: serial.h:128
elm::serial2::from_type
Definition: serial.h:127
elm::serial2::operator&
Serializer & operator&(Serializer &s, const ArrayField< T > &field)
Definition: ArrayField.h:74
elm::field
Field< T > field(CString name, T &value)
Definition: rtti.h:42
elm::serial2::Class::free
void free(void *obj) const override
Definition: serial.h:62
elm::serial2::Serializer::onPointer
virtual void onPointer(const rtti::Type &clazz, const void *object)=0
elm::rtti::AbstractClass::base
const AbstractClass & base(void) const
Definition: Class.h:297
elm::serial2::Unserializer
Definition: Unserializer.h:15
elm::Field::name
CString name(void) const
Definition: rtti.h:38
elm::serial2::ExternalSolver::solve
void * solve(string ref)
Definition: serial2_serial.cpp:482
elm::serial2::Class::Class
Class(CString name, const rtti::AbstractClass &base=type_of< B >().asClass())
Definition: serial.h:58
elm::serial2::Class::serialize
void serialize(elm::serial2::Serializer &ser, const void *data) const override
Definition: serial.h:65
elm::serial2::Enum::instantiate
virtual void * instantiate(void) const
Definition: serial.h:82
elm::serial2::Base
Definition: serial.h:35
elm::serial2::from_enum
Definition: serial.h:134
elm::serial2::AbstractClass::AbstractClass
AbstractClass(CString name, const rtti::AbstractClass &base)
Definition: serial.h:46
elm::rtti::AbstractClass
Definition: Class.h:289
elm::serial2::AbstractClass::asSerial
virtual const Serializable & asSerial(void) const
Definition: serial.h:50
elm::serial2::Unserializer::endField
virtual void endField(void)=0
elm::serial2::Class::unserialize
void unserialize(Unserializer &uns, void *data) const override
Definition: serial.h:69
elm::serial2::Enum::Enum
Enum(cstring name, const Value values[])
Definition: serial.h:79
elm::rtti::Type
Definition: Type.h:83
elm::serial2::Base::ptr
T * ptr
Definition: serial.h:39
elm::serial2::Serializer::beginField
virtual void beginField(CString name)=0
elm::rtti::Enum::make
Definition: Enum.h:46