source: XMLIO_V2/dev/dev_rv/src/XMLIO/group_template.hpp @ 126

Last change on this file since 126 was 126, checked in by hozdoba, 14 years ago

Amélioration de quelques portions de code.
Ajout de contructeurs par copie.

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