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

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

Début de gestion des opérations sur les champs + quelques modifications

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