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

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

Amélioration de quelques portions de code.
Ajout de contructeurs par copie.

File size: 5.2 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
18         Attribut(void)
19            : BaseAttribut(), _hasValue(false)
20         { /* Ne rien faire de plus */ }
21
22         Attribut(const Ctype& value_)
23            : BaseAttribut(), _hasValue(true), value(value_)
24         { /* Ne rien faire de plus */ }
25
26         Attribut(const Attribut& attr)
27            : BaseAttribut(attr), _hasValue(attr._hasValue)
28         { if (_hasValue) value = attr.value ; }
29
30         Attribut& operator = (const Attribut & attr)
31         {
32            _hasValue = attr._hasValue ;
33            if (_hasValue) this->setValue(attr.value) ;
34            return (*this) ;
35         }
36
37         operator Ctype() const
38         {
39            if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !");
40            return (value) ;
41         }
42
43         Ctype* operator ->(void) { return (&value); }
44         Ctype& operator * (void) { return (value) ; }
45
46      public : /* virtual */
47
48         virtual bool hasValue(void) const { return (_hasValue); }
49         virtual const char * getType(void) const = 0;
50
51         virtual void setFromString(const std::string& str)
52         {
53            istringstream iss(str); Ctype val;
54            iss >> val;
55            this->setValue(val);
56         }
57
58         virtual void assignValue(const BaseAttribut* _ba)
59         { value = ((Attribut*)_ba) -> value; _hasValue = true; }
60
61         virtual ostream& print(std::ostream & _out) const
62         { if (_hasValue) _out << " " << getName() << "=\"" << boolalpha << value << "\"" ; return (_out) ; }
63
64         virtual void setValue(const Ctype & value_)
65         { _hasValue = true ; value = value_ ;}
66
67         virtual void getValue(Ctype & value_) const
68         {
69            if (!_hasValue) throw XMLIOUndefinedValueException("L'attribut \"" + this->getName() + "\" est invalide !");
70            value_ = value ;
71         }
72
73         virtual ~Attribut()
74         { /* Ne rien faire de plus */ }
75
76      private :
77
78         bool _hasValue ;
79         Ctype value ;
80
81   }; // class Attribut
82
83#define SET_ARRAY_DIM(type, dim)                                             \
84   template<>                                                                \
85      void Attribut<Array<type,dim> >::setValue (const Array<type,dim>& val) \
86   { _hasValue = true ; value.resize(val.shape()); value = val ; }
87
88#define SET_ARRAY_TYPE(type) \
89   SET_ARRAY_DIM(type,1)     \
90   SET_ARRAY_DIM(type,2)     \
91   SET_ARRAY_DIM(type,3)     \
92   SET_ARRAY_DIM(type,4)
93
94   SET_ARRAY_TYPE(double)
95   SET_ARRAY_TYPE(int)
96   SET_ARRAY_TYPE(bool)
97
98   template <>
99      ostream& Attribut<Array<double,1> >::print(ostream & o) const
100   {
101      if (_hasValue) o << " " << getName() << "=\"" << value(0);
102      /*for (int i = 1; i < value.size(); i++)
103      { o << "," << value(i); }*/
104      o << value(0) << "..." << value(value.size()-1);
105      o << "\"" ;
106      return (o) ;
107   }
108
109   template <>
110      void Attribut<Array<double,1> >::setFromString(const std::string& _str)
111   {
112      istringstream iss(_str) ;
113      char c = '\0'; int size = 0;
114      double d = 0.,valsup = 0., valinf = 0.;
115      std::vector<double> vect; Array<double, 1> arr;
116
117      iss >> d; vect.push_back(d);
118      if (!iss.eof ())
119      {
120         iss >> c;
121         switch (c)
122         {
123            case ',' : // Le tableau est généré valeur par valeur.
124               iss.unget();
125               while(!iss.eof ())
126               { // On récupÚre chacune des valeurs une par une jusqu'à ce que le buffer soit vide.
127                  iss >> c >> d;
128                  if (c != ',')
129                     throw XMLIOUndefinedValueException("Le tableau de valeur est mal défini !");
130                  vect.push_back(d);
131               }
132               size = vect.size();
133               break;
134            case '(' : // Le tableau est généré automatiquement.
135               if (!iss.eof ())
136               { // on récupÚre la borne supérieure
137                  valinf = d;
138                  iss >> size >> c >> d;
139                  if ((c != ')') || (size <= 0))
140                     throw XMLIOUndefinedValueException("Le tableau de valeur est mal défini !");
141                  valsup = d;
142               }
143               d = (valsup - valinf) / (double)(size - 1);
144               for (int j = 1; j <= size; j++)
145                  vect.push_back(valinf + j * d);
146               break;
147            default :
148               throw XMLIOUndefinedValueException("Le tableau de valeur est mal défini !");
149         }
150      }
151
152      arr.resize(size);
153      for (int i = 0; i < size; i++)
154         arr(i) = vect[i];
155
156      this->setValue(arr);
157   }
158
159   template <>
160      void Attribut<bool>::setFromString(const std::string& str)
161   {
162      istringstream iss(str) ;
163      const bool val = (! iss.str().compare(string(".TRUE."))) ? true : false;
164      this->setValue(val);
165   }
166
167   template <>
168      void Attribut<string>::setFromString(const std::string& str)
169   { this->setValue(str); }
170
171} // namespace XMLIOSERVER
172
173#endif //__ATTRIBUT__
Note: See TracBrowser for help on using the repository browser.