source: XMLIO_V2/dev/dev_rv/src/XMLIO/context.hpp @ 132

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

suite interface cpp<->fortran

File size: 11.3 KB
Line 
1#ifndef __CONTEXT__
2#define __CONTEXT__
3
4
5namespace XMLIOSERVER
6{
7   class Context : public ObjectTemplate<Context>
8   {
9      public:
10
11         Context(void)
12            : ObjectTemplate<Context>(), ccalendar(NULL),
13              fieldDef(NULL), fileDef(NULL), axisDef(NULL), gridDef(NULL), domainDef(NULL)
14         {/* Ne rien faire de plus */}
15
16         Context(const string& _id)
17            : ObjectTemplate<Context>(_id), ccalendar(NULL),
18              fieldDef(NULL), fileDef(NULL), axisDef(NULL), gridDef(NULL), domainDef(NULL)
19         {/* Ne rien faire de plus */}
20
21         FieldDefinition  * getFieldDefinition (void) const { return (this->fieldDef ); }
22         FileDefinition   * getFileDefinition  (void) const { return (this->fileDef  ); }
23         AxisDefinition   * getAxisDefinition  (void) const { return (this->axisDef  ); }
24         GridDefinition   * getGridDefinition  (void) const { return (this->gridDef  ); }
25         DomainDefinition * getDomainDefinition(void) const { return (this->domainDef); }
26
27         AbstractCalendar * getCalendar(void) const { return (this->ccalendar ); }
28
29         AbstractCalendar * setCalendar(const string& _calName, const string& _dateStr)
30         {
31            if (_calName.compare("D360")      == 0)
32               return (ccalendar = new D360Calendar(_dateStr));
33            if (_calName.compare("AllLeap")   == 0)
34               return (ccalendar = new AllLeapCalendar(_dateStr));
35            if (_calName.compare("NoLeap")    == 0)
36               return (ccalendar = new NoLeapCalendar(_dateStr));
37            if (_calName.compare("Julian")    == 0)
38               return (ccalendar = new JulianCalendar(_dateStr));
39            if (_calName.compare("Gregorian") == 0)
40               return (ccalendar = new GregorianCalendar(_dateStr));
41
42            WARNING("L'identifiant "+_calName+" est inconnu, le calendrier grégorien sera choisi par défault pour le contexte "+getId());
43
44            return (ccalendar = new GregorianCalendar(_dateStr));
45         }
46
47         ~Context(void)
48         {
49            // Désallocation dynamique de mémoire pour chacun des groupes de définition si nécessaire.
50            if(fieldDef  != NULL) delete fieldDef  ; if(fileDef  != NULL) delete fileDef ;
51            if(axisDef   != NULL) delete axisDef   ; if(gridDef  != NULL) delete gridDef ;
52            if(domainDef != NULL) delete domainDef ;
53
54            // Désallocation dynamique de mémoire pour le calendrier associé au contexte courant si nécessaire.
55            if(ccalendar !=  NULL) delete ccalendar;
56         }
57
58      public : /* virtual */
59
60         virtual void parse (XMLNode& _node)
61         {
62            THashAttributes attributes;
63
64            /// PARSING POUR GESTION DES ENFANTS
65            if (_node.getElementName().compare(Context::GetName()))
66               WARNING("Le noeud est mal nommé mais sera traité comme un context !");
67
68            if (!(_node.goToChildElement()))
69               WARNING("Le context ne contient pas d'enfant !");
70            else
71            {
72              do { // Parcours des contexts pour traitement.
73
74                  string name = _node.getElementName();
75                  attributes.clear();
76                  _node.getAttributes(attributes);
77
78                  if (attributes.end() != attributes.find("id"))
79                  { WARNING("Le noeud de définition possÚde un identifiant, ce dernier ne sera pas pris en compte lors du traitement !"); }
80
81                  if (name.compare(FieldDefinition::GetDefName())  == 0)
82                  // Parsing pour la définition des champs.
83                  { fieldDef  = CreateInstanceAndParse<FieldDefinition >
84                                 (_node, FieldDefinition::GetDefName().c_str()); continue; }
85
86                  if (name.compare(FileDefinition::GetDefName())  == 0)
87                  // Parsing pour la définition des fichiers.
88                  { fileDef   = CreateInstanceAndParse<FileDefinition  >
89                                 (_node, FileDefinition  ::GetDefName().c_str()); continue; }
90
91                  if (name.compare(AxisDefinition::GetDefName())  == 0)
92                  // Parsing pour la définition des axes.
93                  { axisDef   = CreateInstanceAndParse<AxisDefinition  >
94                                 (_node, AxisDefinition  ::GetDefName().c_str()); continue; }
95
96                  if (name.compare(GridDefinition::GetDefName())  == 0)
97                  // Parsing pour la définition des grilles.
98                  { gridDef   = CreateInstanceAndParse<GridDefinition  >
99                                 (_node, GridDefinition  ::GetDefName().c_str()); continue; }
100
101                  if (name.compare(DomainDefinition::GetDefName()) == 0)
102                  // Parsing pour la définition des domaines.
103                  { domainDef = CreateInstanceAndParse<DomainDefinition>
104                                 (_node, DomainDefinition::GetDefName().c_str()); continue; }
105
106                  WARNING("La définition est invalide, seuls les champs, grilles, axes et fichiers peuvent être définis !");
107
108               } while (_node.goToNextElement());
109
110               _node.goToParentElement(); // Retour au parent
111            }
112         }
113
114         virtual bool hasChild(void) const
115         { return ((fieldDef != NULL) or (fileDef != NULL)  or (axisDef != NULL) or (gridDef != NULL) or (domainDef != NULL)); }
116
117         virtual void printChild(ostream& out) const
118         {
119            if(fieldDef  != NULL) out << *(FieldGroup*)       fieldDef  << std::endl;
120            if(fileDef   != NULL) out << *(FileGroup*)        fileDef   << std::endl;
121            if(axisDef   != NULL) out << *(AxisDefinition*)   axisDef   << std::endl;
122            if(gridDef   != NULL) out << *(GridDefinition*)   gridDef   << std::endl;
123            if(domainDef != NULL) out << *(DomainDefinition*) domainDef << std::endl;
124         }
125
126         virtual void solveDescInheritance(const AttributRegistrar* const _parent = 0)
127         {
128            if (_parent != 0) return;
129            // Résolution des héritages descendants pour chacun des groupes de définitions.
130            if(fieldDef  != NULL) fieldDef ->solveDescInheritance();
131            if(fileDef   != NULL) fileDef  ->solveDescInheritance();
132            if(axisDef   != NULL) axisDef  ->solveDescInheritance();
133            if(gridDef   != NULL) gridDef  ->solveDescInheritance();
134            if(domainDef != NULL) domainDef->solveDescInheritance();
135         }
136
137      public : /* static */
138
139         static string GetRootName(void) { return ("simulation"); }
140         static string GetName(void)     { return ("context"); }
141         static string GetDefName(void)  { return (Context::GetName()); }
142
143         static void ShowTree(ostream& os = std::clog)
144         {
145            os << NIndent << "<?xml version=\"1.0\"?>" << std::endl;
146            os << NIndent << "<" << Context::GetRootName() << ">" << std::endl;
147
148            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
149            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
150
151            for (it = AllListContext.begin(); it != AllListContext.end(); it++)
152               // On sort chacun des contextes successivement.
153            {
154               Context::SetCurrentContext((*it).first);
155               os << NIndent << std::endl;
156               os << *((*it).second)[(*it).first] << std::endl;
157            }
158
159            os << NIndent << std::endl;
160            os << NIndent << "</" << Context::GetRootName() << ">" << std::endl  ;
161         }
162
163         static void FreeMemory(void)
164         {
165            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
166            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
167
168            for (it = AllListContext.begin(); it != AllListContext.end(); it++)
169            { Context::SetCurrentContext((*it).first); delete ((*it).second)[(*it).first]; }
170         }
171
172         // Ne plus utiliser, disponible dans les classe treatment.
173         static void SolveInheritance(void)
174         {
175            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
176            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
177
178            for ( it = AllListContext.begin(); it != AllListContext.end(); it++)
179            {
180               // Résolution des héritages descendants (càd des héritages de groupes) pour chacun des contextes.
181               Context::SetCurrentContext((*it).first);
182               ((*it).second)[(*it).first]->solveDescInheritance();
183
184               // Résolution des héritages par référence au niveau des fichiers.
185               const std::vector<CFile*>& allFiles =
186                                 CFile::GetCurrentListObject().getVector();
187
188               for (unsigned int i = 0; i < allFiles.size(); i++)
189                  allFiles[i]->solveFieldRefInheritance();
190            }
191         }
192
193         static bool HasContext(const string& id)
194         {
195            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
196            return (AllListContext.find(id) != AllListContext.end());
197         }
198
199         static Context* GetContext(const string& id)
200         { return (Context::GetAllListObject()[id][id]); }
201
202         static Context* SwapContext(void)
203         {
204            if (Stid.size() == 0)
205            {
206               WARNING("SwapContext impossible car le pile des contextes est vides !");
207               return (NULL);
208            }
209            string lastId = Stid.top (); Stid.pop ();
210            return (Context::GetObject(lastId));
211         }
212
213         static const std::stack<string>& GetStackContextId(void)
214         { return (Context::Stid); }
215
216         static Context* GetCurrentContext(void)
217         { return (Context::GetObject(Context::GetCurrentContextId())); }
218
219         static void SetCurrentContext(const string& id, bool withSwap = false, bool withcheck = true)
220         {
221            if (withSwap) Stid.push (Context::GetCurrentContextId());
222
223            if (!Context::HasContext(id) && withcheck)
224               throw new XMLIOUndefinedValueException
225                     ("Impossible de se placer dans le contexte "+id+" car celui-ci n'existe pas dans l'arborescence!");
226
227            // On modifie le context courrant pour tout les ObjectTemplate
228            Context::SetContext(id);
229
230            // Changement de context pour les champs et groupes de champs.
231            FieldGroup::SetContext(id);  CField::SetContext(id);
232
233            // Changement de context pour les fichiers et groupes de fichiers.
234            FileGroup::SetContext(id);   CFile::SetContext(id);
235
236            // Changement de context pour les grilles et groupes de grilles.
237            GridGroup::SetContext(id);   CGrid::SetContext(id);
238
239            // Changement de context pour les axes et groupes d'axes.
240            AxisGroup::SetContext(id);   CAxis::SetContext(id);
241
242            // Changement de context pour les domaines et groupes de domaines.
243            DomainGroup::SetContext(id); CDomain::SetContext(id);
244         }
245
246      private:
247
248         static std::stack<string> Stid;
249
250         AbstractCalendar * ccalendar;
251
252         FieldDefinition  * fieldDef;
253         FileDefinition   * fileDef;
254         AxisDefinition   * axisDef;
255         GridDefinition   * gridDef;
256         DomainDefinition * domainDef;
257
258   }; //class Context
259
260   std::stack<string> Context::Stid;
261
262}// namespace XMLIOSERVER
263
264#endif  // __CONTEXT__
Note: See TracBrowser for help on using the repository browser.