source: XIOS3/trunk/src/attribute_template_impl.hpp @ 2433

Last change on this file since 2433 was 2388, checked in by jderouillat, 22 months ago

Manage hash values with size_t in hash tables of elements associated to output files

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