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

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

Mise à jour intermédiaire ...
A venir : commit d'une version stable intégrant l'écriture de fichiers NetCDF4.
(en cours de finalisation actuellement)

File size: 7.9 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) : 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
44            for(unsigned int i = 0; i < groupvect.size() ; i++)
45            // on complÚte les attributs des groupes enfants.
46               groupvect[i] -> resolveDescInheritance(this);
47         }
48
49         GroupTemplate<T, U>& createGroup(const string& _id) throw (XMLIOUndefinedValueException)
50         {
51            GroupTemplate<T, U> &obj = *GroupTemplate<T, U>::CreateObject(_id);
52            groupList.addObject(&obj);
53
54            return (obj);
55         }
56
57         GroupTemplate<T, U>& createGroup(void)
58         {
59            GroupTemplate<T, U>& obj = *(GroupTemplate<T, U>::CreateObject());
60            groupList.addObject(&obj);
61
62            return (obj);
63         }
64
65         GroupTemplate<T, U>& getGroup(const string& _id) throw (XMLIOUndefinedValueException) { return (*groupList[_id]); }
66         bool hasGroup(const string _id) { return (groupList.hasMappedValue(_id)); }
67
68         const StrHashMap<GroupTemplate<T, U>* >& getGroupList(void) const { return (groupList); }
69
70         size_t getNbGroup() const {return (groupList.getVectorSize()); }
71
72         /// Pour les objets enfants ///
73
74         T& createChild(const string& _id) throw (XMLIOUndefinedValueException)
75         {
76            T& obj = *ObjectTemplate<T>::CreateObject(_id);
77            childList.addObject(&obj);
78            return (obj);
79         }
80
81         T& createChild(void)
82         {
83            T& obj = *ObjectTemplate<T>::CreateObject();
84            childList.addObject(&obj);
85            return (obj);
86         }
87
88         T& getChild(const string& _id) throw (XMLIOUndefinedValueException) { return (*childList[_id]); }
89         bool hasChild(const string& _id) { return (childList.hasMappedValue(_id)); }
90
91         const StrHashMap<T*>& getCurrentListChild(void) const { return (childList); }
92         const vector<T*>& getCurrentVectorChild(void) const { return (childList.getVector()); }
93
94         size_t getNbChild() const {return (childList.getVectorSize()); }
95
96         void getAllChildren(std::vector<T*>& _allc )
97         {
98            const vector<GroupTemplate<T, U>*>& groupvect = groupList.getVector();
99            _allc.insert (_allc.end(), getCurrentVectorChild().begin(), getCurrentVectorChild().end());
100
101            for(unsigned int i = 0; i < groupvect.size() ; i++)
102               groupvect[i] -> getAllChildren(_allc);
103         }
104
105         virtual ~GroupTemplate()
106         {
107            for (unsigned int i = 0; i < childList.getVector().size(); i++)
108               delete childList.getVector()[i];
109            for (unsigned int i = 0; i < groupList.getVector().size(); i++)
110               delete groupList.getVector()[i];
111         }
112
113      protected:
114
115
116         template <class W> void _parse (XMLNode& _node, bool _withAttr = true)
117         {
118            string name = _node.getElementName();
119            THashAttributes attributes;
120
121            /// PARSING GESTION DES ATTRIBUTS ///
122            if (_withAttr)
123            {
124               _node.getAttributes(attributes);
125               this->setAttributes(attributes);
126
127               if (attributes.end() != attributes.find("src"))
128               { // Si une demande d'inclusion de fichier est trouvée.
129                  XMLNode _node_inc = ObjectTemplate<GroupTemplate<T, U> >::getNodeIncludedFile(attributes["src"], name);
130                  _parse<W> (_node_inc);
131               }
132               attributes.clear();
133            }
134
135            /// PARSING POUR GESTION DES ENFANTS ///
136            if (!(_node.goToChildElement()))
137               WARNING("L'objet de type \""+W::GetName()+"\" ne contient pas d'enfant !");
138            else
139            {
140               do { // Parcours pour traitement.
141
142                  string name = _node.getElementName();
143                  attributes.clear(); _node.getAttributes(attributes);
144
145                  if (name.compare(W::GetName()) == 0) { createGroupAndParse<W>(attributes, _node); continue; }
146                  if (name.compare(T::GetName()) == 0) { createChildAndParse<T>(attributes, _node); continue; }
147
148                  WARNING("Un objet de type \""+W::GetName()+"\" ne peut contenir qu'un object de type \""+T::GetName()+"\" ou \""+W::GetName()+"\" !");
149
150               } while (_node.goToNextElement());
151               _node.goToParentElement(); // Retour au parent
152            }
153         }
154
155         template <class V> V* createGroupAndParse(THashAttributes& attributes, XMLNode& _node)
156         {
157            V* instance_ptr = NULL;
158            if (attributes.end() != attributes.find("id"))
159            {// Si l'identifiant est défini.
160               if (V::HasObject(attributes["id"]))
161                  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
162               instance_ptr = (V*)(&createGroup(attributes["id"]));
163               instance_ptr->parse(_node);
164            }
165            else
166            {// Si l'identifiant n'est pas défini.
167               instance_ptr = (V*)(&createGroup());
168               instance_ptr->parse(_node);
169            }
170            return (instance_ptr);
171         }
172
173         template <class V> V* createChildAndParse(THashAttributes& attributes, XMLNode& _node)
174         {
175            V* instance_ptr = NULL;
176            if (attributes.end() != attributes.find("id"))
177            {// Si l'identifiant est défini.
178               if (V::HasObject(attributes["id"]))
179                  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
180               instance_ptr = (V*)(&createChild(attributes["id"]));
181               instance_ptr->parse(_node);
182            }
183            else
184            {// Si l'identifiant n'est pas défini.
185               instance_ptr = (V*)(&createChild());
186               instance_ptr->parse(_node);
187            }
188            return (instance_ptr);
189         }
190
191      private :
192
193         StrHashMap<T> childList;
194         StrHashMap<GroupTemplate<T, U> > groupList;
195
196   }; // class GroupTemplate
197
198}// namespace XMLIOSERVER
199
200
201#endif // __XMLIO_GROUP_TEMPLATE__
Note: See TracBrowser for help on using the repository browser.