source: XIOS/dev/XIOS_DEV_CMIP6/src/attribute_template_impl.hpp @ 1477

Last change on this file since 1477 was 1477, checked in by oabramkina, 6 years ago

Implementing exception handling for accessing attribute value.

In certain cases attribute's id will be printed in the error message.

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