source: XMLIO_V2/dev/dev_rv/src/XMLIO/xmlio_object_template.hpp @ 110

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

Prise en charge simplifiée des axes et des grilles.
Simplification du code.

File size: 5.6 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 << "<-- Reference resolved for the following "+ T::GetName() +" : \"" << c.baseObject->getId() << "\" -->" << DecEndl << std::endl;
30
31            if (c.hasChild())
32            {
33               out << IncIndent  << "<" << c.getName()   << c.printId() << ar << ">" << std::endl;
34               c.printChild(out); // << Ecriture des objets enfants ici.
35               out << NIndent    << "</" << c.getName()  << ">" << DecEndl;
36            }
37            else out << IncIndent << "<" << c.getName()  << c.printId() << ar << "/>" << DecEndl;
38
39            return (out);
40         }
41
42         string getName(void) const {return (T::GetName()); }
43
44         virtual T* getReference(void) const { return (NULL); }
45
46         virtual bool hasChild(void) const { return (false); }
47         virtual void printChild(ostream& out) const { /* Ne rien faire de plus */ }
48
49         virtual void resolveDescInheritance(const AttributRegistrar* _parent = 0) { addAttributes(*_parent); }
50         virtual void resolveRefInheritance(void)
51         {
52            std::set<T*> sset;
53            T* refer = (T*)this;
54            // On remonte le fil des héritages par référence (Boucle infinie).
55            while((refer = refer->getReference()) != NULL)
56            {
57               if(sset.end() != sset.find(refer))
58               { WARNING ("Dépendance circulaire stoppée pour l'objet de type "+T::GetName()+" sur \""+refer->getId()+"\" !"); break; }
59               addAttributes(*refer);
60               sset.insert(baseObject = refer);
61            }
62         }
63
64         virtual void parse (XMLNode& _node)
65         {
66            string name = _node.getElementName();
67            THashAttributes attributes;
68
69            /// PARSING GESTION DES ATTRIBUTS ///
70            _node.getAttributes(attributes);
71            this->setAttributes(attributes);
72            attributes.clear();
73
74            /// PARSING POUR GESION DES ENFANTS ///
75            // Rien à faire.
76         }
77
78         static T& CreateObject(const string _id) throw (XMLIOUndefinedValueException)
79         {
80            // Si l'identifiant est répertorié, on retourne l'élément existant.
81            if(ObjectTemplate<T>::HasObject(_id))
82               return (ObjectTemplate<T>::GetObject(_id));
83
84            // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié.
85            ObjectTemplate<T>::AllListObj[CurrContext].addObject(new T(_id));
86
87            return (ObjectTemplate<T>::GetObject(_id));
88         }
89
90         static T& CreateObject(void)
91         {
92            T* value = new T;
93            ObjectTemplate<T>::AllListObj[CurrContext].addObject(value);
94            return (*value);
95         }
96
97         static T& GetObject(const string _id) throw (XMLIOUndefinedValueException)
98         { return (*ObjectTemplate<T>::AllListObj[CurrContext][_id]); }
99
100         static bool HasObject(const string _id)
101         {
102            if(ObjectTemplate<T>::AllListObj.find(CurrContext) == ObjectTemplate<T>::AllListObj.end()) return false;
103            return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id));
104         }
105
106         static const StrHashMap<T>& GetCurrentListObject(void) { return (AllListObj[CurrContext]); }
107         static HashMap<string, StrHashMap<T> >& GetAllListObject(void) { return (AllListObj); }
108
109         static void SetContext(const string& id){ ObjectTemplate<T>::CurrContext = id; }
110
111         static string& GetCurrentContextId(void) { return (CurrContext); }
112
113         virtual ~ObjectTemplate(void)
114         {/* Ne rien faire de plus */}
115
116      protected :
117
118         ObjectTemplate(void) : AbstractObject(), baseObject(NULL)
119         {/* Ne rien faire de plus */}
120         ObjectTemplate(const string& _id) : AbstractObject(_id), baseObject(NULL)
121         {/* Ne rien faire de plus */}
122
123         XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName)
124         {
125            ifstream istr( path.c_str() , ifstream::in );
126            return XML::XMLNode::CreateNode(istr, _rootName);
127         }
128
129         template <class V> static V* CreateInstanceAndParse( THashAttributes& attributes, XMLNode& _node, const char* defaultId, bool parseAttr = true )
130         {
131            V* instance_ptr = NULL; string did(defaultId);
132            if (defaultId != NULL)
133            {
134               if (V::HasObject(did))
135                  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!");
136               instance_ptr = (V*)&V::CreateObject(did);
137               instance_ptr->parse(_node, parseAttr);
138            }
139            return (instance_ptr);
140         }
141
142      private :
143         T* baseObject;
144
145         static string CurrContext;
146         static HashMap<string, StrHashMap<T> > AllListObj;
147
148   };// class ObjectTemplate
149
150   template <class T> string ObjectTemplate<T>::CurrContext ;
151   template <class T> HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj;
152
153}// namespace XMLIOSERVER
154
155#endif // __XMLIO_OBJECT_TEMPLATE__
Note: See TracBrowser for help on using the repository browser.