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 #include <cstdint>
27 
28 #ifdef _MSC_VER
29  #pragma warning (push)
30  #pragma warning (disable : 4018)
31 #endif
32 
33 namespace DCMTree_Serialization
34 {
36  template<typename T>
37  inline void serializePrimitiveX(Sink &sink, T value)
38  {
39  sink.writeX(&value, sizeof(value));
40  }
41 
43  template<typename T>
44  inline void deserializePrimitiveX(Source &source, T &value)
45  {
46  source.readX(&value, sizeof(value));
47  }
48 
50  void serializeX(Sink &sink, int value);
51 
53  void deserializeX(Source &source, int &value);
54 
56  void serializeX(Sink &sink, unsigned int value);
57 
59  void deserializeX(Source &source, unsigned int &value);
60 
62  void serializeX(Sink &sink, short value);
63 
65  void deserializeX(Source &source, short &value);
66 
68  void serializeX(Sink &sink, unsigned short value);
69 
71  void deserializeX(Source &source, unsigned short &value);
72 
74  void serializeX(Sink &sink, long value);
75 
77  void deserializeX(Source &source, long &value);
78 
80  void serializeX(Sink &sink, unsigned long value);
81 
83  void deserializeX(Source &source, unsigned long &value);
84 
86  void serializeX(Sink &sink, long long value);
87 
89  void deserializeX(Source &source, long long &value);
90 
92  void serializeX(Sink &sink, unsigned long long value);
93 
95  void deserializeX(Source &source, unsigned long long &value);
96 
98  void serializeX(Sink &sink, float value);
99 
101  void deserializeX(Source &source, float &value);
102 
104  void serializeX(Sink &sink, const double &value);
105 
107  void deserializeX(Source &source, double &value);
108 
110  void serializeX(Sink &sink, bool value);
111 
113  void deserializeX(Source &source, bool &value);
114 
116  void serializeX(Sink &sink, const std::string &value);
117 
119  void deserializeX(Source &source, std::string &value);
120 
122  template<typename T>
123  inline void serializeEnumX(Sink &sink, T value)
124  {
125  sink.writeTypeX(TC_ENUM);
126  serializePrimitiveX(sink, static_cast<int>(value));
127  }
128 
130  template<typename T>
131  inline void deserializeEnumX(Source &source, T &value)
132  {
133  source.readTypeX(TC_ENUM);
134  int i = 0;
136  value = static_cast<T>(i);
137  }
138 
140  inline void serializeX(Sink &sink, const Serializable &value)
141  {
142  value.serializeX(sink);
143  }
144 
146  inline void deserializeX(Source &source, Deserializable &value)
147  {
148  value.deserializeX(source);
149  }
150 
152  template<typename T>
153  inline void serializeX(Sink &sink, boost::shared_ptr<T> value)
154  {
155  serializeX(sink, *value);
156  }
157 
162  template<typename T>
163  inline void deserializeIntoNewX(Source &source, boost::shared_ptr<T>& value)
164  {
165  value = boost::shared_ptr<T>(new T());
166  deserializeX(source, *value);
167  }
168 
172  template<typename T>
173  inline void deserializeIntoExistingX(Source &source, boost::shared_ptr<T> value)
174  {
175  deserializeX(source, *value);
176  }
177 
179  template<typename T,typename U>
180  inline void serializeX(Sink &sink, const std::pair<T,U> &value)
181  {
182  sink.writeTypeX(TC_PAIR);
183  serializeX(sink, value.first);
184  serializeX(sink, value.second);
185  }
186 
188  template<typename T,typename U>
189  inline void deserializeX(Source &source, std::pair<T,U> &value)
190  {
191  source.readTypeX(TC_PAIR);
192  deserializeX(source, value.first);
193  deserializeX(source, value.second);
194  }
195 
197  template<typename T,typename U>
198  inline void serializeX(Sink &sink, const std::map<T,U> &value)
199  {
200  sink.writeTypeX(TC_MAP);
201  if ( std::numeric_limits<boost::uint32_t>::max() < value.size() )
202  {
203  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
204  }
205  serializeX( sink, static_cast<boost::uint32_t>( value.size() ) );
206 
207  typename std::map<T,U>::const_iterator mapEnd = value.end();
208  for(typename std::map<T,U>::const_iterator i = value.begin(); i != mapEnd; i++)
209  {
210  serializeX(sink, (*i).first);
211  serializeX(sink, (*i).second);
212  }
213  }
214 
216  template<typename T,typename U>
217  inline void deserializeX(Source &source, std::map<T,U> &value)
218  {
219  source.readTypeX(TC_MAP);
220  //typename std::map<T,U>::size_type size;
221  boost::uint32_t size;
222  deserializeX(source, size);
223  value.clear();
224  for(typename std::map<T,U>::size_type i = 0; i < size; i++)
225  {
226  T t;
227  deserializeX(source, t);
228  deserializeX(source, value[t]);
229  }
230  }
231 
233  template<typename T,typename U>
234  inline void serializeX(Sink &sink, const std::multimap<T,U> &value)
235  {
236  sink.writeTypeX(TC_MULTIMAP);
237  if ( std::numeric_limits<boost::uint32_t>::max() < value.size() )
238  {
239  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
240  }
241  serializeX( sink, static_cast<boost::uint32_t>( value.size() ) );
242 
243  typename std::multimap<T,U>::const_iterator mapEnd = value.end();
244  for(typename std::multimap<T,U>::const_iterator i = value.begin(); i != mapEnd; i++)
245  {
246  serializeX(sink, (*i).first);
247  serializeX(sink, (*i).second);
248  }
249  }
250 
252  template<typename T,typename U>
253  inline void deserializeX(Source &source, std::multimap<T,U> &value)
254  {
255  source.readTypeX(TC_MULTIMAP);
256  //typename std::multimap<T,U>::size_type size;
257  boost::uint32_t size;
258  deserializeX(source, size);
259  value.clear();
260  for(typename std::multimap<T,U>::size_type i = 0; i < size; i++)
261  {
262  T t;
263  deserializeX(source, t);
264  U u;
265  deserializeX(source, u);
266  value.insert(std::make_pair(t,u));
267  }
268  }
269 
271  template<typename T>
272  inline void serializeX(Sink &sink, const std::set<T> &value)
273  {
274  sink.writeTypeX(TC_SET);
275  if ( std::numeric_limits<boost::uint32_t>::max() < value.size() )
276  {
277  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
278  }
279  serializeX( sink, static_cast<boost::uint32_t>( value.size() ) );
280 
281  typename std::set<T>::const_iterator setEnd = value.end();
282  for(typename std::set<T>::const_iterator i = value.begin(); i != setEnd; i++)
283  {
284  serializeX(sink, (*i));
285  }
286  }
287 
289  template<typename T>
290  inline void deserializeX(Source &source, std::set<T> &value)
291  {
292  source.readTypeX(TC_SET);
293  //typename std::set<T>::size_type size;
294  boost::uint32_t size;
295  deserializeX(source, size);
296  value.clear();
297  for(typename std::set<T>::size_type i = 0; i < size; i++)
298  {
299  T t;
300  deserializeX(source, t);
301  value.insert(t);
302  }
303  }
304 
306  template<typename T>
307  inline void serializeX(Sink &sink, const std::vector<T> &vl)
308  {
309  sink.writeTypeX(TC_VECTOR);
310  // BugID: 31906
311  // size_type of a vector is not standardized between 32 and 64-bit OSs. Force it to 32-bit max.
312  //serializePrimitiveX(sink, vl.size());
313  if ( std::numeric_limits<std::uint32_t>::max() < vl.size() )
314  {
315  throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" );
316  }
317  serializePrimitiveX( sink, static_cast<std::uint32_t>( vl.size() ) );
318 
319  for( auto i = 0u; i < vl.size(); ++i )
320  {
321  serializeX(sink, vl[i]);
322  }
323  }
324 
326  template<typename T>
327  inline void deserializeX(Source &source, std::vector<T> &vl)
328  {
329  source.readTypeX(TC_VECTOR);
330  // BugID: 31906
331  // size_type of a vector is not standardized between 32 and 64-bit OSs
332  //typename std::vector<T>::size_type count;
333  boost::uint32_t count;
334 
336  vl.clear();
337  vl.resize(count);
338  // BugID: 31906
339  //for(typename std::vector<T>::size_type i = 0; i < count; ++i)
340  for( boost::uint32_t i = 0; i < count; ++i)
341  {
342  deserializeX(source, vl[i]);
343  }
344  }
345 
347  template<typename T>
348  inline void serializeX(SerializerIntf<T> &serializer, Sink &sink, const T &value)
349  {
350  serializer.serializeX(sink, value);
351  }
352 
354  template<typename T>
355  inline void deserializeX(DeserializerIntf<T> &deserializer, Source &source, T &value)
356  {
357  value = deserializer.deserializeX(source);
358  }
359 
361  template<typename T>
362  inline bool serialize(Sink &sink, const T &value)
363  {
364  try {
365  serializeX(value);
366  }
367  catch(.../*const SerializationException&*/) {
368  return false;
369  }
370 
371  return true;
372  }
373 
375  template<typename T>
376  inline bool deserialize(Source &source, T &value)
377  {
378  try {
379  deserializeX(source,value);
380  }
381  catch(.../*const SerializationException&*/) {
382  return false;
383  }
384 
385  return true;
386  }
387 }
388 
389 #ifdef _MSC_VER
390  #pragma warning (pop)
391 #endif
392 
393 #endif
@ T
Definition: SoKeyGrabber.h:71
@ U
Definition: SoKeyGrabber.h:72
Exception class for DCMTree.
Interface that 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 that can be used to deserialize objects without a default constructor.
virtual T deserializeX(Source &source)=0
Deserializes an object.
Interface that 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 that 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.