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
Line 
1#ifndef __ATTRIBUT__
2#define __ATTRIBUT__
3
4#include "base_attribut.hpp"
5
6using XMLIOSERVER::BaseAttribut;
7using XMLIOSERVER::XMLIOUndefinedValueException;
8using std::ostringstream;
9using namespace blitz ;
10
11namespace XMLIOSERVER
12{
13   template <class Ctype>
14      class Attribut : public BaseAttribut
15   {
16      public :
17         operator Ctype() const
18         {
19            if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !");
20            return (value) ;
21         }
22
23         Ctype* operator ->() { return (&value) ; }
24         Ctype& operator *() { return (value) ; }
25
26         virtual bool hasValue() const { return (_hasValue); }
27         virtual const char * getType(void) const = 0;
28
29         Attribut(void) : _hasValue(false) {} ;
30         Attribut(const Ctype& value_) : _hasValue(true), value(value_) {} ;
31
32         Attribut(const Attribut& attr) : _hasValue(attr._hasValue)
33         {  if (_hasValue) value = attr.value ; }
34
35         Attribut& operator = (const Attribut & attr)
36         {
37            _hasValue = attr._hasValue ;
38            if (_hasValue) setValue(attr.value) ;
39            return (*this) ;
40         }
41
42         virtual void setFromString(const std::string& str)
43         {
44            istringstream iss(str); Ctype val;
45            iss >> val;
46            this->setValue(val);
47         }
48
49         virtual void assignValue(const BaseAttribut* _ba)
50         { value = ((Attribut*)_ba) -> value; _hasValue = true; }
51
52         virtual ostream& print(ostream & o) const
53         { if (_hasValue) o << " " << getName() << "=\"" << boolalpha << value << "\"" ; return o ; }
54
55         virtual void setValue(const Ctype & value_)
56         { _hasValue = true ; value = value_ ;}
57
58         virtual void getValue(Ctype & value_) const
59         {
60            if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !");
61            value_ = value ;
62         }
63
64      private :
65
66         bool _hasValue ;
67         Ctype value ;
68
69   }; // class Attribut
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
98} // namespace XMLIOSERVER
99
100#endif //__ATTRIBUT__
Note: See TracBrowser for help on using the repository browser.