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

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

Amélioration de quelques portions de code.
Ajout de contructeurs par copie.

File size: 7.5 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 << "<-- Référence résolu sur l'instance de "<< T::GetName()
30                   <<   " nommée \"" << c.baseObject->getId() << "\" -->" << DecEndl << std::endl;
31
32            if (c.isDefinition())
33            {
34               out << NIndent << std::endl;
35               out << NIndent << "<!-- Groupe de définition dans le contexte \"" << ObjectTemplate<T>::GetCurrentContextId() << "\" courant -->" << std::endl;
36            }
37
38            if (c.hasChild())
39            {
40               out << IncIndent  << "<"  << c.getName()  << c.printId() << ar << ">" << std::endl;
41               c.printChild(out); // << Ecriture des objets enfants ici.
42               if (c.hasId() && (c.getName().compare("context") == 0))
43                  out << NIndent << std::endl;
44               out << NIndent    << "</" << c.getName()  << ">" << DecEndl;
45            }
46            else
47               out << IncIndent << "<" << c.getName()  << c.printId() << ar << "/>" << DecEndl;
48
49            return (out);
50         }
51
52         bool isDefinition(void) const
53         {
54            if (!this->hasId()) return (false);
55            return (this->getName().compare(this->getId()) == 0);
56         }
57
58         string getName(void) const
59         {
60            if (this->hasId())
61               if (T::GetDefName().compare(getId()) == 0)
62                  return (T::GetDefName());
63            return (T::GetName());
64         }
65
66         T* getBaseObject(void) const { return (baseObject); }
67
68         const vector<T*>& getVectRefObject(void) const { return (refObject); }
69
70         bool hasRefObject(void) const { return (!refObject.empty()); }
71
72         const T* addRefObject(T* obj)
73         { refObject.push_back (obj); return (obj); }
74
75      public : /* virtual */
76
77         virtual T* getReference(void) const { return (NULL); }
78         virtual bool hasChild(void) const { return (false); }
79
80         virtual void printChild(ostream& out) const
81         { out << NIndent << "<!-- No child -->" << std::endl; }
82
83         virtual void resolveDescInheritance(const AttributRegistrar* _parent = 0)
84         { this->addAttributes(*_parent); }
85
86         virtual void resolveRefInheritance (void)
87         {
88            std::set<T*> sset;
89            T* refer = (T*)this;
90            // On remonte le fil des héritages par référence.
91            while((refer = refer->getReference()) != NULL)
92            {
93               if(sset.end() != sset.find(refer))
94               { WARNING ("Dépendance circulaire stoppée pour l'objet de type "+T::GetName()+" sur \""+refer->getId()+"\" !"); break; }
95               addAttributes(*refer);
96               sset.insert(baseObject = refer);
97            }
98         }
99
100         virtual void parse (XMLNode& _node)
101         {
102            string name = _node.getElementName();
103            THashAttributes attributes;
104
105            /// PARSING GESTION DES ATTRIBUTS ///
106            _node.getAttributes(attributes);
107            this->setAttributes(attributes);
108            attributes.clear();
109
110            /// PARSING POUR GESION DES ENFANTS ///
111            // Rien à faire ici.
112         }
113
114         virtual ~ObjectTemplate(void)
115         {/* Ne rien faire de plus */}
116
117      public : /* static */
118
119         static T* CreateObject(const string& _id) throw (XMLIOUndefinedValueException)
120         {
121            // Si l'identifiant est répertorié, on retourne l'élément existant.
122            if(ObjectTemplate<T>::HasObject(_id))
123               return (ObjectTemplate<T>::GetObject(_id));
124
125            // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié.
126            ObjectTemplate<T>::AllListObj[CurrContext].addObject(new T(_id));
127
128            return (ObjectTemplate<T>::GetObject(_id));
129         }
130
131         static T* CreateObject(void)
132         {
133            T* value = new T;
134            ObjectTemplate<T>::AllListObj[CurrContext].addObject(value);
135            return (value);
136         }
137
138         static T* GetObject(const string& _id) throw (XMLIOUndefinedValueException)
139         { return (ObjectTemplate<T>::AllListObj[CurrContext][_id]); }
140
141         static bool HasObject(const string& _id)
142         {
143            if(ObjectTemplate<T>::AllListObj.find(CurrContext) ==
144               ObjectTemplate<T>::AllListObj.end())
145               return (false);
146            return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id));
147         }
148
149         static const StrHashMap<T>& GetCurrentListObject(void) { return (AllListObj[CurrContext]); }
150         static Poco::HashMap<string, StrHashMap<T> >& GetAllListObject(void) { return (AllListObj); }
151
152         static void SetContext(const string& id) { ObjectTemplate<T>::CurrContext = id; }
153
154         static string& GetCurrentContextId(void) { return (ObjectTemplate<T>::CurrContext); }
155
156      protected :
157
158         ObjectTemplate(void)
159            : AttributRegistrar(), AbstractObject(), baseObject(NULL), refObject()
160         { /* Ne rien faire de plus */ }
161
162         ObjectTemplate(const string& _id)
163            : AttributRegistrar(), AbstractObject(_id), baseObject(NULL), refObject()
164         { /* Ne rien faire de plus */ }
165
166         ObjectTemplate(const ObjectTemplate<T>& _ot, bool _withAttrList = true, bool _withId = true)
167            : AttributRegistrar(), AbstractObject(), baseObject(_ot.baseObject), refObject(_ot.refObject)
168         {
169            // Si requis, ajout de l'identifiant lors de la copie.
170            if (_ot.hasId() && _withId) this->setId(_ot.getId());
171            // Si requis, ajout des attributs lors de la copie.
172            if (_withAttrList) this->addAttributes(_ot);
173         }
174
175         XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName)
176         {
177            ifstream istr( path.c_str() , ifstream::in );
178            return (XML::XMLNode::CreateNode(istr, _rootName));
179         }
180
181         template <class V> static V* CreateInstanceAndParse(XMLNode& _node, const char* defaultId, bool parseAttr = true )
182         {
183            V* instance_ptr = NULL; string did(defaultId);
184            if (defaultId != NULL)
185            {
186               if (V::HasObject(did))
187                  WARNING("Le noeud nommé \""+ did +"\" existe déjà pour les instances de type "+
188                           V::GetName()+", le dernier défini complétera le premier dans le context actuel!");
189               instance_ptr = (V*)V::CreateObject(did);
190               instance_ptr->parse(_node, parseAttr);
191            }
192            return (instance_ptr);
193         }
194
195      private :
196
197         T* baseObject;
198         std::vector<T*> refObject;
199
200      private : /* static */
201
202         static string CurrContext;
203         static Poco::HashMap<string, StrHashMap<T> > AllListObj;
204
205   };// class ObjectTemplate
206
207   // Initialisation des variables statiques de la classe ObjectTemplate.
208   template <class T> string ObjectTemplate<T>::CurrContext ;
209   template <class T> Poco::HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj;
210
211}// namespace XMLIOSERVER
212
213#endif // __XMLIO_OBJECT_TEMPLATE__
Note: See TracBrowser for help on using the repository browser.