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

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

Mise à jour

File size: 6.7 KB
Line 
1#ifndef __ATTRIBUT__
2#define __ATTRIBUT__
3
4#include "base_attribut.hpp"
5
6using XMLIOSERVER::BaseAttribut;
7using std::ostringstream;
8using namespace blitz ;
9
10namespace XMLIOSERVER
11{
12   template <class Ctype>
13      class Attribut : public BaseAttribut
14   {
15      public :
16
17         Attribut(void)
18            : BaseAttribut(), _hasValue(false), value()
19         { /* Ne rien faire de plus */ }
20
21         Attribut(const Ctype& value_)
22            : BaseAttribut(), _hasValue(true), value(value_)
23         { /* Ne rien faire de plus */ }
24
25         Attribut(const Attribut& attr)
26            : BaseAttribut(attr), _hasValue(attr._hasValue)
27         { if (_hasValue) value = attr.value ; }
28
29         Attribut& operator = (const Attribut & attr)
30         {
31            _hasValue = attr._hasValue ;
32            if (_hasValue) this->setValue(attr.value) ;
33            return (*this) ;
34         }
35
36         operator Ctype() const
37         {
38            if (!_hasValue)
39               XMLIOError("L'attribut \"" + this->getName() + "\" est invalide !");
40            return (value) ;
41         }
42
43         Ctype* getValue(void)
44         {
45            //if (!_hasValue) return(NULL); // REVOIR: 4 h de debug à cause de ça !!!
46            return (&value);
47         }
48
49         const Ctype * getValueConst(void) const
50         {
51            //if (!_hasValue) return(NULL); // REVOIR: 4 h de debug à cause de ça !!!
52            return (&value);
53         }
54
55         Ctype* operator ->(void) { return (&value); }
56         Ctype& operator * (void) { return (value) ; }
57
58      public : /* virtual */
59
60         virtual bool hasValue(void) const { return (_hasValue); }
61         virtual const char * getType(void) const = 0;
62
63         virtual void setFromString(const std::string& str)
64         {
65            istringstream iss(str); Ctype val;
66            iss >> val;
67            this->setValue(val);
68         }
69
70         virtual void assignValue(const BaseAttribut* _ba)
71         { value = ((Attribut*)_ba) -> value; _hasValue = true; }
72
73         virtual ostream& print(std::ostream & _out) const
74         {
75            if (_hasValue)
76            {
77               _out << " " << getName()
78                    << "=\"" << boolalpha << value << "\"" ;
79            }
80            return (_out) ;
81         }
82
83         virtual void setValue(const Ctype & value_)
84         { _hasValue = true ; value = value_ ;}
85
86         virtual void getValue(Ctype & value_) const
87         {
88            if (!_hasValue) XMLIOError("L'attribut \"" + this->getName() + "\" est invalide !");
89            value_ = value ;
90         }
91
92         virtual ~Attribut(void)
93         { /* Ne rien faire de plus */ }
94
95      private :
96
97         bool _hasValue ;
98         Ctype value ;
99
100   }; // class Attribut
101
102#define SET_ARRAY_DIM(type, dim)                                             \
103   template<>                                                                \
104      void Attribut<Array<type,dim> >::setValue (const Array<type,dim>& val) \
105   { _hasValue = true ; value.resize(val.shape()); value = val ; }
106
107#define PRINT_ARRAY1(type)                                            \
108   template <>                                                        \
109      ostream& Attribut<Array<type, 1> >::print(ostream & _out) const \
110   {                                                                  \
111      if (_hasValue)                                                  \
112      _out << " " << getName() << "=\"" << value(0)                   \
113           << "(" << value.extent(0) << ")"                           \
114           << value(value.size()-1); _out << "\"" ;                   \
115      return (_out) ;                                                 \
116   }
117
118#define PRINT_ARRAY2(type)                                            \
119   template <>                                                        \
120      ostream& Attribut<Array<type, 2> >::print(ostream & _out) const \
121   {                                                                  \
122      if (_hasValue)                                                  \
123      _out << " " << getName() << "=\"" << value(0, 0)                \
124           << "(" << value.extent(0) << "," << value.extent(1) << ")" \
125           << value(value.extent(0)-1, value.extent(1)-1)             \
126           << "\"" ;                                                  \
127      return (_out) ;                                                 \
128   }
129
130#define SET_ARRAY_TYPE(type) \
131   PRINT_ARRAY1(type)        \
132   PRINT_ARRAY2(type)        \
133   SET_ARRAY_DIM(type, 1)    \
134   SET_ARRAY_DIM(type, 2)    \
135   SET_ARRAY_DIM(type, 3)    \
136   SET_ARRAY_DIM(type, 4)
137
138   SET_ARRAY_TYPE(double)
139   SET_ARRAY_TYPE(float)
140   SET_ARRAY_TYPE(int)
141   SET_ARRAY_TYPE(bool)
142
143#undef SET_ARRAY_DIM
144#undef SET_ARRAY_TYPE
145#undef PRINT_ARRAY
146
147   template <>
148      void Attribut<Array<double, 1> >::setFromString(const std::string& _str)
149   {
150      istringstream iss(_str) ;
151      char c = '\0'; int size = 0;
152      double d = 0.,valsup = 0., valinf = 0.;
153      std::vector<double> vect; Array<double, 1> arr;
154
155      iss >> d; vect.push_back(d);
156      if (!iss.eof ())
157      {
158         iss >> c;
159         switch (c)
160         {
161            case ',' : // Le tableau est généré valeur par valeur.
162               iss.unget();
163               while(!iss.eof ())
164               { // On récupÚre chacune des valeurs une par une jusqu'à ce que le buffer soit vide.
165                  iss >> c >> d;
166                  if (c != ',')
167                     XMLIOError("Le tableau de valeur est mal défini !");
168                  vect.push_back(d);
169               }
170               size = vect.size();
171               break;
172            case '(' : // Le tableau est généré automatiquement.
173               if (!iss.eof ())
174               { // on récupÚre la borne supérieure
175                  valinf = d;
176                  iss >> size >> c >> d;
177                  if ((c != ')') || (size <= 0))
178                     XMLIOError("Le tableau de valeur est mal défini !");
179                  valsup = d;
180               }
181               d = (valsup - valinf) / (double)(size - 1);
182               for (int j = 1; j <= size; j++)
183                  vect.push_back(valinf + j * d);
184               break;
185            default :
186               XMLIOError("Le tableau de valeur est mal défini !");
187         }
188      }
189
190      arr.resize(size);
191      for (int i = 0; i < size; i++)
192         arr(i) = vect[i];
193
194      this->setValue(arr);
195   }
196
197   template <>
198      void Attribut<bool>::setFromString(const std::string& str)
199   {
200      const bool val =
201          (str.compare(string(".TRUE.")) == 0) ? true : false;
202      this->setValue(val);
203   }
204
205   template <>
206      void Attribut<string>::setFromString(const std::string& str)
207   { this->setValue(str); }
208
209} // namespace XMLIOSERVER
210
211#endif //__ATTRIBUT__
Note: See TracBrowser for help on using the repository browser.