source: XIOS/dev/dev_trunk_omp/src/object_factory_impl.hpp @ 1628

Last change on this file since 1628 was 1628, checked in by yushan, 5 years ago

bug fix (Nb of files less than Nb of servers)

  • 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: 6.0 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{
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        //const std::vector<std::shared_ptr<U> > temp;
144        return (*U::AllVectObj_ptr)[context];
145        //return std::vector<std::shared_ptr<U> > (0);
146      }
147     
[219]148   }
[769]149
150   template <typename U>
151   const StdString& CObjectFactory::GetUIdBase(void)
152   {
153      static const StdString base = "__" + U::GetName() + "_undef_id_";
154      return base;
155   }
156
157   template <typename U>
158   StdString CObjectFactory::GenUId(void)
159   {
160      StdOStringStream oss;
[1601]161      if(U::GenId_ptr == NULL) U::GenId_ptr = new xios_map< StdString, long int >;
162      oss << GetUIdBase<U>() << (*U::GenId_ptr)[*CObjectFactory::CurrContext_ptr]++;
[769]163      return oss.str();
164   }
165
166   template <typename U>
167   bool CObjectFactory::IsGenUId(const StdString& id)
168   {
169      const StdString& base = GetUIdBase<U>();
170      return (id.size() > base.size() && id.compare(0, base.size(), base) == 0);
171   }
172
[335]173} // namespace xios
[219]174
[591]175#endif // __XIOS_CObjectFactory_impl__
Note: See TracBrowser for help on using the repository browser.