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 | 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 | 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 <> |
---|
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 | |
---|
124 | } // namespace XMLIOSERVER |
---|
125 | |
---|
126 | #endif //__ATTRIBUT__ |
---|