[2937] | 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 | |
---|
[3214] | 29 | #ifndef _NetworkFunction_H__ |
---|
| 30 | #define _NetworkFunction_H__ |
---|
[2937] | 31 | |
---|
| 32 | #include "NetworkPrereqs.h" |
---|
| 33 | |
---|
[3214] | 34 | #include <cassert> |
---|
| 35 | #include <cstring> |
---|
| 36 | #include <map> |
---|
[2937] | 37 | #include <string> |
---|
[3214] | 38 | |
---|
[7284] | 39 | #include "core/command/Functor.h" |
---|
[3214] | 40 | #include "FunctionCallManager.h" |
---|
[2937] | 41 | #include "synchronisable/Synchronisable.h" |
---|
| 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
| 45 | |
---|
[3304] | 46 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
[2937] | 47 | static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8; |
---|
[3304] | 48 | #else |
---|
[2937] | 49 | static const unsigned int MAX_FUNCTION_POINTER_SIZE = 16; |
---|
| 50 | #endif //ORXONOX_COMPILER_GCC |
---|
| 51 | static const unsigned int MAX_FUNCTION_POINTER_INTS = (MAX_FUNCTION_POINTER_SIZE-1)/4+1; |
---|
| 52 | |
---|
| 53 | struct _NetworkExport NetworkFunctionPointer { |
---|
| 54 | uint32_t pointer[MAX_FUNCTION_POINTER_INTS]; |
---|
| 55 | bool operator<(const NetworkFunctionPointer& b) const |
---|
| 56 | { |
---|
[3304] | 57 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
[2937] | 58 | return pointer[0]<b.pointer[0] ? true : pointer[1]<b.pointer[1]; |
---|
| 59 | #else //ORXONOX_COMPILER_GCC |
---|
| 60 | return pointer[0]<b.pointer[0] ? true : ( pointer[1]<b.pointer[1] ? true : ( pointer[2]<b.pointer[2] ? true : pointer[3]<b.pointer[3] ) ); |
---|
| 61 | #endif //ORXONOX_COMPILER_GCC |
---|
| 62 | } |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
[10470] | 69 | class _NetworkExport NetworkFunctionBase { |
---|
[2937] | 70 | public: |
---|
[10471] | 71 | NetworkFunctionBase(const std::string& name, const NetworkFunctionPointer& p); |
---|
| 72 | virtual ~NetworkFunctionBase() {} |
---|
[6417] | 73 | |
---|
[10471] | 74 | void setNetworkID(uint32_t id); |
---|
[2937] | 75 | inline uint32_t getNetworkID() const { return this->networkID_; } |
---|
[3214] | 76 | inline const std::string& getName() const { return name_; } |
---|
[6417] | 77 | |
---|
[10472] | 78 | virtual bool call(uint32_t objectID)=0; |
---|
| 79 | virtual bool call(uint32_t objectID, const MultiType& mt1)=0; |
---|
| 80 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0; |
---|
| 81 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0; |
---|
| 82 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0; |
---|
| 83 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0; |
---|
| 84 | |
---|
[2937] | 85 | private: |
---|
| 86 | uint32_t networkID_; |
---|
| 87 | std::string name_; |
---|
[6417] | 88 | |
---|
[2937] | 89 | }; |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase { |
---|
| 93 | public: |
---|
[10471] | 94 | NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p) |
---|
| 95 | : NetworkFunctionBase(name, p) |
---|
| 96 | , functor_(functor) |
---|
| 97 | { } |
---|
[6417] | 98 | |
---|
[10472] | 99 | // ignore the objectID because its a static function |
---|
| 100 | virtual bool call(uint32_t objectID){ (*this->functor_)(); return true; } |
---|
| 101 | virtual bool call(uint32_t objectID, const MultiType& mt1){ (*this->functor_)(mt1); return true; } |
---|
| 102 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); return true; } |
---|
| 103 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); return true; } |
---|
| 104 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); return true; } |
---|
| 105 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5){ (*this->functor_)(mt1, mt2, mt3, mt4, mt5); return true; } |
---|
[6417] | 106 | |
---|
[2937] | 107 | private: |
---|
[7284] | 108 | FunctorStaticPtr functor_; |
---|
[6417] | 109 | |
---|
[2937] | 110 | }; |
---|
| 111 | |
---|
| 112 | |
---|
[2941] | 113 | class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase { |
---|
[2937] | 114 | public: |
---|
[10471] | 115 | NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p) |
---|
| 116 | : NetworkFunctionBase(name, p) |
---|
| 117 | { } |
---|
[2937] | 118 | }; |
---|
| 119 | |
---|
| 120 | |
---|
[2995] | 121 | template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase { |
---|
[2937] | 122 | public: |
---|
[10471] | 123 | NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p) |
---|
| 124 | : NetworkMemberFunctionBase(name, p) |
---|
| 125 | , functor_(functor) |
---|
| 126 | { } |
---|
[6417] | 127 | |
---|
[7495] | 128 | inline bool call(uint32_t objectID) |
---|
[6417] | 129 | { |
---|
[2951] | 130 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 131 | { |
---|
[3325] | 132 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID))); |
---|
[7495] | 133 | return true; |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | return false; |
---|
[2951] | 137 | } |
---|
[7495] | 138 | inline bool call(uint32_t objectID, const MultiType& mt1) |
---|
[6417] | 139 | { |
---|
[2951] | 140 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 141 | { |
---|
[3325] | 142 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1); |
---|
[7495] | 143 | return true; |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | return false; |
---|
[2951] | 147 | } |
---|
[7495] | 148 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2) |
---|
[6417] | 149 | { |
---|
[2951] | 150 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 151 | { |
---|
[3325] | 152 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2); |
---|
[7495] | 153 | return true; |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | return false; |
---|
[2951] | 157 | } |
---|
[7495] | 158 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) |
---|
[6417] | 159 | { |
---|
[2951] | 160 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 161 | { |
---|
[3325] | 162 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3); |
---|
[7495] | 163 | return true; |
---|
| 164 | } |
---|
| 165 | else |
---|
| 166 | return false; |
---|
[2951] | 167 | } |
---|
[7495] | 168 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4) |
---|
[6417] | 169 | { |
---|
[2951] | 170 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 171 | { |
---|
[3325] | 172 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4); |
---|
[7495] | 173 | return true; |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | return false; |
---|
[2951] | 177 | } |
---|
[7495] | 178 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) |
---|
[6417] | 179 | { |
---|
[2951] | 180 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 181 | { |
---|
[3325] | 182 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5); |
---|
[7495] | 183 | return true; |
---|
| 184 | } |
---|
| 185 | else |
---|
| 186 | return false; |
---|
[2951] | 187 | } |
---|
[6417] | 188 | |
---|
[2937] | 189 | private: |
---|
[7284] | 190 | FunctorMemberPtr<T> functor_; |
---|
[2937] | 191 | }; |
---|
| 192 | |
---|
| 193 | template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr) |
---|
| 194 | { |
---|
[3304] | 195 | if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0) |
---|
| 196 | memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T)); |
---|
[2937] | 197 | T p2 = ptr; |
---|
| 198 | memcpy( &destptr, &p2, sizeof(T) ); |
---|
| 199 | // for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++) |
---|
| 200 | // *((uint32_t*)destptr+i) = p2>>32*i; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | } |
---|
| 204 | |
---|
[3214] | 205 | #endif /* _NetworkFunction_H__ */ |
---|