[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> |
---|
[3196] | 38 | #include <boost/preprocessor/cat.hpp> |
---|
[3304] | 39 | #include <boost/static_assert.hpp> |
---|
[3214] | 40 | |
---|
[3196] | 41 | #include "core/Functor.h" |
---|
[3325] | 42 | #include "core/Identifier.h" |
---|
[3214] | 43 | #include "FunctionCallManager.h" |
---|
[2937] | 44 | #include "synchronisable/Synchronisable.h" |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | |
---|
[3304] | 49 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
[2937] | 50 | static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8; |
---|
[3304] | 51 | #else |
---|
[2937] | 52 | static const unsigned int MAX_FUNCTION_POINTER_SIZE = 16; |
---|
| 53 | #endif //ORXONOX_COMPILER_GCC |
---|
| 54 | static const unsigned int MAX_FUNCTION_POINTER_INTS = (MAX_FUNCTION_POINTER_SIZE-1)/4+1; |
---|
| 55 | |
---|
| 56 | struct _NetworkExport NetworkFunctionPointer { |
---|
| 57 | uint32_t pointer[MAX_FUNCTION_POINTER_INTS]; |
---|
| 58 | bool operator<(const NetworkFunctionPointer& b) const |
---|
| 59 | { |
---|
[3304] | 60 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
[2937] | 61 | return pointer[0]<b.pointer[0] ? true : pointer[1]<b.pointer[1]; |
---|
| 62 | #else //ORXONOX_COMPILER_GCC |
---|
| 63 | return pointer[0]<b.pointer[0] ? true : ( pointer[1]<b.pointer[1] ? true : ( pointer[2]<b.pointer[2] ? true : pointer[3]<b.pointer[3] ) ); |
---|
| 64 | #endif //ORXONOX_COMPILER_GCC |
---|
| 65 | } |
---|
| 66 | }; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
[2943] | 72 | class _NetworkExport NetworkFunctionBase: virtual public OrxonoxClass { |
---|
[2937] | 73 | public: |
---|
[3214] | 74 | NetworkFunctionBase(const std::string& name); |
---|
[2937] | 75 | ~NetworkFunctionBase(); |
---|
[6387] | 76 | |
---|
[3214] | 77 | virtual void setNetworkID(uint32_t id) { this->networkID_ = id; } |
---|
[2937] | 78 | inline uint32_t getNetworkID() const { return this->networkID_; } |
---|
[3214] | 79 | inline const std::string& getName() const { return name_; } |
---|
[2937] | 80 | static inline bool isStatic( uint32_t networkID ) { return isStaticMap_[networkID]; } |
---|
[6387] | 81 | |
---|
[6160] | 82 | static inline void setNetworkID(const std::string& name, uint32_t id) |
---|
| 83 | { |
---|
| 84 | std::map<std::string, NetworkFunctionBase*>& map = NetworkFunctionBase::getNameMap(); |
---|
[6388] | 85 | assert( map.find(name)!=map.end() ); |
---|
[6160] | 86 | map[name]->setNetworkID(id); |
---|
| 87 | } |
---|
[6387] | 88 | |
---|
[3304] | 89 | static void destroyAllNetworkFunctions(); |
---|
[6387] | 90 | |
---|
[2944] | 91 | protected: |
---|
| 92 | static std::map<uint32_t, bool> isStaticMap_; |
---|
[6387] | 93 | |
---|
[2937] | 94 | private: |
---|
[6160] | 95 | static std::map<std::string, NetworkFunctionBase*>& getNameMap(); |
---|
[2937] | 96 | uint32_t networkID_; |
---|
| 97 | std::string name_; |
---|
[6387] | 98 | |
---|
[2937] | 99 | }; |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase { |
---|
| 103 | public: |
---|
[3214] | 104 | NetworkFunctionStatic(FunctorStatic* functor, const std::string& name, const NetworkFunctionPointer& p); |
---|
[2937] | 105 | ~NetworkFunctionStatic(); |
---|
[6387] | 106 | |
---|
[2937] | 107 | inline void call(){ (*this->functor_)(); } |
---|
| 108 | inline void call(const MultiType& mt1){ (*this->functor_)(mt1); } |
---|
| 109 | inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); } |
---|
| 110 | inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); } |
---|
| 111 | inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); } |
---|
| 112 | inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5){ (*this->functor_)(mt1, mt2, mt3, mt4, mt5); } |
---|
[6387] | 113 | |
---|
[6160] | 114 | virtual void setNetworkID( uint32_t id ) |
---|
| 115 | { NetworkFunctionBase::setNetworkID( id ); NetworkFunctionStatic::getIdMap()[id] = this; } |
---|
| 116 | static inline NetworkFunctionStatic* getNetworkFunction( uint32_t id) |
---|
| 117 | { assert( NetworkFunctionStatic::getIdMap().find(id)!=NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; } |
---|
| 118 | static NetworkFunctionStatic* getFunction( uint32_t id ) |
---|
| 119 | { assert( NetworkFunctionStatic::getIdMap().find(id) != NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; } |
---|
| 120 | static NetworkFunctionStatic* getFunction( const NetworkFunctionPointer& p ) |
---|
| 121 | { assert( NetworkFunctionStatic::getFunctorMap().find(p) != NetworkFunctionStatic::getFunctorMap().end() ); return NetworkFunctionStatic::getFunctorMap()[p]; } |
---|
[6387] | 122 | |
---|
[2937] | 123 | private: |
---|
[6160] | 124 | static std::map<NetworkFunctionPointer, NetworkFunctionStatic*>& getFunctorMap(); |
---|
| 125 | static std::map<uint32_t, NetworkFunctionStatic*>& getIdMap(); |
---|
[2937] | 126 | FunctorStatic* functor_; |
---|
[6387] | 127 | |
---|
[2937] | 128 | }; |
---|
| 129 | |
---|
| 130 | |
---|
[2941] | 131 | class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase { |
---|
[2937] | 132 | public: |
---|
[3214] | 133 | NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p); |
---|
[2937] | 134 | ~NetworkMemberFunctionBase(); |
---|
[6387] | 135 | |
---|
[3214] | 136 | virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; } |
---|
[2937] | 137 | static inline NetworkMemberFunctionBase* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; } |
---|
| 138 | static NetworkMemberFunctionBase* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; } |
---|
| 139 | static NetworkMemberFunctionBase* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; } |
---|
[6387] | 140 | |
---|
[6388] | 141 | // |
---|
[2937] | 142 | virtual void call(uint32_t objectID)=0; |
---|
| 143 | virtual void call(uint32_t objectID, const MultiType& mt1)=0; |
---|
| 144 | virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0; |
---|
| 145 | virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0; |
---|
| 146 | virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0; |
---|
| 147 | virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0; |
---|
[6387] | 148 | |
---|
[2937] | 149 | private: |
---|
| 150 | static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_; |
---|
| 151 | static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_; |
---|
| 152 | }; |
---|
| 153 | |
---|
| 154 | |
---|
[2995] | 155 | template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase { |
---|
[2937] | 156 | public: |
---|
[3214] | 157 | NetworkMemberFunction(FunctorMember<T>* functor, const std::string& name, const NetworkFunctionPointer& p); |
---|
[2937] | 158 | ~NetworkMemberFunction(); |
---|
[6387] | 159 | |
---|
[2951] | 160 | inline void call(uint32_t objectID) |
---|
[6388] | 161 | { |
---|
[2951] | 162 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[3325] | 163 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID))); |
---|
[2951] | 164 | } |
---|
| 165 | inline void call(uint32_t objectID, const MultiType& mt1) |
---|
[6388] | 166 | { |
---|
[2951] | 167 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[3325] | 168 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1); |
---|
[2951] | 169 | } |
---|
| 170 | inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2) |
---|
[6388] | 171 | { |
---|
[2951] | 172 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[3325] | 173 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2); |
---|
[2951] | 174 | } |
---|
| 175 | inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) |
---|
[6388] | 176 | { |
---|
[2951] | 177 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[3325] | 178 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3); |
---|
[2951] | 179 | } |
---|
| 180 | inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4) |
---|
[6388] | 181 | { |
---|
[2951] | 182 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[3325] | 183 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4); |
---|
[2951] | 184 | } |
---|
| 185 | inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) |
---|
[6388] | 186 | { |
---|
[2951] | 187 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[3325] | 188 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5); |
---|
[2951] | 189 | } |
---|
[6387] | 190 | |
---|
[2937] | 191 | private: |
---|
| 192 | FunctorMember<T>* functor_; |
---|
| 193 | }; |
---|
| 194 | |
---|
[3214] | 195 | template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(FunctorMember<T>* functor, const std::string& name, const NetworkFunctionPointer& p): |
---|
[2944] | 196 | NetworkMemberFunctionBase(name, p), functor_(functor) |
---|
| 197 | { |
---|
| 198 | } |
---|
[2948] | 199 | template <class T> NetworkMemberFunction<T>::~NetworkMemberFunction() |
---|
| 200 | { |
---|
| 201 | delete this->functor_; |
---|
| 202 | } |
---|
[2937] | 203 | |
---|
| 204 | template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr) |
---|
| 205 | { |
---|
[3304] | 206 | if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0) |
---|
| 207 | memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T)); |
---|
[2937] | 208 | T p2 = ptr; |
---|
| 209 | memcpy( &destptr, &p2, sizeof(T) ); |
---|
| 210 | // for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++) |
---|
| 211 | // *((uint32_t*)destptr+i) = p2>>32*i; |
---|
| 212 | } |
---|
| 213 | |
---|
[3214] | 214 | template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, const std::string& name ) |
---|
[2937] | 215 | { |
---|
[3304] | 216 | BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above |
---|
[2937] | 217 | NetworkFunctionPointer destptr; |
---|
| 218 | copyPtr( ptr, destptr ); |
---|
| 219 | new NetworkFunctionStatic( createFunctor(ptr), name, destptr ); |
---|
| 220 | return 0; |
---|
| 221 | } |
---|
| 222 | |
---|
[3214] | 223 | template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, const std::string& name ) |
---|
[2937] | 224 | { |
---|
[3304] | 225 | BOOST_STATIC_ASSERT( sizeof(PT)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for a specific kind of member functions than defined above |
---|
[2937] | 226 | NetworkFunctionPointer destptr; |
---|
| 227 | copyPtr( ptr, destptr ); |
---|
[2948] | 228 | new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr ); |
---|
[2937] | 229 | return 0; |
---|
| 230 | } |
---|
| 231 | |
---|
[2948] | 232 | #define registerStaticNetworkFunction( functionPointer ) \ |
---|
[3196] | 233 | static void* BOOST_PP_CAT( NETWORK_FUNCTION_, __LINE__ ) = registerStaticNetworkFunctionFct( functionPointer, #functionPointer ); |
---|
[2948] | 234 | #define registerMemberNetworkFunction( class, function ) \ |
---|
[3196] | 235 | static void* BOOST_PP_CAT( NETWORK_FUNCTION_##class, __LINE__ ) = registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function); |
---|
[2948] | 236 | // call it with functionPointer, clientID, args |
---|
[2937] | 237 | #define callStaticNetworkFunction( functionPointer, ...) \ |
---|
| 238 | { \ |
---|
| 239 | NetworkFunctionPointer p1; \ |
---|
| 240 | copyPtr( functionPointer, p1 ); \ |
---|
[2943] | 241 | FunctionCallManager::addCallStatic(NetworkFunctionStatic::getFunction(p1)->getNetworkID(), __VA_ARGS__); \ |
---|
[2937] | 242 | } |
---|
[2948] | 243 | // call it with class, function, objectID, clientID, args |
---|
| 244 | #define callMemberNetworkFunction( class, function, objectID, ...) \ |
---|
[2937] | 245 | { \ |
---|
| 246 | NetworkFunctionPointer p1; \ |
---|
[2948] | 247 | copyPtr( &class::function, p1 ); \ |
---|
| 248 | FunctionCallManager::addCallMember(NetworkMemberFunctionBase::getFunction(p1)->getNetworkID(), objectID, __VA_ARGS__); \ |
---|
[2937] | 249 | } |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | } |
---|
| 253 | |
---|
[3214] | 254 | #endif /* _NetworkFunction_H__ */ |
---|