[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 | |
---|
[9667] | 39 | #include "core/object/Listable.h" |
---|
| 40 | #include "core/class/Identifier.h" |
---|
[7284] | 41 | #include "core/command/Functor.h" |
---|
[3214] | 42 | #include "FunctionCallManager.h" |
---|
[2937] | 43 | #include "synchronisable/Synchronisable.h" |
---|
| 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | |
---|
[3304] | 48 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
[2937] | 49 | static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8; |
---|
[3304] | 50 | #else |
---|
[2937] | 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 | { |
---|
[3304] | 59 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
[2937] | 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 | |
---|
[9667] | 71 | class _NetworkExport NetworkFunctionBase: virtual public Listable { |
---|
[2937] | 72 | public: |
---|
[3214] | 73 | NetworkFunctionBase(const std::string& name); |
---|
[2937] | 74 | ~NetworkFunctionBase(); |
---|
[6417] | 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_; } |
---|
[6417] | 79 | |
---|
[2937] | 80 | private: |
---|
| 81 | uint32_t networkID_; |
---|
| 82 | std::string name_; |
---|
[6417] | 83 | |
---|
[2937] | 84 | }; |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase { |
---|
| 88 | public: |
---|
[7284] | 89 | NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p); |
---|
[6417] | 90 | |
---|
[2937] | 91 | inline void call(){ (*this->functor_)(); } |
---|
| 92 | inline void call(const MultiType& mt1){ (*this->functor_)(mt1); } |
---|
| 93 | inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); } |
---|
| 94 | inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); } |
---|
| 95 | inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); } |
---|
| 96 | 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); } |
---|
[6417] | 97 | |
---|
| 98 | virtual void setNetworkID( uint32_t id ) |
---|
| 99 | { NetworkFunctionBase::setNetworkID( id ); NetworkFunctionStatic::getIdMap()[id] = this; } |
---|
| 100 | static inline NetworkFunctionStatic* getNetworkFunction( uint32_t id) |
---|
| 101 | { assert( NetworkFunctionStatic::getIdMap().find(id)!=NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; } |
---|
| 102 | static NetworkFunctionStatic* getFunction( uint32_t id ) |
---|
| 103 | { assert( NetworkFunctionStatic::getIdMap().find(id) != NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; } |
---|
| 104 | static NetworkFunctionStatic* getFunction( const NetworkFunctionPointer& p ) |
---|
| 105 | { assert( NetworkFunctionStatic::getFunctorMap().find(p) != NetworkFunctionStatic::getFunctorMap().end() ); return NetworkFunctionStatic::getFunctorMap()[p]; } |
---|
| 106 | |
---|
[2937] | 107 | private: |
---|
[6417] | 108 | static std::map<NetworkFunctionPointer, NetworkFunctionStatic*>& getFunctorMap(); |
---|
| 109 | static std::map<uint32_t, NetworkFunctionStatic*>& getIdMap(); |
---|
[7284] | 110 | FunctorStaticPtr functor_; |
---|
[6417] | 111 | |
---|
[2937] | 112 | }; |
---|
| 113 | |
---|
| 114 | |
---|
[2941] | 115 | class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase { |
---|
[2937] | 116 | public: |
---|
[3214] | 117 | NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p); |
---|
[2937] | 118 | ~NetworkMemberFunctionBase(); |
---|
[6417] | 119 | |
---|
[3214] | 120 | virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; } |
---|
[2937] | 121 | static inline NetworkMemberFunctionBase* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; } |
---|
| 122 | static NetworkMemberFunctionBase* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; } |
---|
| 123 | static NetworkMemberFunctionBase* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; } |
---|
[6417] | 124 | |
---|
| 125 | // |
---|
[7495] | 126 | virtual bool call(uint32_t objectID)=0; |
---|
| 127 | virtual bool call(uint32_t objectID, const MultiType& mt1)=0; |
---|
| 128 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0; |
---|
| 129 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0; |
---|
| 130 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0; |
---|
| 131 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0; |
---|
[6417] | 132 | |
---|
[2937] | 133 | private: |
---|
| 134 | static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_; |
---|
| 135 | static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_; |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | |
---|
[2995] | 139 | template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase { |
---|
[2937] | 140 | public: |
---|
[7284] | 141 | NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p); |
---|
[6417] | 142 | |
---|
[7495] | 143 | inline bool call(uint32_t objectID) |
---|
[6417] | 144 | { |
---|
[2951] | 145 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 146 | { |
---|
[3325] | 147 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID))); |
---|
[7495] | 148 | return true; |
---|
| 149 | } |
---|
| 150 | else |
---|
| 151 | return false; |
---|
[2951] | 152 | } |
---|
[7495] | 153 | inline bool call(uint32_t objectID, const MultiType& mt1) |
---|
[6417] | 154 | { |
---|
[2951] | 155 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 156 | { |
---|
[3325] | 157 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1); |
---|
[7495] | 158 | return true; |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | return false; |
---|
[2951] | 162 | } |
---|
[7495] | 163 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2) |
---|
[6417] | 164 | { |
---|
[2951] | 165 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 166 | { |
---|
[3325] | 167 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2); |
---|
[7495] | 168 | return true; |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | return false; |
---|
[2951] | 172 | } |
---|
[7495] | 173 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) |
---|
[6417] | 174 | { |
---|
[2951] | 175 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 176 | { |
---|
[3325] | 177 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3); |
---|
[7495] | 178 | return true; |
---|
| 179 | } |
---|
| 180 | else |
---|
| 181 | return false; |
---|
[2951] | 182 | } |
---|
[7495] | 183 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4) |
---|
[6417] | 184 | { |
---|
[2951] | 185 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 186 | { |
---|
[3325] | 187 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4); |
---|
[7495] | 188 | return true; |
---|
| 189 | } |
---|
| 190 | else |
---|
| 191 | return false; |
---|
[2951] | 192 | } |
---|
[7495] | 193 | inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) |
---|
[6417] | 194 | { |
---|
[2951] | 195 | if ( Synchronisable::getSynchronisable(objectID)!=0 ) |
---|
[7495] | 196 | { |
---|
[3325] | 197 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5); |
---|
[7495] | 198 | return true; |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | return false; |
---|
[2951] | 202 | } |
---|
[6417] | 203 | |
---|
[2937] | 204 | private: |
---|
[7284] | 205 | FunctorMemberPtr<T> functor_; |
---|
[2937] | 206 | }; |
---|
| 207 | |
---|
[7284] | 208 | template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p): |
---|
[2944] | 209 | NetworkMemberFunctionBase(name, p), functor_(functor) |
---|
| 210 | { |
---|
| 211 | } |
---|
[2937] | 212 | |
---|
| 213 | template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr) |
---|
| 214 | { |
---|
[3304] | 215 | if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0) |
---|
| 216 | memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T)); |
---|
[2937] | 217 | T p2 = ptr; |
---|
| 218 | memcpy( &destptr, &p2, sizeof(T) ); |
---|
| 219 | // for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++) |
---|
| 220 | // *((uint32_t*)destptr+i) = p2>>32*i; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | } |
---|
| 224 | |
---|
[3214] | 225 | #endif /* _NetworkFunction_H__ */ |
---|