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