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
RevLine 
[98]1#ifndef __CONTEXT__
[107]2#define __CONTEXT__
[79]3
[107]4
[98]5namespace XMLIOSERVER
[107]6{
[134]7   class DataTreatment;
8
[98]9   class Context : public ObjectTemplate<Context>
10   {
11      public:
[107]12
[131]13         Context(void)
[134]14            : ObjectTemplate<Context>(),
15              dtreatment(NULL), ccalendar(NULL),
[131]16              fieldDef(NULL), fileDef(NULL), axisDef(NULL), gridDef(NULL), domainDef(NULL)
[134]17         { /* Ne rien faire de plus */ }
[119]18
[131]19         Context(const string& _id)
[134]20            : ObjectTemplate<Context>(_id),
21              dtreatment(NULL), ccalendar(NULL),
[131]22              fieldDef(NULL), fileDef(NULL), axisDef(NULL), gridDef(NULL), domainDef(NULL)
[134]23         { /* Ne rien faire de plus */ }
[107]24
[137]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
[134]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  ); }
[126]45         DomainDefinition * getDomainDefinition(void) const { return (this->domainDef); }
46
[134]47         AbstractCalendar * getCalendar     (void) const { return (ccalendar ); }
48         DataTreatment    * getDataTreatment(void) const { return (dtreatment); }
[131]49
[137]50         AbstractCalendar * setCalendar(AbstractCalendar * const _cal) { return (ccalendar = _cal);}
51
[126]52         AbstractCalendar * setCalendar(const string& _calName, const string& _dateStr)
[107]53         {
[134]54            if(ccalendar  !=  NULL) delete ccalendar;
55
[126]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));
[115]66
[126]67            WARNING("L'identifiant "+_calName+" est inconnu, le calendrier grégorien sera choisi par défault pour le contexte "+getId());
[115]68
[126]69            return (ccalendar = new GregorianCalendar(_dateStr));
[107]70         }
71
[134]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)
[107]79         {
[126]80            // Désallocation dynamique de mémoire pour chacun des groupes de définition si nécessaire.
[134]81            if( fieldDef != NULL) delete  fieldDef ; if(fileDef  != NULL) delete fileDef ;
82            if(  axisDef != NULL) delete   axisDef ; if(gridDef  != NULL) delete gridDef ;
[126]83            if(domainDef != NULL) delete domainDef ;
[107]84
[126]85            // Désallocation dynamique de mémoire pour le calendrier associé au contexte courant si nécessaire.
[134]86            if(ccalendar  !=  NULL) delete ccalendar;
87            // INFO La mémoire dédiée à dtreatment est désallouée ailleurs.
[107]88         }
89
[110]90         virtual void parse (XMLNode& _node)
[98]91         {
92            THashAttributes attributes;
[107]93
[98]94            /// PARSING POUR GESTION DES ENFANTS
[107]95            if (_node.getElementName().compare(Context::GetName()))
[98]96               WARNING("Le noeud est mal nommé mais sera traité comme un context !");
[107]97
[98]98            if (!(_node.goToChildElement()))
[107]99               WARNING("Le context ne contient pas d'enfant !");
[98]100            else
101            {
[137]102              do { // Parcours des contextes pour traitement.
[107]103
[98]104                  string name = _node.getElementName();
105                  attributes.clear();
[107]106                  _node.getAttributes(attributes);
107
[98]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 !"); }
[79]110
[131]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; }
[107]115
[131]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; }
[107]120
[131]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; }
[122]125
[131]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; }
[122]130
[131]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; }
[122]135
[120]136                  WARNING("La définition est invalide, seuls les champs, grilles, axes et fichiers peuvent être définis !");
[107]137
[98]138               } while (_node.goToNextElement());
[107]139
[98]140               _node.goToParentElement(); // Retour au parent
141            }
142         }
[107]143
144         virtual bool hasChild(void) const
[122]145         { return ((fieldDef != NULL) or (fileDef != NULL)  or (axisDef != NULL) or (gridDef != NULL) or (domainDef != NULL)); }
[113]146
[107]147         virtual void printChild(ostream& out) const
148         {
[134]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;
[122]153            if(domainDef != NULL) out << *(DomainDefinition*) domainDef << std::endl;
[107]154         }
[114]155
[128]156         virtual void solveDescInheritance(const AttributRegistrar* const _parent = 0)
[107]157         {
[114]158            if (_parent != 0) return;
[107]159            // Résolution des héritages descendants pour chacun des groupes de définitions.
[134]160            if( fieldDef != NULL)  fieldDef->solveDescInheritance();
161            if(  fileDef != NULL)   fileDef->solveDescInheritance();
162            if(  axisDef != NULL)   axisDef->solveDescInheritance();
163            if(  gridDef != NULL)   gridDef->solveDescInheritance();
[128]164            if(domainDef != NULL) domainDef->solveDescInheritance();
[107]165         }
166
[126]167      public : /* static */
[107]168
[126]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)
[119]174         {
[126]175            os << NIndent << "<?xml version=\"1.0\"?>" << std::endl;
176            os << NIndent << "<" << Context::GetRootName() << ">" << std::endl;
[119]177
[126]178            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
[131]179            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
180
181            for (it = AllListContext.begin(); it != AllListContext.end(); it++)
[126]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            }
[119]188
[126]189            os << NIndent << std::endl;
190            os << NIndent << "</" << Context::GetRootName() << ">" << std::endl  ;
[119]191         }
192
[126]193         static void FreeMemory(void)
[107]194         {
[126]195            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
[131]196            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
197
198            for (it = AllListContext.begin(); it != AllListContext.end(); it++)
[126]199            { Context::SetCurrentContext((*it).first); delete ((*it).second)[(*it).first]; }
200         }
[120]201
[126]202         // Ne plus utiliser, disponible dans les classe treatment.
[128]203         static void SolveInheritance(void)
[126]204         {
205            Poco::HashMap<string, StrHashMap<Context> > &AllListContext = Context::GetAllListObject();
[131]206            Poco::HashMap<string, StrHashMap<Context> >::Iterator it;
207
[134]208            for ( it  = AllListContext.begin();
209                  it != AllListContext.end(); it++)
[126]210            {
211               // Résolution des héritages descendants (càd des héritages de groupes) pour chacun des contextes.
212               Context::SetCurrentContext((*it).first);
[128]213               ((*it).second)[(*it).first]->solveDescInheritance();
[126]214
215               // Résolution des héritages par référence au niveau des fichiers.
[131]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();
[126]221            }
[107]222         }
[104]223
[132]224         static bool HasContext(const string& id)
[134]225         { return (Context::GetAllListObject().find(id) != Context::GetAllListObject().end()); }
[132]226
227         static Context* GetContext(const string& id)
228         { return (Context::GetAllListObject()[id][id]); }
229
[134]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
[126]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
[132]247         static void SetCurrentContext(const string& id, bool withSwap = false, bool withcheck = true)
[126]248         {
249            if (withSwap) Stid.push (Context::GetCurrentContextId());
250
[132]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
[126]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
[98]274      private:
[107]275
[131]276         static std::stack<string> Stid;
[125]277
[134]278         DataTreatment    * dtreatment;
[126]279         AbstractCalendar * ccalendar;
[119]280
[134]281          FieldDefinition * fieldDef;
282           FileDefinition * fileDef;
283           AxisDefinition * axisDef;
284           GridDefinition * gridDef;
[126]285         DomainDefinition * domainDef;
[79]286
[98]287   }; //class Context
[125]288
[131]289   std::stack<string> Context::Stid;
[125]290
[98]291}// namespace XMLIOSERVER
[79]292
[98]293#endif  // __CONTEXT__
Note: See TracBrowser for help on using the repository browser.