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
Line 
1#ifndef __XIOS_CObjectFactory_impl__
2#define __XIOS_CObjectFactory_impl__
3
4#include "object_factory.hpp"
5
6namespace xios
7{
8   /// ////////////////////// Définitions ////////////////////// ///
9   template <typename U>
10       int CObjectFactory::GetObjectNum(void)
11   {
12      if (CurrContext_ptr->size() == 0)
13         ERROR("CObjectFactory::GetObjectNum(void)",
14               << "please define current context id !");
15      if(U::AllVectObj_ptr == NULL) return 0;
16      return (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].size();
17   }
18
19   template <typename U>
20      int CObjectFactory::GetObjectIdNum(void)
21   {
22      if (CurrContext_ptr->size() == 0)
23         ERROR("CObjectFactory::GetObjectIdNum(void)",
24               << "please define current context id !");
25      if(U::AllMapObj_ptr == NULL) return 0;
26      return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].size();
27   }
28
29   template <typename U>
30      bool CObjectFactory::HasObject(const StdString & id)
31   {
32      if (CurrContext_ptr->size() == 0)
33         ERROR("CObjectFactory::HasObject(const StdString & id)",
34               << "[ id = " << id << " ] please define current context id !");
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
39   }
40
41   template <typename U>
42      bool CObjectFactory::HasObject(const StdString & context, const StdString & id)
43   {
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());
48   }
49
50   template <typename U>
51      std::shared_ptr<U> CObjectFactory::GetObject(const U * const object)
52   {
53      if(U::AllVectObj_ptr == NULL) return (std::shared_ptr<U>());
54      if (CurrContext_ptr->size() == 0)
55         ERROR("CObjectFactory::GetObject(const U * const object)",
56               << "please define current context id !");
57      std::vector<std::shared_ptr<U> > & vect = (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr];
58
59      typename std::vector<std::shared_ptr<U> >::const_iterator
60         it = vect.begin(), end = vect.end();
61
62      for (; it != end; it++)
63      {
64         std::shared_ptr<U> ptr = *it;
65         if (ptr.get() == object)
66            return (ptr);
67      }
68
69      ERROR("CObjectFactory::GetObject(const U * const object)",
70               << "[type = " << U::GetName() << ", adress = " << object << "] "
71               << "object was not found.");
72      return (std::shared_ptr<U>()); // jamais atteint
73   }
74
75   template <typename U>
76      std::shared_ptr<U> CObjectFactory::GetObject(const StdString & id)
77   {
78      if(U::AllMapObj_ptr  == NULL) return (std::shared_ptr<U>());
79      if (CurrContext_ptr->size() == 0)
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() << " ] "
85               << "object was not found.");
86      return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr][id];
87   }
88
89   template <typename U>
90      std::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id)
91   {
92      if(U::AllMapObj_ptr  == NULL) return (std::shared_ptr<U>());
93
94      if (!CObjectFactory::HasObject<U>(context,id))
95         ERROR("CObjectFactory::GetObject(const StdString & id)",
96               << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] "
97               << "object was not found.");
98      return (*U::AllMapObj_ptr)[context][id];
99   }
100
101   template <typename U>
102   std::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id)
103   {
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())
108         ERROR("CObjectFactory::CreateObject(const StdString& id)",
109               << "[ id = " << id << " ] please define current context id !");
110
111      if (CObjectFactory::HasObject<U>(id))
112      {
113         return CObjectFactory::GetObject<U>(id);
114      }
115      else
116      {
117         std::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id));
118
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));
121
122         return value;
123      }
124   }
125
126   template <typename U>
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> > &
139         CObjectFactory::GetObjectVector(const StdString & context)
140   {
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     
148   }
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;
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]++;
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
173} // namespace xios
174
175#endif // __XIOS_CObjectFactory_impl__
Note: See TracBrowser for help on using the repository browser.