COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
SharedStateSerializer.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 #include <net/tokenbuffer.h>
8 #include <util/coExport.h>
9 #include <string>
10 #include <vector>
11 #include <set>
12 #include <map>
13 
14 #ifndef SHARED_STATE_SERIALIZER_H
15 #define SHARED_STATE_SERIALIZER_H
16 
17 namespace vrb {
18 
20 
22 {
23  UNDEFINED = 0,
24  BOOL, //1
25  INT, //2
26  FLOAT, //3
27  STRING, //4
28  DOUBLE, //5
29  VECTOR, //6
30  SET, //7
31  MAP //8
32 };
33 template<class T>
35  return UNDEFINED;
36 }
37 template<class T>
39  return VECTOR;
40 }
41 template<class T>
43  return SET;
44 }
45 template<class K, class V>
47  return MAP;
48 }
49 template <>
51 
52 template <>
54 template <>
56 template <>
57 VRBEXPORT SharedStateDataType getSharedStateType<std::string>(const std::string &type);
58 template <>
60 template <>
62 //tries to convert the serializedWithType tokenbuffer to a string
64 
66 
67 
69 template<class T>
70 void serialize(covise::TokenBuffer &tb, const T &value)
71 {
72  tb << value;
73 }
74 
75 template <class T>
76 void serialize(covise::TokenBuffer &tb, const std::vector<T> &value) {
77  int size = value.size();
78  if (size == 0)
79  {
80  tb << UNDEFINED;
81  }
82  else
83  {
84  tb << getSharedStateType(value.front());
85  }
86  tb << size;
87  for (const T entry: value)
88  {
89  serialize(tb, entry);
90  }
91 }
92 
93 template <class T>
94 void serialize(covise::TokenBuffer &tb, const std::set<T> &value) {
95  int size = value.size();
96  if (size == 0)
97  {
98  tb << UNDEFINED;
99  }
100  else
101  {
102  tb << getSharedStateType(*value.begin());
103  }
104  tb << size;
105  for (const T entry : value)
106  {
107  serialize(tb, entry);
108  }
109 }
110 template <class K, class V>
111 void serialize(covise::TokenBuffer& tb, const std::map<K, V>& value) {
112  int size = value.size();
113  if (size == 0)
114  {
115  tb << UNDEFINED;
116  tb << UNDEFINED;
117  }
118  else
119  {
120  tb << getSharedStateType(value.begin()->first);
121  tb << getSharedStateType(value.begin()->second);
122  }
123  tb << size;
124  for (const auto entry : value)
125  {
126  serialize(tb, entry.first);
127  serialize(tb, entry.second);
128  }
129 }
132 template<class T>
134 {
135  tb >> value;
136 }
137 template <class T>
138 void deserialize(covise::TokenBuffer &tb, std::vector<T> &value)
139 {
140  int size, typeID;
141  tb >> typeID;
142  tb >> size;
143  value.clear();
144  value.resize(size);
145  for (int i = 0; i < size; i++)
146  {
147  T entry;
148  deserialize(tb, entry);
149  value[i] = entry;
150  }
151 }
152 
153 template <class T>
154 void deserialize(covise::TokenBuffer &tb, std::set<T> &value)
155 {
156  int size, typeID;
157  tb >> typeID;
158  tb >> size;
159  value.clear();
160  for (int i = 0; i < size; i++)
161  {
162  T entry;
163  deserialize(tb, entry);
164  value.insert(entry);
165  }
166 }
167 template <class K, class V>
168 void deserialize(covise::TokenBuffer& tb, std::map<K, V>& value)
169 {
170  int size, typeID;
171  tb >> typeID;
172  tb >> typeID;
173  tb >> size;
174  value.clear();
175  for (int i = 0; i < size; i++)
176  {
177  K key;
178  V val;
179  deserialize(tb, key);
180  deserialize(tb, val);
181  value[key] = val;
182  }
183 }
185 template<class T>
187 {
188  int typeID = getSharedStateType(value);
189  tb << typeID;
190  serialize(tb, value);
191 }
192 template<class T>
194 {
195  int typeID;
196  tb >> typeID;
197  deserialize(tb, value);
198 }
199 }
200 #endif
GLsizeiptr size
Definition: khronos-glext.h:6610
void deserialize(covise::TokenBuffer &tb, T &value)
Definition: SharedStateSerializer.h:133
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
Definition: SharedStateSerializer.h:29
Definition: SharedStateSerializer.h:23
SharedStateDataType getSharedStateType< float >(const float &type)
Definition: SharedStateSerializer.cpp:20
void deserializeWithType(covise::TokenBuffer &tb, T &value)
Definition: SharedStateSerializer.h:193
Definition: SharedStateSerializer.h:31
void serializeWithType(covise::TokenBuffer &tb, const T &value)
Definition: SharedStateSerializer.h:186
void serialize(covise::TokenBuffer &tb, const T &value)
convert the value to a TokenBuffer
Definition: SharedStateSerializer.h:70
Definition: SharedStateSerializer.h:30
Definition: SharedStateSerializer.h:25
Definition: SharedStateSerializer.h:28
std::string tokenBufferToString(covise::TokenBuffer &&tb, int typeID)
Definition: SharedStateSerializer.cpp:36
Definition: SharedStateSerializer.h:24
SharedStateDataType getSharedStateType< double >(const double &type)
Definition: SharedStateSerializer.cpp:32
#define VRBEXPORT
Definition: coExport.h:81
Definition: SharedStateSerializer.h:26
GLsizei const GLfloat * value
Definition: khronos-glext.h:6760
Definition: SharedStateSerializer.h:27
GLuint GLfloat * val
Definition: khronos-glext.h:7898
SharedStateDataType getSharedStateType< char >(const char &type)
Definition: SharedStateSerializer.cpp:28
SharedStateDataType
Definition: SharedStateSerializer.h:21
SharedStateDataType getSharedStateType(const T &type)
Definition: SharedStateSerializer.h:34
SharedStateDataType getSharedStateType< int >(const int &type)
Definition: SharedStateSerializer.cpp:16
GLenum type
Definition: khronos-glext.h:6279
Definition: tokenbuffer.h:74
SharedStateDataType getSharedStateType< bool >(const bool &type)
Definition: SharedStateSerializer.cpp:12