source: XMLIO_V2/dev/dev_rv/src/XMLIO/context.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: 8.5 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)
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)
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
73         virtual void parse (XMLNode& _node)
74         {
75            THashAttributes attributes;
76
77            /// PARSING POUR GESTION DES ENFANTS
78            if (_node.getElementName().compare(Context::GetName()))
79               WARNING("Le noeud est mal nommé mais sera traité comme un context !");
80
81            if (!(_node.goToChildElement()))
82               WARNING("Le context ne contient pas d'enfant !");
83            else
84            {
85              do { // Parcours des contexts pour traitement.
86
87                  string name = _node.getElementName();
88                  attributes.clear();
89                  _node.getAttributes(attributes);
90
91                  if (attributes.end() != attributes.find("id"))
92                  { WARNING("Le noeud de définition possÚde un identifiant, ce dernier ne sera pas pris en compte lors du traitement !"); }
93
94                  if (name.compare(FieldDefinition::GetDefName()) == 0) // Parsing pour la définition des champs.
95                  { fieldDef = CreateInstanceAndParse<FieldDefinition>(_node, FieldDefinition::GetDefName().c_str()); continue; }
96
97                  if (name.compare(FileDefinition::GetDefName()) == 0) // Parsing pour la définition des fichiers.
98                  { fileDef  = CreateInstanceAndParse<FileDefinition >(_node, FileDefinition ::GetDefName().c_str()); continue; }
99
100                  if (name.compare(AxisDefinition::GetDefName()) == 0) // Parsing pour la définition des axes.
101                  { axisDef  = CreateInstanceAndParse<AxisDefinition >(_node, AxisDefinition ::GetDefName().c_str()); continue; }
102
103                  if (name.compare(GridDefinition::GetDefName()) == 0) // Parsing pour la définition des grilles.
104                  { gridDef  = CreateInstanceAndParse<GridDefinition >(_node, GridDefinition ::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         static string GetName(void)     { return ("context"); }
115         static string GetRootName(void) { return ("simulation"); }
116
117         virtual bool hasChild(void) const
118         { return ((fieldDef != NULL) or (fileDef != NULL)  or (axisDef != NULL) or (gridDef != NULL)); }
119
120         virtual void printChild(ostream& out) const
121         {
122            if(fieldDef != NULL) out << *(FieldGroup*)     fieldDef << std::endl;
123            if(fileDef  != NULL) out << *(FileGroup*)      fileDef  << std::endl;
124            if(axisDef  != NULL) out << *(AxisDefinition*) axisDef  << std::endl;
125            if(gridDef  != NULL) out << *(GridDefinition*) gridDef  << std::endl;
126         }
127
128         virtual void resolveDescInheritance(const AttributRegistrar* const _parent = 0)
129         {
130            if (_parent != 0) return;
131            // Résolution des héritages descendants pour chacun des groupes de définitions.
132            if(fieldDef != NULL) fieldDef->resolveDescInheritance();
133            if(fileDef  != NULL) fileDef ->resolveDescInheritance();
134            if(axisDef  != NULL) axisDef ->resolveDescInheritance();
135            if(gridDef  != NULL) gridDef ->resolveDescInheritance();
136         }
137
138         FieldDefinition* getFieldDefinition(void) const { return (this->fieldDef); }
139         FileDefinition * getFileDefinition(void)  const { return (this->fileDef ); }
140         AxisDefinition * getAxisDefinition(void)  const { return (this->axisDef ); }
141         GridDefinition * getGridDefinition(void)  const { return (this->gridDef ); }
142
143         AbstractCalendar * getCalendar(void) const { return (this->ccalendar ); }
144         AbstractCalendar * setCalendar(const string& _calName, const string& _dateStr)
145         {
146            if (_calName.compare("D360")      == 0)
147               return (ccalendar = new D360Calendar(_dateStr));
148            if (_calName.compare("AllLeap")   == 0)
149               return (ccalendar = new AllLeapCalendar(_dateStr));
150            if (_calName.compare("NoLeap")    == 0)
151               return (ccalendar = new NoLeapCalendar(_dateStr));
152            if (_calName.compare("Julian")    == 0)
153               return (ccalendar = new JulianCalendar(_dateStr));
154            if (_calName.compare("Gregorian") == 0)
155               return (ccalendar = new GregorianCalendar(_dateStr));
156
157            WARNING("L'identifiant "+_calName+" est inconnu, le calendrier grégorien sera choisi par défault pour le contexte "+getId());
158
159            return (ccalendar = new GregorianCalendar(_dateStr));
160         }
161
162         ~Context()
163         {
164            // Désallocation dynamique de mémoire pour chacun des groupes de définition si nécessaire.
165            if(fieldDef != NULL) delete fieldDef; if(fileDef  != NULL) delete fileDef ;
166            if(axisDef  != NULL) delete axisDef ; if(gridDef  != NULL) delete gridDef ;
167
168            // Désallocation dynamique de mémoire pour le calendrier associé au contexte courant si nécessaire.
169            if(ccalendar !=  NULL) delete ccalendar;
170         }
171
172      private:
173
174         AbstractCalendar* ccalendar;
175
176         FieldDefinition*  fieldDef;
177         FileDefinition*   fileDef;
178         AxisDefinition*   axisDef;
179         GridDefinition*   gridDef;
180
181   }; //class Context
182}// namespace XMLIOSERVER
183
184#endif  // __CONTEXT__
Note: See TracBrowser for help on using the repository browser.