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

Last change on this file since 1482 was 1460, checked in by yushan, 6 years ago

branch_openmp merged with XIOS_DEV_CMIP6@1459

  • 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: 7.2 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      //return (U::AllVectObj[CObjectFactory::CurrContext].size());
16      if(U::AllVectObj_ptr == NULL) return 0;
17      return (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr].size();
18   }
19
20   template <typename U>
21      int CObjectFactory::GetObjectIdNum(void)
22   {
23      if (CurrContext_ptr->size() == 0)
24         ERROR("CObjectFactory::GetObjectIdNum(void)",
25               << "please define current context id !");
26      //return (U::AllMapObj[CObjectFactory::CurrContext].size());
27      if(U::AllMapObj_ptr == NULL) return 0;
28      return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].size();
29   }
30
31   template <typename U>
32      bool CObjectFactory::HasObject(const StdString & id)
33   {
34      if (CurrContext_ptr->size() == 0)
35         ERROR("CObjectFactory::HasObject(const StdString & id)",
36               << "[ id = " << id << " ] please define current context id !");
37      //return (U::AllMapObj[CObjectFactory::CurrContext].find(id) !=
38      //        U::AllMapObj[CObjectFactory::CurrContext].end());
39      if(U::AllMapObj_ptr  == NULL)  return false;
40      return ((*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].find(id) !=
41              (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr].end());
42
43   }
44
45   template <typename U>
46      bool CObjectFactory::HasObject(const StdString & context, const StdString & id)
47   {
48
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;
52
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());
55   }
56
57   template <typename U>
58      boost::shared_ptr<U> CObjectFactory::GetObject(const U * const object)
59   {
60      if(U::AllVectObj_ptr == NULL) return (boost::shared_ptr<U>());
61      if (CurrContext_ptr->size() == 0)
62         ERROR("CObjectFactory::GetObject(const U * const object)",
63               << "please define current context id !");
64      //std::vector<boost::shared_ptr<U> > & vect = U::AllVectObj[CObjectFactory::CurrContext];
65      std::vector<boost::shared_ptr<U> > & vect = (*U::AllVectObj_ptr)[*CObjectFactory::CurrContext_ptr];
66
67      typename std::vector<boost::shared_ptr<U> >::const_iterator
68         it = vect.begin(), end = vect.end();
69
70      for (; it != end; it++)
71      {
72         boost::shared_ptr<U> ptr = *it;
73         if (ptr.get() == object)
74            return (ptr);
75      }
76
77      ERROR("CObjectFactory::GetObject(const U * const object)",
78               << "[type = " << U::GetName() << ", adress = " << object << "] "
79               << "object was not found.");
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   {
86      if(U::AllMapObj_ptr  == NULL) return (boost::shared_ptr<U>());
87      if (CurrContext_ptr->size() == 0)
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() << " ] "
93               << "object was not found.");
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]);
102      return (*U::AllMapObj_ptr)[*CObjectFactory::CurrContext_ptr][id];
103   }
104
105   template <typename U>
106      boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id)
107   {
108      if(U::AllMapObj_ptr  == NULL) return (boost::shared_ptr<U>());
109
110      if (!CObjectFactory::HasObject<U>(context,id))
111         ERROR("CObjectFactory::GetObject(const StdString & id)",
112               << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] "
113               << "object was not found.");
114
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];
123   }
124
125   template <typename U>
126   boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id)
127   {
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> > >;
130
131      if (CurrContext_ptr->empty())
132         ERROR("CObjectFactory::CreateObject(const StdString& id)",
133               << "[ id = " << id << " ] please define current context id !");
134
135      if (CObjectFactory::HasObject<U>(id))
136      {
137         return CObjectFactory::GetObject<U>(id);
138      }
139      else
140      {
141         boost::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id));
142
143         //U::AllVectObj[CObjectFactory::CurrContext].insert(U::AllVectObj[CObjectFactory::CurrContext].end(), value);
144         //U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(value->getId(), value));
145
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));
148         //cout<<"CObjectFactory::CreateObject(const StdString& id) [ id = " << id << " ]" <<endl;
149
150         return value;
151      }
152   }
153
154   template <typename U>
155      const std::vector<boost::shared_ptr<U> > &
156         CObjectFactory::GetObjectVector(const StdString & context)
157   {
158      //return (U::AllVectObj[context]);
159      return (*U::AllVectObj_ptr)[context];
160   }
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;
173      if(U::GenId_ptr == NULL) U::GenId_ptr = new xios_map< StdString, long int >;
174      oss << GetUIdBase<U>() << (*U::GenId_ptr)[*CObjectFactory::CurrContext_ptr]++;
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
185} // namespace xios
186
187#endif // __XIOS_CObjectFactory_impl__
Note: See TracBrowser for help on using the repository browser.