| 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 | *      Fabian 'x3n' Landau | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #ifndef _NetworkFunctionIncludes_H__ | 
|---|
| 30 | #define _NetworkFunctionIncludes_H__ | 
|---|
| 31 |  | 
|---|
| 32 | #include "NetworkPrereqs.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include <boost/preprocessor/cat.hpp> | 
|---|
| 35 |  | 
|---|
| 36 | #include "NetworkFunction.h" | 
|---|
| 37 | #include "core/module/StaticallyInitializedInstance.h" | 
|---|
| 38 |  | 
|---|
| 39 | #define registerStaticNetworkFunction( functionPointer ) \ | 
|---|
| 40 | static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) \ | 
|---|
| 41 | = (new orxonox::SI_NF(orxonox::registerStaticNetworkFunctionFct( functionPointer, #functionPointer )))->getFunction() | 
|---|
| 42 |  | 
|---|
| 43 | #define registerMemberNetworkFunction( class, function ) \ | 
|---|
| 44 | static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) \ | 
|---|
| 45 | = (new orxonox::SI_NF(orxonox::registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function)))->getFunction() | 
|---|
| 46 |  | 
|---|
| 47 | namespace orxonox | 
|---|
| 48 | { | 
|---|
| 49 | class _NetworkExport StaticallyInitializedNetworkFunction : public StaticallyInitializedInstance | 
|---|
| 50 | { | 
|---|
| 51 | public: | 
|---|
| 52 | StaticallyInitializedNetworkFunction(NetworkFunctionBase* function) | 
|---|
| 53 | : StaticallyInitializedInstance(StaticInitialization::NETWORK_FUNCTION) | 
|---|
| 54 | , function_(function) | 
|---|
| 55 | {} | 
|---|
| 56 | ~StaticallyInitializedNetworkFunction() { delete function_; } | 
|---|
| 57 |  | 
|---|
| 58 | virtual void load() override; | 
|---|
| 59 | virtual void unload() override; | 
|---|
| 60 |  | 
|---|
| 61 | inline NetworkFunctionBase& getFunction() | 
|---|
| 62 | { return *this->function_; } | 
|---|
| 63 |  | 
|---|
| 64 | private: | 
|---|
| 65 | NetworkFunctionBase* function_; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | typedef StaticallyInitializedNetworkFunction SI_NF; | 
|---|
| 69 |  | 
|---|
| 70 | template<class PT> | 
|---|
| 71 | inline NetworkFunctionBase* registerStaticNetworkFunctionFct(PT ptr, const std::string& name) | 
|---|
| 72 | { | 
|---|
| 73 | static_assert(sizeof(PT) <= sizeof(NetworkFunctionPointer), "check pointer size"); // if this fails your compiler uses bigger pointers for static functions than defined above | 
|---|
| 74 | NetworkFunctionPointer destptr; | 
|---|
| 75 | copyPtr(ptr, destptr); | 
|---|
| 76 | return new NetworkFunctionStatic(createFunctor(ptr), name, destptr); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | template<class T, class PT> | 
|---|
| 80 | inline NetworkFunctionBase* registerMemberNetworkFunctionFct(PT ptr, const std::string& name) | 
|---|
| 81 | { | 
|---|
| 82 | static_assert(sizeof(PT) <= sizeof(NetworkFunctionPointer), "check pointer size"); // if this fails your compiler uses bigger pointers for a specific kind of member functions than defined above | 
|---|
| 83 | NetworkFunctionPointer destptr; | 
|---|
| 84 | copyPtr(ptr, destptr); | 
|---|
| 85 | return new NetworkMemberFunction<T>(createFunctor(ptr), name, destptr); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | _NetworkExport uint32_t getNetworkIdForPointer(const NetworkFunctionPointer& pointer); | 
|---|
| 89 |  | 
|---|
| 90 | // call it with functionPointer, clientID, args | 
|---|
| 91 | template<class PT> | 
|---|
| 92 | void callStaticNetworkFunction(PT ptr, uint32_t clientID, const MultiType& mt1 = MultiType::Null, const MultiType& mt2 = MultiType::Null, const MultiType& mt3 = MultiType::Null, const MultiType& mt4 = MultiType::Null, const MultiType& mt5 = MultiType::Null) | 
|---|
| 93 | { | 
|---|
| 94 | NetworkFunctionPointer destptr; | 
|---|
| 95 | copyPtr(ptr, destptr); | 
|---|
| 96 | FunctionCallManager::addCall(getNetworkIdForPointer(destptr), OBJECTID_UNKNOWN, clientID, mt1, mt2, mt3, mt4, mt5); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | // call it with class::function, objectID, clientID, args | 
|---|
| 100 | template<class PT> | 
|---|
| 101 | void callMemberNetworkFunction(PT ptr, uint32_t objectID, uint32_t clientID, const MultiType& mt1 = MultiType::Null, const MultiType& mt2 = MultiType::Null, const MultiType& mt3 = MultiType::Null, const MultiType& mt4 = MultiType::Null, const MultiType& mt5 = MultiType::Null) | 
|---|
| 102 | { | 
|---|
| 103 | NetworkFunctionPointer destptr; | 
|---|
| 104 | copyPtr(ptr, destptr); | 
|---|
| 105 | FunctionCallManager::addCall(getNetworkIdForPointer(destptr), objectID, clientID, mt1, mt2, mt3, mt4, mt5); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | template<class PT> | 
|---|
| 109 | inline void copyPtr(PT ptr, NetworkFunctionPointer& destptr) | 
|---|
| 110 | { | 
|---|
| 111 | if (sizeof(NetworkFunctionPointer) - sizeof(PT) > 0) | 
|---|
| 112 | memset((uint8_t*)&destptr + sizeof(PT), 0, sizeof(NetworkFunctionPointer) - sizeof(PT)); | 
|---|
| 113 | PT p2 = ptr; | 
|---|
| 114 | memcpy(&destptr, &p2, sizeof(PT)); | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | #endif /* _NetworkFunctionIncludes_H__ */ | 
|---|