1 | #ifndef __XMLIO_GROUP_TEMPLATE__ |
---|
2 | #define __XMLIO_GROUP_TEMPLATE__ |
---|
3 | |
---|
4 | namespace XMLIOSERVER |
---|
5 | { |
---|
6 | template <class T, class U> |
---|
7 | class GroupTemplate : public ObjectTemplate<GroupTemplate<T, U> >, public U |
---|
8 | { |
---|
9 | public: |
---|
10 | |
---|
11 | GroupTemplate(void) : ObjectTemplate<GroupTemplate<T, U> >(), U(), childList(), groupList() |
---|
12 | {/* Ne rien faire de plus */} |
---|
13 | GroupTemplate(const string& _id) : ObjectTemplate<GroupTemplate<T, U> >(_id), U(), childList(), groupList() |
---|
14 | {/* Ne rien faire de plus */} |
---|
15 | |
---|
16 | /// Pour les groupes d'objets enfants /// |
---|
17 | |
---|
18 | static string GetName(void) {return (T::GetName().append("_group")); } |
---|
19 | static string GetDefName(void) {return (T::GetName().append("_definition")); } |
---|
20 | |
---|
21 | virtual bool hasChild(void) const { return ((getNbGroup() + getNbChild())>0); } |
---|
22 | virtual void printChild(ostream& out) const |
---|
23 | { |
---|
24 | // Ecriture des sous-groupes. |
---|
25 | for(unsigned int i = 0; i < groupList.getVector().size() ; i++) |
---|
26 | out << *(groupList.getVector()[i]) << std::endl; |
---|
27 | // Ecriture des enfants. |
---|
28 | for(unsigned int i = 0; i < childList.getVector().size() ; i++) |
---|
29 | out << *(childList.getVector()[i]) << std::endl; |
---|
30 | } |
---|
31 | |
---|
32 | virtual void resolveDescInheritance(const AttributRegistrar* _parent = 0) |
---|
33 | { |
---|
34 | const vector<T*>& childvect = childList.getVector(); |
---|
35 | const vector<GroupTemplate<T, U>*>& groupvect = groupList.getVector(); |
---|
36 | |
---|
37 | // On complÚte les propres attributs du groupe. |
---|
38 | if (_parent!= NULL) AttributRegistrar::addAttributes(*_parent); |
---|
39 | |
---|
40 | for(unsigned int i = 0; i < childvect.size() ; i++) |
---|
41 | // on complÚte les attributs des enfants. |
---|
42 | childvect[i] -> resolveDescInheritance(this); |
---|
43 | for(unsigned int i = 0; i < groupvect.size() ; i++) |
---|
44 | // on complÚte les attributs des groupes enfants. |
---|
45 | groupvect[i] -> resolveDescInheritance(this); |
---|
46 | } |
---|
47 | |
---|
48 | GroupTemplate<T, U>& createGroup(const string& _id) throw (XMLIOUndefinedValueException) |
---|
49 | { |
---|
50 | GroupTemplate<T, U> &obj = GroupTemplate<T, U>::CreateObject(_id); |
---|
51 | groupList.addObject(&obj); |
---|
52 | |
---|
53 | return (obj); |
---|
54 | } |
---|
55 | |
---|
56 | GroupTemplate<T, U>& createGroup(void) |
---|
57 | { |
---|
58 | GroupTemplate<T, U>& obj = GroupTemplate<T, U>::CreateObject(); |
---|
59 | groupList.addObject(&obj); |
---|
60 | |
---|
61 | return (obj); |
---|
62 | } |
---|
63 | |
---|
64 | GroupTemplate<T, U>& getGroup(const string& _id) throw (XMLIOUndefinedValueException) { return (*groupList[_id]); } |
---|
65 | bool hasGroup(const string _id) { return (groupList.hasMappedValue(_id)); } |
---|
66 | |
---|
67 | const StrHashMap<GroupTemplate<T, U>* >& getGroupList(void) const { return (groupList); } |
---|
68 | |
---|
69 | size_t getNbGroup() const {return (groupList.getVectorSize()); } |
---|
70 | |
---|
71 | /// Pour les objets enfants /// |
---|
72 | |
---|
73 | T& createChild(const string& _id) throw (XMLIOUndefinedValueException) |
---|
74 | { |
---|
75 | T& obj = ObjectTemplate<T>::CreateObject(_id); |
---|
76 | childList.addObject(&obj); |
---|
77 | return (obj); |
---|
78 | } |
---|
79 | |
---|
80 | T& createChild(void) |
---|
81 | { |
---|
82 | T& obj = ObjectTemplate<T>::CreateObject(); |
---|
83 | childList.addObject(&obj); |
---|
84 | return (obj); |
---|
85 | } |
---|
86 | |
---|
87 | T& getChild(const string& _id) throw (XMLIOUndefinedValueException) { return (*childList[_id]); } |
---|
88 | bool hasChild(const string& _id) { return (childList.hasMappedValue(_id)); } |
---|
89 | |
---|
90 | const StrHashMap<T*>& getCurrentListChild(void) const { return (childList); } |
---|
91 | const vector<T*>& getCurrentVectorChild(void) const { return (childList.getVector()); } |
---|
92 | |
---|
93 | size_t getNbChild() const {return (childList.getVectorSize()); } |
---|
94 | |
---|
95 | void getAllChildren(std::vector<T*>& _allc ) |
---|
96 | { |
---|
97 | const vector<GroupTemplate<T, U>*>& groupvect = groupList.getVector(); |
---|
98 | _allc.insert (_allc.end(), getCurrentVectorChild().begin(), getCurrentVectorChild().end()); |
---|
99 | |
---|
100 | for(unsigned int i = 0; i < groupvect.size() ; i++) |
---|
101 | groupvect[i] -> getAllChildren(_allc); |
---|
102 | } |
---|
103 | |
---|
104 | virtual ~GroupTemplate() |
---|
105 | { |
---|
106 | for (unsigned int i = 0; i < childList.getVector().size(); i++) |
---|
107 | delete childList.getVector()[i]; |
---|
108 | for (unsigned int i = 0; i < groupList.getVector().size(); i++) |
---|
109 | delete groupList.getVector()[i]; |
---|
110 | } |
---|
111 | |
---|
112 | protected: |
---|
113 | |
---|
114 | |
---|
115 | template <class W> void _parse (XMLNode& _node, bool _withAttr = true) |
---|
116 | { |
---|
117 | string name = _node.getElementName(); |
---|
118 | THashAttributes attributes; |
---|
119 | |
---|
120 | /// PARSING GESTION DES ATTRIBUTS /// |
---|
121 | if (_withAttr) |
---|
122 | { |
---|
123 | _node.getAttributes(attributes); |
---|
124 | this->setAttributes(attributes); |
---|
125 | |
---|
126 | if (attributes.end() != attributes.find("src")) |
---|
127 | { // Si une demande d'inclusion de fichier est trouvée. |
---|
128 | XMLNode _node_inc = ObjectTemplate<GroupTemplate<T, U> >::getNodeIncludedFile(attributes["src"], name); |
---|
129 | _parse<W> (_node_inc); |
---|
130 | } |
---|
131 | attributes.clear(); |
---|
132 | } |
---|
133 | |
---|
134 | /// PARSING POUR GESTION DES ENFANTS /// |
---|
135 | if (!(_node.goToChildElement())) |
---|
136 | WARNING("L'objet de type \""+W::GetName()+"\" ne contient pas d'enfant !"); |
---|
137 | else |
---|
138 | { |
---|
139 | do { // Parcours pour traitement. |
---|
140 | |
---|
141 | string name = _node.getElementName(); |
---|
142 | attributes.clear(); _node.getAttributes(attributes); |
---|
143 | |
---|
144 | if (name.compare(W::GetName()) == 0) { createGroupAndParse<W>(attributes, _node); continue; } |
---|
145 | if (name.compare(T::GetName()) == 0) { createChildAndParse<T>(attributes, _node); continue; } |
---|
146 | |
---|
147 | WARNING("Un objet de type \""+W::GetName()+"\" ne peut contenir qu'un object de type \""+T::GetName()+"\" ou \""+W::GetName()+"\" !"); |
---|
148 | |
---|
149 | } while (_node.goToNextElement()); |
---|
150 | _node.goToParentElement(); // Retour au parent |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | template <class V> V* createGroupAndParse(THashAttributes& attributes, XMLNode& _node) |
---|
155 | { |
---|
156 | V* instance_ptr = NULL; |
---|
157 | if (attributes.end() != attributes.find("id")) |
---|
158 | {// Si l'identifiant est défini. |
---|
159 | if (V::HasObject(attributes["id"])) |
---|
160 | WARNING("Dans le context actuel, une instance de type "+V::GetName()+" nommée \""+attributes["id"]+"\" existe déjà , le second fera référence au premier par défaut !"); // TODO TODO |
---|
161 | instance_ptr = (V*)(&createGroup(attributes["id"])); |
---|
162 | instance_ptr->parse(_node); |
---|
163 | } |
---|
164 | else |
---|
165 | {// Si l'identifiant n'est pas défini. |
---|
166 | instance_ptr = (V*)(&createGroup()); |
---|
167 | instance_ptr->parse(_node); |
---|
168 | } |
---|
169 | return (instance_ptr); |
---|
170 | } |
---|
171 | |
---|
172 | template <class V> V* createChildAndParse(THashAttributes& attributes, XMLNode& _node) |
---|
173 | { |
---|
174 | V* instance_ptr = NULL; |
---|
175 | if (attributes.end() != attributes.find("id")) |
---|
176 | {// Si l'identifiant est défini. |
---|
177 | if (V::HasObject(attributes["id"])) |
---|
178 | WARNING("Dans le context actuel, une instance de type "+V::GetName()+" nommée \""+attributes["id"]+"\" existe déjà , le second fera référence au premier par défaut !"); // TODO TODO |
---|
179 | instance_ptr = (V*)(&createChild(attributes["id"])); |
---|
180 | instance_ptr->parse(_node); |
---|
181 | } |
---|
182 | else |
---|
183 | {// Si l'identifiant n'est pas défini. |
---|
184 | instance_ptr = (V*)(&createChild()); |
---|
185 | instance_ptr->parse(_node); |
---|
186 | } |
---|
187 | return (instance_ptr); |
---|
188 | } |
---|
189 | |
---|
190 | private : |
---|
191 | |
---|
192 | StrHashMap<T> childList; |
---|
193 | StrHashMap<GroupTemplate<T, U> > groupList; |
---|
194 | |
---|
195 | }; // class GroupTemplate |
---|
196 | |
---|
197 | }// namespace XMLIOSERVER |
---|
198 | |
---|
199 | |
---|
200 | #endif // __XMLIO_GROUP_TEMPLATE__ |
---|