|
Sierra Toolkit
Version of the Day
|
00001 /*------------------------------------------------------------------------*/ 00002 /* Copyright 2010 Sandia Corporation. */ 00003 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */ 00004 /* license for use of this work by or on behalf of the U.S. Government. */ 00005 /* Export of this program may require a license from the */ 00006 /* United States Government. */ 00007 /*------------------------------------------------------------------------*/ 00008 00009 #ifndef STK_UTIL_UTIL_MARSHAL_HPP 00010 #define STK_UTIL_UTIL_MARSHAL_HPP 00011 00012 #include <stdint.h> 00013 00014 #include <string> 00015 #include <sstream> 00016 #include <vector> 00017 #include <list> 00018 #include <typeinfo> 00019 00020 namespace stk_classic { 00021 00049 struct Marshal 00050 { 00055 enum { 00056 TYPE_CHECK_NONE = 0x00000000, 00057 TYPE_CHECK_POD = 0x00000001, 00058 TYPE_CHECK_LIST = 0x00000002, 00059 TYPE_CHECK_VECTOR = 0x00000004, 00060 TYPE_CHECK_ALL = 0xFFFFFFFF 00061 }; 00062 00067 Marshal(unsigned type_check = TYPE_CHECK_NONE); 00068 00075 explicit Marshal(const std::string &s); 00076 00083 std::string str() const; 00084 00091 size_t size() const; 00092 00101 void write(const char *address, size_t byte_count); 00102 00111 void read(char *address, size_t byte_count); 00112 00119 operator void * () const; 00120 00121 private: 00122 Marshal(const Marshal &marshal); 00123 Marshal &operator=(const Marshal &); 00124 00125 public: 00126 std::stringstream stream; 00127 unsigned m_typeCheck; 00128 }; 00129 00130 00142 template <typename T> 00143 Marshal &operator<<(Marshal &mout, const T &t); 00144 00155 template <typename T> 00156 Marshal &operator>>(Marshal &min, T &t); 00157 00170 template<> 00171 Marshal &operator<<(Marshal &mout, const std::type_info &t); 00172 00186 template<> 00187 Marshal &operator>>(Marshal &min, const std::type_info &t); 00188 00189 template<> 00190 Marshal &operator<<(Marshal &mout, const signed char &t); 00191 template<> 00192 Marshal &operator<<(Marshal &mout, const unsigned char &t); 00193 template<> 00194 Marshal &operator<<(Marshal &mout, const char &t); 00195 template<> 00196 Marshal &operator<<(Marshal &mout, const short &t); 00197 template<> 00198 Marshal &operator<<(Marshal &mout, const unsigned short &t); 00199 template<> 00200 Marshal &operator<<(Marshal &mout, const int &t); 00201 template<> 00202 Marshal &operator<<(Marshal &mout, const unsigned int &t); 00203 template<> 00204 Marshal &operator<<(Marshal &mout, const long &t); 00205 template<> 00206 Marshal &operator<<(Marshal &mout, const unsigned long &t); 00207 template<> 00208 Marshal &operator<<(Marshal &mout, const long long &t); 00209 template<> 00210 Marshal &operator<<(Marshal &mout, const unsigned long long &t); 00211 template<> 00212 Marshal &operator<<(Marshal &mout, const float &t); 00213 template<> 00214 Marshal &operator<<(Marshal &mout, const double &t); 00215 template<> 00216 Marshal &operator<<(Marshal &mout, const std::string &s); 00217 00218 template<> 00219 Marshal &operator>>(Marshal &min, signed char &t); 00220 template<> 00221 Marshal &operator>>(Marshal &min, unsigned char &t); 00222 template<> 00223 Marshal &operator>>(Marshal &min, char &t); 00224 template<> 00225 Marshal &operator>>(Marshal &min, short &t); 00226 template<> 00227 Marshal &operator>>(Marshal &min, unsigned short &t); 00228 template<> 00229 Marshal &operator>>(Marshal &min, int &t); 00230 template<> 00231 Marshal &operator>>(Marshal &min, unsigned int &t); 00232 template<> 00233 Marshal &operator>>(Marshal &min, long &t); 00234 template<> 00235 Marshal &operator>>(Marshal &min, unsigned long &t); 00236 template<> 00237 Marshal &operator>>(Marshal &min, long long &t); 00238 template<> 00239 Marshal &operator>>(Marshal &min, unsigned long long &t); 00240 template<> 00241 Marshal &operator>>(Marshal &min, float &t); 00242 template<> 00243 Marshal &operator>>(Marshal &min, double &t); 00244 template<> 00245 Marshal &operator>>(Marshal &min, std::string &s); 00246 00247 00248 template <class T> 00249 Marshal &operator<<(Marshal &mout, const std::vector<T> &v) { 00250 if (mout.m_typeCheck & Marshal::TYPE_CHECK_VECTOR) 00251 mout << typeid(v); 00252 00253 size_t size = v.size(); 00254 mout << size; 00255 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) 00256 mout << (*it); 00257 00258 return mout; 00259 } 00260 00261 template <class T> 00262 Marshal &operator>>(Marshal &min, std::vector<T> &v) { 00263 if (min.m_typeCheck & Marshal::TYPE_CHECK_VECTOR) 00264 min >> typeid(v); 00265 00266 size_t size = 0; 00267 min >> size; 00268 v.reserve(size); 00269 for (size_t i = 0; i < size; ++i) { 00270 T t; 00271 min >> t; 00272 v.push_back(t); 00273 } 00274 00275 return min; 00276 } 00277 00278 template <class T> 00279 Marshal &operator<<(Marshal &mout, const std::list<T> &l) { 00280 if (mout.m_typeCheck & Marshal::TYPE_CHECK_LIST) 00281 mout << typeid(l); 00282 00283 size_t size = l.size(); 00284 mout << size; 00285 for (typename std::list<T>::const_iterator it = l.begin(); it != l.end(); ++it) 00286 mout << (*it); 00287 00288 return mout; 00289 } 00290 00291 template <class T> 00292 Marshal &operator>>(Marshal &min, std::list<T> &l) { 00293 if (min.m_typeCheck & Marshal::TYPE_CHECK_LIST) 00294 min >> typeid(l); 00295 00296 size_t size; 00297 min >> size; 00298 for (size_t i = 0; i < size; ++i) { 00299 T t; 00300 min >> t; 00301 l.push_back(t); 00302 } 00303 00304 return min; 00305 } 00306 00307 template <class T> 00308 Marshal &write(Marshal &mout, const T &t) { 00309 mout.write((const char *) &t, sizeof(T)); 00310 00311 return mout; 00312 } 00313 00314 template <typename T> 00315 Marshal &read(Marshal &min, T &t) { 00316 t = T(); 00317 00318 min.read((char *) &t, sizeof(T)); 00319 return min; 00320 } 00321 00322 } // namespace stk_classic 00323 00324 #endif // STK_UTIL_UTIL_MARSHAL_HPP