1 | #ifndef GROUP_TEMPLATE_HPP |
---|
2 | #define GROUP_TEMPLATE_HPP |
---|
3 | |
---|
4 | #include "object_template.hpp" |
---|
5 | #include "xmlio_std.hpp" |
---|
6 | |
---|
7 | template<typename CObject> |
---|
8 | class group : public CObjectTemplate<group<CObject> > |
---|
9 | { |
---|
10 | public : |
---|
11 | |
---|
12 | CObject * defaultAttribut ; |
---|
13 | |
---|
14 | unordered_map<string,CObject *>* childListId ; |
---|
15 | vector<CObject *>* childList ; |
---|
16 | unordered_map<string,group<CObject> *>* groupListId ; |
---|
17 | vector<group<CObject> *>* groupList ; |
---|
18 | |
---|
19 | group(void) : CObjectTemplate< group<CObject> >() |
---|
20 | { |
---|
21 | defaultAttribut=new CObject ; |
---|
22 | childListId=new unordered_map<string,CObject *> ; |
---|
23 | childList=new vector<CObject *> ; |
---|
24 | groupListId=new unordered_map<string,group<CObject> *> ; |
---|
25 | groupList=new vector<group<CObject> *> ; |
---|
26 | } |
---|
27 | |
---|
28 | group(const string Id) : CObjectTemplate< group<CObject> >(Id) |
---|
29 | { |
---|
30 | defaultAttribut=new CObject ; |
---|
31 | childListId=new unordered_map<string,CObject *> ; |
---|
32 | childList=new vector<CObject *> ; |
---|
33 | groupListId=new unordered_map<string,group<CObject> *> ; |
---|
34 | groupList=new vector<group<CObject>*> ; |
---|
35 | } |
---|
36 | |
---|
37 | CObject * getDefaultAttribut(void) {return defaultAttribut ;} |
---|
38 | |
---|
39 | bool foundChild(const string & Id) const |
---|
40 | { |
---|
41 | if (childListId->find(Id)==childListId->end()) return false ; |
---|
42 | else return true ; |
---|
43 | } |
---|
44 | |
---|
45 | CObject * getChild(const string & Id) |
---|
46 | { |
---|
47 | typename unordered_map<string,CObject*>::iterator It ; |
---|
48 | |
---|
49 | It=childListId->find(Id) ; |
---|
50 | if (It==childListId->end()) |
---|
51 | { |
---|
52 | error("group<object>::getChild(const string & Id)")<<"Child <<"<<Id<<">> does not exist"<<endl ; |
---|
53 | return 0 ; |
---|
54 | } |
---|
55 | else return It->second ; |
---|
56 | } |
---|
57 | |
---|
58 | CObject * getNewChild(void) |
---|
59 | { |
---|
60 | CObject * newChild=new CObject ; |
---|
61 | childList->push_back(newChild) ; |
---|
62 | return newChild ; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | CObject* getNewChild(const string & Id) |
---|
67 | { |
---|
68 | CObject* newChild=new CObject(Id) ; |
---|
69 | |
---|
70 | pair<string,CObject *> value(Id,newChild) ; |
---|
71 | if (childListId->insert(value).second) |
---|
72 | { |
---|
73 | childList->push_back(newChild) ; |
---|
74 | return newChild ; |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | error("group<object>::getNewChild(const string & Id)")<<"Child <<"<<Id<<">> is already present"<<endl ; |
---|
79 | return 0 ; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | CObject * getOrAppendChild(const string & Id) |
---|
85 | { |
---|
86 | if (foundChild(Id)) return getChild(Id) ; |
---|
87 | else return getNewChild(Id) ; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | bool foundGroup(const string & Id) const |
---|
92 | { |
---|
93 | if (groupListId->find(Id)==groupListId->end()) return false ; |
---|
94 | else return true ; |
---|
95 | } |
---|
96 | |
---|
97 | group<CObject> * getGroup(const string & Id) |
---|
98 | { |
---|
99 | typename unordered_map<string,group<CObject>*>::iterator It ; |
---|
100 | |
---|
101 | It=groupListId->find(Id) ; |
---|
102 | if (It==groupListId->end()) |
---|
103 | { |
---|
104 | error("group<object>::getGroup(const string & Id)")<<"Group <<"<<Id<<">> does not exist"<<endl ; |
---|
105 | return 0 ; |
---|
106 | } |
---|
107 | else return It->second ; |
---|
108 | } |
---|
109 | |
---|
110 | group<CObject>* getNewGroup(void) |
---|
111 | { |
---|
112 | group<CObject> * newGroup=new group<CObject> ; |
---|
113 | groupList->push_back(newGroup) ; |
---|
114 | return newGroup ; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | group<CObject>* getNewGroup(const string & Id) |
---|
119 | { |
---|
120 | group<CObject>* newGroup=new group<CObject>(Id) ; |
---|
121 | |
---|
122 | pair<string,group<CObject>*> value(Id,newGroup) ; |
---|
123 | if (groupListId->insert(value).second) |
---|
124 | { |
---|
125 | groupList->push_back(newGroup) ; |
---|
126 | return newGroup ; |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | error("group<object>::getNewGroup(const string & Id)")<<"Group <<"<<Id<<">> is already present"<<endl ; |
---|
131 | return 0 ; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | group<CObject> * getOrAppendGroup(const string & Id) |
---|
137 | { |
---|
138 | if (foundGroup(Id)) return getGroup(Id) ; |
---|
139 | else return getNewGroup(Id) ; |
---|
140 | } |
---|
141 | |
---|
142 | static const char* getName(void) |
---|
143 | { |
---|
144 | return (string("group_")+string(CObject::getName())).c_str() ; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | ostream& Print(ostream& o) |
---|
149 | { |
---|
150 | typename vector<CObject *>::iterator it1 ; |
---|
151 | typename vector<group<CObject> *>::iterator it2 ; |
---|
152 | |
---|
153 | o<<"Object :"<<getName()<<" : " ; |
---|
154 | this->PrintId(o)<<inc_endl ; |
---|
155 | this->PrintAttribut(o)<<iendl ; |
---|
156 | o<<"Default Attribut"<<inc_endl ; |
---|
157 | defaultAttribut->Print(o)<<dec_endl ; |
---|
158 | o<<"Group "<<getName()<<IncIndent ; |
---|
159 | for(it2=groupList->begin();it2!=groupList->end();it2++) |
---|
160 | { |
---|
161 | o<<iendl ; |
---|
162 | (*it2)->Print(o) ; |
---|
163 | } |
---|
164 | o<<DecIndent<<iendl ; |
---|
165 | o<<"Child "<<CObject::getName()<<IncIndent ; |
---|
166 | for(it1=childList->begin();it1!=childList->end();it1++) |
---|
167 | { |
---|
168 | o<<iendl; |
---|
169 | (*it1)->Print(o) ; |
---|
170 | } |
---|
171 | o<<DecIndent<<DecIndent ; |
---|
172 | return o ; |
---|
173 | } |
---|
174 | |
---|
175 | void Print(void) |
---|
176 | { |
---|
177 | Print(StdOut) ; |
---|
178 | } |
---|
179 | |
---|
180 | } ; |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | #endif |
---|