source: XMLIO_V2/dev/common/src/xmlio/object_factory_impl.hpp @ 286

Last change on this file since 286 was 286, checked in by ymipsl, 13 years ago

reprise en main de la version de H. Ozdoba. Correction de différentes erreurs de conception et bug.
Version NEMO operationnel en client/server, interoperabilita avec OASIS, reconstition de fichiers via netcdf4/HDF5

YM

File size: 5.3 KB
Line 
1#ifndef __XMLIO_CObjectFactory_impl__
2#define __XMLIO_CObjectFactory_impl__
3
4namespace xmlioserver
5{
6   /// ////////////////////// Définitions ////////////////////// ///
7   template <typename U>
8       int CObjectFactory::GetObjectNum(void)
9   {
10      if (CurrContext.size() == 0)
11         ERROR("CObjectFactory::GetObjectNum(void)",
12               << "please define current context id !");
13      return (U::AllVectObj[CObjectFactory::CurrContext].size());
14   }
15
16   template <typename U>
17      int CObjectFactory::GetObjectIdNum(void)
18   {
19      if (CurrContext.size() == 0)
20         ERROR("CObjectFactory::GetObjectIdNum(void)",
21               << "please define current context id !");
22      return (U::AllMapObj[CObjectFactory::CurrContext].size());
23   }
24
25   template <typename U>
26      bool CObjectFactory::HasObject(const StdString & id)
27   {
28      if (CurrContext.size() == 0)
29         ERROR("CObjectFactory::HasObject(const StdString & id)",
30               << "[ id = " << id << " ] please define current context id !");
31      return (U::AllMapObj[CObjectFactory::CurrContext].find(id) !=
32              U::AllMapObj[CObjectFactory::CurrContext].end());
33   }
34
35   template <typename U>
36      bool CObjectFactory::HasObject(const StdString & context, const StdString & id)
37   {
38      if (U::AllMapObj.find(context) == U::AllMapObj.end()) return false ;
39      else return (U::AllMapObj[context].find(id) !=  U::AllMapObj[context].end());
40   }
41
42   template <typename U>
43      boost::shared_ptr<U> CObjectFactory::GetObject(const U * const object)
44   {
45      if (CurrContext.size() == 0)
46         ERROR("CObjectFactory::GetObject(const U * const object)",
47               << "please define current context id !");
48      std::vector<boost::shared_ptr<U> > & vect =
49                     U::AllVectObj[CObjectFactory::CurrContext];
50                     
51      typename std::vector<boost::shared_ptr<U> >::const_iterator
52         it = vect.begin(), end = vect.end();
53     
54      for (; it != end; it++)
55      {
56         boost::shared_ptr<U> ptr = *it;
57         if (ptr.get() == object)
58            return (ptr);
59      }
60     
61      ERROR("CObjectFactory::GetObject(const U * const object)",
62               << "[type = " << U::GetName() << ", "
63               << "adress = " << object << "]"
64               << " object was not found !");
65      return (boost::shared_ptr<U>()); // jamais atteint
66   }
67
68   template <typename U>
69      boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & id)
70   {
71      if (CurrContext.size() == 0)
72         ERROR("CObjectFactory::GetObject(const StdString & id)",
73               << "[ id = " << id << " ] please define current context id !");
74      if (!CObjectFactory::HasObject<U>(id))
75         ERROR("CObjectFactory::GetObject(const StdString & id)",
76               << "[ id = " << id << ", U = " << U::GetName() << " ] "
77               << " object is not referenced !");
78      return (U::AllMapObj[CObjectFactory::CurrContext][id]);
79   }
80
81   template <typename U>
82      boost::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id)
83   {
84      if (!CObjectFactory::HasObject<U>(context,id))
85         ERROR("CObjectFactory::GetObject(const StdString & id)",
86               << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] "
87               << " object is not referenced !");
88      return (U::AllMapObj[CObjectFactory::CurrContext][id]);
89   }
90
91   template <typename U>
92      boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString & id)
93   {
94      if (CurrContext.size() == 0)
95         ERROR("CObjectFactory::CreateObject(const StdString & id)",
96               << "[ id = " << id << " ] please define current context id !");
97      if (id.size() == 0)
98      {
99         boost::shared_ptr<U> value(new U(CObjectFactory::GenUId<U>()));
100         U::AllVectObj[CObjectFactory::CurrContext].insert
101            (U::AllVectObj[CObjectFactory::CurrContext].end(), value);
102         U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(value->getId(), value));
103         return (value);
104      }
105      else if (CObjectFactory::HasObject<U>(id))
106      {
107         return (CObjectFactory::GetObject<U>(id));
108      }
109      else
110      {
111         boost::shared_ptr<U> value(new U(id));
112         U::AllVectObj[CObjectFactory::CurrContext].insert
113            (U::AllVectObj[CObjectFactory::CurrContext].end(), value);
114         U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(id, value));
115         return (value);
116      }
117   }
118
119   template <typename U>
120      const std::vector<boost::shared_ptr<U> > &
121         CObjectFactory::GetObjectVector(const StdString & context)
122   {
123      return (U::AllVectObj[context]);
124   }
125   
126   template <typename U> 
127       StdString CObjectFactory::GenUId(void)
128       {
129          long int seed ;
130         
131          xios_map<StdString,long int>::iterator it ;
132          it=U::GenId.find(CObjectFactory::CurrContext);
133          if (it==U::GenId.end())
134          {
135            seed=0 ;
136            U::GenId[CObjectFactory::CurrContext]=seed ;
137          }
138          else
139          {
140            seed=it->second ;
141            seed++ ;
142            it->second=seed ;
143          }
144         
145          StdOStringStream oss ;
146          oss<<"__"<<U::GetName()<<"_undef_id_"<<seed ;
147          return StdString(oss.str()) ;
148        }
149         
150} // namespace xmlioserver
151
152#endif // __XMLIO_CObjectFactory_impl__
Note: See TracBrowser for help on using the repository browser.