source: XIOS/dev/branch_yushan/src/object_factory_impl.hpp @ 1102

Last change on this file since 1102 was 1102, checked in by yushan, 7 years ago

clean up

  • 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
16      if(U::AllVectObj == NULL) return 0;
17     
18     
19      return (*U::AllVectObj)[*CObjectFactory::CurrContext_ptr].size();
20   }
21
22   template <typename U>
23      int CObjectFactory::GetObjectIdNum(void)
24   {
25      if (CurrContext_ptr->size() == 0)
26         ERROR("CObjectFactory::GetObjectIdNum(void)",
27               << "please define current context id !");
28      if(U::AllMapObj  == NULL) return 0;
29
30     
31
32      return (* U::AllMapObj) [*CObjectFactory::CurrContext_ptr].size();
33   }
34
35   template <typename U>
36      bool CObjectFactory::HasObject(const StdString & id)
37   {
38      if (CurrContext_ptr->size() == 0)
39         ERROR("CObjectFactory::HasObject(const StdString & id)",
40               << "[ id = " << id << " ] please define current context id !");
41     
42      if(U::AllMapObj  == NULL)  return false;
43
44     
45
46      return ((*U::AllMapObj)[*CObjectFactory::CurrContext_ptr].find(id) !=
47              (*U::AllMapObj)[*CObjectFactory::CurrContext_ptr].end());
48   }
49
50   template <typename U>
51      bool CObjectFactory::HasObject(const StdString & context, const StdString & id)
52   {
53      if(U::AllMapObj  == NULL) return false;
54
55      if (U::AllMapObj->find(context) == U::AllMapObj->end()) return false ;
56
57      else
58      {
59         return ((*U::AllMapObj)[context].find(id) !=  (*U::AllMapObj)[context].end());
60      } 
61         
62   }
63
64   template <typename U>
65      boost::shared_ptr<U> CObjectFactory::GetObject(const U * const object)
66   {
67      if(U::AllVectObj == NULL) return (boost::shared_ptr<U>());
68   
69      if (CurrContext_ptr->size() == 0)
70         ERROR("CObjectFactory::GetObject(const U * const object)",
71               << "please define current context id !");
72      std::vector<boost::shared_ptr<U> > & vect =
73                     (*U::AllVectObj)[*CObjectFactory::CurrContext_ptr];
74
75      typename std::vector<boost::shared_ptr<U> >::const_iterator
76         it = vect.begin(), end = vect.end();
77
78      for (; it != end; it++)
79      {
80         boost::shared_ptr<U> ptr = *it;
81         if (ptr.get() == object)
82            return (ptr);
83      }
84
85      ERROR("CObjectFactory::GetObject(const U * const object)",
86               << "[type = " << U::GetName() << ", adress = " << object << "] "
87               << "object was not found.");
88      return (boost::shared_ptr<U>()); // jamais atteint
89   }
90
91   template <typename U>
92      boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & id)
93   {
94      if(U::AllMapObj  == NULL) return (boost::shared_ptr<U>());
95
96      if (CurrContext_ptr->size() == 0)
97         ERROR("CObjectFactory::GetObject(const StdString & id)",
98               << "[ id = " << id << " ] please define current context id !");
99      if (!CObjectFactory::HasObject<U>(id))
100         ERROR("CObjectFactory::GetObject(const StdString & id)",
101               << "[ id = " << id << ", U = " << U::GetName() << " ] "
102               << "object was not found.");
103      return (*U::AllMapObj)[*CObjectFactory::CurrContext_ptr][id];
104   }
105
106   template <typename U>
107      boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id)
108   {
109      if(U::AllMapObj  == NULL) return (boost::shared_ptr<U>());
110
111      if (!CObjectFactory::HasObject<U>(context,id))
112         ERROR("CObjectFactory::GetObject(const StdString & id)",
113               << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] "
114               << "object was not found.");
115
116      return (*U::AllMapObj)[context][id];
117   }
118
119   template <typename U>
120   boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id)
121   {
122      if(U::AllVectObj == NULL) U::AllVectObj = new xios_map<StdString, std::vector<boost::shared_ptr<U> > >;
123      if(U::AllMapObj  == NULL) U::AllMapObj  = new xios_map<StdString, xios_map<StdString, boost::shared_ptr<U> > >;
124
125      //printf("so far so good : %d %d %d\n", CurrContext_ptr->empty(), CObjectFactory::HasObject<U>(id), id.empty());
126     
127      if (CurrContext_ptr->empty())
128         ERROR("CObjectFactory::CreateObject(const StdString& id)",
129               << "[ id = " << id << " ] please define current context id !");
130
131      if (CObjectFactory::HasObject<U>(id))
132      {
133         return CObjectFactory::GetObject<U>(id);
134      }
135      else
136      {
137         boost::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id));
138         //printf("in CreateObject, value OK\n"); 
139         (* U::AllVectObj)[*CObjectFactory::CurrContext_ptr].insert((*U::AllVectObj)[*CObjectFactory::CurrContext_ptr].end(), value);
140         //printf("AllVectObj insert OK\n");
141         (* U::AllMapObj) [*CObjectFactory::CurrContext_ptr].insert(std::make_pair(value->getId(), value));
142         //printf("AllMapObj insert OK\n");
143
144         return value;
145      }
146   }
147
148   template <typename U>
149      const std::vector<boost::shared_ptr<U> > &
150         CObjectFactory::GetObjectVector(const StdString & context)
151   {
152      if(U::AllVectObj != NULL) 
153     
154
155      return (*U::AllVectObj)[context];
156   }
157
158   template <typename U>
159   const StdString& CObjectFactory::GetUIdBase(void)
160   {
161      static const StdString base = "__" + U::GetName() + "_undef_id_";
162      return base;
163   }
164
165   template <typename U>
166   StdString CObjectFactory::GenUId(void)
167   {
168      StdOStringStream oss;
169      if(U::GenId == NULL) U::GenId = new xios_map< StdString, long int >;
170      oss << GetUIdBase<U>() << (*U::GenId)[*CObjectFactory::CurrContext_ptr]++;
171      return oss.str();
172   }
173
174   template <typename U>
175   bool CObjectFactory::IsGenUId(const StdString& id)
176   {
177      const StdString& base = GetUIdBase<U>();
178      return (id.size() > base.size() && id.compare(0, base.size(), base) == 0);
179   }
180
181} // namespace xios
182
183#endif // __XIOS_CObjectFactory_impl__
Note: See TracBrowser for help on using the repository browser.