Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
collections.h
1 /*
2  * $Id$
3  * Copyright (c) 2006, IRIT UPS.
4  *
5  * Serialization of usual collections.
6  */
7 #ifndef ELM_SERIAL2_COLLECTION_H
8 #define ELM_SERIAL2_COLLECTION_H
9 
10 #include <elm/assert.h>
11 #include <elm/serial2/serial.h>
12 #include <elm/data/Vector.h>
13 
14 namespace elm { namespace serial2 {
15 
16 template <class T> class Vector;
17 
18 template <class C, class T>
20 
21 public:
23 
24  //static inline rtti::Type& type(void) { return __type; }
25  static inline rtti::Type& type(const C& v) { return __type; }
26 
27  inline CollectionSerializer(void): rtti::Type("") { }
28  virtual void *instantiate(void) const { return new C(); };
29 
30  virtual void unserialize(Unserializer& unserializer, void *object) const {
31  C &coll = *static_cast<C *>(object);
32  int cnt = unserializer.countItems();
33  unserializer.beginCompound(&coll);
34  for (int i = 0; i < cnt; i++) {
35  T item;
36  __unserialize(unserializer, item);
37  coll.add(item);
38  }
39  unserializer.endCompound(&coll);
40  }
41 
42  virtual void serialize(Serializer& serializer, const void *object) const {
43  const C &coll = *static_cast<const C *>(object);
44  serializer.beginCompound(&coll);
45  for (typename C::Iter iter(coll); iter; iter++) {
46  serializer.onItem();
47  __serialize(serializer, *iter);
48  }
49  serializer.endCompound(&coll);
50  }
51 
52  static inline void serialize(Serializer& s, const C& v) { serialize(s, &v); }
53  static inline void unserialize(Unserializer& s, C& v) { unserialize(s, &v); }
54 };
55 
56 template <class C, class T>
57 CollectionSerializer<C, T> CollectionSerializer<C, T>::__type;
58 
59 // specialization for Vector
60 template <class T> struct from_class<Vector<T> >: public CollectionSerializer<Vector<T>, T> { };
61 
62 
63 // special case for arrays
64 template <class T>
65 void __serialize(Serializer& serializer, const Array<T>& tab) {
66  serializer.beginCompound(&tab);
67  for(int i = 0; i < tab.count(); i++) {
68  serializer.onItem();
69  __serialize(serializer, tab[i]);
70  }
71  serializer.endCompound(&tab);
72 }
73 
74 template <class T>
75 void __unserialize(Unserializer& serializer, AllocArray<T>& tab) {
76  int cnt = serializer.countItems();
77  if(cnt != 0) {
78  tab = AllocArray<T>(cnt);
79  ASSERT(serializer.beginCompound(&tab));
80  for(int i = 0; i < cnt; i++) {
81  __unserialize(serializer, tab[i]);
82  serializer.nextItem();
83  }
84  serializer.endCompound(&tab);
85  }
86 }
87 
88 template <class T>
89 void __serialize(Serializer& serializer, const AllocArray<T>& tab)
90  { __serialize(serializer, static_cast<const Array<T> &>(tab)); }
91 
92 
93 
94 
95 // Collection serialization
96 
97 // common template
98 template <template <class I> class Coll, class T>
99 class CollecAC: public rtti::Type {
100 
101 public:
102  static CollecAC __type;
103 
104  static inline rtti::Type& type(void) { return __type; }
105  static inline rtti::Type& type(const Coll<T>& v) { return __type; }
106 
107  inline CollecAC(): rtti::Type("Collection") {
108  };
109 
110  virtual void *instantiate(void) { return new Coll<T>(); };
111 
112  virtual void unserialize(Unserializer& unserializer, void *object) {
113 
114  Coll<T> &coll = *static_cast<Coll<T> *>(object);
115  int cnt = unserializer.countItems();
116  unserializer.beginCompound(&coll);
117  for (int i = 0; i < cnt; i++) {
118  T item;
119  __unserialize(unserializer, item);
120  coll.add(item);
121  }
122  unserializer.endCompound(&coll);
123  }
124 
125  virtual void serialize(Serializer& serializer, const void *object) {
126  const Coll<T> &coll = *static_cast<const Coll<T> *>(object);
127  serializer.beginCompound(&coll);
128 
129  for (typename Coll<T>::Iterator iter(coll); iter; iter++) {
130  serializer.onItem();
131  __serialize(serializer, *iter);
132  }
133 
134  serializer.endCompound(&coll);
135  }
136 
137  static inline void serialize(Serializer& s, const Coll<T>& v) { serialize(s, &v); }
138  static inline void unserialize(Unserializer& s, Coll<T>& v) { unserialize(s, &v); }
139 };
140 
141 template <template <class I> class Coll, class T>
142 CollecAC<Coll,T> CollecAC<Coll,T>::__type;
143 
144 
145 
146 } } // elm::serial2
147 
148 #endif // ELM_SERIAL2_COLLECTION_H
149 
150 
elm::serial2::CollecAC::instantiate
virtual void * instantiate(void)
Definition: collections.h:110
elm::serial2::CollectionSerializer::unserialize
virtual void unserialize(Unserializer &unserializer, void *object) const
Definition: collections.h:30
elm::serial2::__unserialize
void __unserialize(Unserializer &s, const ArrayField< T > &field)
Definition: ArrayField.h:62
elm::iter
Definition: util_WAHVector.cpp:157
elm::serial2::CollectionSerializer::type
static rtti::Type & type(const C &v)
Definition: collections.h:25
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::serial2::CollecAC::serialize
static void serialize(Serializer &s, const Coll< T > &v)
Definition: collections.h:137
elm::serial2::CollectionSerializer::serialize
static void serialize(Serializer &s, const C &v)
Definition: collections.h:52
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::rtti::Serializable
Definition: Type.h:50
elm::serial2::CollecAC::__type
static CollecAC __type
Definition: collections.h:102
elm::serial2::CollecAC
Definition: collections.h:99
elm::rtti::Type::Type
Type(string name="")
Definition: rtti.cpp:299
elm::serial2::Serializer::beginCompound
virtual void beginCompound(const void *object)=0
elm::serial2::from_class
Definition: serial.h:111
elm::serial2::CollectionSerializer::unserialize
static void unserialize(Unserializer &s, C &v)
Definition: collections.h:53
elm::serial2::CollecAC::unserialize
virtual void unserialize(Unserializer &unserializer, void *object)
Definition: collections.h:112
elm::serial2::CollecAC::type
static rtti::Type & type(const Coll< T > &v)
Definition: collections.h:105
elm
Definition: adapter.h:26
elm::serial2::Serializer
Definition: Serializer.h:36
elm::serial2::CollectionSerializer::__type
static CollectionSerializer __type
Definition: collections.h:22
elm::serial2::CollecAC::unserialize
static void unserialize(Unserializer &s, Coll< T > &v)
Definition: collections.h:138
elm::serial2::CollectionSerializer
Definition: collections.h:19
elm::serial2::Vector
Definition: collections.h:16
elm::serial2::CollecAC::serialize
virtual void serialize(Serializer &serializer, const void *object)
Definition: collections.h:125
elm::serial2::CollectionSerializer::CollectionSerializer
CollectionSerializer(void)
Definition: collections.h:27
elm::serial2::Serializer::onItem
virtual void onItem(void)=0
elm::serial2::CollecAC::type
static rtti::Type & type(void)
Definition: collections.h:104
elm::serial2::Unserializer
Definition: Unserializer.h:15
elm::AllocArray
Definition: Array.h:117
elm::Array::count
int count(void) const
Definition: Array.h:71
elm::serial2::CollecAC::CollecAC
CollecAC()
Definition: collections.h:107
elm::Array
Definition: Array.h:32
elm::serial2::Serializer::endCompound
virtual void endCompound(const void *)=0
elm::serial2::Unserializer::beginCompound
virtual bool beginCompound(void *)=0
elm::serial2::CollectionSerializer::serialize
virtual void serialize(Serializer &serializer, const void *object) const
Definition: collections.h:42
elm::serial2::Unserializer::countItems
virtual int countItems(void)=0
elm::serial2::CollectionSerializer::instantiate
virtual void * instantiate(void) const
Definition: collections.h:28
elm::rtti::Type
Definition: Type.h:83