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

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