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

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

Début de prise en charge des références (sans contrÎle ni transmission d'attribut pour le moment).

File size: 5.0 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::vector<T*> allRef;
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               allRef.insert(allRef.begin(), baseObject = refer);
57
58            // Gestion des attributs ici.
59         }
60
61         static T& CreateObject(const string _id) throw (XMLIOUndefinedValueException)
62         {
63            // Si l'identifiant est répertorié, on retourne l'élément existant.
64            if(ObjectTemplate<T>::HasObject(_id))
65               return (ObjectTemplate<T>::GetObject(_id));
66
67            // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié.
68            ObjectTemplate<T>::AllListObj[CurrContext].addObject(new T(_id));
69
70            return (ObjectTemplate<T>::GetObject(_id));
71         }
72
73         static T& CreateObject(void)
74         {
75            T* value = new T;
76            ObjectTemplate<T>::AllListObj[CurrContext].addObject(value);
77            return (*value);
78         }
79
80         static T& GetObject(const string _id) throw (XMLIOUndefinedValueException)
81         { return (*ObjectTemplate<T>::AllListObj[CurrContext][_id]); }
82
83         static bool HasObject(const string _id)
84         {
85            if(ObjectTemplate<T>::AllListObj.find(CurrContext) == ObjectTemplate<T>::AllListObj.end()) return false;
86            return (ObjectTemplate<T>::AllListObj[CurrContext].hasMappedValue(_id));
87         }
88
89         static const StrHashMap<T>& GetCurrentListObject(void) { return (AllListObj[CurrContext]); }
90         static HashMap<string, StrHashMap<T> >& GetAllListObject(void) { return (AllListObj); }
91
92         static void SetContext(const string& id){ ObjectTemplate<T>::CurrContext = id; }
93
94         static string& GetCurrentContextId(void) { return (CurrContext); }
95
96         virtual ~ObjectTemplate(void)
97         {/* Ne rien faire de plus */}
98
99      protected :
100
101         ObjectTemplate(void) : AbstractObject(), baseObject(NULL)
102         {/* Ne rien faire de plus */}
103         ObjectTemplate(const string& _id) : AbstractObject(_id), baseObject(NULL)
104         {/* Ne rien faire de plus */}
105
106         XML::XMLNode getNodeIncludedFile(const string& path, const string& _rootName)
107         {
108            ifstream istr( path.c_str() , ifstream::in );
109            return XML::XMLNode::CreateNode(istr, _rootName);
110         }
111
112         template <class V> static V* CreateInstanceAndParse( THashAttributes& attributes, XMLNode& _node, const char* defaultId, bool parseAttr = true )
113         {
114            V* instance_ptr = NULL; string did(defaultId);
115            if (defaultId != NULL)
116            {
117               if (V::HasObject(did))
118                  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!");
119               instance_ptr = (V*)&V::CreateObject(did);
120               instance_ptr->parse(_node, parseAttr);
121            }
122            return (instance_ptr);
123         }
124
125      private :
126         T* baseObject;
127
128         static string CurrContext;
129         static HashMap<string, StrHashMap<T> > AllListObj;
130
131   };// class ObjectTemplate
132
133   template <class T> string ObjectTemplate<T>::CurrContext ;
134   template <class T> HashMap<string, StrHashMap<T> > ObjectTemplate<T>::AllListObj;
135
136}// namespace XMLIOSERVER
137
138#endif // __XMLIO_OBJECT_TEMPLATE__
Note: See TracBrowser for help on using the repository browser.