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, (C) 2008 |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include <cstring> |
---|
30 | #include "util/Math.h" |
---|
31 | |
---|
32 | #ifndef _NETWORK_SYNCHRONISABLEVARIABLESPECIALISATIONS__ |
---|
33 | #define _NETWORK_SYNCHRONISABLEVARIABLESPECIALISATIONS__ |
---|
34 | |
---|
35 | namespace orxonox{ |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | // =================== Template specialisation stuff ============= |
---|
40 | |
---|
41 | // =========== bool |
---|
42 | |
---|
43 | template <> inline uint32_t SynchronisableVariable<const bool>::returnSize() |
---|
44 | { |
---|
45 | return sizeof(uint8_t); |
---|
46 | } |
---|
47 | |
---|
48 | template <> inline void SynchronisableVariable<const bool>::setAndIncrease( uint8_t*& mem ) |
---|
49 | { |
---|
50 | *(uint8_t*)(&this->variable_) = *static_cast<uint8_t*>(mem); |
---|
51 | mem += SynchronisableVariable<const bool>::returnSize(); |
---|
52 | } |
---|
53 | |
---|
54 | template <> inline void SynchronisableVariable<const bool>::getAndIncrease( uint8_t*& mem ) |
---|
55 | { |
---|
56 | *static_cast<uint8_t*>(mem) = *(uint8_t*)(&this->variable_); |
---|
57 | mem += SynchronisableVariable<const bool>::returnSize(); |
---|
58 | } |
---|
59 | |
---|
60 | template <> inline bool SynchronisableVariable<const bool>::checkEquality( uint8_t* mem ) |
---|
61 | { |
---|
62 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)(&this->variable_); |
---|
63 | } |
---|
64 | |
---|
65 | // =========== char |
---|
66 | |
---|
67 | template <> inline uint32_t SynchronisableVariable<const char>::returnSize() |
---|
68 | { |
---|
69 | return sizeof(uint8_t); |
---|
70 | } |
---|
71 | |
---|
72 | template <> inline void SynchronisableVariable<const char>::setAndIncrease( uint8_t*& mem ) |
---|
73 | { |
---|
74 | *(uint8_t*)(&this->variable_) = *static_cast<uint8_t*>(mem); |
---|
75 | mem += SynchronisableVariable<const char>::returnSize(); |
---|
76 | } |
---|
77 | |
---|
78 | template <> inline void SynchronisableVariable<const char>::getAndIncrease( uint8_t*& mem ) |
---|
79 | { |
---|
80 | *static_cast<uint8_t*>(mem) = *(uint8_t*)(&this->variable_); |
---|
81 | mem += SynchronisableVariable<const char>::returnSize(); |
---|
82 | } |
---|
83 | |
---|
84 | template <> inline bool SynchronisableVariable<const char>::checkEquality( uint8_t* mem ) |
---|
85 | { |
---|
86 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)(&this->variable_); |
---|
87 | } |
---|
88 | |
---|
89 | // =========== unsigned char |
---|
90 | |
---|
91 | template <> inline uint32_t SynchronisableVariable<const unsigned char>::returnSize() |
---|
92 | { |
---|
93 | return sizeof(uint8_t); |
---|
94 | } |
---|
95 | |
---|
96 | template <> inline void SynchronisableVariable<const unsigned char>::setAndIncrease( uint8_t*& mem ) |
---|
97 | { |
---|
98 | *(uint8_t*)(&this->variable_) = *static_cast<uint8_t*>(mem); |
---|
99 | mem += SynchronisableVariable<const unsigned char>::returnSize(); |
---|
100 | } |
---|
101 | |
---|
102 | template <> inline void SynchronisableVariable<const unsigned char>::getAndIncrease( uint8_t*& mem ) |
---|
103 | { |
---|
104 | *static_cast<uint8_t*>(mem) = *(uint8_t*)(&this->variable_); |
---|
105 | mem += SynchronisableVariable<const unsigned char>::returnSize(); |
---|
106 | } |
---|
107 | |
---|
108 | template <> inline bool SynchronisableVariable<const unsigned char>::checkEquality( uint8_t* mem ) |
---|
109 | { |
---|
110 | return *static_cast<uint8_t*>(mem) == *(uint8_t*)(&this->variable_); |
---|
111 | } |
---|
112 | |
---|
113 | // =========== short |
---|
114 | |
---|
115 | template <> inline uint32_t SynchronisableVariable<const short>::returnSize() |
---|
116 | { |
---|
117 | return sizeof(int16_t); |
---|
118 | } |
---|
119 | |
---|
120 | template <> inline void SynchronisableVariable<const short>::setAndIncrease( uint8_t*& mem ) |
---|
121 | { |
---|
122 | *(short*)(&this->variable_) = *(int16_t*)(mem); |
---|
123 | mem += SynchronisableVariable<const short>::returnSize(); |
---|
124 | } |
---|
125 | |
---|
126 | template <> inline void SynchronisableVariable<const short>::getAndIncrease( uint8_t*& mem ) |
---|
127 | { |
---|
128 | *(int16_t*)(mem) = this->variable_; |
---|
129 | mem += SynchronisableVariable<const short>::returnSize(); |
---|
130 | } |
---|
131 | |
---|
132 | template <> inline bool SynchronisableVariable<const short>::checkEquality( uint8_t* mem ) |
---|
133 | { |
---|
134 | return *(int16_t*)(mem) == static_cast<int16_t>(this->variable_); |
---|
135 | } |
---|
136 | |
---|
137 | // =========== unsigned short |
---|
138 | |
---|
139 | template <> inline uint32_t SynchronisableVariable<const unsigned short>::returnSize() |
---|
140 | { |
---|
141 | return sizeof(uint16_t); |
---|
142 | } |
---|
143 | |
---|
144 | template <> inline void SynchronisableVariable<const unsigned short>::setAndIncrease( uint8_t*& mem ) |
---|
145 | { |
---|
146 | *(unsigned short*)(&this->variable_) = *(uint16_t*)(mem); |
---|
147 | mem += SynchronisableVariable<const unsigned short>::returnSize(); |
---|
148 | } |
---|
149 | |
---|
150 | template <> inline void SynchronisableVariable<const unsigned short>::getAndIncrease( uint8_t*& mem ) |
---|
151 | { |
---|
152 | *(uint16_t*)(mem) = this->variable_; |
---|
153 | mem += SynchronisableVariable<const unsigned short>::returnSize(); |
---|
154 | } |
---|
155 | |
---|
156 | template <> inline bool SynchronisableVariable<const unsigned short>::checkEquality( uint8_t* mem ) |
---|
157 | { |
---|
158 | return *(uint16_t*)(mem) == this->variable_; |
---|
159 | } |
---|
160 | |
---|
161 | // =========== int |
---|
162 | |
---|
163 | template <> inline uint32_t SynchronisableVariable<const int>::returnSize() |
---|
164 | { |
---|
165 | return sizeof(int32_t); |
---|
166 | } |
---|
167 | |
---|
168 | template <> inline void SynchronisableVariable<const int>::setAndIncrease( uint8_t*& mem ) |
---|
169 | { |
---|
170 | *(int *)(&this->variable_) = *(int32_t*)(mem); |
---|
171 | mem += SynchronisableVariable<const int>::returnSize(); |
---|
172 | } |
---|
173 | |
---|
174 | template <> inline void SynchronisableVariable<const int>::getAndIncrease( uint8_t*& mem ) |
---|
175 | { |
---|
176 | *(int32_t*)(mem) = this->variable_; |
---|
177 | mem += SynchronisableVariable<const int>::returnSize(); |
---|
178 | } |
---|
179 | |
---|
180 | template <> inline bool SynchronisableVariable<const int>::checkEquality( uint8_t* mem ) |
---|
181 | { |
---|
182 | return *(int32_t*)(mem) == this->variable_; |
---|
183 | } |
---|
184 | |
---|
185 | // =========== unsigned int |
---|
186 | |
---|
187 | template <> inline uint32_t SynchronisableVariable<const unsigned int>::returnSize() |
---|
188 | { |
---|
189 | return sizeof(uint32_t); |
---|
190 | } |
---|
191 | |
---|
192 | template <> inline void SynchronisableVariable<const unsigned int>::setAndIncrease( uint8_t*& mem ) |
---|
193 | { |
---|
194 | *(unsigned int*)(&this->variable_) = *(uint32_t*)(mem); |
---|
195 | mem += SynchronisableVariable<const unsigned int>::returnSize(); |
---|
196 | } |
---|
197 | |
---|
198 | template <> inline void SynchronisableVariable<const unsigned int>::getAndIncrease( uint8_t*& mem ) |
---|
199 | { |
---|
200 | *(uint32_t*)(mem) = this->variable_; |
---|
201 | mem += SynchronisableVariable<const unsigned int>::returnSize(); |
---|
202 | } |
---|
203 | |
---|
204 | template <> inline bool SynchronisableVariable<const unsigned int>::checkEquality( uint8_t* mem ) |
---|
205 | { |
---|
206 | return *(uint32_t*)(mem) == this->variable_; |
---|
207 | } |
---|
208 | |
---|
209 | // =========== long |
---|
210 | |
---|
211 | template <> inline uint32_t SynchronisableVariable<const long>::returnSize() |
---|
212 | { |
---|
213 | return sizeof(int32_t); |
---|
214 | } |
---|
215 | |
---|
216 | template <> inline void SynchronisableVariable<const long>::setAndIncrease( uint8_t*& mem ) |
---|
217 | { |
---|
218 | *(long*)(&this->variable_) = *(int32_t*)(mem); |
---|
219 | mem += SynchronisableVariable<const long>::returnSize(); |
---|
220 | } |
---|
221 | |
---|
222 | template <> inline void SynchronisableVariable<const long>::getAndIncrease( uint8_t*& mem ) |
---|
223 | { |
---|
224 | *(int32_t*)(mem) = this->variable_; |
---|
225 | mem += SynchronisableVariable<const long>::returnSize(); |
---|
226 | } |
---|
227 | |
---|
228 | template <> inline bool SynchronisableVariable<const long>::checkEquality( uint8_t* mem ) |
---|
229 | { |
---|
230 | return *(int32_t*)(mem) == this->variable_; |
---|
231 | } |
---|
232 | |
---|
233 | // =========== unsigned long |
---|
234 | |
---|
235 | template <> inline uint32_t SynchronisableVariable<const unsigned long>::returnSize() |
---|
236 | { |
---|
237 | return sizeof(uint32_t); |
---|
238 | } |
---|
239 | |
---|
240 | template <> inline void SynchronisableVariable<const unsigned long>::setAndIncrease( uint8_t*& mem ) |
---|
241 | { |
---|
242 | *(unsigned long*)(&this->variable_) = *(uint32_t*)(mem); |
---|
243 | mem += SynchronisableVariable<const unsigned long>::returnSize(); |
---|
244 | } |
---|
245 | |
---|
246 | template <> inline void SynchronisableVariable<const unsigned long>::getAndIncrease( uint8_t*& mem ) |
---|
247 | { |
---|
248 | *(uint32_t*)(mem) = this->variable_; |
---|
249 | mem += SynchronisableVariable<const unsigned long>::returnSize(); |
---|
250 | } |
---|
251 | |
---|
252 | template <> inline bool SynchronisableVariable<const unsigned long>::checkEquality( uint8_t* mem ) |
---|
253 | { |
---|
254 | return *(uint32_t*)(mem) == this->variable_; |
---|
255 | } |
---|
256 | |
---|
257 | // =========== long long |
---|
258 | |
---|
259 | template <> inline uint32_t SynchronisableVariable<const long long>::returnSize() |
---|
260 | { |
---|
261 | return sizeof(int64_t); |
---|
262 | } |
---|
263 | |
---|
264 | template <> inline void SynchronisableVariable<const long long>::setAndIncrease( uint8_t*& mem ) |
---|
265 | { |
---|
266 | *(long long*)(&this->variable_) = *(int64_t*)(mem); |
---|
267 | mem += SynchronisableVariable<const long long>::returnSize(); |
---|
268 | } |
---|
269 | |
---|
270 | template <> inline void SynchronisableVariable<const long long>::getAndIncrease( uint8_t*& mem ) |
---|
271 | { |
---|
272 | *(int64_t*)(mem) = this->variable_; |
---|
273 | mem += SynchronisableVariable<const long long>::returnSize(); |
---|
274 | } |
---|
275 | |
---|
276 | template <> inline bool SynchronisableVariable<const long long>::checkEquality( uint8_t* mem ) |
---|
277 | { |
---|
278 | return *(int64_t*)(mem) == this->variable_; |
---|
279 | } |
---|
280 | |
---|
281 | // =========== unsigned long long |
---|
282 | |
---|
283 | template <> inline uint32_t SynchronisableVariable<const unsigned long long>::returnSize() |
---|
284 | { |
---|
285 | return sizeof(uint64_t); |
---|
286 | } |
---|
287 | |
---|
288 | template <> inline void SynchronisableVariable<const unsigned long long>::setAndIncrease( uint8_t*& mem ) |
---|
289 | { |
---|
290 | *(unsigned long long*)(&this->variable_) = *(uint64_t*)(mem); |
---|
291 | mem += SynchronisableVariable<const unsigned long long>::returnSize(); |
---|
292 | } |
---|
293 | |
---|
294 | template <> inline void SynchronisableVariable<const unsigned long long>::getAndIncrease( uint8_t*& mem ) |
---|
295 | { |
---|
296 | *(uint64_t*)(mem) = this->variable_; |
---|
297 | mem += SynchronisableVariable<const unsigned long long>::returnSize(); |
---|
298 | } |
---|
299 | |
---|
300 | template <> inline bool SynchronisableVariable<const unsigned long long>::checkEquality( uint8_t* mem ) |
---|
301 | { |
---|
302 | return *(uint64_t*)(mem) == this->variable_; |
---|
303 | } |
---|
304 | |
---|
305 | // =========== float |
---|
306 | |
---|
307 | template <> inline uint32_t SynchronisableVariable<const float>::returnSize() |
---|
308 | { |
---|
309 | return sizeof(uint32_t); |
---|
310 | } |
---|
311 | |
---|
312 | template <> inline void SynchronisableVariable<const float>::setAndIncrease( uint8_t*& mem ) |
---|
313 | { |
---|
314 | *(uint32_t*)(&this->variable_) = *(uint32_t*)(mem); |
---|
315 | mem += SynchronisableVariable<const float>::returnSize(); |
---|
316 | } |
---|
317 | |
---|
318 | template <> inline void SynchronisableVariable<const float>::getAndIncrease( uint8_t*& mem ) |
---|
319 | { |
---|
320 | *(uint32_t*)(mem) = *(uint32_t*)(&this->variable_); |
---|
321 | mem += SynchronisableVariable<const float>::returnSize(); |
---|
322 | } |
---|
323 | |
---|
324 | template <> inline bool SynchronisableVariable<const float>::checkEquality( uint8_t* mem ) |
---|
325 | { |
---|
326 | return *(uint32_t*)(mem) == *(uint32_t*)(&this->variable_); |
---|
327 | } |
---|
328 | |
---|
329 | // =========== double |
---|
330 | |
---|
331 | template <> inline uint32_t SynchronisableVariable<const double>::returnSize() |
---|
332 | { |
---|
333 | return sizeof(uint64_t); |
---|
334 | } |
---|
335 | |
---|
336 | template <> inline void SynchronisableVariable<const double>::setAndIncrease( uint8_t*& mem ) |
---|
337 | { |
---|
338 | *(uint64_t*)(&this->variable_) = *(uint64_t*)(mem); |
---|
339 | mem += SynchronisableVariable<const double>::returnSize(); |
---|
340 | } |
---|
341 | |
---|
342 | template <> inline void SynchronisableVariable<const double>::getAndIncrease( uint8_t*& mem ) |
---|
343 | { |
---|
344 | *(uint64_t*)(mem) = *(uint64_t*)(&this->variable_); |
---|
345 | mem += SynchronisableVariable<const double>::returnSize(); |
---|
346 | } |
---|
347 | |
---|
348 | template <> inline bool SynchronisableVariable<const double>::checkEquality( uint8_t* mem ) |
---|
349 | { |
---|
350 | return *(uint64_t*)(mem) == *(uint64_t*)(&this->variable_); |
---|
351 | } |
---|
352 | |
---|
353 | // =========== long double |
---|
354 | |
---|
355 | template <> inline uint32_t SynchronisableVariable<const long double>::returnSize() |
---|
356 | { |
---|
357 | return sizeof(uint64_t); |
---|
358 | } |
---|
359 | |
---|
360 | template <> inline void SynchronisableVariable<const long double>::setAndIncrease( uint8_t*& mem ) |
---|
361 | { |
---|
362 | double temp; |
---|
363 | memcpy(&temp, mem, sizeof(uint64_t)); |
---|
364 | *(long double*)(&this->variable_) = static_cast<const long double>(temp); |
---|
365 | mem += SynchronisableVariable<const long double>::returnSize(); |
---|
366 | } |
---|
367 | |
---|
368 | template <> inline void SynchronisableVariable<const long double>::getAndIncrease( uint8_t*& mem ) |
---|
369 | { |
---|
370 | double temp = static_cast<double>(this->variable_); |
---|
371 | memcpy(mem, &temp, sizeof(uint64_t)); |
---|
372 | mem += SynchronisableVariable<const long double>::returnSize(); |
---|
373 | } |
---|
374 | |
---|
375 | template <> inline bool SynchronisableVariable<const long double>::checkEquality( uint8_t* mem ) |
---|
376 | { |
---|
377 | double temp = static_cast<double>(this->variable_); |
---|
378 | return memcmp(&temp, mem, sizeof(uint64_t))==0; |
---|
379 | } |
---|
380 | |
---|
381 | // =========== string |
---|
382 | |
---|
383 | template <> inline uint32_t SynchronisableVariable<const std::string>::returnSize() |
---|
384 | { |
---|
385 | return variable_.length()+1; |
---|
386 | } |
---|
387 | |
---|
388 | template <> inline void SynchronisableVariable<const std::string>::getAndIncrease( uint8_t*& mem ) |
---|
389 | { |
---|
390 | memcpy(mem, this->variable_.c_str(), this->variable_.length()+1); |
---|
391 | mem += this->variable_.length()+1; |
---|
392 | } |
---|
393 | |
---|
394 | template <> inline void SynchronisableVariable<const std::string>::setAndIncrease( uint8_t*& mem ) |
---|
395 | { |
---|
396 | *(std::string*)(&this->variable_) = std::string((const char *)mem); |
---|
397 | mem += this->variable_.length()+1; |
---|
398 | } |
---|
399 | |
---|
400 | template <> inline bool SynchronisableVariable<const std::string>::checkEquality( uint8_t* mem ) |
---|
401 | { |
---|
402 | return std::string((const char*)mem)==this->variable_; |
---|
403 | } |
---|
404 | |
---|
405 | // =========== Degree |
---|
406 | |
---|
407 | template <> inline uint32_t SynchronisableVariable<const Degree>::returnSize() |
---|
408 | { |
---|
409 | return sizeof(Ogre::Real); |
---|
410 | } |
---|
411 | |
---|
412 | template <> inline void SynchronisableVariable<const Degree>::getAndIncrease( uint8_t*& mem ) |
---|
413 | { |
---|
414 | Ogre::Real r = this->variable_.valueDegrees(); |
---|
415 | memcpy(mem, &r, returnSize()); |
---|
416 | mem += returnSize(); |
---|
417 | } |
---|
418 | |
---|
419 | template <> inline void SynchronisableVariable<const Degree>::setAndIncrease( uint8_t*& mem ) |
---|
420 | { |
---|
421 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
422 | (Degree&)this->variable_ = *r; |
---|
423 | mem += returnSize(); |
---|
424 | } |
---|
425 | |
---|
426 | template <> inline bool SynchronisableVariable<const Degree>::checkEquality( uint8_t* mem ) |
---|
427 | { |
---|
428 | Ogre::Real* r = (Ogre::Real*)mem; |
---|
429 | return this->variable_==Degree(*r); |
---|
430 | } |
---|
431 | |
---|
432 | } |
---|
433 | |
---|
434 | |
---|
435 | #endif |
---|