source: XMLIO_V2/dev/dev_rv/src/XMLIO/attribut.hpp @ 120

Last change on this file since 120 was 120, checked in by hozdoba, 14 years ago

Mise à jour intermédiaire ...
A venir : commit d'une version stable intégrant l'écriture de fichiers NetCDF4.
(en cours de finalisation actuellement)

File size: 2.9 KB
RevLine 
[98]1#ifndef __ATTRIBUT__
2#define __ATTRIBUT__
[79]3
4#include "base_attribut.hpp"
5
[98]6using XMLIOSERVER::BaseAttribut;
7using XMLIOSERVER::XMLIOUndefinedValueException;
8using std::ostringstream;
[120]9using namespace blitz ;
[98]10
11namespace XMLIOSERVER
[79]12{
[98]13   template <class Ctype>
14      class Attribut : public BaseAttribut
15   {
16      public :
[113]17         operator Ctype() const
18         {
[120]19            if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !");
[113]20            return (value) ;
21         }
[79]22
[120]23         Ctype* operator ->() { return (&value) ; }
24         Ctype& operator *() { return (value) ; }
[108]25
[120]26         virtual bool hasValue() const { return (_hasValue); }
27         virtual const char * getType(void) const = 0;
[113]28
[120]29         Attribut(void) : _hasValue(false) {} ;
30         Attribut(const Ctype& value_) : _hasValue(true), value(value_) {} ;
[108]31
[120]32         Attribut(const Attribut& attr) : _hasValue(attr._hasValue)
33         {  if (_hasValue) value = attr.value ; }
[79]34
[98]35         Attribut& operator = (const Attribut & attr)
[108]36         {
[120]37            _hasValue = attr._hasValue ;
38            if (_hasValue) setValue(attr.value) ;
39            return (*this) ;
[98]40         }
[108]41
[120]42         virtual void setFromString(const std::string& str)
[108]43         {
[120]44            istringstream iss(str); Ctype val;
[98]45            iss >> val;
46            this->setValue(val);
[108]47         }
[79]48
[104]49         virtual void assignValue(const BaseAttribut* _ba)
[120]50         { value = ((Attribut*)_ba) -> value; _hasValue = true; }
[79]51
[98]52         virtual ostream& print(ostream & o) const
[120]53         { if (_hasValue) o << " " << getName() << "=\"" << boolalpha << value << "\"" ; return o ; }
[79]54
[98]55         virtual void setValue(const Ctype & value_)
[120]56         { _hasValue = true ; value = value_ ;}
[108]57
[98]58         virtual void getValue(Ctype & value_) const
59         {
[120]60            if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !");
61            value_ = value ;
[98]62         }
[79]63
[113]64      private :
65
[120]66         bool _hasValue ;
[113]67         Ctype value ;
68
[108]69   }; // class Attribut
[120]70
71#define SET_ARRAY_DIM(type,dim)                                              \
72   template<>                                                                \
73      void Attribut<Array<type,dim> >::setValue (const Array<type,dim>& val) \
74   { _hasValue = true ; value.resize(val.shape()); value = val ; }
75
76#define SET_ARRAY_TYPE(type) \
77   SET_ARRAY_DIM(type,1)     \
78   SET_ARRAY_DIM(type,2)     \
79   SET_ARRAY_DIM(type,3)     \
80   SET_ARRAY_DIM(type,4)
81
82   SET_ARRAY_TYPE(double)
83   SET_ARRAY_TYPE(int)
84   SET_ARRAY_TYPE(bool)
85
86   template <>
87      void Attribut<bool>::setFromString(const std::string& str)
88   {
89      istringstream iss(str) ;
90      bool val = (! iss.str().compare(string(".TRUE."))) ? true : false;
91      this->setValue(val);
92   }
93
94   template <>
95      void Attribut<string>::setFromString(const std::string& str)
96   { this->setValue(str); }
97
[114]98} // namespace XMLIOSERVER
[108]99
[98]100#endif //__ATTRIBUT__
Note: See TracBrowser for help on using the repository browser.