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

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

Mise à jour

File size: 13.1 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  = (FieldDefinition*)
29               FieldDefinition ::CreateObject(FieldDefinition ::GetDefName());
30            this->fileDef   = (FileDefinition*)
31               FileDefinition  ::CreateObject(FileDefinition  ::GetDefName());
32            this->axisDef   = (AxisDefinition*)
33               AxisDefinition  ::CreateObject(AxisDefinition  ::GetDefName());
34            this->gridDef   = (GridDefinition*)
35               GridDefinition  ::CreateObject(GridDefinition  ::GetDefName());
36            this->domainDef = (DomainDefinition*)
37               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         virtual std::string getXmlExtraAttributes(void) const
168         {
169            std::ostringstream oss;
170            oss << " calendar_type=\""<< ccalendar->getId() <<"\""
171                << " start_date=\""   << ccalendar->getInitDate() <<"\"";
172            return (oss.str());
173         }
174
175      public : /* static */
176
177         static string GetRootName(void) { return ("simulation"); }
178         static string GetName(void)     { return ("context"); }
179         static string GetDefName(void)  { return (Context::GetName()); }
180
181         static void ShowTree(ostream& os = std::clog)
182         {
183            std::string __curCtxt = Context::GetCurrentContext()->getId();
184
185            os << NIndent << "<?xml version=\"1.0\"?>" << std::endl;
186            os << NIndent << "<" << Context::GetRootName() << ">" << std::endl;
187
188            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
189            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
190
191            for (it = AllListContext.begin(); it != AllListContext.end(); it++)
192               // On sort chacun des contextes successivement.
193            {
194               Context::SetCurrentContext((*it).first);
195               os << NIndent << std::endl;
196               os << *((*it).second)[(*it).first] << std::endl;
197            }
198
199            os << NIndent << std::endl;
200            os << NIndent << "</" << Context::GetRootName() << ">" << std::endl  ;
201            Context::SetCurrentContext(__curCtxt);
202         }
203
204         static void FreeMemory(void)
205         {
206            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
207            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
208
209            for (it  = AllListContext.begin();
210                 it != AllListContext.end(); it++)
211            {
212               Context::SetCurrentContext((*it).first);
213               delete ((*it).second)[(*it).first];
214            }
215         }
216
217         // Ne plus utiliser, disponible dans les classe treatment.
218         static void SolveInheritance(void)
219         {
220            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
221            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
222
223            for ( it  = AllListContext.begin();
224                  it != AllListContext.end(); it++)
225            {
226               // Résolution des héritages descendants (càd des héritages de groupes) pour chacun des contextes.
227               Context::SetCurrentContext((*it).first);
228               ((*it).second)[(*it).first]->solveDescInheritance();
229
230               // Résolution des héritages par référence au niveau des fichiers.
231               const std::vector<CFile*>& allFiles =
232                                 CFile::GetCurrentListObject().getVector();
233
234               for (unsigned int i = 0; i < allFiles.size(); i++)
235                  allFiles[i]->solveFieldRefInheritance();
236            }
237         }
238
239         static bool HasContext(const string& id)
240         {
241            return (Context::GetAllListObject().find(id) !=
242                    Context::GetAllListObject().end());
243         }
244
245         static Context* GetContext(const string& id)
246         { return (Context::GetAllListObject()[id][id]); }
247
248         static const std::stack<string>& GetStackContextId(void)
249         { return (Context::Stid); }
250
251         static Context* GetCurrentContext(void)
252         { return (Context::GetObject(Context::GetCurrentContextId())); }
253
254         static Context* SwapContext(void)
255         {
256            if (Stid.size() == 0)
257            {
258               WARNING("SwapContext impossible car le pile des contextes est vides !");
259               return (NULL);
260            }
261            string lastId = Stid.top (); Stid.pop ();
262            return (Context::GetObject(lastId));
263         }
264
265         static void SetCurrentContext(const string& id, bool withSwap = false, bool withcheck = true)
266         {
267            if (withSwap) Stid.push (Context::GetCurrentContextId());
268
269            if (!Context::HasContext(id) && withcheck)
270               XMLIOError
271                     ("Impossible de se placer dans le contexte "+id+" car celui-ci n'existe pas dans l'arborescence!");
272
273            // On modifie le context courrant pour tout les ObjectTemplate
274            Context::SetContext(id);
275
276            // Changement de context pour les champs et groupes de champs.
277            FieldGroup::SetContext(id);  CField::SetContext(id);
278
279            // Changement de context pour les fichiers et groupes de fichiers.
280            FileGroup::SetContext(id);   CFile::SetContext(id);
281
282            // Changement de context pour les grilles et groupes de grilles.
283            GridGroup::SetContext(id);   CGrid::SetContext(id);
284
285            // Changement de context pour les axes et groupes d'axes.
286            AxisGroup::SetContext(id);   CAxis::SetContext(id);
287
288            // Changement de context pour les domaines et groupes de domaines.
289            DomainGroup::SetContext(id); CDomain::SetContext(id);
290         }
291
292      private:
293
294         static std::stack<string> Stid;
295
296         DataTreatment    * dtreatment;
297         AbstractCalendar * ccalendar;
298
299          FieldDefinition * fieldDef;
300           FileDefinition * fileDef;
301           AxisDefinition * axisDef;
302           GridDefinition * gridDef;
303         DomainDefinition * domainDef;
304
305   }; //class Context
306
307   std::stack<string> Context::Stid;
308
309}// namespace XMLIOSERVER
310
311#endif  // __CONTEXT__
Note: See TracBrowser for help on using the repository browser.