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

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

Mise à jour

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