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