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