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 | |
---|
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 |
---|
44 | { |
---|
45 | if (hasId()) |
---|
46 | if (T::GetDefName().compare(getId()) == 0) |
---|
47 | return T::GetDefName(); |
---|
48 | return (T::GetName()); |
---|
49 | } |
---|
50 | |
---|
51 | T* getBaseObject(void) const { return (baseObject); } |
---|
52 | |
---|
53 | virtual T* getReference(void) const { return (NULL); } |
---|
54 | |
---|
55 | const vector<T*>& getVectRefObject(void) const { return (refObject); } |
---|
56 | |
---|
57 | bool hasRefObject(void) { return (!refObject.empty()); } |
---|
58 | |
---|
59 | const T* addRefObject(T* obj) |
---|
60 | { refObject.push_back (obj); return (obj); } |
---|
61 | |
---|
62 | virtual bool hasChild(void) const { return (false); } |
---|
63 | virtual void printChild(ostream& out) const { out << NIndent << "<!-- No child -->" << std::endl; } |
---|
64 | |
---|
65 | virtual void resolveDescInheritance(const AttributRegistrar* _parent = 0) { addAttributes(*_parent); } |
---|
66 | virtual void resolveRefInheritance (void) |
---|
67 | { |
---|
68 | std::set<T*> sset; |
---|
69 | T* refer = (T*)this; |
---|
70 | // On remonte le fil des héritages par référence. |
---|
71 | while((refer = refer->getReference()) != NULL) |
---|
72 | { |
---|
73 | if(sset.end() != sset.find(refer)) |
---|
74 | { WARNING ("Dépendance circulaire stoppée pour l'objet de type "+T::GetName()+" sur \""+refer->getId()+"\" !"); break; } |
---|
75 | addAttributes(*refer); |
---|
76 | sset.insert(baseObject = refer); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | virtual void parse (XMLNode& _node) |
---|
81 | { |
---|
82 | string name = _node.getElementName(); |
---|
83 | THashAttributes attributes; |
---|
84 | |
---|
85 | /// PARSING GESTION DES ATTRIBUTS /// |
---|
86 | _node.getAttributes(attributes); |
---|
87 | this->setAttributes(attributes); |
---|
88 | attributes.clear(); |
---|
89 | |
---|
90 | /// PARSING POUR GESION DES ENFANTS /// |
---|
91 | // Rien à faire. |
---|
92 | } |
---|
93 | |
---|
94 | public : /* static */ |
---|
95 | |
---|
96 | static T* CreateObject(const string& _id) throw (XMLIOUndefinedValueException) |
---|
97 | { |
---|
98 | // Si l'identifiant est répertorié, on retourne l'élément existant. |
---|
99 | if(ObjectTemplate<T>::HasObject(_id)) |
---|
100 | return (ObjectTemplate<T>::GetObject(_id)); |
---|
101 | |
---|
102 | // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié. |
---|
103 | ObjectTemplate<T>::AllListObj[CurrContext].addObject(new T(_id)); |
---|
104 | |
---|
105 | return (ObjectTemplate<T>::GetObject(_id)); |
---|
106 | } |
---|
107 | |
---|
108 | static T* CreateObject(void) |
---|
109 | { |
---|
110 | T* value = new T; |
---|
111 | ObjectTemplate<T>::AllListObj[CurrContext].addObject(value); |
---|
112 | return (value); |
---|
113 | } |
---|
114 | |
---|
115 | static T* GetObject(const string& _id) throw (XMLIOUndefinedValueException) |
---|
116 | { return (ObjectTemplate<T>::AllListObj[CurrContext][_id]); } |
---|
117 | |
---|
118 | static bool HasObject(const string& _id) |
---|
119 | { |
---|
120 | if(ObjectTemplate<T>::AllListObj.find(CurrContext) == ObjectTemplate<T>::AllListObj.end()) return false; |
---|
121 | return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id)); |
---|
122 | } |
---|
123 | |
---|
124 | static const StrHashMap<T>& GetCurrentListObject(void) { return (AllListObj[CurrContext]); } |
---|
125 | static Poco::HashMap<string, StrHashMap<T> >& GetAllListObject(void) { return (AllListObj); } |
---|
126 | |
---|
127 | static void SetContext(const string& id){ ObjectTemplate<T>::CurrContext = id; } |
---|
128 | |
---|
129 | static string& GetCurrentContextId(void) { return (CurrContext); } |
---|
130 | |
---|
131 | virtual ~ObjectTemplate(void) |
---|
132 | {/* Ne rien faire de plus */} |
---|
133 | |
---|
134 | protected : |
---|
135 | |
---|
136 | ObjectTemplate(void) : AbstractObject(), baseObject(NULL), refObject() |
---|
137 | {/* Ne rien faire de plus */} |
---|
138 | ObjectTemplate(const string& _id) : AbstractObject(_id), baseObject(NULL), refObject() |
---|
139 | {/* Ne rien faire de plus */} |
---|
140 | |
---|
141 | XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName) |
---|
142 | { |
---|
143 | ifstream istr( path.c_str() , ifstream::in ); |
---|
144 | return XML::XMLNode::CreateNode(istr, _rootName); |
---|
145 | } |
---|
146 | |
---|
147 | template <class V> static V* CreateInstanceAndParse(XMLNode& _node, const char* defaultId, bool parseAttr = true ) |
---|
148 | { |
---|
149 | V* instance_ptr = NULL; string did(defaultId); |
---|
150 | if (defaultId != NULL) |
---|
151 | { |
---|
152 | if (V::HasObject(did)) |
---|
153 | 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!"); |
---|
154 | instance_ptr = (V*)V::CreateObject(did); |
---|
155 | instance_ptr->parse(_node, parseAttr); |
---|
156 | } |
---|
157 | return (instance_ptr); |
---|
158 | } |
---|
159 | |
---|
160 | private : |
---|
161 | T* baseObject; |
---|
162 | vector<T*> refObject; |
---|
163 | |
---|
164 | static string CurrContext; |
---|
165 | static Poco::HashMap<string, StrHashMap<T> > AllListObj; |
---|
166 | |
---|
167 | };// class ObjectTemplate |
---|
168 | |
---|
169 | template <class T> string ObjectTemplate<T>::CurrContext ; |
---|
170 | template <class T> Poco::HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj; |
---|
171 | |
---|
172 | }// namespace XMLIOSERVER |
---|
173 | |
---|
174 | #endif // __XMLIO_OBJECT_TEMPLATE__ |
---|