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