Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
data.h
1 /*
2  * serial support for data structures
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2017, 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_DATA_H_
22 #define ELM_SERIAL2_DATA_H_
23 
24 #include <elm/assert.h>
25 #include <elm/serial2/serial.h>
26 
27 namespace elm {
28 
29 template <class T, class E, class A> class Vector;
30 
31 namespace serial2 {
32 
33 template <class C, class T>
34 class DataSerializer: public rtti::Type {
35 
36 public:
38 
39  static inline rtti::Type& type(void) { return __type; }
40  static inline rtti::Type& type(const C& v) { return __type; }
41 
42  inline DataSerializer(void): rtti::Type("") { }
43  virtual void *instantiate(void) const { return new C(); };
44 
45  virtual void unserialize(Unserializer& unserializer, void *object) {
46  C &coll = *static_cast<C *>(object);
47  int cnt = unserializer.countItems();
48  unserializer.beginCompound(&coll);
49  for (int i = 0; i < cnt; i++) {
50  __unserialize(unserializer, coll.addNew());
51  unserializer.nextItem();
52  }
53  unserializer.endCompound(&coll);
54  }
55 
56  virtual void serialize(Serializer& serializer, const void *object) {
57  const C &coll = *static_cast<const C *>(object);
58  serializer.beginCompound(&coll);
59  for (typename C::Iter iter(coll); iter(); iter++) {
60  serializer.onItem();
61  __serialize(serializer, *iter);
62  }
63  serializer.endCompound(&coll);
64  }
65 
66  static inline void serialize(Serializer& s, const C& v) { __type.serialize(s, &v); }
67  static inline void unserialize(Unserializer& s, C& v) { __type.unserialize(s, &v); }
68 };
69 
70 template <class C, class T>
71 DataSerializer<C, T> DataSerializer<C, T>::__type;
72 
73 // specialization
74 template <class T, class M> struct from_class<elm::Vector<T, M> >: public DataSerializer<Vector<T, M>, T> { };
75 
76 } } // elm::serial2
77 
78 #endif /* INCLUDE_ELM_SERIAL2_DATA_H_ */
elm::serial2::__unserialize
void __unserialize(Unserializer &s, const ArrayField< T > &field)
Definition: ArrayField.h:62
elm::iter
Definition: util_WAHVector.cpp:157
elm::serial2::DataSerializer::unserialize
static void unserialize(Unserializer &s, C &v)
Definition: data.h:67
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::serial2::Unserializer::endCompound
virtual void endCompound(void *)=0
elm::serial2::Unserializer::nextItem
virtual bool nextItem(void)=0
elm::serial2::__serialize
void __serialize(Serializer &s, const ArrayField< T > &field)
Definition: ArrayField.h:50
elm::serial2::DataSerializer::instantiate
virtual void * instantiate(void) const
Definition: data.h:43
elm::rtti::Type::Type
Type(string name="")
Definition: rtti.cpp:299
elm::serial2::Serializer::beginCompound
virtual void beginCompound(const void *object)=0
elm::serial2::DataSerializer::serialize
virtual void serialize(Serializer &serializer, const void *object)
Definition: data.h:56
elm::serial2::from_class
Definition: serial.h:111
elm
Definition: adapter.h:26
elm::serial2::Serializer
Definition: Serializer.h:36
elm::serial2::Vector
Definition: collections.h:16
elm::serial2::Serializer::onItem
virtual void onItem(void)=0
elm::serial2::Unserializer
Definition: Unserializer.h:15
elm::serial2::DataSerializer::serialize
static void serialize(Serializer &s, const C &v)
Definition: data.h:66
elm::serial2::DataSerializer::__type
static DataSerializer __type
Definition: data.h:37
elm::serial2::DataSerializer::unserialize
virtual void unserialize(Unserializer &unserializer, void *object)
Definition: data.h:45
elm::serial2::Serializer::endCompound
virtual void endCompound(const void *)=0
elm::serial2::DataSerializer::DataSerializer
DataSerializer(void)
Definition: data.h:42
elm::serial2::DataSerializer::type
static rtti::Type & type(void)
Definition: data.h:39
elm::serial2::Unserializer::beginCompound
virtual bool beginCompound(void *)=0
elm::serial2::DataSerializer
Definition: data.h:34
Vector
elm::serial2::DataSerializer::type
static rtti::Type & type(const C &v)
Definition: data.h:40
elm::serial2::Unserializer::countItems
virtual int countItems(void)=0
elm::rtti::Type
Definition: Type.h:83