MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
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
34{
36 template <typename T> inline void serializePrimitiveX(Sink& sink, T value)
37 {
38 sink.writeX(&value, sizeof(value));
39 }
40
42 template <typename T> inline void deserializePrimitiveX(Source& source, T& value)
43 {
44 source.readX(&value, sizeof(value));
45 }
46
48 void serializeX(Sink& sink, int value);
49
51 void deserializeX(Source& source, int& value);
52
54 void serializeX(Sink& sink, unsigned int value);
55
57 void deserializeX(Source& source, unsigned int& value);
58
60 void serializeX(Sink& sink, short value);
61
63 void deserializeX(Source& source, short& value);
64
66 void serializeX(Sink& sink, unsigned short value);
67
69 void deserializeX(Source& source, unsigned short& value);
70
72 void serializeX(Sink& sink, long value);
73
75 void deserializeX(Source& source, long& value);
76
78 void serializeX(Sink& sink, unsigned long value);
79
81 void deserializeX(Source& source, unsigned long& value);
82
84 void serializeX(Sink& sink, long long value);
85
87 void deserializeX(Source& source, long long& value);
88
90 void serializeX(Sink& sink, unsigned long long value);
91
93 void deserializeX(Source& source, unsigned long long& value);
94
96 void serializeX(Sink& sink, float value);
97
99 void deserializeX(Source& source, float& value);
100
102 void serializeX(Sink& sink, const double& value);
103
105 void deserializeX(Source& source, double& value);
106
108 void serializeX(Sink& sink, bool value);
109
111 void deserializeX(Source& source, bool& value);
112
114 void serializeX(Sink& sink, const std::string& value);
115
117 void deserializeX(Source& source, std::string& value);
118
120 template <typename T> inline void serializeEnumX(Sink& sink, T value)
121 {
122 sink.writeTypeX(TC_ENUM);
123 serializePrimitiveX(sink, static_cast<int>(value));
124 }
125
127 template <typename T> inline void deserializeEnumX(Source& source, T& value)
128 {
129 source.readTypeX(TC_ENUM);
130 int i = 0;
131 deserializePrimitiveX(source, i);
132 value = static_cast<T>(i);
133 }
134
136 inline void serializeX(Sink& sink, const Serializable& value)
137 {
138 value.serializeX(sink);
139 }
140
142 inline void deserializeX(Source& source, Deserializable& value)
143 {
144 value.deserializeX(source);
145 }
146
148 template <typename T> inline void serializeX(Sink& sink, boost::shared_ptr<T> value)
149 {
150 serializeX(sink, *value);
151 }
152
157 template <typename T> inline void deserializeIntoNewX(Source& source, boost::shared_ptr<T>& value)
158 {
159 value = boost::shared_ptr<T>(new T());
160 deserializeX(source, *value);
161 }
162
166 template <typename T> inline void deserializeIntoExistingX(Source& source, boost::shared_ptr<T> value)
167 {
168 deserializeX(source, *value);
169 }
170
172 template <typename T, typename U> inline void serializeX(Sink& sink, const std::pair<T, U>& value)
173 {
174 sink.writeTypeX(TC_PAIR);
175 serializeX(sink, value.first);
176 serializeX(sink, value.second);
177 }
178
180 template <typename T, typename U> inline void deserializeX(Source& source, std::pair<T, U>& value)
181 {
182 source.readTypeX(TC_PAIR);
183 deserializeX(source, value.first);
184 deserializeX(source, value.second);
185 }
186
188 template <typename T, typename U> inline void serializeX(Sink& sink, const std::map<T, U>& value)
189 {
190 sink.writeTypeX(TC_MAP);
191 if (std::numeric_limits<boost::uint32_t>::max() < value.size()) {
192 throw DCMTree::Exception("DCMTree_Serialization::serializeX()", "Value size too large for serialization");
193 }
194 serializeX(sink, static_cast<boost::uint32_t>(value.size()));
195
196 typename std::map<T, U>::const_iterator mapEnd = value.end();
197 for (typename std::map<T, U>::const_iterator i = value.begin(); i != mapEnd; i++) {
198 serializeX(sink, (*i).first);
199 serializeX(sink, (*i).second);
200 }
201 }
202
204 template <typename T, typename U> inline void deserializeX(Source& source, std::map<T, U>& value)
205 {
206 source.readTypeX(TC_MAP);
207 // typename std::map<T,U>::size_type size;
208 boost::uint32_t size;
209 deserializeX(source, size);
210 value.clear();
211 for (typename std::map<T, U>::size_type i = 0; i < size; i++) {
212 T t;
213 deserializeX(source, t);
214 deserializeX(source, value[t]);
215 }
216 }
217
219 template <typename T, typename U> inline void serializeX(Sink& sink, const std::multimap<T, U>& value)
220 {
221 sink.writeTypeX(TC_MULTIMAP);
222 if (std::numeric_limits<boost::uint32_t>::max() < value.size()) {
223 throw DCMTree::Exception("DCMTree_Serialization::serializeX()", "Value size too large for serialization");
224 }
225 serializeX(sink, static_cast<boost::uint32_t>(value.size()));
226
227 typename std::multimap<T, U>::const_iterator mapEnd = value.end();
228 for (typename std::multimap<T, U>::const_iterator i = value.begin(); i != mapEnd; i++) {
229 serializeX(sink, (*i).first);
230 serializeX(sink, (*i).second);
231 }
232 }
233
235 template <typename T, typename U> inline void deserializeX(Source& source, std::multimap<T, U>& value)
236 {
237 source.readTypeX(TC_MULTIMAP);
238 // typename std::multimap<T,U>::size_type size;
239 boost::uint32_t size;
240 deserializeX(source, size);
241 value.clear();
242 for (typename std::multimap<T, U>::size_type i = 0; i < size; i++) {
243 T t;
244 deserializeX(source, t);
245 U u;
246 deserializeX(source, u);
247 value.insert(std::make_pair(t, u));
248 }
249 }
250
252 template <typename T> inline void serializeX(Sink& sink, const std::set<T>& value)
253 {
254 sink.writeTypeX(TC_SET);
255 if (std::numeric_limits<boost::uint32_t>::max() < value.size()) {
256 throw DCMTree::Exception("DCMTree_Serialization::serializeX()", "Value size too large for serialization");
257 }
258 serializeX(sink, static_cast<boost::uint32_t>(value.size()));
259
260 typename std::set<T>::const_iterator setEnd = value.end();
261 for (typename std::set<T>::const_iterator i = value.begin(); i != setEnd; i++) {
262 serializeX(sink, (*i));
263 }
264 }
265
267 template <typename T> inline void deserializeX(Source& source, std::set<T>& value)
268 {
269 source.readTypeX(TC_SET);
270 // typename std::set<T>::size_type size;
271 boost::uint32_t size;
272 deserializeX(source, size);
273 value.clear();
274 for (typename std::set<T>::size_type i = 0; i < size; i++) {
275 T t;
276 deserializeX(source, t);
277 value.insert(t);
278 }
279 }
280
282 template <typename T> inline void serializeX(Sink& sink, const std::vector<T>& vl)
283 {
284 sink.writeTypeX(TC_VECTOR);
285 // BugID: 31906
286 // size_type of a vector is not standardized between 32 and 64-bit OSs. Force it to 32-bit max.
287 // serializePrimitiveX(sink, vl.size());
288 if (std::numeric_limits<std::uint32_t>::max() < vl.size()) {
289 throw DCMTree::Exception("DCMTree_Serialization::serializeX()", "Value size too large for serialization");
290 }
291 serializePrimitiveX(sink, static_cast<std::uint32_t>(vl.size()));
292
293 for (auto i = 0u; i < vl.size(); ++i) {
294 serializeX(sink, vl[i]);
295 }
296 }
297
299 template <typename T> inline void deserializeX(Source& source, std::vector<T>& vl)
300 {
301 source.readTypeX(TC_VECTOR);
302 // BugID: 31906
303 // size_type of a vector is not standardized between 32 and 64-bit OSs
304 // typename std::vector<T>::size_type count;
305 boost::uint32_t count;
306
307 deserializePrimitiveX(source, count);
308 vl.clear();
309 vl.resize(count);
310 // BugID: 31906
311 // for(typename std::vector<T>::size_type i = 0; i < count; ++i)
312 for (boost::uint32_t i = 0; i < count; ++i) {
313 deserializeX(source, vl[i]);
314 }
315 }
316
318 template <typename T> inline void serializeX(SerializerIntf<T>& serializer, Sink& sink, const T& value)
319 {
320 serializer.serializeX(sink, value);
321 }
322
324 template <typename T> inline void deserializeX(DeserializerIntf<T>& deserializer, Source& source, T& value)
325 {
326 value = deserializer.deserializeX(source);
327 }
328
330 template <typename T> inline bool serialize([[maybe_unused]] Sink& sink, const T& value)
331 {
332 try {
333 serializeX(value);
334 }
335 catch (... /*const SerializationException&*/) {
336 return false;
337 }
338
339 return true;
340 }
341
343 template <typename T> inline bool deserialize(Source& source, T& value)
344 {
345 try {
346 deserializeX(source, value);
347 }
348 catch (... /*const SerializationException&*/) {
349 return false;
350 }
351
352 return true;
353 }
354} // namespace DCMTree_Serialization
355
356#ifdef _MSC_VER
357#pragma warning(pop)
358#endif
359
360#endif
@ T
@ U
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.
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.
Interface of a data sink for the serialization of objects.
Interface of a data source for the deserialization of objects.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
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>.