1 | #ifndef __XMLIO_OBJECT_TEMPLATE__ |
---|
2 | #define __XMLIO_OBJECT_TEMPLATE__ |
---|
3 | |
---|
4 | // Classes utilisées issues de la STL |
---|
5 | using std::pair; |
---|
6 | using std::string; |
---|
7 | using std::ostream; |
---|
8 | |
---|
9 | using XMLIOSERVER::StrHashMap; |
---|
10 | |
---|
11 | namespace XMLIOSERVER |
---|
12 | { |
---|
13 | template <class T> |
---|
14 | class ObjectTemplate : public AbstractObject |
---|
15 | { |
---|
16 | public : |
---|
17 | |
---|
18 | static T& CreateObject(const string _id) throw (XMLIOUndefinedValueException) |
---|
19 | { |
---|
20 | // Si l'identifiant est répertorié, on retourne l'élément existant. |
---|
21 | if(ObjectTemplate<T>::HasObject(_id)) |
---|
22 | return (ObjectTemplate<T>::GetObject(_id)); |
---|
23 | |
---|
24 | // Ajout d'un nouvel objet si l'identifiant n'est pas répertorié. |
---|
25 | T identified_object(_id); |
---|
26 | ObjectTemplate<T>::CurrListObj.addObject(identified_object); |
---|
27 | ObjectTemplate<T>::GetObject(_id).registerAllAttributes(); |
---|
28 | return (ObjectTemplate<T>::GetObject(_id)); |
---|
29 | } |
---|
30 | |
---|
31 | static T& CreateObject(void) |
---|
32 | { |
---|
33 | ObjectTemplate<T>::CurrListObj.addObject(*(new T)); //<< Perte mémoire ici |
---|
34 | return (*(ObjectTemplate<T>::CurrListObj.getVector()[ObjectTemplate<T>::CurrListObj.getVectorSize()-1])); |
---|
35 | } |
---|
36 | |
---|
37 | static T& GetObject(const string _id) throw (XMLIOUndefinedValueException) { return (ObjectTemplate<T>::CurrListObj[_id]); } |
---|
38 | static bool HasObject(const string _id) { return (ObjectTemplate<T>::CurrListObj.hasMappedValue(_id)); } |
---|
39 | |
---|
40 | static const StrHashMap<T> GetCurrentListObject(void) { return (CurrListObj); } |
---|
41 | |
---|
42 | virtual void registerAllAttributes(void) {}; |
---|
43 | |
---|
44 | virtual const char* getName(void) const {return ("ObjectTemplate"); } |
---|
45 | |
---|
46 | /// POUR TESTS /// |
---|
47 | |
---|
48 | #define OBJECTTEMPLATE_TEST(DESC, SIZE, VECTORSIZE) \ |
---|
49 | if(ObjectTemplate<T>::CurrListObj.getSize() != SIZE or ObjectTemplate<T>::CurrListObj.getVectorSize() != VECTORSIZE) \ |
---|
50 | { log << "Test de la classe ObjectTemplate > " << DESC << "... echec !" << std::endl; return (false); } |
---|
51 | |
---|
52 | static bool CLASS_TEST(ostream& log = std::clog) |
---|
53 | { |
---|
54 | //////////////////////////////////////////////////////////////////// |
---|
55 | T& obj0= ObjectTemplate<T>::CreateObject("toto"); |
---|
56 | T& obj1= ObjectTemplate<T>::CreateObject("tata"); |
---|
57 | OBJECTTEMPLATE_TEST("Ajout d'objets identifiés au conteneur", 2, 2); |
---|
58 | T& obj2= ObjectTemplate<T>::CreateObject("tata"); |
---|
59 | OBJECTTEMPLATE_TEST("Ajout d'objets identifiés déjà existants au conteneur", 2, 2); |
---|
60 | T& obj3= ObjectTemplate<T>::CreateObject(); |
---|
61 | OBJECTTEMPLATE_TEST("Ajout d'objets non-identifiés au conteneur", 2, 3); |
---|
62 | //////////////////////////////////////////////////////////////////// |
---|
63 | |
---|
64 | log << "Test ObjectTemplate ... ok !" << std::endl; |
---|
65 | return (true); |
---|
66 | }; |
---|
67 | #undef OBJECTTEMPLATE_TEST |
---|
68 | |
---|
69 | |
---|
70 | virtual ~ObjectTemplate(void) |
---|
71 | {/* Ne rien faire de plus */} |
---|
72 | |
---|
73 | protected : |
---|
74 | |
---|
75 | ObjectTemplate(void) : AbstractObject() |
---|
76 | {/* Ne rien faire de plus */} |
---|
77 | ObjectTemplate(const string& _id) : AbstractObject(_id) |
---|
78 | {/* Ne rien faire de plus */} |
---|
79 | |
---|
80 | private : |
---|
81 | static StrHashMap<T> CurrListObj; |
---|
82 | |
---|
83 | };// class ObjectTemplate |
---|
84 | |
---|
85 | template <class T> StrHashMap<T> ObjectTemplate<T>::CurrListObj ; |
---|
86 | |
---|
87 | }// namespace XMLIOSERVER |
---|
88 | |
---|
89 | #endif // __XMLIO_OBJECT_TEMPLATE__ |
---|