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

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

Plusieurs modifications en attendant une version propre et stable.

File size: 3.6 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 <>
[122]87      ostream& Attribut<Array<double,1> >::print(ostream & o) const
88   {
89      if (_hasValue) o << " " << getName() << "=\"" << value(0);
90      for (int i = 1; i < value.size(); i++) { o << "," << value(i); }
91      o << "\"" ;
92      return (o) ;
93   }
94
95   template <>
96      void Attribut<Array<double,1> >::setFromString(const std::string& str)
97   {
98      istringstream iss(str) ;
99      char c = '\0'; double d = 0.;
100      vector<double> vect;
101      Array<double, 1> arr;
102
103      iss >> d; vect.push_back(d);
104      while(!iss.eof ()) { iss >> c >> d; vect.push_back(d); }
105
106      arr.resize(vect.size());
107      for (unsigned int i = 0; i < vect.size(); i++) arr(i) = vect[i];
108
109      this->setValue(arr);
110   }
111
112   template <>
[120]113      void Attribut<bool>::setFromString(const std::string& str)
114   {
115      istringstream iss(str) ;
116      bool val = (! iss.str().compare(string(".TRUE."))) ? true : false;
117      this->setValue(val);
118   }
119
120   template <>
121      void Attribut<string>::setFromString(const std::string& str)
122   { this->setValue(str); }
123
[114]124} // namespace XMLIOSERVER
[108]125
[98]126#endif //__ATTRIBUT__
Note: See TracBrowser for help on using the repository browser.