MeVisLab Toolbox Reference
DCMTree_SerializationHelper.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef DCM_TREE_SERIALIZATION_HELPER_H
14 #define DCM_TREE_SERIALIZATION_HELPER_H
15 
16 #include "DCMTree_Serialization.h"
17 #include "DCMTree_Exception.h"
18 
19 #include <boost/shared_ptr.hpp>
20 
21 #include <string>
22 #include <map>
23 #include <vector>
24 #include <set>
25 #include <limits>
26 
27 #ifdef _MSC_VER
28  #pragma warning (push)
29  #pragma warning (disable : 4018)
30 #endif
31 
32 namespace DCMTree_Serialization
33 {
35  template<typename T>
36  inline void serializePrimitiveX(Sink &sink, T value)
37  {
38  sink.writeX(&value, sizeof(value));
39  }
40 
42  template<typename T>
43  inline void deserializePrimitiveX(Source &source, T &value)
44  {
45  source.readX(&value, sizeof(value));
46  }
47 
49  void serializeX(Sink &sink, int value);
50 
52  void deserializeX(Source &source, int &value);
53 
55  void serializeX(Sink &sink, unsigned int value);
56 
58  void deserializeX(Source &source, unsigned int &value);
59 
61  void serializeX(Sink &sink, short value);
62 
64  void deserializeX(Source &source, short &value);
65 
67  void serializeX(Sink &sink, unsigned short value);
68 
70  void deserializeX(Source &source, unsigned short &value);
71 
73  void serializeX(Sink &sink, long value);
74 
76  void deserializeX(Source &source, long &value);
77 
79  void serializeX(Sink &sink, unsigned long value);
80 
82  void deserializeX(Source &source, unsigned long &value);
83 
85  void serializeX(Sink &sink, long long value);
86 
88  void deserializeX(Source &source, long long &value);
89 
91  void serializeX(Sink &sink, unsigned long long value);
92 
94  void deserializeX(Source &source, unsigned long long &value);
95 
97  void serializeX(Sink &sink, float value);
98 
100  void deserializeX(Source &source, float &value);
101 
103  void serializeX(Sink &sink, const double &value);
104 
106  void deserializeX(Source &source, double &value);
107 
109  void serializeX(Sink &sink, bool value);
110 
112  void deserializeX(Source &source, bool &value);
113 
115  void serializeX(Sink &sink, const std::string &value);
116 
118  void deserializeX(Source &source, std::string &value);
119 
121  template<typename T>
122  inline void serializeEnumX(Sink &sink, T value)
123  {
124  sink.writeTypeX(TC_ENUM);
125  serializePrimitiveX(sink, static_cast<int>(value));
126  }
127 
129  template<typename T>
130  inline void deserializeEnumX(Source &source, T &value)
131  {
132  source.readTypeX(TC_ENUM);
133  int i = 0;
135  value = static_cast<T>(i);
136  }
137 
139  inline void serializeX(Sink &sink, const Serializable &value)
140  {
141  value.serializeX(sink);
142  }
143 
145  inline void deserializeX(Source &source, Deserializable &value)
146  {
147  value.deserializeX(source);
148  }
149 
151  template<typename T>
152  inline void serializeX(Sink &sink, boost::shared_ptr<T> value)
153  {
154  serializeX(sink, *value);
155  }
156 
161  template<typename T>
162  inline void deserializeIntoNewX(Source &source, boost::shared_ptr<T>& value)
163  {
164  value = boost::shared_ptr<T>(new T());
165  deserializeX(source, *value);
166  }
167 
171  template<typename T>
172  inline void deserializeIntoExistingX(Source &source, boost::shared_ptr<T> value)
173  {
174  deserializeX(source, *value);
175  }
176 
178  template<typename T,typename U>
179  inline void serializeX(Sink &sink, const std::pair<T,U> &value)
180  {
181  sink.writeTypeX(TC_PAIR);
182  serializeX(sink, value.first);
183  serializeX(sink, value.second);
184  }
185 
187  template<typename T,typename U>
188  inline void deserializeX(Source &source, std::pair<T,U> &value)
189  {
190  source.readTypeX(TC_PAIR);
191  deserializeX(source, value.first);
192  deserializeX(source, value.second);
193  }
194 
196  template<typename T,typename U>
197  inline void serializeX(Sink &sink, const std::map<T,U> &value)
198  {
199  sink.writeTypeX(TC_MAP);
200  if ( std::numeric_limits<boost::uint32_t>::max() < value.size() )
201  {
202  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
203  }
204  serializeX( sink, static_cast<boost::uint32_t>( value.size() ) );
205 
206  typename std::map<T,U>::const_iterator mapEnd = value.end();
207  for(typename std::map<T,U>::const_iterator i = value.begin(); i != mapEnd; i++)
208  {
209  serializeX(sink, (*i).first);
210  serializeX(sink, (*i).second);
211  }
212  }
213 
215  template<typename T,typename U>
216  inline void deserializeX(Source &source, std::map<T,U> &value)
217  {
218  source.readTypeX(TC_MAP);
219  //typename std::map<T,U>::size_type size;
220  boost::uint32_t size;
221  deserializeX(source, size);
222  value.clear();
223  for(typename std::map<T,U>::size_type i = 0; i < size; i++)
224  {
225  T t;
226  deserializeX(source, t);
227  deserializeX(source, value[t]);
228  }
229  }
230 
232  template<typename T,typename U>
233  inline void serializeX(Sink &sink, const std::multimap<T,U> &value)
234  {
235  sink.writeTypeX(TC_MULTIMAP);
236  if ( std::numeric_limits<boost::uint32_t>::max() < value.size() )
237  {
238  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
239  }
240  serializeX( sink, static_cast<boost::uint32_t>( value.size() ) );
241 
242  typename std::multimap<T,U>::const_iterator mapEnd = value.end();
243  for(typename std::multimap<T,U>::const_iterator i = value.begin(); i != mapEnd; i++)
244  {
245  serializeX(sink, (*i).first);
246  serializeX(sink, (*i).second);
247  }
248  }
249 
251  template<typename T,typename U>
252  inline void deserializeX(Source &source, std::multimap<T,U> &value)
253  {
254  source.readTypeX(TC_MULTIMAP);
255  //typename std::multimap<T,U>::size_type size;
256  boost::uint32_t size;
257  deserializeX(source, size);
258  value.clear();
259  for(typename std::multimap<T,U>::size_type i = 0; i < size; i++)
260  {
261  T t;
262  deserializeX(source, t);
263  U u;
264  deserializeX(source, u);
265  value.insert(std::make_pair(t,u));
266  }
267  }
268 
270  template<typename T>
271  inline void serializeX(Sink &sink, const std::set<T> &value)
272  {
273  sink.writeTypeX(TC_SET);
274  if ( std::numeric_limits<boost::uint32_t>::max() < value.size() )
275  {
276  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
277  }
278  serializeX( sink, static_cast<boost::uint32_t>( value.size() ) );
279 
280  typename std::set<T>::const_iterator setEnd = value.end();
281  for(typename std::set<T>::const_iterator i = value.begin(); i != setEnd; i++)
282  {
283  serializeX(sink, (*i));
284  }
285  }
286 
288  template<typename T>
289  inline void deserializeX(Source &source, std::set<T> &value)
290  {
291  source.readTypeX(TC_SET);
292  //typename std::set<T>::size_type size;
293  boost::uint32_t size;
294  deserializeX(source, size);
295  value.clear();
296  for(typename std::set<T>::size_type i = 0; i < size; i++)
297  {
298  T t;
299  deserializeX(source, t);
300  value.insert(t);
301  }
302  }
303 
305  template<typename T>
306  inline void serializeX(Sink &sink, const std::vector<T> &vl)
307  {
308  sink.writeTypeX(TC_VECTOR);
309  // BugID: 31906
310  // size_type of a vector is not standardized between 32 and 64 bit OSs. Force it to 32 bit max.
311  //serializePrimitiveX(sink, vl.size());
312  if ( std::numeric_limits<std::uint32_t>::max() < vl.size() )
313  {
314  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
315  }
316  serializePrimitiveX( sink, static_cast<std::uint32_t>( vl.size() ) );
317 
318  for( auto i = 0u; i < vl.size(); ++i )
319  {
320  serializeX(sink, vl[i]);
321  }
322  }
323 
325  template<typename T>
326  inline void deserializeX(Source &source, std::vector<T> &vl)
327  {
328  source.readTypeX(TC_VECTOR);
329  // BugID: 31906
330  // size_type of a vector is not standardized between 32 and 64 bit OSs
331  //typename std::vector<T>::size_type count;
332  boost::uint32_t count;
333 
335  vl.clear();
336  vl.resize(count);
337  // BugID: 31906
338  //for(typename std::vector<T>::size_type i = 0; i < count; ++i)
339  for( boost::uint32_t i = 0; i < count; ++i)
340  {
341  deserializeX(source, vl[i]);
342  }
343  }
344 
346  template<typename T>
347  inline void serializeX(SerializerIntf<T> &serializer, Sink &sink, const T &value)
348  {
349  serializer.serializeX(sink, value);
350  }
351 
353  template<typename T>
354  inline void deserializeX(DeserializerIntf<T> &deserializer, Source &source, T &value)
355  {
356  value = deserializer.deserializeX(source);
357  }
358 
360  template<typename T>
361  inline bool serialize(Sink &sink, const T &value)
362  {
363  try {
364  serializeX(value);
365  }
366  catch(.../*const SerializationException&*/) {
367  return false;
368  }
369 
370  return true;
371  }
372 
374  template<typename T>
375  inline bool deserialize(Source &source, T &value)
376  {
377  try {
378  deserializeX(source,value);
379  }
380  catch(.../*const SerializationException&*/) {
381  return false;
382  }
383 
384  return true;
385  }
386 }
387 
388 #ifdef _MSC_VER
389  #pragma warning (pop)
390 #endif
391 
392 #endif
@ T
Definition: SoKeyGrabber.h:71
@ U
Definition: SoKeyGrabber.h:72
Exception class for DCMTree.
Interface which provides a method to deserialize an object.
virtual void deserializeX(Source &source)=0
Overwrites data of this object with data from Source.
Interface of a class, which can be used to deserialize objects without a default constructor.
virtual T deserializeX(Source &source)=0
Deserializes an object.
Interface which provides a method to serialize an object.
virtual void serializeX(Sink &sink) const =0
Writes data of this object to Sink.
Interface of a class, which can be used to serialize objects.
virtual void serializeX(Sink &sink, const T &value)=0
Serializes an object.
Interface of a data sink for the serialization of objects.
void writeX(const std::string &value)
Writes a QString.
void writeTypeX(TypeCode tc)
Writes a TypeCode.
Interface of a data source for the deserialization of objects.
void deserializePrimitiveX(Source &source, T &value)
Deserializes a primitive type like int, long.
void serializeX(Sink &sink, const DCMTree::TransferSyntax &syntax)
void serializeEnumX(Sink &sink, T value)
Serializes an enum.
bool deserialize(Source &source, T &value)
Adapts deserializeX to return an error code instead of throwing an exception.
void deserializeIntoNewX(Source &source, boost::shared_ptr< T > &value)
Deserializes boost::shared_ptr<T>.
void deserializeEnumX(Source &source, T &value)
Deserializes an enum.
bool serialize(Sink &sink, const T &value)
Adapts serializeX to return an error code instead of throwing an exception.
void deserializeX(Source &source, DCMTree::TransferSyntax &syntax)
void serializePrimitiveX(Sink &sink, T value)
Serializes a primitive type like int, long.
void deserializeIntoExistingX(Source &source, boost::shared_ptr< T > value)
Deserializes boost::shared_ptr<T>.
boost::graph_traits< ml_graph_ptr >::vertex_descriptor source(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for u of the edge (u,v) represented by e.