[2245] | 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, (C) 2008 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "SynchronisableVariable.h" |
---|
| 30 | #include <cstring> |
---|
| 31 | #include "util/Math.h" |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | namespace orxonox{ |
---|
| 35 | |
---|
| 36 | uint8_t SynchronisableVariableBase::state_ = 0; |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | // =================== Template specialisation stuff ============= |
---|
| 41 | |
---|
| 42 | // =========== bool |
---|
| 43 | |
---|
| 44 | template <> uint32_t SynchronisableVariable<const bool>::returnSize() |
---|
| 45 | { |
---|
| 46 | return sizeof(uint8_t); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | template <> void SynchronisableVariable<const bool>::setAndIncrease( uint8_t*& mem ) |
---|
| 50 | { |
---|
| 51 | *(uint8_t*)(&this->variable_) = *static_cast<uint8_t*>(mem); |
---|
| 52 | mem += SynchronisableVariable<const bool>::returnSize(); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | template <> void SynchronisableVariable<const bool>::getAndIncrease( uint8_t*& mem ) |
---|
| 56 | { |
---|
| 57 | *static_cast<uint8_t*>(mem) = *(uint8_t*)(&this->variable_); |
---|
| 58 | mem += SynchronisableVariable<const bool>::returnSize(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | template <> bool SynchronisableVariable<const bool>::checkEquality( uint8_t* mem ) |
---|
| 62 | { |
---|
| 63 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)(&this->variable_); |
---|
| 64 | } |
---|
| 65 | |
---|
[2309] | 66 | // =========== char |
---|
| 67 | |
---|
| 68 | template <> uint32_t SynchronisableVariable<const char>::returnSize() |
---|
| 69 | { |
---|
| 70 | return sizeof(uint8_t); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | template <> void SynchronisableVariable<const char>::setAndIncrease( uint8_t*& mem ) |
---|
| 74 | { |
---|
| 75 | *(uint8_t*)(&this->variable_) = *static_cast<uint8_t*>(mem); |
---|
| 76 | mem += SynchronisableVariable<const char>::returnSize(); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | template <> void SynchronisableVariable<const char>::getAndIncrease( uint8_t*& mem ) |
---|
| 80 | { |
---|
| 81 | *static_cast<uint8_t*>(mem) = *(uint8_t*)(&this->variable_); |
---|
| 82 | mem += SynchronisableVariable<const char>::returnSize(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | template <> bool SynchronisableVariable<const char>::checkEquality( uint8_t* mem ) |
---|
| 86 | { |
---|
| 87 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)(&this->variable_); |
---|
| 88 | } |
---|
| 89 | |
---|
[2245] | 90 | // =========== unsigned char |
---|
| 91 | |
---|
| 92 | template <> uint32_t SynchronisableVariable<const unsigned char>::returnSize() |
---|
| 93 | { |
---|
| 94 | return sizeof(uint8_t); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | template <> void SynchronisableVariable<const unsigned char>::setAndIncrease( uint8_t*& mem ) |
---|
| 98 | { |
---|
| 99 | *(uint8_t*)(&this->variable_) = *static_cast<uint8_t*>(mem); |
---|
| 100 | mem += SynchronisableVariable<const unsigned char>::returnSize(); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | template <> void SynchronisableVariable<const unsigned char>::getAndIncrease( uint8_t*& mem ) |
---|
| 104 | { |
---|
| 105 | *static_cast<uint8_t*>(mem) = *(uint8_t*)(&this->variable_); |
---|
| 106 | mem += SynchronisableVariable<const unsigned char>::returnSize(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | template <> bool SynchronisableVariable<const unsigned char>::checkEquality( uint8_t* mem ) |
---|
| 110 | { |
---|
| 111 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)(&this->variable_); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // =========== short |
---|
| 115 | |
---|
| 116 | template <> uint32_t SynchronisableVariable<const short>::returnSize() |
---|
| 117 | { |
---|
| 118 | return sizeof(int16_t); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | template <> void SynchronisableVariable<const short>::setAndIncrease( uint8_t*& mem ) |
---|
| 122 | { |
---|
| 123 | *(short*)(&this->variable_) = *(int16_t*)(mem); |
---|
| 124 | mem += SynchronisableVariable<const short>::returnSize(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | template <> void SynchronisableVariable<const short>::getAndIncrease( uint8_t*& mem ) |
---|
| 128 | { |
---|
| 129 | *(int16_t*)(mem) = this->variable_; |
---|
| 130 | mem += SynchronisableVariable<const short>::returnSize(); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | template <> bool SynchronisableVariable<const short>::checkEquality( uint8_t* mem ) |
---|
| 134 | { |
---|
| 135 | return *(int16_t*)(mem) == static_cast<int16_t>(this->variable_); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // =========== unsigned short |
---|
| 139 | |
---|
| 140 | template <> uint32_t SynchronisableVariable<const unsigned short>::returnSize() |
---|
| 141 | { |
---|
| 142 | return sizeof(uint16_t); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | template <> void SynchronisableVariable<const unsigned short>::setAndIncrease( uint8_t*& mem ) |
---|
| 146 | { |
---|
| 147 | *(unsigned short*)(&this->variable_) = *(uint16_t*)(mem); |
---|
| 148 | mem += SynchronisableVariable<const unsigned short>::returnSize(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | template <> void SynchronisableVariable<const unsigned short>::getAndIncrease( uint8_t*& mem ) |
---|
| 152 | { |
---|
| 153 | *(uint16_t*)(mem) = this->variable_; |
---|
| 154 | mem += SynchronisableVariable<const unsigned short>::returnSize(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | template <> bool SynchronisableVariable<const unsigned short>::checkEquality( uint8_t* mem ) |
---|
| 158 | { |
---|
| 159 | return *(uint16_t*)(mem) == this->variable_; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | // =========== int |
---|
| 163 | |
---|
| 164 | template <> uint32_t SynchronisableVariable<const int>::returnSize() |
---|
| 165 | { |
---|
| 166 | return sizeof(int32_t); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | template <> void SynchronisableVariable<const int>::setAndIncrease( uint8_t*& mem ) |
---|
| 170 | { |
---|
| 171 | *(int *)(&this->variable_) = *(int32_t*)(mem); |
---|
| 172 | mem += SynchronisableVariable<const int>::returnSize(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | template <> void SynchronisableVariable<const int>::getAndIncrease( uint8_t*& mem ) |
---|
| 176 | { |
---|
| 177 | *(int32_t*)(mem) = this->variable_; |
---|
| 178 | mem += SynchronisableVariable<const int>::returnSize(); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | template <> bool SynchronisableVariable<const int>::checkEquality( uint8_t* mem ) |
---|
| 182 | { |
---|
| 183 | return *(int32_t*)(mem) == this->variable_; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | // =========== unsigned int |
---|
| 187 | |
---|
| 188 | template <> uint32_t SynchronisableVariable<const unsigned int>::returnSize() |
---|
| 189 | { |
---|
| 190 | return sizeof(uint32_t); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | template <> void SynchronisableVariable<const unsigned int>::setAndIncrease( uint8_t*& mem ) |
---|
| 194 | { |
---|
| 195 | *(unsigned int*)(&this->variable_) = *(uint32_t*)(mem); |
---|
| 196 | mem += SynchronisableVariable<const unsigned int>::returnSize(); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | template <> void SynchronisableVariable<const unsigned int>::getAndIncrease( uint8_t*& mem ) |
---|
| 200 | { |
---|
| 201 | *(uint32_t*)(mem) = this->variable_; |
---|
| 202 | mem += SynchronisableVariable<const unsigned int>::returnSize(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | template <> bool SynchronisableVariable<const unsigned int>::checkEquality( uint8_t* mem ) |
---|
| 206 | { |
---|
| 207 | return *(uint32_t*)(mem) == this->variable_; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // =========== long |
---|
| 211 | |
---|
| 212 | template <> uint32_t SynchronisableVariable<const long>::returnSize() |
---|
| 213 | { |
---|
| 214 | return sizeof(int32_t); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | template <> void SynchronisableVariable<const long>::setAndIncrease( uint8_t*& mem ) |
---|
| 218 | { |
---|
| 219 | *(long*)(&this->variable_) = *(int32_t*)(mem); |
---|
| 220 | mem += SynchronisableVariable<const long>::returnSize(); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | template <> void SynchronisableVariable<const long>::getAndIncrease( uint8_t*& mem ) |
---|
| 224 | { |
---|
| 225 | *(int32_t*)(mem) = this->variable_; |
---|
| 226 | mem += SynchronisableVariable<const long>::returnSize(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | template <> bool SynchronisableVariable<const long>::checkEquality( uint8_t* mem ) |
---|
| 230 | { |
---|
| 231 | return *(int32_t*)(mem) == this->variable_; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | // =========== unsigned long |
---|
| 235 | |
---|
| 236 | template <> uint32_t SynchronisableVariable<const unsigned long>::returnSize() |
---|
| 237 | { |
---|
| 238 | return sizeof(uint32_t); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | template <> void SynchronisableVariable<const unsigned long>::setAndIncrease( uint8_t*& mem ) |
---|
| 242 | { |
---|
| 243 | *(unsigned long*)(&this->variable_) = *(uint32_t*)(mem); |
---|
| 244 | mem += SynchronisableVariable<const unsigned long>::returnSize(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | template <> void SynchronisableVariable<const unsigned long>::getAndIncrease( uint8_t*& mem ) |
---|
| 248 | { |
---|
| 249 | *(uint32_t*)(mem) = this->variable_; |
---|
| 250 | mem += SynchronisableVariable<const unsigned long>::returnSize(); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | template <> bool SynchronisableVariable<const unsigned long>::checkEquality( uint8_t* mem ) |
---|
| 254 | { |
---|
| 255 | return *(uint32_t*)(mem) == this->variable_; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // =========== long long |
---|
| 259 | |
---|
| 260 | template <> uint32_t SynchronisableVariable<const long long>::returnSize() |
---|
| 261 | { |
---|
| 262 | return sizeof(int64_t); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | template <> void SynchronisableVariable<const long long>::setAndIncrease( uint8_t*& mem ) |
---|
| 266 | { |
---|
| 267 | *(long long*)(&this->variable_) = *(int64_t*)(mem); |
---|
| 268 | mem += SynchronisableVariable<const long long>::returnSize(); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | template <> void SynchronisableVariable<const long long>::getAndIncrease( uint8_t*& mem ) |
---|
| 272 | { |
---|
| 273 | *(int64_t*)(mem) = this->variable_; |
---|
| 274 | mem += SynchronisableVariable<const long long>::returnSize(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | template <> bool SynchronisableVariable<const long long>::checkEquality( uint8_t* mem ) |
---|
| 278 | { |
---|
| 279 | return *(int64_t*)(mem) == this->variable_; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | // =========== unsigned long long |
---|
| 283 | |
---|
| 284 | template <> uint32_t SynchronisableVariable<const unsigned long long>::returnSize() |
---|
| 285 | { |
---|
| 286 | return sizeof(uint64_t); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | template <> void SynchronisableVariable<const unsigned long long>::setAndIncrease( uint8_t*& mem ) |
---|
| 290 | { |
---|
| 291 | *(unsigned long long*)(&this->variable_) = *(uint64_t*)(mem); |
---|
| 292 | mem += SynchronisableVariable<const unsigned long long>::returnSize(); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | template <> void SynchronisableVariable<const unsigned long long>::getAndIncrease( uint8_t*& mem ) |
---|
| 296 | { |
---|
| 297 | *(uint64_t*)(mem) = this->variable_; |
---|
| 298 | mem += SynchronisableVariable<const unsigned long long>::returnSize(); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | template <> bool SynchronisableVariable<const unsigned long long>::checkEquality( uint8_t* mem ) |
---|
| 302 | { |
---|
| 303 | return *(uint64_t*)(mem) == this->variable_; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | // =========== float |
---|
| 307 | |
---|
| 308 | template <> uint32_t SynchronisableVariable<const float>::returnSize() |
---|
| 309 | { |
---|
| 310 | return sizeof(uint32_t); |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | template <> void SynchronisableVariable<const float>::setAndIncrease( uint8_t*& mem ) |
---|
| 314 | { |
---|
| 315 | *(uint32_t*)(&this->variable_) = *(uint32_t*)(mem); |
---|
| 316 | mem += SynchronisableVariable<const float>::returnSize(); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | template <> void SynchronisableVariable<const float>::getAndIncrease( uint8_t*& mem ) |
---|
| 320 | { |
---|
| 321 | *(uint32_t*)(mem) = *(uint32_t*)(&this->variable_); |
---|
| 322 | mem += SynchronisableVariable<const float>::returnSize(); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | template <> bool SynchronisableVariable<const float>::checkEquality( uint8_t* mem ) |
---|
| 326 | { |
---|
| 327 | return *(uint32_t*)(mem) == *(uint32_t*)(&this->variable_); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | // =========== double |
---|
| 331 | |
---|
| 332 | template <> uint32_t SynchronisableVariable<const double>::returnSize() |
---|
| 333 | { |
---|
| 334 | return sizeof(uint64_t); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | template <> void SynchronisableVariable<const double>::setAndIncrease( uint8_t*& mem ) |
---|
| 338 | { |
---|
| 339 | *(uint64_t*)(&this->variable_) = *(uint64_t*)(mem); |
---|
| 340 | mem += SynchronisableVariable<const double>::returnSize(); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | template <> void SynchronisableVariable<const double>::getAndIncrease( uint8_t*& mem ) |
---|
| 344 | { |
---|
| 345 | *(uint64_t*)(mem) = *(uint64_t*)(&this->variable_); |
---|
| 346 | mem += SynchronisableVariable<const double>::returnSize(); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | template <> bool SynchronisableVariable<const double>::checkEquality( uint8_t* mem ) |
---|
| 350 | { |
---|
| 351 | return *(uint64_t*)(mem) == *(uint64_t*)(&this->variable_); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | // =========== long double |
---|
| 355 | |
---|
| 356 | template <> uint32_t SynchronisableVariable<const long double>::returnSize() |
---|
| 357 | { |
---|
| 358 | return sizeof(uint64_t); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | template <> void SynchronisableVariable<const long double>::setAndIncrease( uint8_t*& mem ) |
---|
| 362 | { |
---|
| 363 | double temp; |
---|
[2710] | 364 | memcpy(&temp, mem, sizeof(uint64_t)); |
---|
[2245] | 365 | *(long double*)(&this->variable_) = static_cast<const long double>(temp); |
---|
| 366 | mem += SynchronisableVariable<const long double>::returnSize(); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | template <> void SynchronisableVariable<const long double>::getAndIncrease( uint8_t*& mem ) |
---|
| 370 | { |
---|
| 371 | double temp = static_cast<double>(this->variable_); |
---|
[2710] | 372 | memcpy(mem, &temp, sizeof(uint64_t)); |
---|
[2245] | 373 | mem += SynchronisableVariable<const long double>::returnSize(); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | template <> bool SynchronisableVariable<const long double>::checkEquality( uint8_t* mem ) |
---|
| 377 | { |
---|
| 378 | double temp = static_cast<double>(this->variable_); |
---|
[2710] | 379 | return memcmp(&temp, mem, sizeof(uint64_t))==0; |
---|
[2245] | 380 | } |
---|
| 381 | |
---|
| 382 | // =========== string |
---|
| 383 | |
---|
| 384 | template <> uint32_t SynchronisableVariable<const std::string>::returnSize() |
---|
| 385 | { |
---|
| 386 | return variable_.length()+1; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | template <> void SynchronisableVariable<const std::string>::getAndIncrease( uint8_t*& mem ) |
---|
| 390 | { |
---|
| 391 | memcpy(mem, this->variable_.c_str(), this->variable_.length()+1); |
---|
| 392 | mem += this->variable_.length()+1; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | template <> void SynchronisableVariable<const std::string>::setAndIncrease( uint8_t*& mem ) |
---|
| 396 | { |
---|
| 397 | *(std::string*)(&this->variable_) = std::string((const char *)mem); |
---|
| 398 | mem += this->variable_.length()+1; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | template <> bool SynchronisableVariable<const std::string>::checkEquality( uint8_t* mem ) |
---|
| 402 | { |
---|
| 403 | return std::string((const char*)mem)==this->variable_; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | // =========== Degree |
---|
| 407 | |
---|
| 408 | template <> uint32_t SynchronisableVariable<const Degree>::returnSize() |
---|
| 409 | { |
---|
| 410 | return sizeof(Ogre::Real); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | template <> void SynchronisableVariable<const Degree>::getAndIncrease( uint8_t*& mem ) |
---|
| 414 | { |
---|
| 415 | Ogre::Real r = this->variable_.valueDegrees(); |
---|
| 416 | memcpy(mem, &r, returnSize()); |
---|
| 417 | mem += returnSize(); |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | template <> void SynchronisableVariable<const Degree>::setAndIncrease( uint8_t*& mem ) |
---|
| 421 | { |
---|
| 422 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
[2255] | 423 | (Degree&)this->variable_ = *r; |
---|
[2245] | 424 | mem += returnSize(); |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | template <> bool SynchronisableVariable<const Degree>::checkEquality( uint8_t* mem ) |
---|
| 428 | { |
---|
| 429 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
| 430 | return this->variable_==Degree(*r); |
---|
| 431 | } |
---|
| 432 | |
---|
[2355] | 433 | } |
---|