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