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

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

Pour sauvegarde

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