1 | /* ************************************************************************** * |
---|
2 | * Copyright © IPSL/LSCE, XMLIOServer, Avril 2010 - Octobre 2011 * |
---|
3 | * ************************************************************************** */ |
---|
4 | |
---|
5 | /** |
---|
6 | * \file attribute_template.cpp |
---|
7 | * \brief Gestion des attributs typés d'objet (implémentation). |
---|
8 | * \author Hervé Ozdoba |
---|
9 | * \version 0.4 |
---|
10 | * \date 1er Juin 2011 |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __XIOS_NO_EXTERN |
---|
14 | |
---|
15 | // C++ standard headers |
---|
16 | #include <string> |
---|
17 | #include <iostream> |
---|
18 | #include <cfloat> |
---|
19 | |
---|
20 | // boost headers |
---|
21 | #include <boost/cstdint.hpp> |
---|
22 | #include <boost/shared_ptr.hpp> |
---|
23 | #include <boost/multi_array.hpp> |
---|
24 | #include <boost/lexical_cast.hpp> |
---|
25 | |
---|
26 | #endif // __XIOS_NO_EXTERN |
---|
27 | |
---|
28 | // XMLIOServer headers |
---|
29 | #include "attribute_template.hpp" |
---|
30 | #include "attribute_template_impl.hpp" |
---|
31 | #include "attribute_impl.hpp" |
---|
32 | |
---|
33 | // /////////////////////////////// Définitions ////////////////////////////// // |
---|
34 | |
---|
35 | namespace xmlioserver { |
---|
36 | namespace tree { |
---|
37 | |
---|
38 | //-------------------------------------------------------------------------- |
---|
39 | //-------- Spécialisations des templates pour la fonction [toString] ------- |
---|
40 | //-------------------------------------------------------------------------- |
---|
41 | |
---|
42 | template <> |
---|
43 | std::string CAttributeTemplate<bool>::toString(void) const |
---|
44 | { |
---|
45 | std::ostringstream oss; |
---|
46 | if (!this->isEmpty() && this->hasId()) |
---|
47 | { |
---|
48 | if (this->getValue()) |
---|
49 | oss << this->getName() << "=\".TRUE.\""; |
---|
50 | else |
---|
51 | oss << this->getName() << "=\".FALSE.\""; |
---|
52 | } |
---|
53 | return (oss.str()); |
---|
54 | } |
---|
55 | |
---|
56 | //-------------------------------------------------------------------------- |
---|
57 | //------ Spécialisations des templates pour la fonction [fromString] ------- |
---|
58 | //-------------------------------------------------------------------------- |
---|
59 | |
---|
60 | template <> // Chaîne de caractÚres. |
---|
61 | void CAttributeTemplate<std::string>::fromString(const std::string & _str) |
---|
62 | { |
---|
63 | this->setValue(_str); |
---|
64 | } |
---|
65 | |
---|
66 | template <> // Entier |
---|
67 | void CAttributeTemplate<int>::fromString(const std::string & _str) |
---|
68 | { |
---|
69 | try |
---|
70 | { |
---|
71 | this->setValue(boost::lexical_cast<int>(_str)); |
---|
72 | } |
---|
73 | catch(boost::bad_lexical_cast &) |
---|
74 | { |
---|
75 | XIOS_ERROR("void CAttributeTemplate<int>::fromString(const StdString & _str)", |
---|
76 | << "[ str = " << _str << " ] Bad cast !"); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | template <> // Double |
---|
81 | void CAttributeTemplate<double>::fromString(const std::string & _str) |
---|
82 | { |
---|
83 | if (_str.find("max") != std::string::npos) |
---|
84 | { |
---|
85 | this->setValue(DBL_MAX); |
---|
86 | return; |
---|
87 | } |
---|
88 | if (_str.find("min") != std::string::npos) |
---|
89 | { |
---|
90 | this->setValue(DBL_MIN); |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | try |
---|
95 | { |
---|
96 | this->setValue(boost::lexical_cast<double>(_str)); |
---|
97 | } |
---|
98 | catch(boost::bad_lexical_cast &) |
---|
99 | { |
---|
100 | XIOS_ERROR("void CAttributeTemplate<double>::fromString(const StdString & str)", |
---|
101 | << "[ str = " << _str << " ] Bad cast !"); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | template <> // Booléen |
---|
106 | void CAttributeTemplate<bool>::fromString(const std::string & _str) |
---|
107 | { |
---|
108 | if (_str.find(".TRUE.") != std::string::npos) |
---|
109 | this->setValue(true); |
---|
110 | else |
---|
111 | this->setValue(false); |
---|
112 | } |
---|
113 | |
---|
114 | //-------------------------------------------------------------------------- |
---|
115 | |
---|
116 | template<> // Tableau |
---|
117 | void CAttributeTemplate<boost::shared_ptr<boost::multi_array<double, 1> > > |
---|
118 | ::fromString(const std::string & _str) |
---|
119 | { |
---|
120 | boost::shared_ptr<boost::multi_array<double, 1> > array_sptr |
---|
121 | (new boost::multi_array<double, 1>(boost::extents[1], boost::fortran_storage_order())); |
---|
122 | |
---|
123 | boost::multi_array<double, 1> & array = *array_sptr; |
---|
124 | this->setValue(array_sptr); |
---|
125 | |
---|
126 | std::istringstream iss(_str) ; |
---|
127 | char c = '\0'; int size = 0; |
---|
128 | double d = 0.,valsup = 0., valinf = 0.; |
---|
129 | std::vector<double> vect; |
---|
130 | |
---|
131 | iss >> d; vect.push_back(d); |
---|
132 | if (!iss.eof ()) |
---|
133 | { |
---|
134 | iss >> c; |
---|
135 | switch (c) |
---|
136 | { |
---|
137 | case ',' : // Le tableau est généré valeur par valeur. |
---|
138 | iss.unget(); |
---|
139 | while(!iss.eof ()) |
---|
140 | { // On récupÚre chacune des valeurs une par une jusqu'à ce que le buffer soit vide. |
---|
141 | iss >> c >> d; |
---|
142 | if (c != ',') |
---|
143 | XIOS_ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)", |
---|
144 | << "[ str = " << _str << " ] bad definition of array !"); |
---|
145 | vect.push_back(d); |
---|
146 | } |
---|
147 | size = vect.size(); |
---|
148 | break; |
---|
149 | case '(' : // Le tableau est généré automatiquement. |
---|
150 | if (!iss.eof ()) |
---|
151 | { // on récupÚre la borne supérieure |
---|
152 | valinf = d; |
---|
153 | iss >> size >> c >> d; |
---|
154 | if ((c != ')') || (size <= 0)) |
---|
155 | XIOS_ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)", |
---|
156 | << "[ str = " << _str << " ] bad definition of array !"); |
---|
157 | valsup = d; |
---|
158 | } |
---|
159 | d = (valsup - valinf) / (double)(size - 1); |
---|
160 | for (int j = 1; j <= size; j++) |
---|
161 | vect.push_back(valinf + j * d); |
---|
162 | break; |
---|
163 | default : |
---|
164 | XIOS_ERROR("CAttributeTemplate<ARRAY(...)>::fromString(const StdString & str)", |
---|
165 | << "[ str = " << _str << " ] bad definition of array !"); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | array.resize(boost::extents[size]); |
---|
170 | for (int i = 0; i < size; i++) |
---|
171 | array[i] = vect[i]; |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | //-------------------------------------------------------------------------- |
---|
176 | //-------- Spécialisations des templates pour la fonction [toBinary] ------- |
---|
177 | //-------------------------------------------------------------------------- |
---|
178 | |
---|
179 | template <> // Chaîne de caractÚres. |
---|
180 | void CAttributeTemplate<std::string>::toBinary (std::ostream & _os) const |
---|
181 | { |
---|
182 | std::string str = this->getValue(); |
---|
183 | std::size_t size = str.size(); |
---|
184 | _os.write (reinterpret_cast<const char*>(&size) , sizeof(std::size_t)); |
---|
185 | _os.write (str.data(), size * sizeof(char)); |
---|
186 | } |
---|
187 | |
---|
188 | template <> // Entier |
---|
189 | void CAttributeTemplate<int>::toBinary(std::ostream & _os) const |
---|
190 | { |
---|
191 | int value = this->getValue(); |
---|
192 | _os.write (reinterpret_cast<const char*>(&value) , sizeof(int)); |
---|
193 | } |
---|
194 | |
---|
195 | template <> // Booléen |
---|
196 | void CAttributeTemplate<bool>::toBinary(std::ostream & _os) const |
---|
197 | { |
---|
198 | bool value = this->getValue(); |
---|
199 | _os.write (reinterpret_cast<const char*>(&value) , sizeof(bool)); |
---|
200 | } |
---|
201 | |
---|
202 | template <> // Double |
---|
203 | void CAttributeTemplate<double>::toBinary(std::ostream & _os) const |
---|
204 | { |
---|
205 | double value = this->getValue(); |
---|
206 | _os.write (reinterpret_cast<const char*>(&value) , sizeof(double)); |
---|
207 | } |
---|
208 | |
---|
209 | //-------------------------------------------------------------------------- |
---|
210 | //------ Spécialisations des templates pour la fonction [fromBinary] ------- |
---|
211 | //-------------------------------------------------------------------------- |
---|
212 | |
---|
213 | template <> // Chaîne de caractÚres. |
---|
214 | void CAttributeTemplate<std::string>::fromBinary(std::istream & _is) |
---|
215 | { |
---|
216 | std::size_t size = 0; |
---|
217 | _is.read (reinterpret_cast<char*>(&size), sizeof(std::size_t)); |
---|
218 | std::string value(size, ' '); |
---|
219 | _is.read (const_cast<char *>(value.data()), size * sizeof(char)); |
---|
220 | this->setValue(value); |
---|
221 | } |
---|
222 | |
---|
223 | template <> // Entier |
---|
224 | void CAttributeTemplate<int>::fromBinary(std::istream & _is) |
---|
225 | { |
---|
226 | int value = 0; |
---|
227 | _is.read (reinterpret_cast<char*>(&value), sizeof(int)); |
---|
228 | this->setValue(value); |
---|
229 | } |
---|
230 | |
---|
231 | template <> // Booléen |
---|
232 | void CAttributeTemplate<bool>::fromBinary(std::istream & _is) |
---|
233 | { |
---|
234 | bool value = false; |
---|
235 | _is.read (reinterpret_cast<char*>(&value), sizeof(bool)); |
---|
236 | this->setValue(value); |
---|
237 | } |
---|
238 | |
---|
239 | template <> // Double |
---|
240 | void CAttributeTemplate<double>::fromBinary(std::istream & _is) |
---|
241 | { |
---|
242 | double value = 0.; |
---|
243 | _is.read (reinterpret_cast<char*>(&value), sizeof(double)); |
---|
244 | this->setValue(value); |
---|
245 | } |
---|
246 | |
---|
247 | } // namespace tree |
---|
248 | } // namespace xmlioserver |
---|