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

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

suite interface cpp<->fortran

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