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 | #include <boost/static_assert.hpp> |
---|
36 | |
---|
37 | #include "NetworkFunction.h" |
---|
38 | #include "NetworkFunctionManager.h" |
---|
39 | #include "core/module/StaticallyInitializedInstance.h" |
---|
40 | |
---|
41 | #define registerStaticNetworkFunction( functionPointer ) \ |
---|
42 | static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) \ |
---|
43 | = (new orxonox::SI_NF(orxonox::registerStaticNetworkFunctionFct( functionPointer, #functionPointer )))->getFunction() |
---|
44 | |
---|
45 | #define registerMemberNetworkFunction( class, function ) \ |
---|
46 | static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) \ |
---|
47 | = (new orxonox::SI_NF(orxonox::registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function)))->getFunction() |
---|
48 | |
---|
49 | // call it with functionPointer, clientID, args |
---|
50 | #define callStaticNetworkFunction( functionPointer, ...) \ |
---|
51 | { \ |
---|
52 | NetworkFunctionPointer p1; \ |
---|
53 | copyPtr( functionPointer, p1 ); \ |
---|
54 | FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \ |
---|
55 | } |
---|
56 | |
---|
57 | // call it with class, function, objectID, clientID, args |
---|
58 | #define callMemberNetworkFunction( class, function, objectID, ...) \ |
---|
59 | { \ |
---|
60 | NetworkFunctionPointer p1; \ |
---|
61 | copyPtr( &class::function, p1 ); \ |
---|
62 | FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), objectID, __VA_ARGS__); \ |
---|
63 | } |
---|
64 | |
---|
65 | namespace orxonox |
---|
66 | { |
---|
67 | class _CoreExport StaticallyInitializedNetworkFunction : public StaticallyInitializedInstance |
---|
68 | { |
---|
69 | public: |
---|
70 | StaticallyInitializedNetworkFunction(NetworkFunctionBase* function) : function_(function) {} |
---|
71 | |
---|
72 | virtual void load() |
---|
73 | { NetworkFunctionManager::getInstance().registerFunction(this->function_); } |
---|
74 | virtual void unload() |
---|
75 | { NetworkFunctionManager::getInstance().unregisterFunction(this->function_); } |
---|
76 | |
---|
77 | inline NetworkFunctionBase& getFunction() |
---|
78 | { return *this->function_; } |
---|
79 | |
---|
80 | private: |
---|
81 | NetworkFunctionBase* function_; |
---|
82 | }; |
---|
83 | |
---|
84 | typedef StaticallyInitializedNetworkFunction SI_NF; |
---|
85 | |
---|
86 | template<class T> inline NetworkFunctionBase* registerStaticNetworkFunctionFct( T ptr, const std::string& name ) |
---|
87 | { |
---|
88 | BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above |
---|
89 | NetworkFunctionPointer destptr; |
---|
90 | copyPtr( ptr, destptr ); |
---|
91 | return new NetworkFunctionStatic( createFunctor(ptr), name, destptr ); |
---|
92 | } |
---|
93 | |
---|
94 | template<class T, class PT> inline NetworkFunctionBase* registerMemberNetworkFunctionFct( PT ptr, const std::string& name ) |
---|
95 | { |
---|
96 | 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 |
---|
97 | NetworkFunctionPointer destptr; |
---|
98 | copyPtr( ptr, destptr ); |
---|
99 | return new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr ); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | #endif /* _NetworkFunctionIncludes_H__ */ |
---|