[308] | 1 | #ifndef __XIOS_TYPE_SPECIALISATION_HPP__ |
---|
| 2 | #define __XIOS_TYPE_SPECIALISATION_HPP__ |
---|
| 3 | |
---|
| 4 | #include "xmlioserver_spl.hpp" |
---|
| 5 | #include "exception.hpp" |
---|
| 6 | #include "buffer_in.hpp" |
---|
| 7 | #include "buffer_out.hpp" |
---|
[369] | 8 | #include "type.hpp" |
---|
| 9 | #include <string> |
---|
| 10 | #include <boost/algorithm/string.hpp> |
---|
| 11 | |
---|
[335] | 12 | namespace xios |
---|
[308] | 13 | { |
---|
| 14 | |
---|
[369] | 15 | // template specialization for bool |
---|
[308] | 16 | |
---|
[369] | 17 | template <> void CType<bool>::_fromString(const string& str) |
---|
| 18 | { |
---|
| 19 | string tmpStr=boost::to_lower_copy(boost::trim_copy(str)) ; |
---|
| 20 | if (tmpStr=="true" || tmpStr==".true." || tmpStr=="yes" || tmpStr=="y") set(true) ; |
---|
| 21 | else if (tmpStr=="false" || tmpStr==".false." || tmpStr=="no" || tmpStr=="n") set(false) ; |
---|
| 22 | else ERROR("template <> CType<bool>::fromString(const string& str)",<< tmpStr << " cannot be converted in a boolean value") ; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | template <> string CType<bool>::_toString(void) const |
---|
| 26 | { |
---|
| 27 | if (get()) return string("true") ; |
---|
| 28 | else return string("false") ; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | template <> void CType_ref<bool>::_fromString(const string& str) const |
---|
| 32 | { |
---|
| 33 | string tmpStr=boost::to_lower_copy(boost::trim_copy(str)) ; |
---|
| 34 | if (tmpStr=="true" || tmpStr==".true." || tmpStr=="yes" || tmpStr=="y") set(true) ; |
---|
| 35 | else if (tmpStr=="false" || tmpStr==".false." || tmpStr=="no" || tmpStr=="n") set(false) ; |
---|
| 36 | else ERROR("template <> CType<bool>::fromString(const string& str)",<< tmpStr << " cannot be converted in a boolean value") ; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | template <> void CType_ref<bool>::_fromString(const string& str) |
---|
| 40 | { |
---|
| 41 | string tmpStr=boost::to_lower_copy(boost::trim_copy(str)) ; |
---|
| 42 | if (tmpStr=="true" || tmpStr==".true." || tmpStr=="yes" || tmpStr=="y") set(true) ; |
---|
| 43 | else if (tmpStr=="false" || tmpStr==".false." || tmpStr=="no" || tmpStr=="n") set(false) ; |
---|
| 44 | else ERROR("template <> CType<bool>::fromString(const string& str)",<< tmpStr << " cannot be converted in a boolean value") ; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | template <> string CType_ref<bool>::_toString(void) const |
---|
| 48 | { |
---|
| 49 | if (get()) return string("true") ; |
---|
| 50 | else return string("false") ; |
---|
| 51 | } |
---|
[308] | 52 | |
---|
[369] | 53 | // template specialization for string |
---|
| 54 | |
---|
[308] | 55 | template <> |
---|
[369] | 56 | size_t CType<string>::_size() const |
---|
[308] | 57 | { |
---|
| 58 | size_t typeSize=0 ; |
---|
| 59 | typeSize+=sizeof(size_t) ; |
---|
| 60 | typeSize+=ptrValue->size() ; |
---|
| 61 | return typeSize ; |
---|
| 62 | } |
---|
[369] | 63 | |
---|
| 64 | template <> |
---|
| 65 | size_t CType_ref<string>::_size() const |
---|
| 66 | { |
---|
| 67 | size_t typeSize=0 ; |
---|
| 68 | typeSize+=sizeof(size_t) ; |
---|
| 69 | typeSize+=ptrValue->size() ; |
---|
| 70 | return typeSize ; |
---|
| 71 | } |
---|
[308] | 72 | |
---|
| 73 | template <> |
---|
[369] | 74 | void CType<string>::_fromString(const string& str) |
---|
[308] | 75 | { |
---|
[369] | 76 | allocate() ; |
---|
| 77 | *ptrValue=str ; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | template <> |
---|
| 81 | void CType_ref<string>::_fromString(const string& str) |
---|
| 82 | { |
---|
| 83 | checkEmpty() ; |
---|
| 84 | *ptrValue=str ; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | template <> |
---|
| 88 | void CType_ref<string>::_fromString(const string& str) const |
---|
| 89 | { |
---|
| 90 | checkEmpty() ; |
---|
| 91 | *ptrValue=str ; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | template <> |
---|
| 95 | bool CType<string>::_toBuffer(CBufferOut& buffer) const |
---|
| 96 | { |
---|
[308] | 97 | if (buffer.remain()<size()) return false ; |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | bool ret=true ; |
---|
| 101 | if (ret) ret&=buffer.put(ptrValue->size()) ; |
---|
| 102 | if (ret) ret&=buffer.put(ptrValue->data(),ptrValue->size()) ; |
---|
| 103 | return ret ; |
---|
| 104 | } |
---|
| 105 | } |
---|
[369] | 106 | |
---|
| 107 | template <> |
---|
| 108 | bool CType_ref<string>::_toBuffer(CBufferOut& buffer) const |
---|
| 109 | { |
---|
| 110 | if (buffer.remain()<size()) return false ; |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | bool ret=true ; |
---|
| 114 | if (ret) ret&=buffer.put(ptrValue->size()) ; |
---|
| 115 | if (ret) ret&=buffer.put(ptrValue->data(),ptrValue->size()) ; |
---|
| 116 | return ret ; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | template <> |
---|
| 123 | bool CType<string>::_fromBuffer(CBufferIn& buffer) |
---|
| 124 | { |
---|
| 125 | allocate() ; |
---|
| 126 | bool ret=true ; |
---|
| 127 | size_t typeSize ; |
---|
| 128 | if (ret) ret&=buffer.get(typeSize) ; |
---|
| 129 | |
---|
| 130 | char* str; |
---|
| 131 | str= (char*) buffer.ptr() ; |
---|
| 132 | if (ret) buffer.advance(typeSize) ; |
---|
| 133 | if (ret) *ptrValue=string(str,typeSize) ; |
---|
| 134 | |
---|
| 135 | return ret ; |
---|
| 136 | } |
---|
[308] | 137 | |
---|
| 138 | template <> |
---|
[369] | 139 | bool CType_ref<string>::_fromBuffer(CBufferIn& buffer) const |
---|
[308] | 140 | { |
---|
| 141 | bool ret=true ; |
---|
| 142 | size_t typeSize ; |
---|
| 143 | if (ret) ret&=buffer.get(typeSize) ; |
---|
| 144 | |
---|
| 145 | char* str; |
---|
| 146 | str= (char*) buffer.ptr() ; |
---|
| 147 | if (ret) buffer.advance(typeSize) ; |
---|
| 148 | if (ret) *ptrValue=string(str,typeSize) ; |
---|
| 149 | |
---|
| 150 | return ret ; |
---|
[369] | 151 | } |
---|
| 152 | |
---|
| 153 | template <> |
---|
| 154 | bool CType_ref<string>::_fromBuffer(CBufferIn& buffer) |
---|
| 155 | { |
---|
| 156 | bool ret=true ; |
---|
| 157 | size_t typeSize ; |
---|
| 158 | if (ret) ret&=buffer.get(typeSize) ; |
---|
| 159 | |
---|
| 160 | char* str; |
---|
| 161 | str= (char*) buffer.ptr() ; |
---|
| 162 | if (ret) buffer.advance(typeSize) ; |
---|
| 163 | if (ret) *ptrValue=string(str,typeSize) ; |
---|
| 164 | |
---|
| 165 | return ret ; |
---|
| 166 | } |
---|
[308] | 167 | // template specialisation for CArray |
---|
| 168 | /* |
---|
| 169 | template<> |
---|
| 170 | size_t CType< ARRAY(int, 1)>::size() const |
---|
| 171 | { |
---|
| 172 | return (*(this->ptrValue))->getSize() ; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | template <> |
---|
| 176 | bool CType<ARRAY(int, 1)>::toBuffer(CBufferOut& buffer) const |
---|
| 177 | { |
---|
| 178 | return (*(this->ptrValue))->toBuffer(buffer) ; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | template <> |
---|
| 182 | bool CType<ARRAY(int, 1)>::fromBuffer(CBufferIn& buffer) const |
---|
| 183 | { |
---|
| 184 | return (*(this->ptrValue))->fromBuffer(buffer) ; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | template <> |
---|
| 188 | void CType<ARRAY(int, 1)>::fromString(const string& str) const |
---|
| 189 | { |
---|
| 190 | // to implement |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | template <> |
---|
| 194 | string CType<ARRAY(int, 1)>::toString(void) const |
---|
| 195 | { |
---|
| 196 | // to implement |
---|
| 197 | return string("") ; |
---|
| 198 | |
---|
| 199 | } |
---|
| 200 | */ |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | /* |
---|
| 204 | template<size_t numDim> |
---|
| 205 | boost::detail::multi_array::extent_gen<numDim> getExtentNull(void) { return getExtentNull<numDim-1>()[0];} |
---|
| 206 | |
---|
| 207 | template<> |
---|
| 208 | boost::detail::multi_array::extent_gen<1> getExtentNull<1>(void) { return extents[0]; } |
---|
| 209 | */ |
---|
[369] | 210 | /* |
---|
[354] | 211 | #define CTYPE_ARRAY(ValueType,NumsDims) \ |
---|
[369] | 212 | \ |
---|
| 213 | template <> \ |
---|
| 214 | void CType<ARRAY(ValueType,NumsDims)>::allocate(void) \ |
---|
| 215 | { \ |
---|
| 216 | if (empty) ptrValue=new shared_ptr<CArray<ValueType, NumsDims> >(new CArray<ValueType, NumsDims>()) ;\ |
---|
| 217 | empty=false ; \ |
---|
| 218 | } \ |
---|
| 219 | \ |
---|
[308] | 220 | template<> \ |
---|
[369] | 221 | size_t CType< ARRAY(ValueType,NumsDims)>::_size() const \ |
---|
[308] | 222 | { \ |
---|
| 223 | return (*(this->ptrValue))->getSize() ; \ |
---|
| 224 | } \ |
---|
| 225 | \ |
---|
[369] | 226 | template<> \ |
---|
| 227 | size_t CType_ref< ARRAY(ValueType,NumsDims)>::_size() const \ |
---|
| 228 | { \ |
---|
| 229 | return (*(this->ptrValue))->getSize() ; \ |
---|
| 230 | } \ |
---|
| 231 | \ |
---|
[308] | 232 | template <> \ |
---|
[369] | 233 | bool CType<ARRAY(ValueType,NumsDims)>::_toBuffer(CBufferOut& buffer) const \ |
---|
[308] | 234 | { \ |
---|
| 235 | return (*(this->ptrValue))->toBuffer(buffer) ; \ |
---|
| 236 | } \ |
---|
| 237 | \ |
---|
| 238 | template <> \ |
---|
[369] | 239 | bool CType_ref<ARRAY(ValueType,NumsDims)>::_toBuffer(CBufferOut& buffer) const \ |
---|
[308] | 240 | { \ |
---|
[369] | 241 | return (*(this->ptrValue))->toBuffer(buffer) ; \ |
---|
| 242 | } \ |
---|
| 243 | \ |
---|
| 244 | template <> \ |
---|
| 245 | bool CType<ARRAY(ValueType,NumsDims)>::_fromBuffer(CBufferIn& buffer) \ |
---|
| 246 | { \ |
---|
| 247 | allocate(); \ |
---|
| 248 | return (*(ptrValue))->fromBuffer(buffer) ; \ |
---|
| 249 | } \ |
---|
| 250 | \ |
---|
| 251 | template <> \ |
---|
| 252 | bool CType_ref<ARRAY(ValueType,NumsDims)>::_fromBuffer(CBufferIn& buffer) const \ |
---|
| 253 | { \ |
---|
| 254 | checkEmpty() ; \ |
---|
| 255 | return (*(this->ptrValue))->fromBuffer(buffer) ; \ |
---|
| 256 | } \ |
---|
| 257 | \ |
---|
| 258 | template <> \ |
---|
| 259 | bool CType_ref<ARRAY(ValueType,NumsDims)>::_fromBuffer(CBufferIn& buffer) \ |
---|
| 260 | { \ |
---|
[308] | 261 | shared_ptr<CArray<ValueType, NumsDims> > tmp(new CArray<ValueType, NumsDims>() ) ; \ |
---|
| 262 | *(this->ptrValue)=tmp ;\ |
---|
| 263 | return (*(this->ptrValue))->fromBuffer(buffer) ; \ |
---|
| 264 | } \ |
---|
| 265 | \ |
---|
[369] | 266 | \ |
---|
[308] | 267 | template <> \ |
---|
[369] | 268 | void CType<ARRAY(ValueType,NumsDims)>::_fromString(const string& str) \ |
---|
[308] | 269 | { \ |
---|
[369] | 270 | \ |
---|
[308] | 271 | } \ |
---|
| 272 | \ |
---|
| 273 | template <> \ |
---|
[369] | 274 | void CType_ref<ARRAY(ValueType,NumsDims)>::_fromString(const string& str) const \ |
---|
[308] | 275 | { \ |
---|
[369] | 276 | \ |
---|
| 277 | } \ |
---|
| 278 | template <> \ |
---|
| 279 | void CType_ref<ARRAY(ValueType,NumsDims)>::_fromString(const string& str) \ |
---|
| 280 | { \ |
---|
| 281 | \ |
---|
| 282 | } \ |
---|
| 283 | \ |
---|
| 284 | template <> \ |
---|
| 285 | string CType<ARRAY(ValueType,NumsDims)>::_toString(void) const \ |
---|
| 286 | { \ |
---|
| 287 | \ |
---|
[308] | 288 | return string("") ; \ |
---|
[369] | 289 | } \ |
---|
| 290 | \ |
---|
| 291 | template <> \ |
---|
| 292 | string CType_ref<ARRAY(ValueType,NumsDims)>::_toString(void) const \ |
---|
| 293 | { \ |
---|
| 294 | \ |
---|
| 295 | return string("") ; \ |
---|
[308] | 296 | } |
---|
| 297 | //CTYPE_ARRAY(double,1) |
---|
| 298 | |
---|
| 299 | //CTYPE_ARRAY(double,2) |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | CTYPE_ARRAY(int,1) |
---|
| 303 | CTYPE_ARRAY(int,2) |
---|
| 304 | CTYPE_ARRAY(int,3) |
---|
| 305 | CTYPE_ARRAY(bool,1) |
---|
| 306 | CTYPE_ARRAY(bool,2) |
---|
| 307 | CTYPE_ARRAY(bool,3) |
---|
| 308 | CTYPE_ARRAY(double,1) |
---|
| 309 | CTYPE_ARRAY(double,2) |
---|
| 310 | CTYPE_ARRAY(double,3) |
---|
| 311 | CTYPE_ARRAY(float,1) |
---|
| 312 | CTYPE_ARRAY(float,2) |
---|
| 313 | CTYPE_ARRAY(float,3) |
---|
[369] | 314 | */ |
---|
[308] | 315 | } |
---|
| 316 | |
---|
| 317 | #endif |
---|