[591] | 1 | #ifndef __XIOS_CObjectFactory_impl__ |
---|
| 2 | #define __XIOS_CObjectFactory_impl__ |
---|
[219] | 3 | |
---|
[352] | 4 | #include "object_factory.hpp" |
---|
| 5 | |
---|
[335] | 6 | namespace xios |
---|
[219] | 7 | { |
---|
| 8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 9 | template <typename U> |
---|
| 10 | int CObjectFactory::GetObjectNum(void) |
---|
| 11 | { |
---|
[1601] | 12 | if (CurrContext_ptr->size() == 0) |
---|
[219] | 13 | ERROR("CObjectFactory::GetObjectNum(void)", |
---|
| 14 | << "please define current context id !"); |
---|
[1601] | 15 | if(U::AllVectObj_ptr == NULL) return 0; |
---|
| 16 | return (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].size(); |
---|
[219] | 17 | } |
---|
| 18 | |
---|
| 19 | template <typename U> |
---|
| 20 | int CObjectFactory::GetObjectIdNum(void) |
---|
| 21 | { |
---|
[1601] | 22 | if (CurrContext_ptr->size() == 0) |
---|
[219] | 23 | ERROR("CObjectFactory::GetObjectIdNum(void)", |
---|
| 24 | << "please define current context id !"); |
---|
[1601] | 25 | if(U::AllMapObj_ptr == NULL) return 0; |
---|
| 26 | return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].size(); |
---|
[219] | 27 | } |
---|
| 28 | |
---|
| 29 | template <typename U> |
---|
| 30 | bool CObjectFactory::HasObject(const StdString & id) |
---|
| 31 | { |
---|
[1601] | 32 | if (CurrContext_ptr->size() == 0) |
---|
[219] | 33 | ERROR("CObjectFactory::HasObject(const StdString & id)", |
---|
| 34 | << "[ id = " << id << " ] please define current context id !"); |
---|
[1601] | 35 | if(U::AllMapObj_ptr == NULL) return false; |
---|
| 36 | return ((*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].find(id) != |
---|
| 37 | (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].end()); |
---|
| 38 | |
---|
[219] | 39 | } |
---|
| 40 | |
---|
| 41 | template <typename U> |
---|
[286] | 42 | bool CObjectFactory::HasObject(const StdString & context, const StdString & id) |
---|
| 43 | { |
---|
[1601] | 44 | if(U::AllMapObj_ptr == NULL) return false; |
---|
| 45 | |
---|
| 46 | if (U::AllMapObj_ptr->find(context) == U::AllMapObj_ptr->end()) return false ; |
---|
| 47 | else return ((*U::AllMapObj_ptr)[context].find(id) != (*U::AllMapObj_ptr)[context].end()); |
---|
[286] | 48 | } |
---|
| 49 | |
---|
| 50 | template <typename U> |
---|
[1542] | 51 | std::shared_ptr<U> CObjectFactory::GetObject(const U * const object) |
---|
[219] | 52 | { |
---|
[1601] | 53 | if(U::AllVectObj_ptr == NULL) return (std::shared_ptr<U>()); |
---|
| 54 | if (CurrContext_ptr->size() == 0) |
---|
[219] | 55 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
| 56 | << "please define current context id !"); |
---|
[1601] | 57 | std::vector<std::shared_ptr<U> > & vect = (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr]; |
---|
[769] | 58 | |
---|
[1542] | 59 | typename std::vector<std::shared_ptr<U> >::const_iterator |
---|
[219] | 60 | it = vect.begin(), end = vect.end(); |
---|
[769] | 61 | |
---|
[219] | 62 | for (; it != end; it++) |
---|
| 63 | { |
---|
[1542] | 64 | std::shared_ptr<U> ptr = *it; |
---|
[219] | 65 | if (ptr.get() == object) |
---|
| 66 | return (ptr); |
---|
| 67 | } |
---|
[769] | 68 | |
---|
[219] | 69 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
[680] | 70 | << "[type = " << U::GetName() << ", adress = " << object << "] " |
---|
| 71 | << "object was not found."); |
---|
[1542] | 72 | return (std::shared_ptr<U>()); // jamais atteint |
---|
[219] | 73 | } |
---|
| 74 | |
---|
| 75 | template <typename U> |
---|
[1542] | 76 | std::shared_ptr<U> CObjectFactory::GetObject(const StdString & id) |
---|
[219] | 77 | { |
---|
[1601] | 78 | if(U::AllMapObj_ptr == NULL) return (std::shared_ptr<U>()); |
---|
| 79 | if (CurrContext_ptr->size() == 0) |
---|
[219] | 80 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
| 81 | << "[ id = " << id << " ] please define current context id !"); |
---|
| 82 | if (!CObjectFactory::HasObject<U>(id)) |
---|
| 83 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
| 84 | << "[ id = " << id << ", U = " << U::GetName() << " ] " |
---|
[680] | 85 | << "object was not found."); |
---|
[1601] | 86 | return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr][id]; |
---|
[219] | 87 | } |
---|
| 88 | |
---|
| 89 | template <typename U> |
---|
[1542] | 90 | std::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id) |
---|
[286] | 91 | { |
---|
[1601] | 92 | if(U::AllMapObj_ptr == NULL) return (std::shared_ptr<U>()); |
---|
| 93 | |
---|
[286] | 94 | if (!CObjectFactory::HasObject<U>(context,id)) |
---|
| 95 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
| 96 | << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] " |
---|
[680] | 97 | << "object was not found."); |
---|
[1601] | 98 | return (*U::AllMapObj_ptr)[context][id]; |
---|
[286] | 99 | } |
---|
| 100 | |
---|
| 101 | template <typename U> |
---|
[1542] | 102 | std::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id) |
---|
[219] | 103 | { |
---|
[1601] | 104 | if(U::AllVectObj_ptr == NULL) U::AllVectObj_ptr = new xios_map<StdString, std::vector<std::shared_ptr<U> > >; |
---|
| 105 | if(U::AllMapObj_ptr == NULL) U::AllMapObj_ptr = new xios_map<StdString, xios_map<StdString, std::shared_ptr<U> > >; |
---|
| 106 | |
---|
| 107 | if (CurrContext_ptr->empty()) |
---|
[769] | 108 | ERROR("CObjectFactory::CreateObject(const StdString& id)", |
---|
[219] | 109 | << "[ id = " << id << " ] please define current context id !"); |
---|
[769] | 110 | |
---|
| 111 | if (CObjectFactory::HasObject<U>(id)) |
---|
[219] | 112 | { |
---|
[769] | 113 | return CObjectFactory::GetObject<U>(id); |
---|
[219] | 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
[1542] | 117 | std::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id)); |
---|
[769] | 118 | |
---|
[1601] | 119 | (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].insert((*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].end(), value); |
---|
| 120 | (*U::AllMapObj_ptr) [*CObjectFactory::CurrContext_ptr].insert(std::make_pair(value->getId(), value)); |
---|
[769] | 121 | |
---|
| 122 | return value; |
---|
[219] | 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | template <typename U> |
---|
[1628] | 127 | int CObjectFactory::CheckObjectVector() |
---|
| 128 | { |
---|
| 129 | if(U::AllVectObj_ptr) |
---|
| 130 | { |
---|
| 131 | return 1; |
---|
| 132 | } |
---|
| 133 | return 0; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | template <typename U> |
---|
| 138 | const std::vector<std::shared_ptr<U> > & |
---|
[219] | 139 | CObjectFactory::GetObjectVector(const StdString & context) |
---|
| 140 | { |
---|
[1628] | 141 | if(U::AllVectObj_ptr) |
---|
| 142 | { |
---|
| 143 | return (*U::AllVectObj_ptr)[context]; |
---|
| 144 | } |
---|
| 145 | |
---|
[219] | 146 | } |
---|
[769] | 147 | |
---|
| 148 | template <typename U> |
---|
| 149 | const StdString& CObjectFactory::GetUIdBase(void) |
---|
| 150 | { |
---|
| 151 | static const StdString base = "__" + U::GetName() + "_undef_id_"; |
---|
| 152 | return base; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | template <typename U> |
---|
| 156 | StdString CObjectFactory::GenUId(void) |
---|
| 157 | { |
---|
| 158 | StdOStringStream oss; |
---|
[1601] | 159 | if(U::GenId_ptr == NULL) U::GenId_ptr = new xios_map< StdString, long int >; |
---|
| 160 | oss << GetUIdBase<U>() << (*U::GenId_ptr)[*CObjectFactory::CurrContext_ptr]++; |
---|
[769] | 161 | return oss.str(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | template <typename U> |
---|
| 165 | bool CObjectFactory::IsGenUId(const StdString& id) |
---|
| 166 | { |
---|
| 167 | const StdString& base = GetUIdBase<U>(); |
---|
| 168 | return (id.size() > base.size() && id.compare(0, base.size(), base) == 0); |
---|
| 169 | } |
---|
| 170 | |
---|
[335] | 171 | } // namespace xios |
---|
[219] | 172 | |
---|
[591] | 173 | #endif // __XIOS_CObjectFactory_impl__ |
---|