[112] | 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) |
---|
[126] | 29 | out << IncIndent << "<-- Référence résolu sur l'instance de "<< T::GetName() |
---|
| 30 | << " nommée \"" << c.baseObject->getId() << "\" -->" << DecEndl << std::endl; |
---|
[112] | 31 | |
---|
[126] | 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 | |
---|
[112] | 38 | if (c.hasChild()) |
---|
| 39 | { |
---|
[126] | 40 | out << IncIndent << "<" << c.getName() << c.printId() << ar << ">" << std::endl; |
---|
[112] | 41 | c.printChild(out); // << Ecriture des objets enfants ici. |
---|
[126] | 42 | if (c.hasId() && (c.getName().compare("context") == 0)) |
---|
| 43 | out << NIndent << std::endl; |
---|
[112] | 44 | out << NIndent << "</" << c.getName() << ">" << DecEndl; |
---|
| 45 | } |
---|
[126] | 46 | else |
---|
| 47 | out << IncIndent << "<" << c.getName() << c.printId() << ar << "/>" << DecEndl; |
---|
[112] | 48 | |
---|
| 49 | return (out); |
---|
| 50 | } |
---|
| 51 | |
---|
[126] | 52 | bool isDefinition(void) const |
---|
| 53 | { |
---|
| 54 | if (!this->hasId()) return (false); |
---|
| 55 | return (this->getName().compare(this->getId()) == 0); |
---|
| 56 | } |
---|
| 57 | |
---|
[125] | 58 | string getName(void) const |
---|
| 59 | { |
---|
[126] | 60 | if (this->hasId()) |
---|
[125] | 61 | if (T::GetDefName().compare(getId()) == 0) |
---|
[126] | 62 | return (T::GetDefName()); |
---|
[125] | 63 | return (T::GetName()); |
---|
| 64 | } |
---|
[112] | 65 | |
---|
[124] | 66 | T* getBaseObject(void) const { return (baseObject); } |
---|
[122] | 67 | |
---|
[124] | 68 | const vector<T*>& getVectRefObject(void) const { return (refObject); } |
---|
| 69 | |
---|
[126] | 70 | bool hasRefObject(void) const { return (!refObject.empty()); } |
---|
[124] | 71 | |
---|
| 72 | const T* addRefObject(T* obj) |
---|
| 73 | { refObject.push_back (obj); return (obj); } |
---|
| 74 | |
---|
[126] | 75 | public : /* virtual */ |
---|
| 76 | |
---|
| 77 | virtual T* getReference(void) const { return (NULL); } |
---|
[112] | 78 | virtual bool hasChild(void) const { return (false); } |
---|
| 79 | |
---|
[126] | 80 | virtual void printChild(ostream& out) const |
---|
| 81 | { out << NIndent << "<!-- No child -->" << std::endl; } |
---|
| 82 | |
---|
[128] | 83 | virtual void solveDescInheritance(const AttributRegistrar* _parent = 0) |
---|
[126] | 84 | { this->addAttributes(*_parent); } |
---|
| 85 | |
---|
[128] | 86 | virtual void solveRefInheritance (void) |
---|
[112] | 87 | { |
---|
| 88 | std::set<T*> sset; |
---|
| 89 | T* refer = (T*)this; |
---|
[115] | 90 | // On remonte le fil des héritages par référence. |
---|
[112] | 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 /// |
---|
[126] | 111 | // Rien à faire ici. |
---|
[112] | 112 | } |
---|
| 113 | |
---|
[126] | 114 | virtual ~ObjectTemplate(void) |
---|
| 115 | {/* Ne rien faire de plus */} |
---|
| 116 | |
---|
[124] | 117 | public : /* static */ |
---|
| 118 | |
---|
[120] | 119 | static T* CreateObject(const string& _id) throw (XMLIOUndefinedValueException) |
---|
[112] | 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 | |
---|
[120] | 131 | static T* CreateObject(void) |
---|
[112] | 132 | { |
---|
| 133 | T* value = new T; |
---|
| 134 | ObjectTemplate<T>::AllListObj[CurrContext].addObject(value); |
---|
[120] | 135 | return (value); |
---|
[112] | 136 | } |
---|
| 137 | |
---|
[120] | 138 | static T* GetObject(const string& _id) throw (XMLIOUndefinedValueException) |
---|
| 139 | { return (ObjectTemplate<T>::AllListObj[CurrContext][_id]); } |
---|
[112] | 140 | |
---|
[115] | 141 | static bool HasObject(const string& _id) |
---|
[112] | 142 | { |
---|
[126] | 143 | if(ObjectTemplate<T>::AllListObj.find(CurrContext) == |
---|
| 144 | ObjectTemplate<T>::AllListObj.end()) |
---|
| 145 | return (false); |
---|
[112] | 146 | return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id)); |
---|
| 147 | } |
---|
| 148 | |
---|
[137] | 149 | static const StrHashMap<T>& GetCurrentListObject(void) |
---|
| 150 | { return (AllListObj[CurrContext]); } |
---|
[112] | 151 | |
---|
[137] | 152 | static Poco::HashMap<string, StrHashMap<T> >& GetAllListObject(void) |
---|
| 153 | { return (AllListObj); } |
---|
[112] | 154 | |
---|
[137] | 155 | static void SetContext(const string& id) |
---|
| 156 | { ObjectTemplate<T>::CurrContext = id; } |
---|
[112] | 157 | |
---|
[137] | 158 | static string& GetCurrentContextId(void) |
---|
| 159 | { return (ObjectTemplate<T>::CurrContext); } |
---|
| 160 | |
---|
[112] | 161 | protected : |
---|
| 162 | |
---|
[126] | 163 | ObjectTemplate(void) |
---|
| 164 | : AttributRegistrar(), AbstractObject(), baseObject(NULL), refObject() |
---|
| 165 | { /* Ne rien faire de plus */ } |
---|
[112] | 166 | |
---|
[126] | 167 | ObjectTemplate(const string& _id) |
---|
| 168 | : AttributRegistrar(), AbstractObject(_id), baseObject(NULL), refObject() |
---|
| 169 | { /* Ne rien faire de plus */ } |
---|
| 170 | |
---|
| 171 | ObjectTemplate(const ObjectTemplate<T>& _ot, bool _withAttrList = true, bool _withId = true) |
---|
| 172 | : AttributRegistrar(), AbstractObject(), baseObject(_ot.baseObject), refObject(_ot.refObject) |
---|
| 173 | { |
---|
| 174 | // Si requis, ajout de l'identifiant lors de la copie. |
---|
| 175 | if (_ot.hasId() && _withId) this->setId(_ot.getId()); |
---|
| 176 | // Si requis, ajout des attributs lors de la copie. |
---|
| 177 | if (_withAttrList) this->addAttributes(_ot); |
---|
| 178 | } |
---|
| 179 | |
---|
[112] | 180 | XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName) |
---|
| 181 | { |
---|
| 182 | ifstream istr( path.c_str() , ifstream::in ); |
---|
[126] | 183 | return (XML::XMLNode::CreateNode(istr, _rootName)); |
---|
[112] | 184 | } |
---|
| 185 | |
---|
[137] | 186 | template <class V> |
---|
| 187 | static V* CreateInstanceAndParse(XMLNode& _node, const char* defaultId, bool parseAttr = true ) |
---|
[112] | 188 | { |
---|
| 189 | V* instance_ptr = NULL; string did(defaultId); |
---|
| 190 | if (defaultId != NULL) |
---|
| 191 | { |
---|
| 192 | if (V::HasObject(did)) |
---|
[126] | 193 | WARNING("Le noeud nommé \""+ did +"\" existe déjà pour les instances de type "+ |
---|
| 194 | V::GetName()+", le dernier défini complétera le premier dans le context actuel!"); |
---|
[120] | 195 | instance_ptr = (V*)V::CreateObject(did); |
---|
[112] | 196 | instance_ptr->parse(_node, parseAttr); |
---|
| 197 | } |
---|
| 198 | return (instance_ptr); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | private : |
---|
[126] | 202 | |
---|
[112] | 203 | T* baseObject; |
---|
[126] | 204 | std::vector<T*> refObject; |
---|
[112] | 205 | |
---|
[126] | 206 | private : /* static */ |
---|
| 207 | |
---|
[112] | 208 | static string CurrContext; |
---|
[120] | 209 | static Poco::HashMap<string, StrHashMap<T> > AllListObj; |
---|
[112] | 210 | |
---|
| 211 | };// class ObjectTemplate |
---|
| 212 | |
---|
[126] | 213 | // Initialisation des variables statiques de la classe ObjectTemplate. |
---|
[112] | 214 | template <class T> string ObjectTemplate<T>::CurrContext ; |
---|
[120] | 215 | template <class T> Poco::HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj; |
---|
[112] | 216 | |
---|
| 217 | }// namespace XMLIOSERVER |
---|
| 218 | |
---|
| 219 | #endif // __XMLIO_OBJECT_TEMPLATE__ |
---|