[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 | /** @brief returns the size of the variable in a datastream */ |
---|
[6123] | 43 | template <class T> inline uint32_t returnSize( const T& variable ); |
---|
[2861] | 44 | /** @brief loads the value of a variable out of the bytestream and increases the mem pointer */ |
---|
[6123] | 45 | template <class T> inline void loadAndIncrease( const T& variable, uint8_t*& mem ); |
---|
[2861] | 46 | /** @brief saves the value of a variable into the bytestream and increases the mem pointer */ |
---|
[6123] | 47 | template <class T> inline void saveAndIncrease( const T& variable, uint8_t*& mem ); |
---|
[2861] | 48 | /** @brief checks whether the variable of type T is the same as in the bytestream */ |
---|
[6123] | 49 | template <class T> inline bool checkEquality( const T& variable, uint8_t* mem ); |
---|
[2861] | 50 | |
---|
| 51 | // =================== Template specialisation stuff ============= |
---|
| 52 | |
---|
| 53 | // =========== bool |
---|
| 54 | |
---|
| 55 | template <> inline uint32_t returnSize( const bool& variable ) |
---|
| 56 | { |
---|
| 57 | return sizeof(uint8_t); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | template <> inline void loadAndIncrease( const bool& variable, uint8_t*& mem ) |
---|
| 61 | { |
---|
| 62 | *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem); |
---|
| 63 | mem += returnSize( variable ); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | template <> inline void saveAndIncrease( const bool& variable, uint8_t*& mem ) |
---|
| 67 | { |
---|
| 68 | *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable ); |
---|
| 69 | mem += returnSize( variable ); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | template <> inline bool checkEquality( const bool& variable, uint8_t* mem ) |
---|
| 73 | { |
---|
| 74 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable ); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // =========== char |
---|
| 78 | |
---|
| 79 | template <> inline uint32_t returnSize( const char& variable ) |
---|
| 80 | { |
---|
| 81 | return sizeof(uint8_t); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | template <> inline void loadAndIncrease( const char& variable, uint8_t*& mem ) |
---|
| 85 | { |
---|
| 86 | *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem); |
---|
| 87 | mem += returnSize( variable ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | template <> inline void saveAndIncrease( const char& variable, uint8_t*& mem ) |
---|
| 91 | { |
---|
| 92 | *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable ); |
---|
| 93 | mem += returnSize( variable ); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | template <> inline bool checkEquality( const char& variable, uint8_t* mem ) |
---|
| 97 | { |
---|
| 98 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable ); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // =========== unsigned char |
---|
| 102 | |
---|
| 103 | template <> inline uint32_t returnSize( const unsigned char& variable ) |
---|
| 104 | { |
---|
| 105 | return sizeof(uint8_t); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | template <> inline void loadAndIncrease( const unsigned char& variable, uint8_t*& mem ) |
---|
| 109 | { |
---|
| 110 | *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem); |
---|
| 111 | mem += returnSize( variable ); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | template <> inline void saveAndIncrease( const unsigned char& variable, uint8_t*& mem ) |
---|
| 115 | { |
---|
| 116 | *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable ); |
---|
| 117 | mem += returnSize( variable ); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | template <> inline bool checkEquality( const unsigned char& variable, uint8_t* mem ) |
---|
| 121 | { |
---|
| 122 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // =========== short |
---|
| 126 | |
---|
| 127 | template <> inline uint32_t returnSize( const short& variable ) |
---|
| 128 | { |
---|
| 129 | return sizeof(int16_t); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | template <> inline void loadAndIncrease( const short& variable, uint8_t*& mem ) |
---|
| 133 | { |
---|
| 134 | *(short*)( &variable ) = *(int16_t*)(mem); |
---|
| 135 | mem += returnSize( variable ); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | template <> inline void saveAndIncrease( const short& variable, uint8_t*& mem ) |
---|
| 139 | { |
---|
| 140 | *(int16_t*)(mem) = variable; |
---|
| 141 | mem += returnSize( variable ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | template <> inline bool checkEquality( const short& variable, uint8_t* mem ) |
---|
| 145 | { |
---|
| 146 | return *(int16_t*)(mem) == static_cast<int16_t>(variable); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | // =========== unsigned short |
---|
| 150 | |
---|
| 151 | template <> inline uint32_t returnSize( const unsigned short& variable ) |
---|
| 152 | { |
---|
| 153 | return sizeof(uint16_t); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | template <> inline void loadAndIncrease( const unsigned short& variable, uint8_t*& mem ) |
---|
| 157 | { |
---|
| 158 | *(unsigned short*)( &variable ) = *(uint16_t*)(mem); |
---|
| 159 | mem += returnSize( variable ); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | template <> inline void saveAndIncrease( const unsigned short& variable, uint8_t*& mem ) |
---|
| 163 | { |
---|
| 164 | *(uint16_t*)(mem) = variable; |
---|
| 165 | mem += returnSize( variable ); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | template <> inline bool checkEquality( const unsigned short& variable, uint8_t* mem ) |
---|
| 169 | { |
---|
| 170 | return *(uint16_t*)(mem) == variable; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // =========== int |
---|
| 174 | |
---|
| 175 | template <> inline uint32_t returnSize( const int& variable ) |
---|
| 176 | { |
---|
| 177 | return sizeof(int32_t); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | template <> inline void loadAndIncrease( const int& variable, uint8_t*& mem ) |
---|
| 181 | { |
---|
| 182 | *(int *)( &variable ) = *(int32_t*)(mem); |
---|
| 183 | mem += returnSize( variable ); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | template <> inline void saveAndIncrease( const int& variable, uint8_t*& mem ) |
---|
| 187 | { |
---|
| 188 | *(int32_t*)(mem) = variable; |
---|
| 189 | mem += returnSize( variable ); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | template <> inline bool checkEquality( const int& variable, uint8_t* mem ) |
---|
| 193 | { |
---|
| 194 | return *(int32_t*)(mem) == variable; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | // =========== unsigned int |
---|
| 198 | |
---|
| 199 | template <> inline uint32_t returnSize( const unsigned int& variable ) |
---|
| 200 | { |
---|
| 201 | return sizeof(uint32_t); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | template <> inline void loadAndIncrease( const unsigned int& variable, uint8_t*& mem ) |
---|
| 205 | { |
---|
| 206 | *(unsigned int*)( &variable ) = *(uint32_t*)(mem); |
---|
| 207 | mem += returnSize( variable ); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | template <> inline void saveAndIncrease( const unsigned int& variable, uint8_t*& mem ) |
---|
| 211 | { |
---|
| 212 | *(uint32_t*)(mem) = variable; |
---|
| 213 | mem += returnSize( variable ); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | template <> inline bool checkEquality( const unsigned int& variable, uint8_t* mem ) |
---|
| 217 | { |
---|
| 218 | return *(uint32_t*)(mem) == variable; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | // =========== long |
---|
| 222 | |
---|
| 223 | template <> inline uint32_t returnSize( const long& variable ) |
---|
| 224 | { |
---|
| 225 | return sizeof(int32_t); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | template <> inline void loadAndIncrease( const long& variable, uint8_t*& mem ) |
---|
| 229 | { |
---|
| 230 | *(long*)( &variable ) = *(int32_t*)(mem); |
---|
| 231 | mem += returnSize( variable ); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | template <> inline void saveAndIncrease( const long& variable, uint8_t*& mem ) |
---|
| 235 | { |
---|
| 236 | *(int32_t*)(mem) = variable; |
---|
| 237 | mem += returnSize( variable ); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | template <> inline bool checkEquality( const long& variable, uint8_t* mem ) |
---|
| 241 | { |
---|
| 242 | return *(int32_t*)(mem) == variable; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | // =========== unsigned long |
---|
| 246 | |
---|
| 247 | template <> inline uint32_t returnSize( const unsigned long& variable ) |
---|
| 248 | { |
---|
| 249 | return sizeof(uint32_t); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | template <> inline void loadAndIncrease( const unsigned long& variable, uint8_t*& mem ) |
---|
| 253 | { |
---|
| 254 | *(unsigned long*)( &variable ) = *(uint32_t*)(mem); |
---|
| 255 | mem += returnSize( variable ); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | template <> inline void saveAndIncrease( const unsigned long& variable, uint8_t*& mem ) |
---|
| 259 | { |
---|
| 260 | *(uint32_t*)(mem) = variable; |
---|
| 261 | mem += returnSize( variable ); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | template <> inline bool checkEquality( const unsigned long& variable, uint8_t* mem ) |
---|
| 265 | { |
---|
| 266 | return *(uint32_t*)(mem) == variable; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | // =========== long long |
---|
| 270 | |
---|
| 271 | template <> inline uint32_t returnSize( const long long& variable ) |
---|
| 272 | { |
---|
| 273 | return sizeof(int64_t); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | template <> inline void loadAndIncrease( const long long& variable, uint8_t*& mem ) |
---|
| 277 | { |
---|
| 278 | *(long long*)( &variable ) = *(int64_t*)(mem); |
---|
| 279 | mem += returnSize( variable ); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | template <> inline void saveAndIncrease( const long long& variable, uint8_t*& mem ) |
---|
| 283 | { |
---|
| 284 | *(int64_t*)(mem) = variable; |
---|
| 285 | mem += returnSize( variable ); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | template <> inline bool checkEquality( const long long& variable, uint8_t* mem ) |
---|
| 289 | { |
---|
| 290 | return *(int64_t*)(mem) == variable; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | // =========== unsigned long long |
---|
| 294 | |
---|
| 295 | template <> inline uint32_t returnSize( const unsigned long long& variable ) |
---|
| 296 | { |
---|
| 297 | return sizeof(uint64_t); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | template <> inline void loadAndIncrease( const unsigned long long& variable, uint8_t*& mem ) |
---|
| 301 | { |
---|
| 302 | *(unsigned long long*)( &variable ) = *(uint64_t*)(mem); |
---|
| 303 | mem += returnSize( variable ); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | template <> inline void saveAndIncrease( const unsigned long long& variable, uint8_t*& mem ) |
---|
| 307 | { |
---|
| 308 | *(uint64_t*)(mem) = variable; |
---|
| 309 | mem += returnSize( variable ); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | template <> inline bool checkEquality( const unsigned long long& variable, uint8_t* mem ) |
---|
| 313 | { |
---|
| 314 | return *(uint64_t*)(mem) == variable; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | // =========== float |
---|
| 318 | |
---|
| 319 | template <> inline uint32_t returnSize( const float& variable ) |
---|
| 320 | { |
---|
| 321 | return sizeof(uint32_t); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | template <> inline void loadAndIncrease( const float& variable, uint8_t*& mem ) |
---|
| 325 | { |
---|
| 326 | *(uint32_t*)( &variable ) = *(uint32_t*)(mem); |
---|
| 327 | mem += returnSize( variable ); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | template <> inline void saveAndIncrease( const float& variable, uint8_t*& mem ) |
---|
| 331 | { |
---|
| 332 | *(uint32_t*)(mem) = *(uint32_t*)( &variable ); |
---|
| 333 | mem += returnSize( variable ); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | template <> inline bool checkEquality( const float& variable, uint8_t* mem ) |
---|
| 337 | { |
---|
| 338 | return *(uint32_t*)(mem) == *(uint32_t*)( &variable ); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | // =========== double |
---|
| 342 | |
---|
| 343 | template <> inline uint32_t returnSize( const double& variable ) |
---|
| 344 | { |
---|
| 345 | return sizeof(uint64_t); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | template <> inline void loadAndIncrease( const double& variable, uint8_t*& mem ) |
---|
| 349 | { |
---|
| 350 | *(uint64_t*)( &variable ) = *(uint64_t*)(mem); |
---|
| 351 | mem += returnSize( variable ); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | template <> inline void saveAndIncrease( const double& variable, uint8_t*& mem ) |
---|
| 355 | { |
---|
| 356 | *(uint64_t*)(mem) = *(uint64_t*)( &variable ); |
---|
| 357 | mem += returnSize( variable ); |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | template <> inline bool checkEquality( const double& variable, uint8_t* mem ) |
---|
| 361 | { |
---|
| 362 | return *(uint64_t*)(mem) == *(uint64_t*)( &variable ); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | // =========== long double |
---|
| 366 | |
---|
| 367 | template <> inline uint32_t returnSize( const long double& variable ) |
---|
| 368 | { |
---|
| 369 | return sizeof(uint64_t); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | template <> inline void loadAndIncrease( const long double& variable, uint8_t*& mem ) |
---|
| 373 | { |
---|
| 374 | double temp; |
---|
| 375 | memcpy(&temp, mem, sizeof(uint64_t)); |
---|
| 376 | *(long double*)( &variable ) = static_cast<const long double>(temp); |
---|
| 377 | mem += returnSize( variable ); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | template <> inline void saveAndIncrease( const long double& variable, uint8_t*& mem ) |
---|
| 381 | { |
---|
| 382 | double temp = static_cast<double>(variable); |
---|
| 383 | memcpy(mem, &temp, sizeof(uint64_t)); |
---|
| 384 | mem += returnSize( variable ); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | template <> inline bool checkEquality( const long double& variable, uint8_t* mem ) |
---|
| 388 | { |
---|
| 389 | double temp = static_cast<double>(variable); |
---|
| 390 | return memcmp(&temp, mem, sizeof(uint64_t))==0; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | // =========== string |
---|
| 394 | |
---|
| 395 | template <> inline uint32_t returnSize( const std::string& variable ) |
---|
| 396 | { |
---|
| 397 | return variable.length()+1; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | template <> inline void saveAndIncrease( const std::string& variable, uint8_t*& mem ) |
---|
| 401 | { |
---|
| 402 | memcpy(mem, variable.c_str(), variable.length()+1); |
---|
| 403 | mem += variable.length()+1; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | template <> inline void loadAndIncrease( const std::string& variable, uint8_t*& mem ) |
---|
| 407 | { |
---|
[3304] | 408 | *(std::string*)( &variable ) = (const char *)mem; |
---|
[2861] | 409 | mem += variable.length()+1; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | template <> inline bool checkEquality( const std::string& variable, uint8_t* mem ) |
---|
| 413 | { |
---|
[3304] | 414 | //return std::string((const char*)mem)==variable; |
---|
| 415 | return (const char*)mem==variable; |
---|
[2861] | 416 | } |
---|
| 417 | |
---|
| 418 | // =========== Degree |
---|
| 419 | |
---|
| 420 | template <> inline uint32_t returnSize( const Degree& variable ) |
---|
| 421 | { |
---|
| 422 | return sizeof(Ogre::Real); |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | template <> inline void saveAndIncrease( const Degree& variable, uint8_t*& mem ) |
---|
| 426 | { |
---|
| 427 | Ogre::Real r = variable.valueDegrees(); |
---|
| 428 | memcpy(mem, &r, returnSize( variable )); |
---|
| 429 | mem += returnSize( variable ); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | template <> inline void loadAndIncrease( const Degree& variable, uint8_t*& mem ) |
---|
| 433 | { |
---|
| 434 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 435 | (Degree&)variable = *r; |
---|
| 436 | mem += returnSize( variable ); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | template <> inline bool checkEquality( const Degree& variable, uint8_t* mem ) |
---|
| 440 | { |
---|
| 441 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 442 | return variable==Degree(*r); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | // =========== Radian |
---|
| 446 | |
---|
| 447 | template <> inline uint32_t returnSize( const Radian& variable ) |
---|
| 448 | { |
---|
| 449 | return sizeof(Ogre::Real); |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | template <> inline void saveAndIncrease( const Radian& variable, uint8_t*& mem ) |
---|
| 453 | { |
---|
| 454 | Ogre::Real r = variable.valueRadians(); |
---|
| 455 | memcpy(mem, &r, returnSize( variable )); |
---|
| 456 | mem += returnSize( variable ); |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | template <> inline void loadAndIncrease( const Radian& variable, uint8_t*& mem ) |
---|
| 460 | { |
---|
| 461 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 462 | (Radian&)variable = *r; |
---|
| 463 | mem += returnSize( variable ); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | template <> inline bool checkEquality( const Radian& variable, uint8_t* mem ) |
---|
| 467 | { |
---|
| 468 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 469 | return variable==Degree(*r); |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | |
---|
| 474 | #endif |
---|