source: XIOS/trunk/src/attribute_template_impl.hpp @ 1105

Last change on this file since 1105 was 1105, checked in by mhnguyen, 7 years ago

Adding comparison operator between objects of XIOS.
Two objects of a same type are considered equal if they have same non-empty
attributes which have same values

+) Add operator== to class CArray
+) Add comparison operator to some basic attribute classes
+) Add operator== to date and duration (It seems that they don't serve much)

Test
+) On Curie
+) No Unit tests but test with transformation work (the next commit)

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