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

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

mise à jour

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