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