1 | #include "attribute_template.hpp" |
---|
2 | #include "attribute_template_impl.hpp" |
---|
3 | |
---|
4 | #include <cfloat> |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
9 | |
---|
10 | /** Spécialisations des templates pour la fonction [toString] **/ |
---|
11 | |
---|
12 | template <> |
---|
13 | StdString CAttributeTemplate<bool>::toString(void) const |
---|
14 | { |
---|
15 | StdOStringStream oss; |
---|
16 | if (!this->isEmpty() && this->hasId()) |
---|
17 | { |
---|
18 | if (this->getValue()) |
---|
19 | oss << this->getName() << "=\".TRUE.\""; |
---|
20 | else |
---|
21 | oss << this->getName() << "=\".FALSE.\""; |
---|
22 | } |
---|
23 | return (oss.str()); |
---|
24 | } |
---|
25 | |
---|
26 | //--------------------------------------------------------------- |
---|
27 | |
---|
28 | /** Spécialisations des templates pour la fonction [fromString] **/ |
---|
29 | |
---|
30 | template <> // Chaîne de caractÚres. |
---|
31 | void CAttributeTemplate<StdString>::fromString(const StdString & str) |
---|
32 | { |
---|
33 | this->setValue(str); |
---|
34 | } |
---|
35 | |
---|
36 | template <> // Entier |
---|
37 | void CAttributeTemplate<int>::fromString(const StdString & str) |
---|
38 | { |
---|
39 | try |
---|
40 | { |
---|
41 | this->setValue(boost::lexical_cast<int>(str)); |
---|
42 | } |
---|
43 | catch(boost::bad_lexical_cast &) |
---|
44 | { |
---|
45 | ERROR("void CAttributeTemplate<int>::fromString(const StdString & str)", |
---|
46 | << "[ str = " << str << " ] Bad cast !"); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | template <> // Double |
---|
51 | void CAttributeTemplate<double>::fromString(const StdString & str) |
---|
52 | { |
---|
53 | if (str.find("max") != StdString::npos) |
---|
54 | { |
---|
55 | this->setValue(DBL_MAX); |
---|
56 | return; |
---|
57 | } |
---|
58 | if (str.find("min") != StdString::npos) |
---|
59 | { |
---|
60 | this->setValue(DBL_MIN); |
---|
61 | return; |
---|
62 | } |
---|
63 | |
---|
64 | try |
---|
65 | { |
---|
66 | this->setValue(boost::lexical_cast<double>(str)); |
---|
67 | } |
---|
68 | catch(boost::bad_lexical_cast &) |
---|
69 | { |
---|
70 | ERROR("void CAttributeTemplate<double>::fromString(const StdString & str)", |
---|
71 | << "[ str = " << str << " ] Bad cast !"); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | template <> // Booléen |
---|
76 | void CAttributeTemplate<bool>::fromString(const StdString & str) |
---|
77 | { |
---|
78 | if (str.find(".TRUE.") != StdString::npos) |
---|
79 | this->setValue(true); |
---|
80 | else |
---|
81 | this->setValue(false); |
---|
82 | } |
---|
83 | |
---|
84 | //--------------------------------------------------------------- |
---|
85 | |
---|
86 | template<> // Tableau |
---|
87 | void CAttributeTemplate<ARRAY(double, 1)>::fromString(const StdString & str) |
---|
88 | { |
---|
89 | ARRAY_CREATE(array_sptr, double, 1, [1]); |
---|
90 | CArray<double, 1> & array = *array_sptr; |
---|
91 | this->setValue(array_sptr); |
---|
92 | |
---|
93 | StdIStringStream iss(str) ; |
---|
94 | char c = '\0'; int size = 0; |
---|
95 | double d = 0.,valsup = 0., valinf = 0.; |
---|
96 | std::vector<double> vect; |
---|
97 | |
---|
98 | iss >> d; vect.push_back(d); |
---|
99 | size = vect.size(); |
---|
100 | if (!iss.eof ()) |
---|
101 | { |
---|
102 | iss >> c; |
---|
103 | switch (c) |
---|
104 | { |
---|
105 | case ',' : // Le tableau est généré valeur par valeur. |
---|
106 | iss.unget(); |
---|
107 | while(!iss.eof ()) |
---|
108 | { // On récupÚre chacune des valeurs une par une jusqu'à ce que le buffer soit vide. |
---|
109 | iss >> c >> d; |
---|
110 | if (c != ',') |
---|
111 | ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)", |
---|
112 | << "[ str = " << str << " ] bad definition of array !"); |
---|
113 | vect.push_back(d); |
---|
114 | } |
---|
115 | size = vect.size(); |
---|
116 | break; |
---|
117 | case '(' : // Le tableau est généré automatiquement. |
---|
118 | if (!iss.eof ()) |
---|
119 | { // on récupÚre la borne supérieure |
---|
120 | valinf = d; |
---|
121 | iss >> size >> c >> d; |
---|
122 | if ((c != ')') || (size <= 0)) |
---|
123 | ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)", |
---|
124 | << "[ str = " << str << " ] bad definition of array !"); |
---|
125 | valsup = d; |
---|
126 | } |
---|
127 | d = (valsup - valinf) / (double)(size - 1); |
---|
128 | for (int j = 1; j <= size; j++) |
---|
129 | vect.push_back(valinf + j * d); |
---|
130 | break; |
---|
131 | default : |
---|
132 | ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)", |
---|
133 | << "[ str = " << str << " ] bad definition of array !"); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | array.resize(boost::extents[size]); |
---|
138 | for (int i = 0; i < size; i++) |
---|
139 | array[i] = vect[i]; |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | //--------------------------------------------------------------- |
---|
144 | |
---|
145 | /** Spécialisations des templates pour la fonction [toBinary] **/ |
---|
146 | |
---|
147 | template <> // Chaîne de caractÚres. |
---|
148 | void CAttributeTemplate<StdString>::toBinary (StdOStream & os) const |
---|
149 | { |
---|
150 | StdString str = this->getValue(); |
---|
151 | StdSize size = str.size(); |
---|
152 | os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize)); |
---|
153 | os.write (str.data(), size * sizeof(char)); |
---|
154 | } |
---|
155 | |
---|
156 | template <> // Entier |
---|
157 | void CAttributeTemplate<int>::toBinary(StdOStream & os) const |
---|
158 | { |
---|
159 | int value = this->getValue(); |
---|
160 | os.write (reinterpret_cast<const char*>(&value) , sizeof(int)); |
---|
161 | } |
---|
162 | |
---|
163 | template <> // Booléen |
---|
164 | void CAttributeTemplate<bool>::toBinary(StdOStream & os) const |
---|
165 | { |
---|
166 | bool value = this->getValue(); |
---|
167 | os.write (reinterpret_cast<const char*>(&value) , sizeof(bool)); |
---|
168 | } |
---|
169 | |
---|
170 | template <> // Double |
---|
171 | void CAttributeTemplate<double>::toBinary(StdOStream & os) const |
---|
172 | { |
---|
173 | double value = this->getValue(); |
---|
174 | os.write (reinterpret_cast<const char*>(&value) , sizeof(double)); |
---|
175 | } |
---|
176 | |
---|
177 | //--------------------------------------------------------------- |
---|
178 | |
---|
179 | /** Spécialisations des templates pour la fonction [fromBinary] **/ |
---|
180 | |
---|
181 | template <> // Chaîne de caractÚres. |
---|
182 | void CAttributeTemplate<StdString>::fromBinary(StdIStream & is) |
---|
183 | { |
---|
184 | StdSize size = 0; |
---|
185 | is.read (reinterpret_cast<char*>(&size), sizeof(StdSize)); |
---|
186 | StdString value(size, ' '); |
---|
187 | is.read (const_cast<char *>(value.data()), size * sizeof(char)); |
---|
188 | this->setValue(value); |
---|
189 | } |
---|
190 | |
---|
191 | template <> // Entier |
---|
192 | void CAttributeTemplate<int>::fromBinary(StdIStream & is) |
---|
193 | { |
---|
194 | int value = 0; |
---|
195 | is.read (reinterpret_cast<char*>(&value), sizeof(int)); |
---|
196 | this->setValue(value); |
---|
197 | } |
---|
198 | |
---|
199 | template <> // Booléen |
---|
200 | void CAttributeTemplate<bool>::fromBinary(StdIStream & is) |
---|
201 | { |
---|
202 | bool value = false; |
---|
203 | is.read (reinterpret_cast<char*>(&value), sizeof(bool)); |
---|
204 | this->setValue(value); |
---|
205 | } |
---|
206 | |
---|
207 | template <> // Double |
---|
208 | void CAttributeTemplate<double>::fromBinary(StdIStream & is) |
---|
209 | { |
---|
210 | double value = 0.; |
---|
211 | is.read (reinterpret_cast<char*>(&value), sizeof(double)); |
---|
212 | this->setValue(value); |
---|
213 | } |
---|
214 | |
---|
215 | ///-------------------------------------------------------------- |
---|
216 | } // namespace xios |
---|