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 | |
---|
25 | friend ostream& operator<< (ostream& out, const T& c) |
---|
26 | { |
---|
27 | const AttributRegistrar &ar = c; |
---|
28 | if (c.hasChild()) |
---|
29 | { |
---|
30 | out << IncIndent << "<" << c.getName() << c.printId() << ar << ">" << std::endl; |
---|
31 | c.printChild(out); // << Ecriture des objets enfants ici. |
---|
32 | out << NIndent << "</" << c.getName() << ">" << DecEndl; |
---|
33 | } |
---|
34 | else out << IncIndent << "<" << c.getName() << c.printId() << ar << "/>" << DecEndl; |
---|
35 | |
---|
36 | return (out); |
---|
37 | } |
---|
38 | |
---|
39 | string getName(void) const {return (T::GetName()); } |
---|
40 | |
---|
41 | virtual bool hasChild(void) const { return (false); } |
---|
42 | virtual void printChild(ostream& out) const { /* Ne rien faire de plus */ } |
---|
43 | virtual void resolveDescInheritance(const AttributRegistrar* _parent = 0) { addAttributes(*_parent); } |
---|
44 | |
---|
45 | static T& CreateObject(const string _id) throw (XMLIOUndefinedValueException) |
---|
46 | { |
---|
47 | // Si l'identifiant est répertorié, on retourne l'élément existant. |
---|
48 | if(ObjectTemplate<T>::HasObject(_id)) |
---|
49 | return (ObjectTemplate<T>::GetObject(_id)); |
---|
50 | |
---|
51 | // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié. |
---|
52 | ObjectTemplate<T>::AllListObj[CurrContext].addObject(new T(_id)); |
---|
53 | |
---|
54 | return (ObjectTemplate<T>::GetObject(_id)); |
---|
55 | } |
---|
56 | |
---|
57 | static T& CreateObject(void) |
---|
58 | { |
---|
59 | T* value = new T; |
---|
60 | ObjectTemplate<T>::AllListObj[CurrContext].addObject(value); |
---|
61 | return (*value); |
---|
62 | } |
---|
63 | |
---|
64 | static T& GetObject(const string _id) throw (XMLIOUndefinedValueException) |
---|
65 | { return (*ObjectTemplate<T>::AllListObj[CurrContext][_id]); } |
---|
66 | |
---|
67 | static bool HasObject(const string _id) |
---|
68 | { |
---|
69 | if(ObjectTemplate<T>::AllListObj.find(CurrContext) == ObjectTemplate<T>::AllListObj.end()) return false; |
---|
70 | return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id)); |
---|
71 | } |
---|
72 | |
---|
73 | static const StrHashMap<T>& GetCurrentListObject(void) { return (AllListObj[CurrContext]); } |
---|
74 | static HashMap<string, StrHashMap<T> >& GetAllListObject(void) { return (AllListObj); } |
---|
75 | |
---|
76 | static void SetContext(const string& id){ ObjectTemplate<T>::CurrContext = id; } |
---|
77 | |
---|
78 | static string& GetCurrentContextId(void) { return (CurrContext); } |
---|
79 | |
---|
80 | virtual ~ObjectTemplate(void) |
---|
81 | {/* Ne rien faire de plus */} |
---|
82 | |
---|
83 | protected : |
---|
84 | |
---|
85 | ObjectTemplate(void) : AbstractObject() |
---|
86 | {/* Ne rien faire de plus */} |
---|
87 | ObjectTemplate(const string& _id) : AbstractObject(_id) |
---|
88 | {/* Ne rien faire de plus */} |
---|
89 | |
---|
90 | XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName) |
---|
91 | { |
---|
92 | ifstream istr( path.c_str() , ifstream::in ); |
---|
93 | return XML::XMLNode::CreateNode(istr, _rootName); |
---|
94 | } |
---|
95 | |
---|
96 | template <class V> static V* CreateInstanceAndParse( THashAttributes& attributes, XMLNode& _node, const char* defaultId, bool parseAttr = true ) |
---|
97 | { |
---|
98 | V* instance_ptr = NULL; string did(defaultId); |
---|
99 | if (defaultId != NULL) |
---|
100 | { |
---|
101 | if (V::HasObject(did)) |
---|
102 | 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!"); |
---|
103 | instance_ptr = (V*)&V::CreateObject(did); |
---|
104 | instance_ptr->parse(_node, parseAttr); |
---|
105 | } |
---|
106 | return (instance_ptr); |
---|
107 | } |
---|
108 | |
---|
109 | private : |
---|
110 | |
---|
111 | static string CurrContext; |
---|
112 | static HashMap<string, StrHashMap<T> > AllListObj; |
---|
113 | |
---|
114 | };// class ObjectTemplate |
---|
115 | |
---|
116 | template <class T> string ObjectTemplate<T>::CurrContext ; |
---|
117 | template <class T> HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj; |
---|
118 | |
---|
119 | }// namespace XMLIOSERVER |
---|
120 | |
---|
121 | #endif // __XMLIO_OBJECT_TEMPLATE__ |
---|