[90] | 1 | #ifndef OBJECT_TEMPLATE_HPP |
---|
| 2 | #define OBJECT_TEMPLATE_HPP |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | #include "base_object.hpp" |
---|
| 6 | #include "xmlio_std.hpp" |
---|
| 7 | |
---|
| 8 | template <typename CObject> |
---|
| 9 | class CObjectTemplate : public CBase_object |
---|
| 10 | { |
---|
| 11 | public: |
---|
| 12 | |
---|
| 13 | static vector<CObject *> * List ; |
---|
| 14 | static unordered_map<string,CObject *>* listId ; |
---|
| 15 | |
---|
| 16 | CObjectTemplate(void) : CBase_object() {} ; |
---|
| 17 | CObjectTemplate(const string & Id) : CBase_object(Id) {} ; |
---|
| 18 | |
---|
| 19 | static void SwapContext(unordered_map<string,CObject *>* newListId,vector<CObject *> * newList) |
---|
| 20 | { |
---|
| 21 | listId=newListId ; |
---|
| 22 | List=newList ; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | static bool found(const string & Id) |
---|
| 26 | { |
---|
| 27 | if (listId->find(Id)==listId->end()) return false ; |
---|
| 28 | else return true ; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | static CObject* get(const string & Id) |
---|
| 32 | { |
---|
| 33 | typename unordered_map<string,CObject*>::iterator It ; |
---|
| 34 | |
---|
| 35 | It=listId->find(Id) ; |
---|
| 36 | if (It==listId->end()) |
---|
| 37 | { |
---|
| 38 | error("CObjectTemplate<object>::get(const string & Id)")<<"Object <<"<<Id<<">> does not exist"<<endl ; |
---|
| 39 | return 0 ; |
---|
| 40 | } |
---|
| 41 | else return It->second ; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | static CObject* getNew(void) |
---|
| 45 | { |
---|
| 46 | CObject * newObject=new CObject ; |
---|
| 47 | List->push_back(newObject) ; |
---|
| 48 | return newObject ; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | static CObject* getNew(const string & Id) |
---|
| 52 | { |
---|
| 53 | CObject* newField=new CObject(Id) ; |
---|
| 54 | |
---|
| 55 | pair<string,CObject *> value(Id,newField) ; |
---|
| 56 | if (listId->insert(value).second) |
---|
| 57 | { |
---|
| 58 | List->push_back(newField) ; |
---|
| 59 | return newField ; |
---|
| 60 | } |
---|
| 61 | else |
---|
| 62 | { |
---|
| 63 | error("CObjectTemplate::getNew(const string & Id)")<<"Object <<"<<Id<<">> is already present"<<endl ; |
---|
| 64 | return 0 ; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | static CObject* getOrAppend(const string & Id) |
---|
| 69 | { |
---|
| 70 | if (found(Id)) return get(Id) ; |
---|
| 71 | else return getNew(Id) ; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | ostream & Print(ostream& o) |
---|
| 75 | { |
---|
| 76 | o<<"Object : "<<CObject::getName()<<" : " ; |
---|
| 77 | PrintId(o)<<inc_endl ; |
---|
| 78 | PrintAttribut(o) ; |
---|
| 79 | o<<DecIndent ; |
---|
| 80 | return o ; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void Print(void) |
---|
| 84 | { |
---|
| 85 | Print(StdOut) ; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | } ; |
---|
| 90 | |
---|
| 91 | template <typename CObject> |
---|
| 92 | vector<CObject *>* CObjectTemplate<CObject>::List=new vector<CObject*> ; |
---|
| 93 | |
---|
| 94 | template <typename CObject> |
---|
| 95 | unordered_map<string,CObject *>* CObjectTemplate<CObject>::listId=new unordered_map<string,CObject *> ; |
---|
| 96 | #endif |
---|