MeVisLabToolboxReference
|
00001 //----------------------------------------------------------------------------- 00002 // **InsertLicense** code 00003 //----------------------------------------------------------------------------- 00005 00010 //----------------------------------------------------------------------------- 00011 00012 #ifndef CLASS_DCMTREE_SERIALIZATIONHELPER 00013 #define CLASS_DCMTREE_SERIALIZATIONHELPER 00014 00015 #include "DCMTree_Serialization.h" 00016 #include "DCMTree_Exception.h" 00017 00018 #include "boost/shared_ptr.hpp" 00019 00020 #include <string> 00021 #include <map> 00022 #include <vector> 00023 #include <set> 00024 #include <limits> 00025 00026 #ifdef _MSC_VER 00027 #pragma warning (push) 00028 #pragma warning (disable : 4018) 00029 #endif 00030 00031 namespace DCMTree_Serialization 00032 { 00034 template<typename T> 00035 inline void serializePrimitiveX(Sink &sink, T value) 00036 { 00037 sink.writeX(&value, sizeof(value)); 00038 } 00039 00041 template<typename T> 00042 inline void deserializePrimitiveX(Source &source, T &value) 00043 { 00044 source.readX(&value, sizeof(value)); 00045 } 00046 00048 void serializeX(Sink &sink, int value); 00049 00051 void deserializeX(Source &source, int &value); 00052 00054 void serializeX(Sink &sink, unsigned int value); 00055 00057 void deserializeX(Source &source, unsigned int &value); 00058 00060 void serializeX(Sink &sink, short value); 00061 00063 void deserializeX(Source &source, short &value); 00064 00066 void serializeX(Sink &sink, unsigned short value); 00067 00069 void deserializeX(Source &source, unsigned short &value); 00070 00072 void serializeX(Sink &sink, long value); 00073 00075 void deserializeX(Source &source, long &value); 00076 00078 void serializeX(Sink &sink, unsigned long value); 00079 00081 void deserializeX(Source &source, unsigned long &value); 00082 00084 void serializeX(Sink &sink, long long value); 00085 00087 void deserializeX(Source &source, long long &value); 00088 00090 void serializeX(Sink &sink, unsigned long long value); 00091 00093 void deserializeX(Source &source, unsigned long long &value); 00094 00096 void serializeX(Sink &sink, float value); 00097 00099 void deserializeX(Source &source, float &value); 00100 00102 void serializeX(Sink &sink, const double &value); 00103 00105 void deserializeX(Source &source, double &value); 00106 00108 void serializeX(Sink &sink, bool value); 00109 00111 void deserializeX(Source &source, bool &value); 00112 00114 void serializeX(Sink &sink, const std::string &value); 00115 00117 void deserializeX(Source &source, std::string &value); 00118 00120 template<typename T> 00121 inline void serializeEnumX(Sink &sink, T value) 00122 { 00123 sink.writeTypeX(TC_ENUM); 00124 serializePrimitiveX(sink, static_cast<int>(value)); 00125 } 00126 00128 template<typename T> 00129 inline void deserializeEnumX(Source &source, T &value) 00130 { 00131 source.readTypeX(TC_ENUM); 00132 int i = 0; 00133 deserializePrimitiveX(source, i); 00134 value = static_cast<T>(i); 00135 } 00136 00138 inline void serializeX(Sink &sink, const Serializable &value) 00139 { 00140 value.serializeX(sink); 00141 } 00142 00144 inline void deserializeX(Source &source, Deserializable &value) 00145 { 00146 value.deserializeX(source); 00147 } 00148 00150 template<typename T> 00151 inline void serializeX(Sink &sink, boost::shared_ptr<T> value) 00152 { 00153 serializeX(sink, *value); 00154 } 00155 00160 template<typename T> 00161 inline void deserializeIntoNewX(Source &source, boost::shared_ptr<T>& value) 00162 { 00163 value = boost::shared_ptr<T>(new T()); 00164 deserializeX(source, *value); 00165 } 00166 00170 template<typename T> 00171 inline void deserializeIntoExistingX(Source &source, boost::shared_ptr<T> value) 00172 { 00173 deserializeX(source, *value); 00174 } 00175 00177 template<typename T,typename U> 00178 inline void serializeX(Sink &sink, const std::pair<T,U> &value) 00179 { 00180 sink.writeTypeX(TC_PAIR); 00181 serializeX(sink, value.first); 00182 serializeX(sink, value.second); 00183 } 00184 00186 template<typename T,typename U> 00187 inline void deserializeX(Source &source, std::pair<T,U> &value) 00188 { 00189 source.readTypeX(TC_PAIR); 00190 deserializeX(source, value.first); 00191 deserializeX(source, value.second); 00192 } 00193 00195 template<typename T,typename U> 00196 inline void serializeX(Sink &sink, const std::map<T,U> &value) 00197 { 00198 sink.writeTypeX(TC_MAP); 00199 if ( std::numeric_limits<boost::uint32_t>::max() < value.size() ) 00200 { 00201 throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" ); 00202 } 00203 serializeX( sink, static_cast<boost::uint32_t>( value.size() ) ); 00204 00205 typename std::map<T,U>::const_iterator mapEnd = value.end(); 00206 for(typename std::map<T,U>::const_iterator i = value.begin(); i != mapEnd; i++) 00207 { 00208 serializeX(sink, (*i).first); 00209 serializeX(sink, (*i).second); 00210 } 00211 } 00212 00214 template<typename T,typename U> 00215 inline void deserializeX(Source &source, std::map<T,U> &value) 00216 { 00217 source.readTypeX(TC_MAP); 00218 //typename std::map<T,U>::size_type size; 00219 boost::uint32_t size; 00220 deserializeX(source, size); 00221 value.clear(); 00222 for(typename std::map<T,U>::size_type i = 0; i < size; i++) 00223 { 00224 T t; 00225 deserializeX(source, t); 00226 deserializeX(source, value[t]); 00227 } 00228 } 00229 00231 template<typename T,typename U> 00232 inline void serializeX(Sink &sink, const std::multimap<T,U> &value) 00233 { 00234 sink.writeTypeX(TC_MULTIMAP); 00235 if ( std::numeric_limits<boost::uint32_t>::max() < value.size() ) 00236 { 00237 throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" ); 00238 } 00239 serializeX( sink, static_cast<boost::uint32_t>( value.size() ) ); 00240 00241 typename std::multimap<T,U>::const_iterator mapEnd = value.end(); 00242 for(typename std::multimap<T,U>::const_iterator i = value.begin(); i != mapEnd; i++) 00243 { 00244 serializeX(sink, (*i).first); 00245 serializeX(sink, (*i).second); 00246 } 00247 } 00248 00250 template<typename T,typename U> 00251 inline void deserializeX(Source &source, std::multimap<T,U> &value) 00252 { 00253 source.readTypeX(TC_MULTIMAP); 00254 //typename std::multimap<T,U>::size_type size; 00255 boost::uint32_t size; 00256 deserializeX(source, size); 00257 value.clear(); 00258 for(typename std::multimap<T,U>::size_type i = 0; i < size; i++) 00259 { 00260 T t; 00261 deserializeX(source, t); 00262 U u; 00263 deserializeX(source, u); 00264 value.insert(std::make_pair(t,u)); 00265 } 00266 } 00267 00269 template<typename T> 00270 inline void serializeX(Sink &sink, const std::set<T> &value) 00271 { 00272 sink.writeTypeX(TC_SET); 00273 if ( std::numeric_limits<boost::uint32_t>::max() < value.size() ) 00274 { 00275 throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" ); 00276 } 00277 serializeX( sink, static_cast<boost::uint32_t>( value.size() ) ); 00278 00279 typename std::set<T>::const_iterator setEnd = value.end(); 00280 for(typename std::set<T>::const_iterator i = value.begin(); i != setEnd; i++) 00281 { 00282 serializeX(sink, (*i)); 00283 } 00284 } 00285 00287 template<typename T> 00288 inline void deserializeX(Source &source, std::set<T> &value) 00289 { 00290 source.readTypeX(TC_SET); 00291 //typename std::set<T>::size_type size; 00292 boost::uint32_t size; 00293 deserializeX(source, size); 00294 value.clear(); 00295 for(typename std::set<T>::size_type i = 0; i < size; i++) 00296 { 00297 T t; 00298 deserializeX(source, t); 00299 value.insert(t); 00300 } 00301 } 00302 00304 template<typename T> 00305 inline void serializeX(Sink &sink, const std::vector<T> &vl) 00306 { 00307 sink.writeTypeX(TC_VECTOR); 00308 // BugID: 31906 00309 // size_type of a vector is not standardized between 32 and 64 bit OSs. Force it to 32 bit max. 00310 //serializePrimitiveX(sink, vl.size()); 00311 if ( std::numeric_limits<boost::uint32_t>::max() < vl.size() ) 00312 { 00313 throw DCMTree::Exception( "DCMTree_Serialization::serializeX()", "Value size too large for serialization" ); 00314 } 00315 serializePrimitiveX( sink, static_cast<boost::uint32_t>( vl.size() ) ); 00316 00317 // BugID: 31906 00318 //for(typename std::vector<T>::size_type i = 0; i < vl.size(); ++i) 00319 for( boost::int64_t i = 0; i < vl.size(); ++i ) 00320 { 00321 serializeX(sink, vl[i]); 00322 } 00323 } 00324 00326 template<typename T> 00327 inline void deserializeX(Source &source, std::vector<T> &vl) 00328 { 00329 source.readTypeX(TC_VECTOR); 00330 // BugID: 31906 00331 // size_type of a vector is not standardized between 32 and 64 bit OSs 00332 //typename std::vector<T>::size_type count; 00333 boost::uint32_t count; 00334 00335 deserializePrimitiveX(source, count); 00336 vl.clear(); 00337 vl.resize(count); 00338 // BugID: 31906 00339 //for(typename std::vector<T>::size_type i = 0; i < count; ++i) 00340 for( boost::uint32_t i = 0; i < count; ++i) 00341 { 00342 deserializeX(source, vl[i]); 00343 } 00344 } 00345 00347 template<typename T> 00348 inline void serializeX(SerializerIntf<T> &serializer, Sink &sink, const T &value) 00349 { 00350 serializer.serializeX(sink, value); 00351 } 00352 00354 template<typename T> 00355 inline void deserializeX(DeserializerIntf<T> &deserializer, Source &source, T &value) 00356 { 00357 value = deserializer.deserializeX(source); 00358 } 00359 00361 template<typename T> 00362 inline bool serialize(Sink &sink, const T &value) 00363 { 00364 try { 00365 serializeX(value); 00366 } 00367 catch(.../*const SerializationException&*/) { 00368 return false; 00369 } 00370 00371 return true; 00372 } 00373 00375 template<typename T> 00376 inline bool deserialize(Source &source, T &value) 00377 { 00378 try { 00379 deserializeX(source,value); 00380 } 00381 catch(.../*const SerializationException&*/) { 00382 return false; 00383 } 00384 00385 return true; 00386 } 00387 } 00388 00389 #ifdef _MSC_VER 00390 #pragma warning (pop) 00391 #endif 00392 00393 #endif