/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Oliver Scheuss, (C) 2008 * Co-authors: * ... * */ #include "SynchronisableVariable.h" #include #include "util/Math.h" namespace orxonox{ uint8_t SynchronisableVariableBase::state_ = 0; // =================== Template specialisation stuff ============= // =========== bool template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint8_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(uint8_t*)(&this->variable_) = *static_cast(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *static_cast(mem) = *(uint8_t*)(&this->variable_); mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *static_cast(mem) == *(uint8_t*)(&this->variable_); } // =========== unsigned char template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint8_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(uint8_t*)(&this->variable_) = *static_cast(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *static_cast(mem) = *(uint8_t*)(&this->variable_); mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *static_cast(mem) == *(uint8_t*)(&this->variable_); } // =========== short template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(int16_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(short*)(&this->variable_) = *(int16_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(int16_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(int16_t*)(mem) == static_cast(this->variable_); } // =========== unsigned short template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint16_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(unsigned short*)(&this->variable_) = *(uint16_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(uint16_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(uint16_t*)(mem) == this->variable_; } // =========== int template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(int32_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(int *)(&this->variable_) = *(int32_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(int32_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(int32_t*)(mem) == this->variable_; } // =========== unsigned int template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint32_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(unsigned int*)(&this->variable_) = *(uint32_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(uint32_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(uint32_t*)(mem) == this->variable_; } // =========== long template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(int32_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(long*)(&this->variable_) = *(int32_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(int32_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(int32_t*)(mem) == this->variable_; } // =========== unsigned long template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint32_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(unsigned long*)(&this->variable_) = *(uint32_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(uint32_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(uint32_t*)(mem) == this->variable_; } // =========== long long template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(int64_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(long long*)(&this->variable_) = *(int64_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(int64_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(int64_t*)(mem) == this->variable_; } // =========== unsigned long long template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint64_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(unsigned long long*)(&this->variable_) = *(uint64_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(uint64_t*)(mem) = this->variable_; mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(uint64_t*)(mem) == this->variable_; } // =========== float template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint32_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(uint32_t*)(&this->variable_) = *(uint32_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(uint32_t*)(mem) = *(uint32_t*)(&this->variable_); mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(uint32_t*)(mem) == *(uint32_t*)(&this->variable_); } // =========== double template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint64_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(uint64_t*)(&this->variable_) = *(uint64_t*)(mem); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { *(uint64_t*)(mem) = *(uint64_t*)(&this->variable_); mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return *(uint64_t*)(mem) == *(uint64_t*)(&this->variable_); } // =========== long double template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(uint64_t); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { double temp; *(uint64_t*)(&temp) = *(uint64_t*)(mem); *(long double*)(&this->variable_) = static_cast(temp); mem += SynchronisableVariable::returnSize(); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { double temp = static_cast(this->variable_); *(uint64_t*)(mem) = *(uint64_t*)(&temp); mem += SynchronisableVariable::returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { double temp = static_cast(this->variable_); return *(uint64_t*)(mem) == *(uint64_t*)(&temp); } // =========== string template <> uint32_t SynchronisableVariable::returnSize() { return variable_.length()+1; } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { memcpy(mem, this->variable_.c_str(), this->variable_.length()+1); mem += this->variable_.length()+1; } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { *(std::string*)(&this->variable_) = std::string((const char *)mem); mem += this->variable_.length()+1; } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { return std::string((const char*)mem)==this->variable_; } // =========== Degree template <> uint32_t SynchronisableVariable::returnSize() { return sizeof(Ogre::Real); } template <> void SynchronisableVariable::getAndIncrease( uint8_t*& mem ) { Ogre::Real r = this->variable_.valueDegrees(); memcpy(mem, &r, returnSize()); mem += returnSize(); } template <> void SynchronisableVariable::setAndIncrease( uint8_t*& mem ) { Ogre::Real* r = (Ogre::Real*)mem; (Degree&)this->variable_ = *r; mem += returnSize(); } template <> bool SynchronisableVariable::checkEquality( uint8_t* mem ) { Ogre::Real* r = (Ogre::Real*)mem; return this->variable_==Degree(*r); } }