[2861] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Functions to serialise most of the types/classed used in Orxonox |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #ifndef _Serialise_H__ |
---|
| 35 | #define _Serialise_H__ |
---|
| 36 | |
---|
| 37 | #include <cstring> |
---|
| 38 | #include "util/Math.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox{ |
---|
| 41 | |
---|
| 42 | // general template declaration |
---|
| 43 | |
---|
| 44 | /** @brief returns the size of the variable in a datastream */ |
---|
| 45 | template <class T> inline uint32_t returnSize( const T& ); |
---|
| 46 | /** @brief loads the value of a variable out of the bytestream and increases the mem pointer */ |
---|
| 47 | template <class T> inline void loadAndIncrease( const T&, uint8_t*& ); |
---|
| 48 | /** @brief saves the value of a variable into the bytestream and increases the mem pointer */ |
---|
| 49 | template <class T> inline void saveAndIncrease( const T&, uint8_t*& ); |
---|
| 50 | /** @brief checks whether the variable of type T is the same as in the bytestream */ |
---|
| 51 | template <class T> inline bool checkEquality( const T&, uint8_t* ); |
---|
| 52 | |
---|
| 53 | // =================== Template specialisation stuff ============= |
---|
| 54 | |
---|
| 55 | // =========== bool |
---|
| 56 | |
---|
| 57 | template <> inline uint32_t returnSize( const bool& variable ) |
---|
| 58 | { |
---|
| 59 | return sizeof(uint8_t); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | template <> inline void loadAndIncrease( const bool& variable, uint8_t*& mem ) |
---|
| 63 | { |
---|
| 64 | *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem); |
---|
| 65 | mem += returnSize( variable ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | template <> inline void saveAndIncrease( const bool& variable, uint8_t*& mem ) |
---|
| 69 | { |
---|
| 70 | *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable ); |
---|
| 71 | mem += returnSize( variable ); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | template <> inline bool checkEquality( const bool& variable, uint8_t* mem ) |
---|
| 75 | { |
---|
| 76 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable ); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // =========== char |
---|
| 80 | |
---|
| 81 | template <> inline uint32_t returnSize( const char& variable ) |
---|
| 82 | { |
---|
| 83 | return sizeof(uint8_t); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | template <> inline void loadAndIncrease( const char& variable, uint8_t*& mem ) |
---|
| 87 | { |
---|
| 88 | *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem); |
---|
| 89 | mem += returnSize( variable ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | template <> inline void saveAndIncrease( const char& variable, uint8_t*& mem ) |
---|
| 93 | { |
---|
| 94 | *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable ); |
---|
| 95 | mem += returnSize( variable ); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | template <> inline bool checkEquality( const char& variable, uint8_t* mem ) |
---|
| 99 | { |
---|
| 100 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable ); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | // =========== unsigned char |
---|
| 104 | |
---|
| 105 | template <> inline uint32_t returnSize( const unsigned char& variable ) |
---|
| 106 | { |
---|
| 107 | return sizeof(uint8_t); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | template <> inline void loadAndIncrease( const unsigned char& variable, uint8_t*& mem ) |
---|
| 111 | { |
---|
| 112 | *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem); |
---|
| 113 | mem += returnSize( variable ); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | template <> inline void saveAndIncrease( const unsigned char& variable, uint8_t*& mem ) |
---|
| 117 | { |
---|
| 118 | *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable ); |
---|
| 119 | mem += returnSize( variable ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | template <> inline bool checkEquality( const unsigned char& variable, uint8_t* mem ) |
---|
| 123 | { |
---|
| 124 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable ); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // =========== short |
---|
| 128 | |
---|
| 129 | template <> inline uint32_t returnSize( const short& variable ) |
---|
| 130 | { |
---|
| 131 | return sizeof(int16_t); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | template <> inline void loadAndIncrease( const short& variable, uint8_t*& mem ) |
---|
| 135 | { |
---|
| 136 | *(short*)( &variable ) = *(int16_t*)(mem); |
---|
| 137 | mem += returnSize( variable ); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | template <> inline void saveAndIncrease( const short& variable, uint8_t*& mem ) |
---|
| 141 | { |
---|
| 142 | *(int16_t*)(mem) = variable; |
---|
| 143 | mem += returnSize( variable ); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | template <> inline bool checkEquality( const short& variable, uint8_t* mem ) |
---|
| 147 | { |
---|
| 148 | return *(int16_t*)(mem) == static_cast<int16_t>(variable); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // =========== unsigned short |
---|
| 152 | |
---|
| 153 | template <> inline uint32_t returnSize( const unsigned short& variable ) |
---|
| 154 | { |
---|
| 155 | return sizeof(uint16_t); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | template <> inline void loadAndIncrease( const unsigned short& variable, uint8_t*& mem ) |
---|
| 159 | { |
---|
| 160 | *(unsigned short*)( &variable ) = *(uint16_t*)(mem); |
---|
| 161 | mem += returnSize( variable ); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | template <> inline void saveAndIncrease( const unsigned short& variable, uint8_t*& mem ) |
---|
| 165 | { |
---|
| 166 | *(uint16_t*)(mem) = variable; |
---|
| 167 | mem += returnSize( variable ); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | template <> inline bool checkEquality( const unsigned short& variable, uint8_t* mem ) |
---|
| 171 | { |
---|
| 172 | return *(uint16_t*)(mem) == variable; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | // =========== int |
---|
| 176 | |
---|
| 177 | template <> inline uint32_t returnSize( const int& variable ) |
---|
| 178 | { |
---|
| 179 | return sizeof(int32_t); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | template <> inline void loadAndIncrease( const int& variable, uint8_t*& mem ) |
---|
| 183 | { |
---|
| 184 | *(int *)( &variable ) = *(int32_t*)(mem); |
---|
| 185 | mem += returnSize( variable ); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | template <> inline void saveAndIncrease( const int& variable, uint8_t*& mem ) |
---|
| 189 | { |
---|
| 190 | *(int32_t*)(mem) = variable; |
---|
| 191 | mem += returnSize( variable ); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | template <> inline bool checkEquality( const int& variable, uint8_t* mem ) |
---|
| 195 | { |
---|
| 196 | return *(int32_t*)(mem) == variable; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // =========== unsigned int |
---|
| 200 | |
---|
| 201 | template <> inline uint32_t returnSize( const unsigned int& variable ) |
---|
| 202 | { |
---|
| 203 | return sizeof(uint32_t); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | template <> inline void loadAndIncrease( const unsigned int& variable, uint8_t*& mem ) |
---|
| 207 | { |
---|
| 208 | *(unsigned int*)( &variable ) = *(uint32_t*)(mem); |
---|
| 209 | mem += returnSize( variable ); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | template <> inline void saveAndIncrease( const unsigned int& variable, uint8_t*& mem ) |
---|
| 213 | { |
---|
| 214 | *(uint32_t*)(mem) = variable; |
---|
| 215 | mem += returnSize( variable ); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | template <> inline bool checkEquality( const unsigned int& variable, uint8_t* mem ) |
---|
| 219 | { |
---|
| 220 | return *(uint32_t*)(mem) == variable; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // =========== long |
---|
| 224 | |
---|
| 225 | template <> inline uint32_t returnSize( const long& variable ) |
---|
| 226 | { |
---|
| 227 | return sizeof(int32_t); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | template <> inline void loadAndIncrease( const long& variable, uint8_t*& mem ) |
---|
| 231 | { |
---|
| 232 | *(long*)( &variable ) = *(int32_t*)(mem); |
---|
| 233 | mem += returnSize( variable ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | template <> inline void saveAndIncrease( const long& variable, uint8_t*& mem ) |
---|
| 237 | { |
---|
| 238 | *(int32_t*)(mem) = variable; |
---|
| 239 | mem += returnSize( variable ); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | template <> inline bool checkEquality( const long& variable, uint8_t* mem ) |
---|
| 243 | { |
---|
| 244 | return *(int32_t*)(mem) == variable; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | // =========== unsigned long |
---|
| 248 | |
---|
| 249 | template <> inline uint32_t returnSize( const unsigned long& variable ) |
---|
| 250 | { |
---|
| 251 | return sizeof(uint32_t); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | template <> inline void loadAndIncrease( const unsigned long& variable, uint8_t*& mem ) |
---|
| 255 | { |
---|
| 256 | *(unsigned long*)( &variable ) = *(uint32_t*)(mem); |
---|
| 257 | mem += returnSize( variable ); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | template <> inline void saveAndIncrease( const unsigned long& variable, uint8_t*& mem ) |
---|
| 261 | { |
---|
| 262 | *(uint32_t*)(mem) = variable; |
---|
| 263 | mem += returnSize( variable ); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | template <> inline bool checkEquality( const unsigned long& variable, uint8_t* mem ) |
---|
| 267 | { |
---|
| 268 | return *(uint32_t*)(mem) == variable; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | // =========== long long |
---|
| 272 | |
---|
| 273 | template <> inline uint32_t returnSize( const long long& variable ) |
---|
| 274 | { |
---|
| 275 | return sizeof(int64_t); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | template <> inline void loadAndIncrease( const long long& variable, uint8_t*& mem ) |
---|
| 279 | { |
---|
| 280 | *(long long*)( &variable ) = *(int64_t*)(mem); |
---|
| 281 | mem += returnSize( variable ); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | template <> inline void saveAndIncrease( const long long& variable, uint8_t*& mem ) |
---|
| 285 | { |
---|
| 286 | *(int64_t*)(mem) = variable; |
---|
| 287 | mem += returnSize( variable ); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | template <> inline bool checkEquality( const long long& variable, uint8_t* mem ) |
---|
| 291 | { |
---|
| 292 | return *(int64_t*)(mem) == variable; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | // =========== unsigned long long |
---|
| 296 | |
---|
| 297 | template <> inline uint32_t returnSize( const unsigned long long& variable ) |
---|
| 298 | { |
---|
| 299 | return sizeof(uint64_t); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | template <> inline void loadAndIncrease( const unsigned long long& variable, uint8_t*& mem ) |
---|
| 303 | { |
---|
| 304 | *(unsigned long long*)( &variable ) = *(uint64_t*)(mem); |
---|
| 305 | mem += returnSize( variable ); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | template <> inline void saveAndIncrease( const unsigned long long& variable, uint8_t*& mem ) |
---|
| 309 | { |
---|
| 310 | *(uint64_t*)(mem) = variable; |
---|
| 311 | mem += returnSize( variable ); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | template <> inline bool checkEquality( const unsigned long long& variable, uint8_t* mem ) |
---|
| 315 | { |
---|
| 316 | return *(uint64_t*)(mem) == variable; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | // =========== float |
---|
| 320 | |
---|
| 321 | template <> inline uint32_t returnSize( const float& variable ) |
---|
| 322 | { |
---|
| 323 | return sizeof(uint32_t); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | template <> inline void loadAndIncrease( const float& variable, uint8_t*& mem ) |
---|
| 327 | { |
---|
| 328 | *(uint32_t*)( &variable ) = *(uint32_t*)(mem); |
---|
| 329 | mem += returnSize( variable ); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | template <> inline void saveAndIncrease( const float& variable, uint8_t*& mem ) |
---|
| 333 | { |
---|
| 334 | *(uint32_t*)(mem) = *(uint32_t*)( &variable ); |
---|
| 335 | mem += returnSize( variable ); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | template <> inline bool checkEquality( const float& variable, uint8_t* mem ) |
---|
| 339 | { |
---|
| 340 | return *(uint32_t*)(mem) == *(uint32_t*)( &variable ); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | // =========== double |
---|
| 344 | |
---|
| 345 | template <> inline uint32_t returnSize( const double& variable ) |
---|
| 346 | { |
---|
| 347 | return sizeof(uint64_t); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | template <> inline void loadAndIncrease( const double& variable, uint8_t*& mem ) |
---|
| 351 | { |
---|
| 352 | *(uint64_t*)( &variable ) = *(uint64_t*)(mem); |
---|
| 353 | mem += returnSize( variable ); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | template <> inline void saveAndIncrease( const double& variable, uint8_t*& mem ) |
---|
| 357 | { |
---|
| 358 | *(uint64_t*)(mem) = *(uint64_t*)( &variable ); |
---|
| 359 | mem += returnSize( variable ); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | template <> inline bool checkEquality( const double& variable, uint8_t* mem ) |
---|
| 363 | { |
---|
| 364 | return *(uint64_t*)(mem) == *(uint64_t*)( &variable ); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | // =========== long double |
---|
| 368 | |
---|
| 369 | template <> inline uint32_t returnSize( const long double& variable ) |
---|
| 370 | { |
---|
| 371 | return sizeof(uint64_t); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | template <> inline void loadAndIncrease( const long double& variable, uint8_t*& mem ) |
---|
| 375 | { |
---|
| 376 | double temp; |
---|
| 377 | memcpy(&temp, mem, sizeof(uint64_t)); |
---|
| 378 | *(long double*)( &variable ) = static_cast<const long double>(temp); |
---|
| 379 | mem += returnSize( variable ); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | template <> inline void saveAndIncrease( const long double& variable, uint8_t*& mem ) |
---|
| 383 | { |
---|
| 384 | double temp = static_cast<double>(variable); |
---|
| 385 | memcpy(mem, &temp, sizeof(uint64_t)); |
---|
| 386 | mem += returnSize( variable ); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | template <> inline bool checkEquality( const long double& variable, uint8_t* mem ) |
---|
| 390 | { |
---|
| 391 | double temp = static_cast<double>(variable); |
---|
| 392 | return memcmp(&temp, mem, sizeof(uint64_t))==0; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | // =========== string |
---|
| 396 | |
---|
| 397 | template <> inline uint32_t returnSize( const std::string& variable ) |
---|
| 398 | { |
---|
| 399 | return variable.length()+1; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | template <> inline void saveAndIncrease( const std::string& variable, uint8_t*& mem ) |
---|
| 403 | { |
---|
| 404 | memcpy(mem, variable.c_str(), variable.length()+1); |
---|
| 405 | mem += variable.length()+1; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | template <> inline void loadAndIncrease( const std::string& variable, uint8_t*& mem ) |
---|
| 409 | { |
---|
| 410 | *(std::string*)( &variable ) = std::string((const char *)mem); |
---|
| 411 | mem += variable.length()+1; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | template <> inline bool checkEquality( const std::string& variable, uint8_t* mem ) |
---|
| 415 | { |
---|
| 416 | return std::string((const char*)mem)==variable; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | // =========== Degree |
---|
| 420 | |
---|
| 421 | template <> inline uint32_t returnSize( const Degree& variable ) |
---|
| 422 | { |
---|
| 423 | return sizeof(Ogre::Real); |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | template <> inline void saveAndIncrease( const Degree& variable, uint8_t*& mem ) |
---|
| 427 | { |
---|
| 428 | Ogre::Real r = variable.valueDegrees(); |
---|
| 429 | memcpy(mem, &r, returnSize( variable )); |
---|
| 430 | mem += returnSize( variable ); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | template <> inline void loadAndIncrease( const Degree& variable, uint8_t*& mem ) |
---|
| 434 | { |
---|
| 435 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 436 | (Degree&)variable = *r; |
---|
| 437 | mem += returnSize( variable ); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | template <> inline bool checkEquality( const Degree& variable, uint8_t* mem ) |
---|
| 441 | { |
---|
| 442 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 443 | return variable==Degree(*r); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | // =========== Radian |
---|
| 447 | |
---|
| 448 | template <> inline uint32_t returnSize( const Radian& variable ) |
---|
| 449 | { |
---|
| 450 | return sizeof(Ogre::Real); |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | template <> inline void saveAndIncrease( const Radian& variable, uint8_t*& mem ) |
---|
| 454 | { |
---|
| 455 | Ogre::Real r = variable.valueRadians(); |
---|
| 456 | memcpy(mem, &r, returnSize( variable )); |
---|
| 457 | mem += returnSize( variable ); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | template <> inline void loadAndIncrease( const Radian& variable, uint8_t*& mem ) |
---|
| 461 | { |
---|
| 462 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 463 | (Radian&)variable = *r; |
---|
| 464 | mem += returnSize( variable ); |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | template <> inline bool checkEquality( const Radian& variable, uint8_t* mem ) |
---|
| 468 | { |
---|
| 469 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 470 | return variable==Degree(*r); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | #endif |
---|