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 | |
---|
29 | #ifndef _NetworkFunction_H__ |
---|
30 | #define _NetworkFunction_H__ |
---|
31 | |
---|
32 | #include "NetworkPrereqs.h" |
---|
33 | |
---|
34 | #include <cassert> |
---|
35 | #include <cstring> |
---|
36 | #include <map> |
---|
37 | #include <string> |
---|
38 | |
---|
39 | #include "core/command/Functor.h" |
---|
40 | #include "FunctionCallManager.h" |
---|
41 | #include "synchronisable/Synchronisable.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | |
---|
46 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
47 | static constexpr unsigned int MAX_FUNCTION_POINTER_SIZE = 8; |
---|
48 | #else |
---|
49 | static constexpr unsigned int MAX_FUNCTION_POINTER_SIZE = 16; |
---|
50 | #endif //ORXONOX_COMPILER_GCC |
---|
51 | static constexpr unsigned int MAX_FUNCTION_POINTER_INTS = (MAX_FUNCTION_POINTER_SIZE-1)/4+1; |
---|
52 | |
---|
53 | struct _NetworkExport NetworkFunctionPointer { |
---|
54 | uint32_t pointer[MAX_FUNCTION_POINTER_INTS]; |
---|
55 | bool operator<(const NetworkFunctionPointer& b) const |
---|
56 | { |
---|
57 | #if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32) |
---|
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 |
---|
76 | } |
---|
77 | }; |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
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 | |
---|
93 | virtual bool call(uint32_t objectID)=0; |
---|
94 | virtual bool call(uint32_t objectID, const MultiType& mt1)=0; |
---|
95 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0; |
---|
96 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0; |
---|
97 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0; |
---|
98 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0; |
---|
99 | |
---|
100 | private: |
---|
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) override{ (*this->functor_)(); return true; } |
---|
117 | virtual bool call(uint32_t objectID, const MultiType& mt1) override{ (*this->functor_)(mt1); return true; } |
---|
118 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2) override{ (*this->functor_)(mt1, mt2); return true; } |
---|
119 | virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) override{ (*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) override{ (*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) override{ (*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 | { } |
---|
134 | }; |
---|
135 | |
---|
136 | |
---|
137 | template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase { |
---|
138 | public: |
---|
139 | NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p) |
---|
140 | : NetworkMemberFunctionBase(name, p) |
---|
141 | , functor_(functor) |
---|
142 | { } |
---|
143 | |
---|
144 | virtual inline bool call(uint32_t objectID) override |
---|
145 | { |
---|
146 | if ( Synchronisable::getSynchronisable(objectID)!=nullptr ) |
---|
147 | { |
---|
148 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID))); |
---|
149 | return true; |
---|
150 | } |
---|
151 | else |
---|
152 | return false; |
---|
153 | } |
---|
154 | virtual inline bool call(uint32_t objectID, const MultiType& mt1) override |
---|
155 | { |
---|
156 | if ( Synchronisable::getSynchronisable(objectID)!=nullptr ) |
---|
157 | { |
---|
158 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1); |
---|
159 | return true; |
---|
160 | } |
---|
161 | else |
---|
162 | return false; |
---|
163 | } |
---|
164 | virtual inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2) override |
---|
165 | { |
---|
166 | if ( Synchronisable::getSynchronisable(objectID)!=nullptr ) |
---|
167 | { |
---|
168 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2); |
---|
169 | return true; |
---|
170 | } |
---|
171 | else |
---|
172 | return false; |
---|
173 | } |
---|
174 | virtual inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) override |
---|
175 | { |
---|
176 | if ( Synchronisable::getSynchronisable(objectID)!=nullptr ) |
---|
177 | { |
---|
178 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3); |
---|
179 | return true; |
---|
180 | } |
---|
181 | else |
---|
182 | return false; |
---|
183 | } |
---|
184 | virtual inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4) override |
---|
185 | { |
---|
186 | if ( Synchronisable::getSynchronisable(objectID)!=nullptr ) |
---|
187 | { |
---|
188 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4); |
---|
189 | return true; |
---|
190 | } |
---|
191 | else |
---|
192 | return false; |
---|
193 | } |
---|
194 | virtual inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) override |
---|
195 | { |
---|
196 | if ( Synchronisable::getSynchronisable(objectID)!=nullptr ) |
---|
197 | { |
---|
198 | (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5); |
---|
199 | return true; |
---|
200 | } |
---|
201 | else |
---|
202 | return false; |
---|
203 | } |
---|
204 | |
---|
205 | private: |
---|
206 | FunctorMemberPtr<T> functor_; |
---|
207 | }; |
---|
208 | |
---|
209 | } |
---|
210 | |
---|
211 | #endif /* _NetworkFunction_H__ */ |
---|