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

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