New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
attribute_template_impl.hpp in vendors/XIOS/current/src – NEMO

source: vendors/XIOS/current/src/attribute_template_impl.hpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1#ifndef __XMLIO_CAttributeTemplate_impl__
2#define __XMLIO_CAttributeTemplate_impl__
3
4#include "array.hpp"
5#include "type.hpp"
6#include "buffer_in.hpp"
7#include "buffer_out.hpp"
8#include "generate_interface_impl.hpp"
9
10
11 
12namespace xios
13{
14
15      /// ////////////////////// Définitions ////////////////////// ///
16      template <class T>
17         CAttributeTemplate<T>::CAttributeTemplate(const StdString & id)
18         : CAttribute(id)
19      { /* Ne rien faire de plus */ }
20
21      template <class T>
22         CAttributeTemplate<T>::CAttributeTemplate(const StdString & id, const T & value)
23         : CAttribute(id)
24      {
25         this->setValue(value);
26      }
27
28      template <class T>
29         CAttributeTemplate<T>::CAttributeTemplate(const CAttribute & attribut)
30         throw (CException)
31         : CAttribute(attribut)
32      {
33         if (!attribut.isEmpty() && !attribut.isType<T>())
34            ERROR("CAttributeTemplate", << "Invalid instantiation !");
35      }
36
37      template <class T>
38         CAttributeTemplate<T>::CAttributeTemplate(const StdString & id,
39                              xios_map<StdString, CAttribute*> & umap)
40         : CAttribute(id)
41      {
42         umap.insert(umap.end(), std::make_pair(id, this));
43      }
44
45      template <class T>
46         CAttributeTemplate<T>::CAttributeTemplate
47            (const StdString & id, const T & value,
48             xios_map<StdString, CAttribute*> & umap)
49         : CAttribute(id)
50      {
51         this->setValue(value);
52         umap.insert(umap.end(), std::make_pair(id, this));
53      }
54
55      template <class T>
56         CAttributeTemplate<T>::~CAttributeTemplate(void)
57      { 
58         this->clear();
59      }
60
61      ///--------------------------------------------------------------
62
63      template <class T>
64         T CAttributeTemplate<T>::getValue(void) const
65      {
66         if (SuperClass::isEmpty())
67         {
68            ERROR("T CAttributeTemplate<T>::getValue(void) const",
69                  << "[ id = " << this->getId() << "]"
70                  << " L'attribut est requis mais n'est pas défini !");
71          }
72         return (SuperClass::getValue<T>());
73      }
74
75      template <class T>
76         T* CAttributeTemplate<T>::getRef(void)
77      {
78         if (SuperClass::isEmpty())
79         {
80            ERROR("T CAttributeTemplate<T>::getValue(void) const",
81                  << "[ id = " << this->getId() << "]"
82                  << " L'attribut est requis mais n'est pas défini !");
83          }
84         return (SuperClass::getRef<T>());
85      }
86
87      template <class T>
88         void CAttributeTemplate<T>::setValue(const T & value)
89      {
90         SuperClass::setValue<T>(value);
91      }
92
93      //---------------------------------------------------------------
94
95      template <class T>
96         T CAttributeTemplate<T>::operator=(const T & value)
97      {
98         this->setValue(value);
99         return (this->getValue());
100      }
101
102      //---------------------------------------------------------------
103
104      template <class T>
105         StdString CAttributeTemplate<T>::toString(void) const
106      {
107         StdOStringStream oss;
108         if (!this->isEmpty() && this->hasId())
109            oss << this->getName() << "=\"" << this->getValue() << "\"";
110         return (oss.str());
111      }
112
113      template <class T>
114         void CAttributeTemplate<T>::fromString(const StdString & str)
115      {
116         ERROR("CAttributeTemplate<T>::fromString(const StdString & str)",
117               << "[ str = " << str << " ] Not implemented yet !");
118      }
119
120      //---------------------------------------------------------------
121
122      template <class T>
123         void CAttributeTemplate<T>::toBinary (StdOStream & os) const
124      {
125         this->getValue()->toBinary(os);
126      }
127
128      template <class T>
129         void CAttributeTemplate<T>::fromBinary(StdIStream & is)
130      {
131         T value;
132         FromBinary(is, value);
133         this->setValue(value);
134      }
135
136      template <class T>
137         bool CAttributeTemplate<T>::toBuffer (CBufferOut& buffer) const
138      {
139         if (isEmpty()) return buffer.put(true) ;
140         else
141         {
142           bool ret=true ;
143           CType<T> val(*boost::any_cast<T>(&value)) ;
144           ret&=buffer.put(false) ;
145           ret&=val.toBuffer(buffer) ;
146           return ret ;
147         }
148      }
149
150      template <class T>
151      bool CAttributeTemplate<T>::fromBuffer(CBufferIn& buffer)
152      {
153        bool empty ;
154        bool ret=true ;
155        ret&=buffer.get(empty) ;
156        if (empty) 
157        {
158          clear() ;
159          return ret ;
160        }
161        else
162        {
163          if (isEmpty())
164          {
165            T val ;
166            setValue(val) ;
167          }
168          T* V=const_cast<T*>(boost::any_cast<T>(&value)) ;
169          CType<T> val(*V) ;
170          return val.fromBuffer(buffer) ;
171        }
172      }
173
174      template <class T>
175      size_t CAttributeTemplate<T>::size(void) const
176      {
177        if (isEmpty()) return sizeof(bool) ;
178        else
179        {
180          CType<T> val(*const_cast<T*>(boost::any_cast<T>(&value))) ;
181          return val.size()+sizeof(bool) ;
182        }
183      }
184
185      template <typename T>
186      void CAttributeTemplate<T>::generateCInterface(ostream& oss,const string& className)
187      {
188        CInterface::AttributeCInterface<T>(oss, className, this->getName()) ;
189      }
190     
191      template <typename T>
192      void CAttributeTemplate<T>::generateFortran2003Interface(ostream& oss,const string& className)
193      {
194        CInterface::AttributeFortran2003Interface<T>(oss, className, this->getName()) ;
195      }
196     
197      template <typename T>
198      void CAttributeTemplate<T>::generateFortranInterfaceDeclaration_(ostream& oss,const string& className)
199      {
200        CInterface::AttributeFortranInterfaceDeclaration<T>(oss, className, this->getName()+"_") ;
201      }
202 
203      template <typename T>
204      void CAttributeTemplate<T>::generateFortranInterfaceBody_(ostream& oss,const string& className)
205      {
206        CInterface::AttributeFortranInterfaceBody<T>(oss, className, this->getName()) ;
207      }
208
209      template <typename T>
210      void CAttributeTemplate<T>::generateFortranInterfaceDeclaration(ostream& oss,const string& className)
211      {
212        CInterface::AttributeFortranInterfaceDeclaration<T>(oss, className, this->getName()) ;
213      }
214     
215      template <typename T>
216      void CAttributeTemplate<T>::generateFortranInterfaceGetDeclaration_(ostream& oss,const string& className)
217      {
218        CInterface::AttributeFortranInterfaceGetDeclaration<T>(oss, className, this->getName()+"_") ;
219      }
220 
221      template <typename T>
222      void CAttributeTemplate<T>::generateFortranInterfaceGetBody_(ostream& oss,const string& className)
223      {
224        CInterface::AttributeFortranInterfaceGetBody<T>(oss, className, this->getName()) ;
225      }
226
227      template <typename T>
228      void CAttributeTemplate<T>::generateFortranInterfaceGetDeclaration(ostream& oss,const string& className)
229      {
230        CInterface::AttributeFortranInterfaceGetDeclaration<T>(oss, className, this->getName()) ;
231      }
232 
233     
234      //---------------------------------------------------------------
235
236      /** Spécialisations des templates pour la fonction [toString] **/
237
238      template <>
239         StdString CAttributeTemplate<bool>::toString(void) const;
240
241      //---------------------------------------------------------------
242
243      /** Spécialisations des templates pour la fonction [fromString] **/
244
245      template <> // Chaîne de caractères.
246         void CAttributeTemplate<StdString>::fromString(const StdString & str);
247
248      template <> // Entier
249         void CAttributeTemplate<int>::fromString(const StdString & str);
250
251      template <> // Booléen
252         void CAttributeTemplate<bool>::fromString(const StdString & str);
253
254      template <> // Double
255         void CAttributeTemplate<double>::fromString(const StdString & str);
256
257      template<> // Tableau
258         void CAttributeTemplate<ARRAY(double, 1)>::fromString(const StdString & str);
259
260      //---------------------------------------------------------------
261
262      /** Spécialisations des templates pour la fonction [toBinary] **/
263
264      template <> // Chaîne de caractères.
265         void CAttributeTemplate<StdString>::toBinary (StdOStream & os) const;
266
267      template <> // Entier
268         void CAttributeTemplate<int>::toBinary(StdOStream & os) const;
269
270      template <> // Booléen
271         void CAttributeTemplate<bool>::toBinary(StdOStream & os) const;
272         
273      template <> // Double
274         void CAttributeTemplate<double>::toBinary(StdOStream & os) const;
275
276      //---------------------------------------------------------------
277
278      /** Spécialisations des templates pour la fonction [fromBinary] **/
279
280      template <> // Chaîne de caractères.
281         void CAttributeTemplate<StdString>::fromBinary(StdIStream & is);
282
283      template <> // Entier
284         void CAttributeTemplate<int>::fromBinary(StdIStream & is);
285
286      template <> // Booléen
287         void CAttributeTemplate<bool>::fromBinary(StdIStream & is);
288         
289      template <> // Double
290         void CAttributeTemplate<double>::fromBinary(StdIStream & is);
291
292      ///--------------------------------------------------------------
293} // namespace xios
294
295#endif // __XMLIO_CAttributeTemplate_impl__
Note: See TracBrowser for help on using the repository browser.