#ifndef __XIOS_CObjectFactory_impl__ #define __XIOS_CObjectFactory_impl__ #include "object_factory.hpp" namespace xios { /// ////////////////////// Définitions ////////////////////// /// template int CObjectFactory::GetObjectNum(void) { if (CurrContext_ptr->size() == 0) ERROR("CObjectFactory::GetObjectNum(void)", << "please define current context id !"); if(U::AllVectObj_ptr == NULL) return 0; return (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].size(); } template int CObjectFactory::GetObjectIdNum(void) { if (CurrContext_ptr->size() == 0) ERROR("CObjectFactory::GetObjectIdNum(void)", << "please define current context id !"); if(U::AllMapObj_ptr == NULL) return 0; return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].size(); } template bool CObjectFactory::HasObject(const StdString & id) { if (CurrContext_ptr->size() == 0) ERROR("CObjectFactory::HasObject(const StdString & id)", << "[ id = " << id << " ] please define current context id !"); if(U::AllMapObj_ptr == NULL) return false; return ((*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].find(id) != (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].end()); } template bool CObjectFactory::HasObject(const StdString & context, const StdString & id) { if(U::AllMapObj_ptr == NULL) return false; if (U::AllMapObj_ptr->find(context) == U::AllMapObj_ptr->end()) return false ; else return ((*U::AllMapObj_ptr)[context].find(id) != (*U::AllMapObj_ptr)[context].end()); } template std::shared_ptr CObjectFactory::GetObject(const U * const object) { if(U::AllVectObj_ptr == NULL) return (std::shared_ptr()); if (CurrContext_ptr->size() == 0) ERROR("CObjectFactory::GetObject(const U * const object)", << "please define current context id !"); std::vector > & vect = (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr]; typename std::vector >::const_iterator it = vect.begin(), end = vect.end(); for (; it != end; it++) { std::shared_ptr ptr = *it; if (ptr.get() == object) return (ptr); } ERROR("CObjectFactory::GetObject(const U * const object)", << "[type = " << U::GetName() << ", adress = " << object << "] " << "object was not found."); return (std::shared_ptr()); // jamais atteint } template std::shared_ptr CObjectFactory::GetObject(const StdString & id) { if(U::AllMapObj_ptr == NULL) return (std::shared_ptr()); if (CurrContext_ptr->size() == 0) ERROR("CObjectFactory::GetObject(const StdString & id)", << "[ id = " << id << " ] please define current context id !"); if (!CObjectFactory::HasObject(id)) ERROR("CObjectFactory::GetObject(const StdString & id)", << "[ id = " << id << ", U = " << U::GetName() << " ] " << "object was not found."); return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr][id]; } template std::shared_ptr CObjectFactory::GetObject(const StdString & context, const StdString & id) { if(U::AllMapObj_ptr == NULL) return (std::shared_ptr()); if (!CObjectFactory::HasObject(context,id)) ERROR("CObjectFactory::GetObject(const StdString & id)", << "[ id = " << id << ", U = " << U::GetName() <<", context = "< std::shared_ptr CObjectFactory::CreateObject(const StdString& id) { if(U::AllVectObj_ptr == NULL) U::AllVectObj_ptr = new xios_map > >; if(U::AllMapObj_ptr == NULL) U::AllMapObj_ptr = new xios_map > >; if (CurrContext_ptr->empty()) ERROR("CObjectFactory::CreateObject(const StdString& id)", << "[ id = " << id << " ] please define current context id !"); if (CObjectFactory::HasObject(id)) { return CObjectFactory::GetObject(id); } else { std::shared_ptr value(new U(id.empty() ? CObjectFactory::GenUId() : id)); (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].insert((*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].end(), value); (*U::AllMapObj_ptr) [*CObjectFactory::CurrContext_ptr].insert(std::make_pair(value->getId(), value)); return value; } } template int CObjectFactory::CheckObjectVector() { if(U::AllVectObj_ptr) { return 1; } return 0; } template const std::vector > & CObjectFactory::GetObjectVector(const StdString & context) { if(U::AllVectObj_ptr) { //const std::vector > temp; return (*U::AllVectObj_ptr)[context]; //return std::vector > (0); } } template const StdString& CObjectFactory::GetUIdBase(void) { static const StdString base = "__" + U::GetName() + "_undef_id_"; return base; } template StdString CObjectFactory::GenUId(void) { StdOStringStream oss; if(U::GenId_ptr == NULL) U::GenId_ptr = new xios_map< StdString, long int >; oss << GetUIdBase() << (*U::GenId_ptr)[*CObjectFactory::CurrContext_ptr]++; return oss.str(); } template bool CObjectFactory::IsGenUId(const StdString& id) { const StdString& base = GetUIdBase(); return (id.size() > base.size() && id.compare(0, base.size(), base) == 0); } } // namespace xios #endif // __XIOS_CObjectFactory_impl__