1 /***************************************************************************
2 *
3 * Copyright 2012 BMW Car IT GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19
20 #ifndef __SCENESTORE_H__
21 #define __SCENESTORE_H__
22
23
24 //=========================================================================
25 // helper macros fileformat
26 //=========================================================================
27 #include <StringMapTree.h>
28
29 #include <string>
30 #include <sstream>
31 #include <iostream>
32 #include <iomanip>
33
34 using std::string;
35 using std::stringstream;
36 using std::ostream;
37
38 #include <list>
39 using std::list;
40
41 #include <vector>
42 using std::vector;
43
44
45
46
getPrimitiveType(T var)47 template<typename T> string getPrimitiveType(T var)
48 {
49 return "";
50 }
51
getPrimitiveType(T * var)52 template<typename T> string getPrimitiveType(T* var)
53 {
54 (void) var;//suppress warning: unsued variable
55 T var2 = 0;
56 return getPrimitiveType(var2) + "*";
57 }
58
getPrimitiveType(bool var)59 template<> string getPrimitiveType(bool var)
60 {
61 (void) var;//suppress warning: unsued variable
62 return "bool";
63 }
64
getPrimitiveType(char var)65 template<> string getPrimitiveType(char var)
66 {
67 (void) var;//suppress warning: unsued variable
68 return "char";
69 }
70
getPrimitiveType(signed char var)71 template<> string getPrimitiveType(signed char var)
72 {
73 (void) var;//suppress warning: unsued variable
74 return "signed char";
75 }
76
getPrimitiveType(unsigned char var)77 template<> string getPrimitiveType(unsigned char var)
78 {
79 (void) var;//suppress warning: unsued variable
80 return "unsigned char";
81 }
82
getPrimitiveType(wchar_t var)83 template<> string getPrimitiveType(wchar_t var)
84 {
85 (void) var;//suppress warning: unsued variable
86 return "wchar_t";
87 }
88
getPrimitiveType(short int var)89 template<> string getPrimitiveType(short int var)
90 {
91 (void) var;//suppress warning: unsued variable
92 return "short int";
93 }
94
getPrimitiveType(unsigned short int var)95 template<> string getPrimitiveType(unsigned short int var)
96 {
97 (void) var;//suppress warning: unsued variable
98 return "unsigned short int";
99 }
100
getPrimitiveType(long int var)101 template<> string getPrimitiveType(long int var)
102 {
103 (void) var;//suppress warning: unsued variable
104 return "long int";
105 }
106
getPrimitiveType(unsigned long int var)107 template<> string getPrimitiveType(unsigned long int var)
108 {
109 (void) var;//suppress warning: unsued variable
110 return "unsigned long int";
111 }
112
getPrimitiveType(int var)113 template<> string getPrimitiveType(int var)
114 {
115 (void) var;//suppress warning: unsued variable
116 return "int";
117 }
118
getPrimitiveType(unsigned int var)119 template<> string getPrimitiveType(unsigned int var)
120 {
121 (void) var;//suppress warning: unsued variable
122 return "unsigned int";
123 }
124
getPrimitiveType(float var)125 template<> string getPrimitiveType(float var)
126 {
127 (void) var;//suppress warning: unsued variable
128 return "float";
129 }
130
getPrimitiveType(double var)131 template<> string getPrimitiveType(double var)
132 {
133 (void) var;//suppress warning: unsued variable
134 return "double";
135 }
136
getPrimitiveType(long double var)137 template<> string getPrimitiveType(long double var)
138 {
139 (void) var;//suppress warning: unsued variable
140 return "long double";
141 }
142
getPrimitiveType(string var)143 template<> string getPrimitiveType(string var)
144 {
145 (void) var;//suppress warning: unsued variable
146 return "string";
147 }
148
149
150 #if defined(__GXX_EXPERIMENTAL_CXX0X) || __cplusplus >= 201103L
getPrimitiveType(char16_t var)151 template<> string getPrimitiveType(char16_t var)
152 {
153 (void) var;//suppress warning: unsued variable
154 return "char16_t";
155 }
156
getPrimitiveType(char32_t var)157 template<> string getPrimitiveType(char32_t var)
158 {
159 (void) var;//suppress warning: unsued variable
160 return "char32_t";
161 }
162
getPrimitiveType(long long int var)163 template<> string getPrimitiveType(long long int var)
164 {
165 (void) var;//suppress warning: unsued variable
166 return "long long int";
167 }
168
getPrimitiveType(unsigned long long int var)169 template<> string getPrimitiveType(unsigned long long int var)
170 {
171 (void) var;//suppress warning: unsued variable
172 return "unsigned long long int";
173 }
174 #endif
175
176
177 struct WrapperHelper
178 {
179 public:
180 const string mType;
WrapperHelperWrapperHelper181 WrapperHelper(string t) :
182 mType(t)
183 {
184 }
185
~WrapperHelperWrapperHelper186 virtual ~WrapperHelper()
187 {
188 }
189
fromStringWrapperHelper190 virtual void fromString(string s)
191 {
192 (void) s;//suppress warning: unsued variable
193 }
194
asStringWrapperHelper195 virtual string asString()
196 {
197 return "";
198 }
199
toStringMapTreeWrapperHelper200 virtual void toStringMapTree(StringMapTree* parent)
201 {
202 (void) parent;//suppress warning: unsued variable
203 }
204
toGrammarMapTreeWrapperHelper205 virtual void toGrammarMapTree(StringMapTree* tree)
206 {
207 (void) tree;//suppress warning: unsued variable
208 }
209
tryCloneWrapperHelper210 virtual WrapperHelper* tryClone(string type, StringMapTree* tree)
211 {
212 (void) type;//suppress warning: unsued variable
213 (void) tree;//suppress warning: unsued variable
214 return NULL;
215 }
216
addToComplexWrapperWrapperHelper217 virtual void addToComplexWrapper(WrapperHelper* wrapper)
218 {
219 (void) wrapper;//suppress warning: unsued variable
220 }
221
getWrapperPrimitiveTypeWrapperHelper222 virtual string getWrapperPrimitiveType()
223 {
224 return "";
225 }
226 };
227
228 template<typename T>
229 struct ComplexWrapper : public WrapperHelper
230 {
231 public:
232 list<T> components;
233
ComplexWrapperComplexWrapper234 ComplexWrapper(string t) :
235 WrapperHelper(t)
236 {
237 }
238
toStringMapTreeComplexWrapper239 virtual void toStringMapTree(StringMapTree* parent)
240 {
241 for (typename list<T>::iterator it = components.begin(); it != components.end(); ++it)
242 {
243 StringMapTree* node = new StringMapTree;
244 (*it)->toStringMapTree(node);
245 parent->mChildren.push_back(node);
246 }
247 }
248 };
249
250 template<typename T>
251 struct BasicWrapper : public WrapperHelper
252 {
253 public:
254 T value;
255
BasicWrapperBasicWrapper256 BasicWrapper(string t) :
257 WrapperHelper(t)
258 {
259 }
260
fromStringBasicWrapper261 virtual void fromString(string s)
262 {
263 stringstream ss;
264 ss.str(s);
265 ss >> std::skipws >> value;
266 }
267
asStringBasicWrapper268 virtual string asString()
269 {
270 stringstream ss;
271 ss << value;
272 return ss.str();
273 }
274
getWrapperPrimitiveTypeBasicWrapper275 virtual string getWrapperPrimitiveType()
276 {
277 return getPrimitiveType(value);
278 }
279 };
280
281 template<>
282 class BasicWrapper<string>: public WrapperHelper
283 {
284 public:
285 string value;
286
BasicWrapper(string t)287 BasicWrapper(string t) :
288 WrapperHelper(t)
289 {
290 }
291
fromString(string s)292 virtual void fromString(string s)
293 {
294 value = s;
295 }
296
asString()297 virtual string asString()
298 {
299 return value;
300 }
301
getWrapperPrimitiveType()302 virtual string getWrapperPrimitiveType()
303 {
304 return getPrimitiveType(value);
305 }
306 };
307
308 template<>
309 class BasicWrapper<char*>: public WrapperHelper
310 {
311 public:
312 const char* value;
313
BasicWrapper(char * t)314 BasicWrapper(char* t) :
315 WrapperHelper(t)
316 {
317 }
318
fromString(string s)319 virtual void fromString(string s)
320 {
321 value = s.c_str();
322 }
323
asString()324 virtual string asString()
325 {
326 return value;
327 }
328
getWrapperPrimitiveType()329 virtual string getWrapperPrimitiveType()
330 {
331 return getPrimitiveType(value);
332 }
333 };
334
335 template<typename T>
336 struct DummyWrapper : public WrapperHelper
337 {
338 public:
339 T value;
340
DummyWrapperDummyWrapper341 DummyWrapper(string t) :
342 WrapperHelper(t)
343 {
344 }
345
tryCloneDummyWrapper346 virtual WrapperHelper* tryClone(string type, StringMapTree* tree)
347 {
348 if (type == mType)
349 {
350 DummyWrapper<T>* newWrapper = new DummyWrapper<T>(type);
351 newWrapper->value.fromStringMapTree(tree);
352 return newWrapper;
353 }
354
355 return NULL;
356 }
357
toGrammarMapTreeDummyWrapper358 virtual void toGrammarMapTree(StringMapTree* tree)
359 {
360 value.toGrammarMapTree(tree);
361 }
362
addToComplexWrapperDummyWrapper363 virtual void addToComplexWrapper(WrapperHelper* wrapper)
364 {
365 ComplexWrapper<T*>* complexWrapper = static_cast<ComplexWrapper<T*>* >(wrapper);
366 complexWrapper->components.push_back(&value);
367 }
368 };
369
370 map<int, string> _globalTypeIndexdToType;
371
372 #define OBJECT(class_name) \
373 class class_name;\
374 ostream& operator<<(ostream& out, class_name& obj )\
375 {\
376 (void) obj;\
377 return out;\
378 }\
379 istream& operator>>(istream& in, class_name& obj)\
380 {\
381 (void) obj;\
382 return in;\
383 }\
384 class class_name { \
385 public:\
386 map<string, WrapperHelper*> properties;\
387 map<string, string> typesMap;\
388 map<string, WrapperHelper*> components;\
389 map<string, WrapperHelper*> dummyComponentClones;\
390 public:\
391 const static int classNameIndex = __COUNTER__;\
392 static int getClassNameIndex()\
393 {\
394 return classNameIndex;\
395 }\
396 \
397 string mClassName;\
398 template<typename T> bool get(string key, T* p)\
399 {\
400 if(properties.find(key) != properties.end())\
401 {\
402 BasicWrapper<T>* obj = (static_cast<BasicWrapper<T>*> (properties[key]));\
403 if(obj) *p = obj->value;\
404 return obj != NULL;\
405 }\
406 return false;\
407 }\
408 template<typename T> bool get(int* count, T*** p)\
409 {\
410 string type = _globalTypeIndexdToType[T::getClassNameIndex()];\
411 if(components.find(type) != components.end())\
412 {\
413 ComplexWrapper<T*>* obj = static_cast<ComplexWrapper<T*>* >(components[type]);\
414 if(obj){\
415 vector<T*>* temp = new vector<T*>(obj->components.begin(), obj->components.end());\
416 *p = temp->data();\
417 *count = obj->components.size();\
418 }\
419 return obj != NULL;\
420 }\
421 return false;\
422 }\
423 template<typename T> bool get(list<T*>* p)\
424 {\
425 string type = _globalTypeIndexdToType[T::getClassNameIndex()];\
426 if(components.find(type) != components.end())\
427 {\
428 ComplexWrapper<T*>* obj = static_cast<ComplexWrapper<T*>* >(components[type]);\
429 if(obj){\
430 *p = obj->components;\
431 }\
432 return obj != NULL;\
433 }\
434 return false;\
435 }\
436 template<typename T> bool set(string key, const T& p)\
437 {\
438 BasicWrapper<T>* obj = (static_cast<BasicWrapper<T>*> (properties[key]));\
439 if(obj) obj->value = p;\
440 return obj != NULL;\
441 }\
442 template<typename T> bool set(string key, T* p)\
443 {\
444 BasicWrapper<T*>* obj = (static_cast<BasicWrapper<T*>*> (properties[key]));\
445 if(obj) obj->value = p;\
446 return obj != NULL;\
447 }\
448 string getType(string key)\
449 {\
450 return typesMap[key];\
451 }\
452 template<typename T> bool add(T* obj)\
453 {\
454 if(components.find(obj->mClassName) != components.end())\
455 {\
456 (static_cast<ComplexWrapper<T*>* > (components[obj->mClassName])->components).push_back(obj);\
457 return true;\
458 }\
459 return false;\
460 }\
461 template<typename T> bool unwrapAndAdd(DummyWrapper<T>* obj)\
462 {\
463 if(components.find(obj->mClassName) != components.end())\
464 {\
465 (static_cast<ComplexWrapper<T*>* > (components[obj->mClassName])->components).push_back(obj);\
466 return true;\
467 }\
468 return false;\
469 }\
470 template<typename T> bool remove(T* obj)\
471 {\
472 if(components.find(obj->mClassName) != components.end())\
473 {\
474 (static_cast<ComplexWrapper<T*>* > (components[obj->mClassName])->components).remove(obj);\
475 return true;\
476 }\
477 return false;\
478 }\
479 ~class_name()\
480 {\
481 for(map<string, WrapperHelper*>::iterator it = properties.begin(); it != properties.end();++it)\
482 delete (*it).second;\
483 for(map<string, WrapperHelper*>::iterator it = components.begin(); it != components.end();++it)\
484 delete (*it).second;\
485 }\
486 class_name():mClassName(#class_name)\
487 {\
488 _globalTypeIndexdToType[getClassNameIndex()] = #class_name;
489
490 #define PROPERTY(type, name) \
491 properties[#name] = new BasicWrapper<type>(#type);\
492 typesMap[#name] = #type;
493
494
495 #define CONTAINS(type) \
496 components[#type] = new ComplexWrapper<type*>(#type);\
497 dummyComponentClones[#type]= (new DummyWrapper<type>(#type));
498
499
500 #define OBJECTEND \
501 }\
502 void toGrammarMapTree(StringMapTree* node)\
503 {\
504 node->mNodeLabel = mClassName;\
505 for(map<string, WrapperHelper*>::iterator it = properties.begin(); it != properties.end();++it)\
506 {\
507 node->mNodeValues[(*it).first] = make_pair((*it).second->mType, it->second->getWrapperPrimitiveType());\
508 }\
509 for(map<string, WrapperHelper*>::iterator it = dummyComponentClones.begin(); it != dummyComponentClones.end();++it)\
510 {\
511 StringMapTree* child = new StringMapTree;\
512 node->mChildren.push_back(child);\
513 it->second->toGrammarMapTree(child);\
514 }\
515 }\
516 void toStringMapTree(StringMapTree* node)\
517 {\
518 node->mNodeLabel = mClassName;\
519 for(map<string, WrapperHelper*>::iterator it = properties.begin(); it != properties.end();++it)\
520 {\
521 node->mNodeValues[(*it).first] = make_pair((*it).second->mType, (*it).second->asString());\
522 }\
523 for(map<string, WrapperHelper*>::iterator it = components.begin(); it != components.end();++it)\
524 {\
525 it->second->toStringMapTree(node);\
526 }\
527 }\
528 void fromStringMapTree(StringMapTree* node)\
529 {\
530 mClassName = node->mNodeLabel;\
531 for(map<string,pair<string,string> >::iterator it = node->mNodeValues.begin(); it != node->mNodeValues.end();++it)\
532 {\
533 properties[it->first]->fromString(it->second.second);\
534 }\
535 for(list<StringMapTree*>::iterator it = node->mChildren.begin(); it != node->mChildren.end(); ++it )\
536 {\
537 string type = (*it)->mNodeLabel;\
538 WrapperHelper* wrapper = dummyComponentClones[type]->tryClone(type, (*it));\
539 WrapperHelper* complexWrapper = components[type];\
540 wrapper->addToComplexWrapper(complexWrapper);\
541 }\
542 }\
543 };
544
545
546
547 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
548 //**************************************************************************************************************************//
549 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
550
551
552
553 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
554 //**************************************************************************************************************************//
555 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
556
557 OBJECT(IlmSurface)
558 PROPERTY(t_ilm_surface, id)
559 PROPERTY(t_ilm_float, opacity)
560 PROPERTY(t_ilm_uint, sourceX)
561 PROPERTY(t_ilm_uint, sourceY)
562 PROPERTY(t_ilm_uint, sourceWidth)
563 PROPERTY(t_ilm_uint, sourceHeight)
564 PROPERTY(t_ilm_uint, origSourceWidth)
565 PROPERTY(t_ilm_uint, origSourceHeight)
566 PROPERTY(t_ilm_uint, destX)
567 PROPERTY(t_ilm_uint, destY)
568 PROPERTY(t_ilm_uint, destWidth)
569 PROPERTY(t_ilm_uint, destHeight)
570 PROPERTY(t_ilm_bool, visibility)
571 PROPERTY(t_ilm_uint, frameCounter)
572 PROPERTY(t_ilm_uint, drawCounter)
573 PROPERTY(t_ilm_uint, updateCounter)
574 PROPERTY(t_ilm_uint, pixelformat)
575 PROPERTY(t_ilm_uint, nativeSurface)
576 /***/
577 OBJECTEND
578
579 OBJECT(IlmLayer)
580 PROPERTY(t_ilm_layer, id)
581 PROPERTY(t_ilm_float, opacity)
582 PROPERTY(t_ilm_uint, sourceX)
583 PROPERTY(t_ilm_uint, sourceY)
584 PROPERTY(t_ilm_uint, sourceWidth)
585 PROPERTY(t_ilm_uint, sourceHeight)
586 PROPERTY(t_ilm_uint, origSourceWidth)
587 PROPERTY(t_ilm_uint, origSourceHeight)
588 PROPERTY(t_ilm_uint, destX)
589 PROPERTY(t_ilm_uint, destY)
590 PROPERTY(t_ilm_uint, destWidth)
591 PROPERTY(t_ilm_uint, destHeight)
592 PROPERTY(t_ilm_bool, visibility)
593 PROPERTY(t_ilm_uint, type)
594 /***/
595 CONTAINS(IlmSurface)
596 OBJECTEND
597
598 OBJECT(IlmDisplay)
599 PROPERTY(t_ilm_display, id)
600 PROPERTY(t_ilm_uint, width)
601 PROPERTY(t_ilm_uint, height)
602 /***/
603 CONTAINS(IlmLayer)
604 OBJECTEND
605
606 OBJECT(IlmScene)
607 /***/
608 CONTAINS(IlmSurface)
609 CONTAINS(IlmLayer)
610 CONTAINS(IlmDisplay)
611 OBJECTEND
612
613 #endif /* __SCENESTORE_H__ */
614