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