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