source: XMLIO_V2/dev/dev_rv/src/XMLIO/object_template.hpp @ 120

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

Mise à jour intermédiaire ...
A venir : commit d'une version stable intégrant l'écriture de fichiers NetCDF4.
(en cours de finalisation actuellement)

File size: 5.6 KB
Line 
1#ifndef __XMLIO_OBJECT_TEMPLATE__
2#define __XMLIO_OBJECT_TEMPLATE__
3
4#include "declare_attribut.hpp"
5#include "attribut_registrar.hpp"
6
7// Classes utilisées issues de la STL
8using std::pair;
9using std::string;
10using std::ostream;
11
12using XMLIOSERVER::StrHashMap;
13
14using XMLIOSERVER::XML::XMLNode;
15using XMLIOSERVER::XML::THashAttributes;
16
17namespace XMLIOSERVER
18{
19   template <class T>
20      class ObjectTemplate : public AbstractObject, public virtual AttributRegistrar
21   {
22      public :
23
24         friend ostream& operator<< (ostream& out, const T& c)
25         {
26            const AttributRegistrar &ar = c;
27
28            if (c.baseObject != NULL)
29               out <<  IncIndent << "<-- Reference resolved for the following "+ T::GetName() +
30                       " : \"" << c.baseObject->getId() << "\" -->" << DecEndl << std::endl;
31
32            if (c.hasChild())
33            {
34               out << IncIndent  << "<" << c.getName()   << c.printId() << ar << ">" << std::endl;
35               c.printChild(out); // << Ecriture des objets enfants ici.
36               out << NIndent    << "</" << c.getName()  << ">" << DecEndl;
37            }
38            else out << IncIndent << "<" << c.getName()  << c.printId() << ar << "/>" << DecEndl;
39
40            return (out);
41         }
42
43         string getName(void) const {return (T::GetName()); }
44
45         virtual T* getReference(void) const { return (NULL); }
46
47         virtual bool hasChild(void) const { return (false); }
48         virtual void printChild(ostream& out) const { out << NIndent << "<!-- No child -->" << std::endl; }
49
50         virtual void resolveDescInheritance(const AttributRegistrar* _parent = 0) { addAttributes(*_parent); }
51         virtual void resolveRefInheritance (void)
52         {
53            std::set<T*> sset;
54            T* refer = (T*)this;
55            // On remonte le fil des héritages par référence.
56            while((refer = refer->getReference()) != NULL)
57            {
58               if(sset.end() != sset.find(refer))
59               { WARNING ("Dépendance circulaire stoppée pour l'objet de type "+T::GetName()+" sur \""+refer->getId()+"\" !"); break; }
60               addAttributes(*refer);
61               sset.insert(baseObject = refer);
62            }
63         }
64
65         virtual void parse (XMLNode& _node)
66         {
67            string name = _node.getElementName();
68            THashAttributes attributes;
69
70            /// PARSING GESTION DES ATTRIBUTS ///
71            _node.getAttributes(attributes);
72            this->setAttributes(attributes);
73            attributes.clear();
74
75            /// PARSING POUR GESION DES ENFANTS ///
76            // Rien à faire.
77         }
78
79         static T* CreateObject(const string& _id) throw (XMLIOUndefinedValueException)
80         {
81            // Si l'identifiant est répertorié, on retourne l'élément existant.
82            if(ObjectTemplate<T>::HasObject(_id))
83               return (ObjectTemplate<T>::GetObject(_id));
84
85            // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié.
86            ObjectTemplate<T>::AllListObj[CurrContext].addObject(new T(_id));
87
88            return (ObjectTemplate<T>::GetObject(_id));
89         }
90
91         static T* CreateObject(void)
92         {
93            T* value = new T;
94            ObjectTemplate<T>::AllListObj[CurrContext].addObject(value);
95            return (value);
96         }
97
98         static T* GetObject(const string& _id) throw (XMLIOUndefinedValueException)
99         { return (ObjectTemplate<T>::AllListObj[CurrContext][_id]); }
100
101         static bool HasObject(const string& _id)
102         {
103            if(ObjectTemplate<T>::AllListObj.find(CurrContext) == ObjectTemplate<T>::AllListObj.end()) return false;
104            return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id));
105         }
106
107         static const StrHashMap<T>& GetCurrentListObject(void) { return (AllListObj[CurrContext]); }
108         static Poco::HashMap<string, StrHashMap<T> >& GetAllListObject(void) { return (AllListObj); }
109
110         static void SetContext(const string& id){ ObjectTemplate<T>::CurrContext = id; }
111
112         static string& GetCurrentContextId(void) { return (CurrContext); }
113
114         virtual ~ObjectTemplate(void)
115         {/* Ne rien faire de plus */}
116
117      protected :
118
119         ObjectTemplate(void) : AbstractObject(), baseObject(NULL)
120         {/* Ne rien faire de plus */}
121         ObjectTemplate(const string& _id) : AbstractObject(_id), baseObject(NULL)
122         {/* Ne rien faire de plus */}
123
124         XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName)
125         {
126            ifstream istr( path.c_str() , ifstream::in );
127            return XML::XMLNode::CreateNode(istr, _rootName);
128         }
129
130         template <class V> static V* CreateInstanceAndParse(XMLNode& _node, const char* defaultId, bool parseAttr = true )
131         {
132            V* instance_ptr = NULL; string did(defaultId);
133            if (defaultId != NULL)
134            {
135               if (V::HasObject(did))
136                  WARNING("Le noeud nommé \""+ did +"\" existe déjà pour les instances de type "+V::GetName()+", le dernier défini complétera le premier dans le context actuel!");
137               instance_ptr = (V*)V::CreateObject(did);
138               instance_ptr->parse(_node, parseAttr);
139            }
140            return (instance_ptr);
141         }
142
143      private :
144         T* baseObject;
145
146         static string CurrContext;
147         static Poco::HashMap<string, StrHashMap<T> > AllListObj;
148
149   };// class ObjectTemplate
150
151   template <class T> string ObjectTemplate<T>::CurrContext ;
152   template <class T> Poco::HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj;
153
154}// namespace XMLIOSERVER
155
156#endif // __XMLIO_OBJECT_TEMPLATE__
Note: See TracBrowser for help on using the repository browser.