Changeset 10624 for code/trunk/src/libraries/network/NetworkFunction.h
- Timestamp:
- Oct 4, 2015, 9:12:21 PM (10 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
-
code/trunk/src/libraries/network/NetworkFunction.h
r9667 r10624 36 36 #include <map> 37 37 #include <string> 38 #include <boost/preprocessor/cat.hpp> 39 #include <boost/static_assert.hpp> 40 41 #include "core/object/Listable.h" 42 #include "core/class/Identifier.h" 38 43 39 #include "core/command/Functor.h" 44 40 #include "FunctionCallManager.h" … … 60 56 { 61 57 #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) 62 return pointer[0]<b.pointer[0] ? true : pointer[1]<b.pointer[1]; 63 #else //ORXONOX_COMPILER_GCC 64 return pointer[0]<b.pointer[0] ? true : ( pointer[1]<b.pointer[1] ? true : ( pointer[2]<b.pointer[2] ? true : pointer[3]<b.pointer[3] ) ); 65 #endif //ORXONOX_COMPILER_GCC 58 if (pointer[0] != b.pointer[0]) 59 return pointer[0] < b.pointer[0]; 60 else if (pointer[1] != b.pointer[1]) 61 return pointer[1] < b.pointer[1]; 62 else 63 return false; 64 #else 65 if (pointer[0] != b.pointer[0]) 66 return pointer[0] < b.pointer[0]; 67 else if (pointer[1] != b.pointer[1]) 68 return pointer[1] < b.pointer[1]; 69 else if (pointer[2] != b.pointer[2]) 70 return pointer[2] < b.pointer[2]; 71 else if (pointer[3] != b.pointer[3]) 72 return pointer[3] < b.pointer[3]; 73 else 74 return false; 75 #endif 66 76 } 67 77 }; … … 71 81 72 82 73 class _NetworkExport NetworkFunctionBase: virtual public Listable { 74 public: 75 NetworkFunctionBase(const std::string& name); 76 ~NetworkFunctionBase(); 77 78 virtual void setNetworkID(uint32_t id) { this->networkID_ = id; } 79 inline uint32_t getNetworkID() const { return this->networkID_; } 80 inline const std::string& getName() const { return name_; } 81 static inline bool isStatic( uint32_t networkID ) { return isStaticMap_[networkID]; } 82 83 static inline void setNetworkID(const std::string& name, uint32_t id) 84 { 85 std::map<std::string, NetworkFunctionBase*>& map = NetworkFunctionBase::getNameMap(); 86 assert( map.find(name)!=map.end() ); 87 map[name]->setNetworkID(id); 88 } 89 90 static void destroyAllNetworkFunctions(); 91 92 protected: 93 static std::map<uint32_t, bool> isStaticMap_; 94 95 private: 96 static std::map<std::string, NetworkFunctionBase*>& getNameMap(); 97 uint32_t networkID_; 98 std::string name_; 99 100 }; 101 102 103 class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase { 104 public: 105 NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p); 106 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); } 113 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]; } 122 123 private: 124 static std::map<NetworkFunctionPointer, NetworkFunctionStatic*>& getFunctorMap(); 125 static std::map<uint32_t, NetworkFunctionStatic*>& getIdMap(); 126 FunctorStaticPtr functor_; 127 128 }; 129 130 131 class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase { 132 public: 133 NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p); 134 ~NetworkMemberFunctionBase(); 135 136 virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; } 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]; } 140 141 // 83 class _NetworkExport NetworkFunctionBase { 84 public: 85 NetworkFunctionBase(const std::string& name, const NetworkFunctionPointer& pointer); 86 virtual ~NetworkFunctionBase() {} 87 88 void setNetworkID(uint32_t id); 89 inline uint32_t getNetworkID() const { return this->networkID_; } 90 inline const std::string& getName() const { return this->name_; } 91 inline const NetworkFunctionPointer& getPointer() const { return this->pointer_; } 92 142 93 virtual bool call(uint32_t objectID)=0; 143 94 virtual bool call(uint32_t objectID, const MultiType& mt1)=0; … … 148 99 149 100 private: 150 static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_; 151 static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_; 101 uint32_t networkID_; 102 std::string name_; 103 NetworkFunctionPointer pointer_; 104 105 }; 106 107 108 class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase { 109 public: 110 NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p) 111 : NetworkFunctionBase(name, p) 112 , functor_(functor) 113 { } 114 115 // ignore the objectID because its a static function 116 virtual bool call(uint32_t objectID){ (*this->functor_)(); return true; } 117 virtual bool call(uint32_t objectID, const MultiType& mt1){ (*this->functor_)(mt1); return true; } 118 virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); return true; } 119 virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); return true; } 120 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; } 121 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; } 122 123 private: 124 FunctorStaticPtr functor_; 125 126 }; 127 128 129 class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase { 130 public: 131 NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p) 132 : NetworkFunctionBase(name, p) 133 { } 152 134 }; 153 135 … … 155 137 template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase { 156 138 public: 157 NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p); 139 NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p) 140 : NetworkMemberFunctionBase(name, p) 141 , functor_(functor) 142 { } 158 143 159 144 inline bool call(uint32_t objectID) … … 222 207 }; 223 208 224 template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p):225 NetworkMemberFunctionBase(name, p), functor_(functor)226 {227 209 } 228 210 229 template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)230 {231 if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0)232 memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));233 T p2 = ptr;234 memcpy( &destptr, &p2, sizeof(T) );235 // for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++)236 // *((uint32_t*)destptr+i) = p2>>32*i;237 }238 239 template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, const std::string& name )240 {241 BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above242 NetworkFunctionPointer destptr;243 copyPtr( ptr, destptr );244 new NetworkFunctionStatic( createFunctor(ptr), name, destptr );245 return 0;246 }247 248 template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )249 {250 BOOST_STATIC_ASSERT( sizeof(PT)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for a specific kind of member functions than defined above251 NetworkFunctionPointer destptr;252 copyPtr( ptr, destptr );253 new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );254 return 0;255 }256 257 #define registerStaticNetworkFunction( functionPointer ) \258 static void* BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) = registerStaticNetworkFunctionFct( functionPointer, #functionPointer );259 #define registerMemberNetworkFunction( class, function ) \260 static void* BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) = registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function);261 // call it with functionPointer, clientID, args262 #define callStaticNetworkFunction( functionPointer, ...) \263 { \264 NetworkFunctionPointer p1; \265 copyPtr( functionPointer, p1 ); \266 FunctionCallManager::addCallStatic(NetworkFunctionStatic::getFunction(p1)->getNetworkID(), __VA_ARGS__); \267 }268 // call it with class, function, objectID, clientID, args269 #define callMemberNetworkFunction( class, function, objectID, ...) \270 { \271 NetworkFunctionPointer p1; \272 copyPtr( &class::function, p1 ); \273 FunctionCallManager::addCallMember(NetworkMemberFunctionBase::getFunction(p1)->getNetworkID(), objectID, __VA_ARGS__); \274 }275 276 277 }278 279 211 #endif /* _NetworkFunction_H__ */
Note: See TracChangeset
for help on using the changeset viewer.