source: XIOS/dev/branch_openmp/src/object_factory_impl.hpp @ 1552

Last change on this file since 1552 was 1545, checked in by yushan, 6 years ago

branch_openmp merged with trunk r1544

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 5.7 KB
RevLine 
[591]1#ifndef __XIOS_CObjectFactory_impl__
2#define __XIOS_CObjectFactory_impl__
[219]3
[352]4#include "object_factory.hpp"
5
[335]6namespace 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      if(U::AllVectObj_ptr == NULL) return 0;
[1334]16      return (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].size();
[219]17   }
18
19   template <typename U>
20      int CObjectFactory::GetObjectIdNum(void)
21   {
[1334]22      if (CurrContext_ptr->size() == 0)
[219]23         ERROR("CObjectFactory::GetObjectIdNum(void)",
24               << "please define current context id !");
[1328]25      if(U::AllMapObj_ptr == NULL) return 0;
[1334]26      return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].size();
[219]27   }
28
29   template <typename U>
30      bool CObjectFactory::HasObject(const StdString & id)
31   {
[1334]32      if (CurrContext_ptr->size() == 0)
[219]33         ERROR("CObjectFactory::HasObject(const StdString & id)",
34               << "[ id = " << id << " ] please define current context id !");
[1328]35      if(U::AllMapObj_ptr  == NULL)  return false;
[1334]36      return ((*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].find(id) !=
37              (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].end());
[1134]38
[219]39   }
40
41   template <typename U>
[286]42      bool CObjectFactory::HasObject(const StdString & context, const StdString & id)
43   {
[1328]44      if(U::AllMapObj_ptr  == NULL) return false;
[1134]45
[1328]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>
[1545]51      std::shared_ptr<U> CObjectFactory::GetObject(const U * const object)
[219]52   {
[1545]53      if(U::AllVectObj_ptr == NULL) return (std::shared_ptr<U>());
[1334]54      if (CurrContext_ptr->size() == 0)
[219]55         ERROR("CObjectFactory::GetObject(const U * const object)",
56               << "please define current context id !");
[1545]57      std::vector<std::shared_ptr<U> > & vect = (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr];
[769]58
[1545]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      {
[1545]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.");
[1545]72      return (std::shared_ptr<U>()); // jamais atteint
[219]73   }
74
75   template <typename U>
[1545]76      std::shared_ptr<U> CObjectFactory::GetObject(const StdString & id)
[219]77   {
[1545]78      if(U::AllMapObj_ptr  == NULL) return (std::shared_ptr<U>());
[1334]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.");
[1334]86      return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr][id];
[219]87   }
88
89   template <typename U>
[1545]90      std::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id)
[286]91   {
[1545]92      if(U::AllMapObj_ptr  == NULL) return (std::shared_ptr<U>());
[1134]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.");
[1328]98      return (*U::AllMapObj_ptr)[context][id];
[286]99   }
100
101   template <typename U>
[1545]102   std::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id)
[219]103   {
[1545]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> > >;
[1134]106
[1334]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      {
[1545]117         std::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id));
[769]118
[1334]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));
[1328]121
[769]122         return value;
[219]123      }
124   }
125
126   template <typename U>
[1545]127      const std::vector<std::shared_ptr<U> > &
[219]128         CObjectFactory::GetObjectVector(const StdString & context)
129   {
[1328]130      return (*U::AllVectObj_ptr)[context];
[219]131   }
[769]132
133   template <typename U>
134   const StdString& CObjectFactory::GetUIdBase(void)
135   {
136      static const StdString base = "__" + U::GetName() + "_undef_id_";
137      return base;
138   }
139
140   template <typename U>
141   StdString CObjectFactory::GenUId(void)
142   {
143      StdOStringStream oss;
[1328]144      if(U::GenId_ptr == NULL) U::GenId_ptr = new xios_map< StdString, long int >;
[1334]145      oss << GetUIdBase<U>() << (*U::GenId_ptr)[*CObjectFactory::CurrContext_ptr]++;
[769]146      return oss.str();
147   }
148
149   template <typename U>
150   bool CObjectFactory::IsGenUId(const StdString& id)
151   {
152      const StdString& base = GetUIdBase<U>();
153      return (id.size() > base.size() && id.compare(0, base.size(), base) == 0);
154   }
155
[335]156} // namespace xios
[219]157
[591]158#endif // __XIOS_CObjectFactory_impl__
Note: See TracBrowser for help on using the repository browser.