source: XIOS/trunk/src/object_factory_impl.hpp @ 756

Last change on this file since 756 was 680, checked in by rlacroix, 9 years ago

Rephrase some error messages so that they are clearer.

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