source: cfg_itronx+oil_gcc/toppers/oil/oil_object.hpp@ 165

Last change on this file since 165 was 54, checked in by ertl-ishikawa, 12 years ago

cfg+oil対応コンフィギュレータを追加

File size: 26.0 KB
Line 
1/*
2 * TOPPERS Software
3 * Toyohashi Open Platform for Embedded Real-Time Systems
4 *
5 * Copyright (C) 2010 by Meika Sugimoto
6 *
7 * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
8 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
9 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
10 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
11 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
12 * スコード中に含まれていること.
13 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
14 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
15 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
16 * の無保証規定を掲載すること.
17 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
18 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
19 * と.
20 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
21 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
22 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
23 * 報告すること.
24 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
25 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
26 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
27 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
28 * 免責すること.
29 *
30 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
31 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
32 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
33 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
34 * の責任を負わない.
35 *
36 */
37
38#ifndef OIL_OBJECT_H
39#define OIL_OBJECT_H
40
41#include <string.h>
42#include <vector>
43#include <map>
44#include <algorithm>
45#include <boost/regex.hpp>
46#include <boost/lexical_cast.hpp>
47
48#include "toppers/diagnostics.hpp"
49
50using namespace std;
51
52namespace toppers
53{
54 namespace oil
55 {
56 extern void dump(boost::smatch *r);
57
58 namespace oil_definition
59 {
60 // クラスの前方宣言
61 class object_parameter_def;
62 class object_definition;
63 }
64
65 // パラメータタイプ
66 enum PARAMETER_TYPE
67 {
68 TYPE_UINT = 1 ,
69 TYPE_INT ,
70 TYPE_FLOAT ,
71 TYPE_STRING ,
72 TYPE_BOOLEAN ,
73 TYPE_ENUM ,
74 TYPE_REF ,
75 TYPE_UNKNOWN
76 };
77
78 typedef struct
79 {
80 string obj_type;
81 string obj_name;
82 } object_ref;
83
84 namespace oil_implementation
85 {
86
87 // クラスの前方宣言
88 class object_parameter_impl;
89
90 class oil_object_impl
91 {
92 public:
93 oil_object_impl(string object_name)
94 {
95 name = object_name;
96 }
97 ~oil_object_impl();
98 int add_parameter(string param_name , string parameter_descripition);
99 bool validate_object_configuration
100 (oil_definition::object_definition* obj_def , std::vector<object_ref> *references);
101 void display_implementation(void);
102 private:
103 // modified by takuya 110823
104 //bool oil_object_impl::validate_object_parameter
105 bool validate_object_parameter
106 (oil_definition::object_parameter_def *obj_param , bool *multiple ,
107 std::vector<object_ref> *references);
108 string name;
109 std::vector<object_parameter_impl*> params;
110 };
111
112 // 値制限の種類
113 enum VALUE_LIMIT_TYPE
114 {
115 LIMIT_RANGE = 1 ,
116 LIMIT_LIST ,
117 LIMIT_NONE
118 };
119 // デフォルト値の種類
120 enum DEFAULT_TYPE
121 {
122 NO_DEFAULT = 1 ,
123 AUTO ,
124 HAVE_DEFAULT ,
125 HAVE_NOT_DEFINE
126 };
127
128 class object_parameter_impl
129 {
130 public:
131 object_parameter_impl(string param_name)
132 {
133 name = param_name;
134 }
135 object_parameter_impl(string param_name , string parameter_description)
136 {
137 name = param_name;
138
139 // AUTO属性の解析
140 if(parameter_description.find("WITH_AUTO") != string::npos)
141 {
142 assign_auto = true;
143 }
144 else
145 {
146 assign_auto = false;
147 }
148
149 /// ENUMのときに問題あり
150 // 複数定義有無の解析
151 boost::regex brace("\\[\\s*\\]\\s*(=.+)?;");
152 if(boost::regex_search(parameter_description , brace))
153 {
154 multiple = true;
155 }
156 else
157 {
158 multiple = false;
159 }
160
161 // デフォルト値の解析
162 boost::regex default_value("=\\s+(\\S+)\\s*;");
163 boost::smatch result;
164 string::const_iterator start = parameter_description.begin();
165 string::const_iterator end = parameter_description.end();
166 if(boost::regex_search(start , end , result , default_value))
167 {
168 default_description = result.str(1);
169 if(default_description == "AUTO")
170 {
171 default_type = AUTO;
172 }
173 else if(default_description == "NO_DEFAULT")
174 {
175 default_type = NO_DEFAULT;
176 }
177 else
178 {
179 default_type = HAVE_DEFAULT;
180 }
181 }
182 else
183 {
184 default_type = HAVE_NOT_DEFINE;
185 default_description.erase(0);
186 }
187 }
188
189 string get_name(void)
190 {
191 return name;
192 }
193
194 DEFAULT_TYPE get_default(string *str)
195 {
196 if((default_type == HAVE_DEFAULT)
197 || (default_type == AUTO))
198 {
199 *str = default_description;
200 }
201
202 return default_type;
203 }
204
205 virtual void display_implementation(void)
206 {
207 cout << "Parameter Name : " << name << endl;
208 cout << "Multilple : " << ((multiple == true)? "Yes" : "No") << endl;
209 cout << "Auto Assignment : " << ((assign_auto == true)? "Yes" : "No") << endl;
210 cout << "Default Value : ";
211 if(default_type == NO_DEFAULT)
212 {
213 cout << "NO_DEFAULT";
214 }
215 else if(default_type == AUTO)
216 {
217 cout << "AUTO";
218 }
219 else
220 {
221 cout << "HAVE_DEFAULT";
222 }
223 cout << endl;
224 }
225
226 virtual bool validate_parameter_name(string param_name)
227 {
228 if(name == param_name)
229 {
230 return true;
231 }
232 else
233 {
234 return false;
235 }
236 }
237
238 bool validate_object_parameter
239 (oil_definition::object_parameter_def *obj , bool *auto_param);
240 //virtual bool object_parameter_impl::validate_object_parameter
241 virtual bool validate_object_parameter
242 (oil_definition::object_parameter_def *obj)
243 {
244 return false;
245 }
246 virtual void set_parameter_type(oil_definition::object_parameter_def *obj);
247
248 bool get_multiple(void)
249 {
250 return multiple;
251 }
252 protected:
253 inline const char* return_format(double x , int type)
254 {
255 return "%lf";
256 }
257
258 inline const char* return_format(int64_t x , int type)
259 {
260 if(type == 0)
261 {
262 // modified by takuya 110823
263 // return "%I64x";
264 return "%llx";
265 }
266 else
267 {
268 // modified by takuya 110823
269 // return "%I64d";
270 return "%lld";
271 }
272 }
273
274 inline const char* return_format(uint64_t x , int type)
275 {
276 if(type == 0)
277 {
278 // modified by takuya 110823
279 // return "%I64x";
280 return "%llx";
281 }
282 else
283 {
284 // modified by takuya 110823
285 // return "%I64u";
286 return "%llu";
287 }
288 }
289
290 template <typename T>
291 T str2val(const char *str , T dummy)
292 {
293 T temp;
294 T zero = 0 , one = 1;
295 if((str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X')))
296 {
297 // modified by takuya 110823
298 //sscanf_s(&str[2] , return_format(zero , 0) , &temp);
299 sscanf(&str[2] , return_format(zero , 0) , &temp);
300 }
301 else
302 {
303 // modified by takuya 110823
304 //sscanf_s(&str[0] , return_format(zero , 1) , &temp);
305 sscanf(&str[0] , return_format(zero , 1) , &temp);
306 }
307 return temp;
308 }
309
310 template <typename T>
311 void split_number_list(const char *str , std::vector<T> *list)
312 {
313 const char *p = str;
314 char val[32];
315 int i;
316 T zero = 0;
317
318 // リストを解析
319 do
320 {
321 i = 0;
322 // スペースをとばす
323 while(*p == ' ')
324 {
325 p++;
326 }
327 // 数値部分の解析
328 while(*p != ' ' && (*p != '\0'))
329 {
330 val[i++] = *p++;
331 }
332 // 後ろのスペースとカンマもとばす
333 while((*p == ' ') || (*p == ','))
334 {
335 p++;
336 }
337 // 数値の解析とリストへの挿入
338 val[i] = '\0';
339 list->push_back((T)str2val(val , zero));
340 }while(*p != '\0');
341 }
342
343 template <class T , typename Y>
344 void dump_limit_value(T* obj)
345 {
346 if(obj->limit_type == LIMIT_RANGE)
347 {
348 cout << "Min value : " << obj->min << endl;
349 cout << "Max value : " << obj->max << endl;
350 }
351 else if(obj->limit_type == LIMIT_LIST)
352 {
353 // modified by takuya 110823
354 //std::vector<Y>::iterator p;
355 typename std::vector<Y>::iterator p;
356
357 cout << "Limit value : ";
358 for(p = obj->value_list.begin() ; p != obj->value_list.end() ; p++)
359 {
360 cout << *p << ",";
361 }
362 cout << endl;
363 }
364 else
365 {
366 cout << "Limit value : None" << endl;
367 }
368
369 if(obj->default_type == HAVE_DEFAULT)
370 {
371 cout << "Default value : " << obj->default_value << endl;
372 }
373 cout << endl;
374 }
375
376 int id;
377 string name;
378 bool multiple;
379 string default_description;
380 bool assign_auto;
381 DEFAULT_TYPE default_type;
382 };
383
384 // INT型(INT32,INT64)
385 class parameter_int_impl : public object_parameter_impl
386 {
387 friend class object_parameter_impl;
388
389 public:
390 parameter_int_impl(string param_name , string parameter_description)
391 : object_parameter_impl(param_name , parameter_description)
392 {
393 // 制限値のパース
394 boost::regex limit_list("\\[\\s*((\\w)+(\\s*,\\s*\\w+)*)\\s*\\]");
395 boost::regex limit_range("\\[\\s*(\\w+)\\s*\\.\\.\\s*(\\w+)\\s*\\]");
396 string::const_iterator start = parameter_description.begin();
397 string::const_iterator end = parameter_description.end();
398 boost::smatch result;
399
400 // リスト形式かのチェック
401 if(boost::regex_search(start , end , result , limit_list))
402 {
403 limit_type = LIMIT_LIST;
404 string str = result.str(1);
405 split_number_list(str.c_str() , &value_list);
406 }
407 // レンジ形式かのチェック
408 else if(boost::regex_search(start , end , result , limit_range))
409 {
410 limit_type = LIMIT_RANGE;
411 string str;
412
413 // 最小値
414 str = result.str(1);
415 min = str2val(str.c_str() , (int64_t)0);
416 // 最大値
417 str = result.str(2);
418 max = str2val(str.c_str() , (int64_t)0);
419 }
420 else
421 {
422 min = 0;
423 if(parameter_description.find("INT32") != string::npos)
424 {
425 max = INT_MAX;
426 }
427 else
428 {
429 max = INT64_MAX;
430 }
431 limit_type = LIMIT_RANGE;
432 }
433
434 // デフォルト値の算出
435 if(default_type == HAVE_DEFAULT)
436 {
437 default_value = str2val(default_description.c_str() , (int64_t)0);
438 }
439 }
440 virtual void display_implementation(void)
441 {
442 cout << "Parameter types : INT" << endl;
443 object_parameter_impl::display_implementation();
444 dump_limit_value<parameter_int_impl , int64_t>(this);
445 }
446 bool validate_object_parameter(oil_definition::object_parameter_def *obj);
447 void set_parameter_type(oil_definition::object_parameter_def *obj);
448 protected:
449 VALUE_LIMIT_TYPE limit_type;
450 std::vector<int64_t> value_list;
451 int64_t default_value;
452 int64_t max;
453 int64_t min;
454 };
455
456 // UINT型(UINT32,UINT64)
457 class parameter_uint_impl : public object_parameter_impl
458 {
459 friend class object_parameter_impl;
460
461 public:
462 parameter_uint_impl(string param_name , string parameter_description)
463 : object_parameter_impl(param_name , parameter_description)
464 {
465 // 制限値のパース
466 boost::regex limit_list("\\[\\s*((\\w)+(\\s*,\\s*\\w+)*)\\s*\\]");
467 boost::regex limit_range("\\[\\s*(\\w+)\\s*\\.\\.\\s*(\\w+)\\s*\\]");
468 string::const_iterator start = parameter_description.begin();
469 string::const_iterator end = parameter_description.end();
470 boost::smatch result;
471
472 // リスト形式かのチェック
473 if(boost::regex_search(start , end , result , limit_list))
474 {
475 limit_type = LIMIT_LIST;
476 string str = result.str(1);
477 split_number_list(str.c_str() , &value_list);
478 }
479 // レンジ形式かのチェック
480 else if(boost::regex_search(start , end , result , limit_range))
481 {
482 limit_type = LIMIT_RANGE;
483 string str;
484
485 // 最小値
486 str = result.str(1);
487 min = str2val(str.c_str() , (uint64_t)0);
488 // 最大値
489 str = result.str(2);
490 max = str2val(str.c_str() , (uint64_t)0);
491 }
492 else
493 {
494 min = 0;
495 if(parameter_description.find("UINT32") != string::npos)
496 {
497 max = UINT_MAX;
498 }
499 else
500 {
501 max = UINT64_MAX;
502 }
503 limit_type = LIMIT_RANGE;
504 }
505 // デフォルト値の算出
506 if(default_type == HAVE_DEFAULT)
507 {
508 default_value = str2val(default_description.c_str() , (uint64_t)0);
509 }
510 }
511
512 virtual void display_implementation(void)
513 {
514 cout << "Parameter types : UINT" << endl;
515 object_parameter_impl::display_implementation();
516 dump_limit_value<parameter_uint_impl , uint64_t>(this);
517 }
518 bool validate_object_parameter(oil_definition::object_parameter_def *obj);
519 void set_parameter_type(oil_definition::object_parameter_def *obj);
520 protected:
521 VALUE_LIMIT_TYPE limit_type;
522 std::vector<uint64_t> value_list;
523 uint64_t max;
524 uint64_t min;
525 uint64_t default_value;
526 };
527
528 // FLOAT型(FLOAT)
529 class parameter_float_impl : public object_parameter_impl
530 {
531 friend class object_parameter_impl;
532
533 public:
534 parameter_float_impl(string param_name , string parameter_description)
535 : object_parameter_impl(param_name , parameter_description)
536 {
537 // 制限値のパース
538 boost::regex limit_list("\\[\\s*((\\w(\\.\\w+)?)+(\\s*,\\s*\\w+(\\.\\w+)?)*)\\s*\\]");
539 boost::regex limit_range("\\[\\s*(\\w+(\\.\\w+)?)\\s*\\.\\.\\s*(\\w+(\\.\\w+)?)\\s*\\]");
540 string::const_iterator start = parameter_description.begin();
541 string::const_iterator end = parameter_description.end();
542 boost::smatch result;
543
544 // リスト形式かのチェック
545 if(boost::regex_search(start , end , result , limit_list))
546 {
547 limit_type = LIMIT_LIST;
548 string str = result.str(1);
549 split_number_list(str.c_str() , &value_list);
550 }
551 // レンジ形式かのチェック
552 else if(boost::regex_search(start , end , result , limit_range))
553 {
554 limit_type = LIMIT_RANGE;
555 string str;
556
557 // 最小値
558 str = result.str(1);
559 min = str2val(str.c_str() , (double)0);
560 // 最大値
561 str = result.str(3);
562 max = str2val(str.c_str() , (double)0);
563 }
564 else
565 {
566 limit_type = LIMIT_NONE;
567 }
568 // デフォルト値の算出
569 if(default_type == HAVE_DEFAULT)
570 {
571 default_value = str2val(default_description.c_str() , (double)0);
572 }
573 }
574 virtual void display_implementation(void)
575 {
576 cout << "Parameter types : FLOAT" << endl;
577 object_parameter_impl::display_implementation();
578 dump_limit_value<parameter_float_impl , double>(this);
579 }
580 bool validate_object_parameter(oil_definition::object_parameter_def *obj);
581 void set_parameter_type(oil_definition::object_parameter_def *obj);
582 protected:
583 VALUE_LIMIT_TYPE limit_type;
584 std::vector<double> value_list;
585 double max;
586 double min;
587 double default_value;
588 };
589
590 // STRING型
591 class parameter_string_impl : public object_parameter_impl
592 {
593 public:
594 parameter_string_impl(string param_name , string parameter_description)
595 : object_parameter_impl(param_name , parameter_description)
596 {
597 // デフォルト値の代入
598 if(default_type == HAVE_DEFAULT)
599 {
600 default_value = default_description;
601 }
602 }
603
604 virtual void display_implementation(void)
605 {
606 cout << "Parameter types : STRING" << endl;
607 object_parameter_impl::display_implementation();
608 if(default_type == HAVE_DEFAULT)
609 {
610 cout << "Default value : " << default_value << endl;
611 }
612 }
613 virtual bool validate_object_parameter
614 (oil_definition::object_parameter_def *obj);
615 virtual void set_parameter_type(oil_definition::object_parameter_def *obj);
616 protected:
617 string default_value;
618 };
619
620 // BOOLEAN型
621 class parameter_boolean_impl : public parameter_string_impl
622 {
623 public:
624 parameter_boolean_impl(string param_name , string parameter_description)
625 : parameter_string_impl(param_name , parameter_description)
626 {
627 }
628 virtual bool validate_object_parameter
629 (oil_definition::object_parameter_def *obj);
630 virtual void set_parameter_type(oil_definition::object_parameter_def *obj);
631 };
632
633 // ENUM型
634 class parameter_enum_impl : public object_parameter_impl
635 {
636 public:
637 parameter_enum_impl(string param_name , string parameter_description)
638 : object_parameter_impl(param_name , parameter_description)
639 {
640 boost::regex enum_description("(\\w+\\s+[^;]+);(\\s*(.+)\\s)*");
641 boost::regex enum_parse("(\\w+\\s+\\[)?\\s*((\\w+)\\s*(\\{([^\\{\\}]*)\\})?)(\\s*,\\s*(.+))*");
642 boost::regex subparam_name("([^\\[\\]\\s]+)\\s+((WITH_AUTO)?\\s+(\\[.+\\])?)?([^\\[\\]\\s]+)");
643 boost::regex space("\\s*");
644 string::const_iterator start = parameter_description.begin();
645 string::const_iterator end = parameter_description.end();
646 boost::smatch result;
647 toppers::oil::oil_implementation::oil_object_impl *sub_impl;
648 string name;
649
650 // 1個ずつパラメータを切り分けて処理
651 while(boost::regex_search(start , end , result , enum_parse))
652 {
653 string temp = result.str(5);
654 string::const_iterator start2 = temp.begin();
655 string::const_iterator end2 = temp.end();
656 boost::smatch result2;
657
658 try
659 {
660 /// try catch
661 sub_impl = new oil_object_impl(param_name);
662 name = result.str(3);
663 // パラメータの切り分け
664 while(boost::regex_search(start2 , end2 , result2 , enum_description))
665 {
666 string temp2 = result2.str(1);
667 string::const_iterator start3 = temp2.begin();
668 string::const_iterator end3 = temp2.end();
669 boost::smatch result3;
670 // パラメータ名の抽出
671 (void)boost::regex_search(start3 , end3 , result3 , subparam_name);
672
673 sub_impl->add_parameter(result3.str(5) , result2.str(1) + ";");
674 // パラメータがなくなったらループを抜ける
675 if(boost::regex_match(result2.str(3) , space))
676 {
677 break;
678 }
679 temp.erase(0 , temp.find(result2.str(3)));
680 end2 = temp.end();
681 }
682
683 // コンテナへの登録
684 sub_params.insert(pair<string , oil_object_impl*>(name , sub_impl));
685
686 // パラメータがなくなったらループを抜ける
687 if(boost::regex_match(result.str(7) , space))
688 {
689 break;
690 }
691 parameter_description.erase(0 , parameter_description.find(result.str(7)));
692 end = parameter_description.end();
693 }
694 catch( ... )
695 {
696 toppers::fatal("Memory allocation error.");
697 }
698 }
699 }
700
701 ~parameter_enum_impl()
702 {
703 std::map<std::string , oil_object_impl*>::iterator p;
704
705 for(p = sub_params.begin() ; p != sub_params.end() ; p++)
706 {
707 delete (*p).second;
708 }
709 }
710
711 virtual void display_implementation(void)
712 {
713 std::map<std::string , oil_object_impl*>::iterator p;
714
715 cout << "Parameter types : ENUM" << endl;
716 object_parameter_impl::display_implementation();
717
718 cout << "-------------- Sub parameters --------------" << endl;
719 for(p = sub_params.begin() ; p != sub_params.end() ; p++)
720 {
721 cout << "Sub param name : " << (*p).first << endl;
722 (*p).second->display_implementation();
723 }
724 cout << "--------------------------------------------" << endl;
725
726 }
727
728 bool validate_object_parameter(oil_definition::object_parameter_def *obj ,
729 std::vector<toppers::oil::object_ref> *object_refs);
730 void set_parameter_type
731 (oil_definition::object_parameter_def *obj);
732 protected:
733 std::map<std::string , oil_object_impl*> sub_params;
734 };
735
736 // 参照型
737 class parameter_object_ref_impl : public object_parameter_impl
738 {
739 public:
740 parameter_object_ref_impl(string param_name , string parameter_description)
741 : object_parameter_impl(param_name , parameter_description)
742 {
743 // 参照するオブジェクト名の抽出
744 boost::regex ref_type("(\\w+)_TYPE");
745 string::const_iterator start = parameter_description.begin();
746 string::const_iterator end = parameter_description.end();
747 boost::smatch result;
748
749 if(boost::regex_search(start , end , result , ref_type))
750 {
751 reftype_name = result.str(1);
752 }
753 /// デフォルト値の読み込み
754 }
755 virtual void display_implementation(void)
756 {
757 cout << "Parameter types : OBJECT REF" << endl;
758 object_parameter_impl::display_implementation();
759 cout << "Reference Type : " << reftype_name << endl << endl;
760 }
761
762 bool validate_object_parameter
763 (oil_definition::object_parameter_def *obj , std::vector<toppers::oil::object_ref> *object_refs);
764 void set_parameter_type(oil_definition::object_parameter_def *obj);
765 protected:
766 string reftype_name;
767 };
768 }
769
770 namespace oil_definition
771 {
772 // オブジェクト定義のクラス
773 class object_definition
774 {
775 public:
776 object_definition(string object_name_type , object_parameter_def* parent = NULL)
777 {
778 boost::regex object_declaration("(\\w+)\\s+([\\w\\.]+)");
779 string::const_iterator start = object_name_type.begin();
780 string::const_iterator end = object_name_type.end();
781 boost::smatch result;
782
783 // 結び付けられているパラメータへのポインタを格納
784 parent_parameter = parent;
785 // タイプとネームをパースして格納
786 if(boost::regex_match(start , end , result , object_declaration))
787 {
788 object_type = result.str(1);
789 name = result.str(2);
790 }
791 else
792 {
793 /* ありえない */
794 }
795 }
796
797 ~object_definition();
798
799 int add_parameter(string param_name , string param_value);
800
801 string get_object_type(void)
802 {
803 return object_type;
804 }
805
806 std::vector<object_parameter_def*>* get_params(void)
807 {
808 return &params;
809 }
810 void display_definition(void);
811 void set_id(int obj_id)
812 {
813 id = obj_id;
814 }
815 int get_id(void)
816 {
817 return id;
818 }
819 std::string get_name(void)
820 {
821 return name;
822 }
823 object_parameter_def* get_parent(void)
824 {
825 return parent_parameter;
826 }
827
828 private:
829 int id;
830 string name;
831 string object_type;
832 std::vector<object_parameter_def*> params;
833 object_parameter_def* parent_parameter;
834 };
835
836 // オブジェクト定義パラメータの基本クラス
837 class object_parameter_def
838 {
839 public:
840 object_parameter_def(string def_name , string def_value , string parent)
841 {
842 boost::regex have_subparam("(\\w+)\\s*\\{\\s*(([^\\{\\}])+)\\s*\\}");
843 string::const_iterator start = def_value.begin();
844 string::const_iterator end = def_value.end();
845 boost::smatch result;
846
847 name = def_name;
848 parent_name = parent;
849
850 // サブパラメータがある場合はさらに分解
851 if(boost::regex_search(start , end , result , have_subparam))
852 {
853 string type_name;
854 object_definition *subparam;
855
856 value = result.str(1);
857
858 type_name = result.str(1) + " SUBPARAM";
859
860 // サブパラメータを格納するオブジェクトを生成
861 try
862 {
863 subparam = new object_definition(name + " " + name , this);
864 }
865 catch( std::bad_alloc )
866 {
867 toppers::fatal("Memory allocation error.");
868 }
869
870 boost::regex subparam_regex("((\\w+)\\s*=\\s*([\\w\"]+\\s*(\\{\\s*[^\\{\\}]\\s*\\})?));(.+)");
871 boost::regex space("\\s*");
872 string temp = result.str(2);
873 string::const_iterator start2 = temp.begin();
874 string::const_iterator end2 = temp.end();
875 boost::smatch result2;
876
877 while(boost::regex_search(start2 , end2 , result2 , subparam_regex))
878 {
879 subparam->add_parameter(result2.str(2) , result2.str(3));
880
881 // 分解し終わったら抜ける
882 if(boost::regex_match(result2.str(5) , space))
883 {
884 break;
885 }
886 temp.erase(0 , temp.find(result2.str(5)));
887 end2 = temp.end();
888 }
889 // コンテナに追加
890 subparams.push_back(subparam);
891 }
892 else
893 {
894 value = def_value;
895 }
896 }
897
898 ~object_parameter_def()
899 {
900 std::vector<object_definition*>::iterator p;
901
902 for(p = subparams.begin() ; p != subparams.end() ; p++)
903 {
904 delete (*p);
905 }
906 }
907
908 string get_parameter_name(void)
909 {
910 return name;
911 }
912
913 string get_value(void)
914 {
915 return value;
916 }
917
918 string get_parent_name(void)
919 {
920 return parent_name;
921 }
922
923 std::vector<object_definition*>* get_subparams(void)
924 {
925 return &subparams;
926 }
927
928 void display_definition(void)
929 {
930 std::vector<object_definition*>::iterator p;
931
932 cout << "\tName : " << name << " Value : " << value << endl;
933 for(p = subparams.begin(); p != subparams.end(); p++)
934 {
935 cout << "\t";
936 (*p)->display_definition();
937 }
938 }
939 void set_type(PARAMETER_TYPE param_type)
940 {
941 type = param_type;
942 }
943 PARAMETER_TYPE get_type(void)
944 {
945 return type;
946 }
947
948 protected:
949 string name;
950 string value;
951 string parent_name;
952 PARAMETER_TYPE type;
953 std::vector<object_definition*> subparams;
954 };
955
956 typedef std::map< std::string, std::vector< object_definition* > > cfg_obj_map;
957 cfg_obj_map merge(std::vector<object_definition*> *obj_defs ,
958 cfg_obj_map& out , string prefix = string("") ,
959 string suffix = string("") , long fixed_id = UINT_MAX);
960 void assign_id(cfg_obj_map obj_defs , long fixed_id = UINT_MAX);
961
962 } /* oil_definition */
963
964
965 } /* oil */
966}
967
968
969#endif /* OIL_OBJECT_H */
Note: See TracBrowser for help on using the repository browser.