[591] | 1 | #ifndef __XIOS_CAttributeTemplate_impl__ |
---|
| 2 | #define __XIOS_CAttributeTemplate_impl__ |
---|
[219] | 3 | |
---|
[300] | 4 | #include "type.hpp" |
---|
| 5 | #include "buffer_in.hpp" |
---|
| 6 | #include "buffer_out.hpp" |
---|
[352] | 7 | #include "generate_interface.hpp" |
---|
| 8 | #include "attribute_template.hpp" |
---|
[219] | 9 | |
---|
[775] | 10 | |
---|
[335] | 11 | namespace xios |
---|
[219] | 12 | { |
---|
[313] | 13 | |
---|
[219] | 14 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 15 | template <class T> |
---|
| 16 | CAttributeTemplate<T>::CAttributeTemplate(const StdString & id) |
---|
| 17 | : CAttribute(id) |
---|
| 18 | { /* Ne rien faire de plus */ } |
---|
| 19 | |
---|
| 20 | template <class T> |
---|
| 21 | CAttributeTemplate<T>::CAttributeTemplate(const StdString & id, const T & value) |
---|
| 22 | : CAttribute(id) |
---|
| 23 | { |
---|
| 24 | this->setValue(value); |
---|
| 25 | } |
---|
[369] | 26 | /* |
---|
[219] | 27 | template <class T> |
---|
| 28 | CAttributeTemplate<T>::CAttributeTemplate(const CAttribute & attribut) |
---|
| 29 | throw (CException) |
---|
| 30 | : CAttribute(attribut) |
---|
| 31 | { |
---|
| 32 | if (!attribut.isEmpty() && !attribut.isType<T>()) |
---|
| 33 | ERROR("CAttributeTemplate", << "Invalid instantiation !"); |
---|
| 34 | } |
---|
[369] | 35 | */ |
---|
[219] | 36 | template <class T> |
---|
| 37 | CAttributeTemplate<T>::CAttributeTemplate(const StdString & id, |
---|
| 38 | xios_map<StdString, CAttribute*> & umap) |
---|
| 39 | : CAttribute(id) |
---|
| 40 | { |
---|
| 41 | umap.insert(umap.end(), std::make_pair(id, this)); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | template <class T> |
---|
| 45 | CAttributeTemplate<T>::CAttributeTemplate |
---|
| 46 | (const StdString & id, const T & value, |
---|
| 47 | xios_map<StdString, CAttribute*> & umap) |
---|
| 48 | : CAttribute(id) |
---|
| 49 | { |
---|
| 50 | this->setValue(value); |
---|
| 51 | umap.insert(umap.end(), std::make_pair(id, this)); |
---|
| 52 | } |
---|
[369] | 53 | /* |
---|
[219] | 54 | template <class T> |
---|
[369] | 55 | CAttributeTemplate<T>::~CAttributeTemplate(void) |
---|
[775] | 56 | { |
---|
[369] | 57 | // this->CType<T>::reset() ; |
---|
| 58 | // this->clear(); |
---|
[219] | 59 | } |
---|
[369] | 60 | */ |
---|
[219] | 61 | ///-------------------------------------------------------------- |
---|
[460] | 62 | template <class T> |
---|
| 63 | void CAttributeTemplate<T>::reset(void) |
---|
| 64 | { |
---|
| 65 | CType<T>::reset() ; |
---|
| 66 | inheritedValue.reset() ; |
---|
| 67 | } |
---|
[219] | 68 | |
---|
| 69 | template <class T> |
---|
[1482] | 70 | void CAttributeTemplate<T>::checkEmpty(void) const |
---|
[219] | 71 | { |
---|
[1482] | 72 | if (CType<T>::empty) |
---|
[1028] | 73 | { |
---|
[1482] | 74 | StdString msg("On checking attribute with id="); |
---|
| 75 | msg.append(this->getId()); |
---|
| 76 | msg.append(" : "); |
---|
| 77 | msg.append("data is not initialized\n"); |
---|
| 78 | ERROR("template <typename T> void CType<T>::checkEmpty(void) const", << msg); |
---|
| 79 | } |
---|
[219] | 80 | } |
---|
[1545] | 81 | |
---|
[1642] | 82 | |
---|
[219] | 83 | template <class T> |
---|
[1642] | 84 | T CAttributeTemplate<T>::getValue(void) const |
---|
[300] | 85 | { |
---|
[1482] | 86 | return CType<T>::get() ; |
---|
[1642] | 87 | |
---|
| 88 | /* |
---|
| 89 | if (SuperClass::isEmpty()) |
---|
| 90 | { |
---|
| 91 | ERROR("T CAttributeTemplate<T>::getValue(void) const", |
---|
| 92 | << "[ id = " << this->getId() << "]" |
---|
| 93 | << " L'attribut est requis mais n'est pas défini !"); |
---|
| 94 | } |
---|
| 95 | return (SuperClass::getValue<T>()); |
---|
| 96 | */ |
---|
[300] | 97 | } |
---|
| 98 | |
---|
[1642] | 99 | /* |
---|
| 100 | template <class T> |
---|
| 101 | T* CAttributeTemplate<T>::getRef(void) |
---|
| 102 | { |
---|
| 103 | if (SuperClass::isEmpty()) |
---|
| 104 | { |
---|
| 105 | ERROR("T CAttributeTemplate<T>::getValue(void) const", |
---|
| 106 | << "[ id = " << this->getId() << "]" |
---|
| 107 | << " L'attribut est requis mais n'est pas défini !"); |
---|
| 108 | } |
---|
| 109 | return (SuperClass::getRef<T>()); |
---|
| 110 | } |
---|
| 111 | */ |
---|
[1482] | 112 | |
---|
[300] | 113 | template <class T> |
---|
[1642] | 114 | void CAttributeTemplate<T>::setValue(const T & value) |
---|
[219] | 115 | { |
---|
[369] | 116 | CType<T>::set(value) ; |
---|
| 117 | // SuperClass::setValue<T>(value); |
---|
[219] | 118 | } |
---|
| 119 | |
---|
[369] | 120 | template <class T> |
---|
| 121 | void CAttributeTemplate<T>::set(const CAttribute& attr) |
---|
| 122 | { |
---|
| 123 | this->set(dynamic_cast<const CAttributeTemplate<T>& >(attr)) ; |
---|
[775] | 124 | } |
---|
[369] | 125 | |
---|
| 126 | template <class T> |
---|
| 127 | void CAttributeTemplate<T>::set(const CAttributeTemplate& attr) |
---|
| 128 | { |
---|
| 129 | CType<T>::set(attr) ; |
---|
[775] | 130 | } |
---|
[369] | 131 | |
---|
[445] | 132 | template <class T> |
---|
| 133 | void CAttributeTemplate<T>::setInheritedValue(const CAttribute& attr) |
---|
| 134 | { |
---|
| 135 | this->setInheritedValue(dynamic_cast<const CAttributeTemplate<T>& >(attr)) ; |
---|
[775] | 136 | } |
---|
[445] | 137 | |
---|
| 138 | template <class T> |
---|
| 139 | void CAttributeTemplate<T>::setInheritedValue(const CAttributeTemplate& attr) |
---|
| 140 | { |
---|
[1112] | 141 | if (this->isEmpty() && _canInherite && attr.hasInheritedValue()) inheritedValue.set(attr.getInheritedValue()) ; |
---|
[775] | 142 | } |
---|
[445] | 143 | |
---|
| 144 | template <class T> |
---|
| 145 | T CAttributeTemplate<T>::getInheritedValue(void) const |
---|
| 146 | { |
---|
| 147 | if (this->isEmpty()) return inheritedValue.get() ; |
---|
| 148 | else return getValue() ; |
---|
[775] | 149 | } |
---|
| 150 | |
---|
[445] | 151 | template <class T> |
---|
| 152 | bool CAttributeTemplate<T>::hasInheritedValue(void) const |
---|
| 153 | { |
---|
| 154 | return !this->isEmpty() || !inheritedValue.isEmpty() ; |
---|
[775] | 155 | } |
---|
| 156 | |
---|
[1105] | 157 | template <class T> |
---|
| 158 | bool CAttributeTemplate<T>::isEqual(const CAttribute& attr) |
---|
| 159 | { |
---|
| 160 | const CAttributeTemplate<T>& tmp = dynamic_cast<const CAttributeTemplate<T>& >(attr); |
---|
[1117] | 161 | return this->isEqual_(tmp); |
---|
[1105] | 162 | } |
---|
| 163 | |
---|
| 164 | template <class T> |
---|
[1117] | 165 | bool CAttributeTemplate<T>::isEqual_(const CAttributeTemplate& attr) |
---|
[1105] | 166 | { |
---|
| 167 | if ((!this->hasInheritedValue() && !attr.hasInheritedValue())) |
---|
| 168 | return true; |
---|
| 169 | if (this->hasInheritedValue() && attr.hasInheritedValue()) |
---|
| 170 | return (this->getInheritedValue() == attr.getInheritedValue()); |
---|
| 171 | else |
---|
| 172 | return false; |
---|
| 173 | } |
---|
| 174 | |
---|
[219] | 175 | //--------------------------------------------------------------- |
---|
| 176 | |
---|
| 177 | template <class T> |
---|
[369] | 178 | CAttributeTemplate<T>& CAttributeTemplate<T>::operator=(const T & value) |
---|
[219] | 179 | { |
---|
| 180 | this->setValue(value); |
---|
[369] | 181 | // return (this->getValue()); |
---|
| 182 | return *this; |
---|
[219] | 183 | } |
---|
| 184 | |
---|
| 185 | //--------------------------------------------------------------- |
---|
| 186 | |
---|
| 187 | template <class T> |
---|
[369] | 188 | StdString CAttributeTemplate<T>::_toString(void) const |
---|
[219] | 189 | { |
---|
| 190 | StdOStringStream oss; |
---|
[369] | 191 | if (!CType<T>::isEmpty() && this->hasId()) |
---|
| 192 | oss << this->getName() << "=\"" << CType<T>::toString() << "\""; |
---|
[219] | 193 | return (oss.str()); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | template <class T> |
---|
[369] | 197 | void CAttributeTemplate<T>::_fromString(const StdString & str) |
---|
[219] | 198 | { |
---|
[369] | 199 | CType<T>::fromString(str) ; |
---|
[219] | 200 | } |
---|
| 201 | |
---|
| 202 | //--------------------------------------------------------------- |
---|
[1642] | 203 | |
---|
| 204 | template <class T> |
---|
| 205 | StdString CAttributeTemplate<T>::_dump(void) const |
---|
| 206 | { |
---|
| 207 | StdOStringStream oss; |
---|
| 208 | if (!CType<T>::isEmpty() && this->hasId()) |
---|
| 209 | oss << this->getName() << "=\"" << CType<T>::dump() << "\""; |
---|
| 210 | return (oss.str()); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | //--------------------------------------------------------------- |
---|
[369] | 215 | /* |
---|
[219] | 216 | template <class T> |
---|
| 217 | void CAttributeTemplate<T>::toBinary (StdOStream & os) const |
---|
| 218 | { |
---|
| 219 | this->getValue()->toBinary(os); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | template <class T> |
---|
| 223 | void CAttributeTemplate<T>::fromBinary(StdIStream & is) |
---|
| 224 | { |
---|
| 225 | T value; |
---|
| 226 | FromBinary(is, value); |
---|
| 227 | this->setValue(value); |
---|
| 228 | } |
---|
[369] | 229 | */ |
---|
[300] | 230 | template <class T> |
---|
[369] | 231 | bool CAttributeTemplate<T>::_toBuffer (CBufferOut& buffer) const |
---|
[300] | 232 | { |
---|
[369] | 233 | return CType<T>::toBuffer(buffer) ; |
---|
[775] | 234 | /* |
---|
[300] | 235 | if (isEmpty()) return buffer.put(true) ; |
---|
| 236 | else |
---|
| 237 | { |
---|
| 238 | bool ret=true ; |
---|
| 239 | CType<T> val(*boost::any_cast<T>(&value)) ; |
---|
| 240 | ret&=buffer.put(false) ; |
---|
| 241 | ret&=val.toBuffer(buffer) ; |
---|
| 242 | return ret ; |
---|
| 243 | } |
---|
[369] | 244 | */ |
---|
[300] | 245 | } |
---|
| 246 | |
---|
| 247 | template <class T> |
---|
[369] | 248 | bool CAttributeTemplate<T>::_fromBuffer(CBufferIn& buffer) |
---|
[300] | 249 | { |
---|
[369] | 250 | return CType<T>::fromBuffer(buffer) ; |
---|
[775] | 251 | /* |
---|
[300] | 252 | bool empty ; |
---|
| 253 | bool ret=true ; |
---|
| 254 | ret&=buffer.get(empty) ; |
---|
[775] | 255 | if (empty) |
---|
[300] | 256 | { |
---|
| 257 | clear() ; |
---|
| 258 | return ret ; |
---|
| 259 | } |
---|
| 260 | else |
---|
| 261 | { |
---|
| 262 | if (isEmpty()) |
---|
| 263 | { |
---|
| 264 | T val ; |
---|
| 265 | setValue(val) ; |
---|
| 266 | } |
---|
| 267 | T* V=const_cast<T*>(boost::any_cast<T>(&value)) ; |
---|
| 268 | CType<T> val(*V) ; |
---|
| 269 | return val.fromBuffer(buffer) ; |
---|
| 270 | } |
---|
[369] | 271 | */ |
---|
[300] | 272 | } |
---|
[369] | 273 | /* |
---|
[300] | 274 | template <class T> |
---|
| 275 | size_t CAttributeTemplate<T>::size(void) const |
---|
[775] | 276 | { |
---|
[369] | 277 | return CType<T>::size() ;*/ |
---|
[775] | 278 | /* |
---|
[300] | 279 | if (isEmpty()) return sizeof(bool) ; |
---|
| 280 | else |
---|
| 281 | { |
---|
| 282 | CType<T> val(*const_cast<T*>(boost::any_cast<T>(&value))) ; |
---|
| 283 | return val.size()+sizeof(bool) ; |
---|
| 284 | } |
---|
[369] | 285 | */ |
---|
| 286 | /* }*/ |
---|
[300] | 287 | |
---|
[313] | 288 | template <typename T> |
---|
| 289 | void CAttributeTemplate<T>::generateCInterface(ostream& oss,const string& className) |
---|
| 290 | { |
---|
[778] | 291 | CInterface::AttributeCInterface<T>(oss, className, this->getName()); |
---|
[313] | 292 | } |
---|
[775] | 293 | |
---|
[313] | 294 | template <typename T> |
---|
| 295 | void CAttributeTemplate<T>::generateFortran2003Interface(ostream& oss,const string& className) |
---|
| 296 | { |
---|
[778] | 297 | CInterface::AttributeFortran2003Interface<T>(oss, className, this->getName()); |
---|
[313] | 298 | } |
---|
[775] | 299 | |
---|
[313] | 300 | template <typename T> |
---|
| 301 | void CAttributeTemplate<T>::generateFortranInterfaceDeclaration_(ostream& oss,const string& className) |
---|
| 302 | { |
---|
[778] | 303 | CInterface::AttributeFortranInterfaceDeclaration<T>(oss, className, this->getName() + "_"); |
---|
[313] | 304 | } |
---|
[775] | 305 | |
---|
[313] | 306 | template <typename T> |
---|
| 307 | void CAttributeTemplate<T>::generateFortranInterfaceBody_(ostream& oss,const string& className) |
---|
| 308 | { |
---|
[778] | 309 | CInterface::AttributeFortranInterfaceBody<T>(oss, className, this->getName()); |
---|
[313] | 310 | } |
---|
| 311 | |
---|
| 312 | template <typename T> |
---|
| 313 | void CAttributeTemplate<T>::generateFortranInterfaceDeclaration(ostream& oss,const string& className) |
---|
| 314 | { |
---|
[778] | 315 | CInterface::AttributeFortranInterfaceDeclaration<T>(oss, className, this->getName()); |
---|
[313] | 316 | } |
---|
[775] | 317 | |
---|
[313] | 318 | template <typename T> |
---|
| 319 | void CAttributeTemplate<T>::generateFortranInterfaceGetDeclaration_(ostream& oss,const string& className) |
---|
| 320 | { |
---|
[778] | 321 | CInterface::AttributeFortranInterfaceGetDeclaration<T>(oss, className, this->getName() + "_"); |
---|
[313] | 322 | } |
---|
[775] | 323 | |
---|
| 324 | |
---|
[313] | 325 | template <typename T> |
---|
| 326 | void CAttributeTemplate<T>::generateFortranInterfaceGetBody_(ostream& oss,const string& className) |
---|
| 327 | { |
---|
[778] | 328 | CInterface::AttributeFortranInterfaceGetBody<T>(oss, className, this->getName()); |
---|
[313] | 329 | } |
---|
| 330 | |
---|
| 331 | template <typename T> |
---|
| 332 | void CAttributeTemplate<T>::generateFortranInterfaceGetDeclaration(ostream& oss,const string& className) |
---|
| 333 | { |
---|
[778] | 334 | CInterface::AttributeFortranInterfaceGetDeclaration<T>(oss, className, this->getName()); |
---|
[313] | 335 | } |
---|
[432] | 336 | |
---|
[775] | 337 | |
---|
| 338 | /* |
---|
[219] | 339 | //--------------------------------------------------------------- |
---|
| 340 | |
---|
[775] | 341 | // Spécialisations des templates pour la fonction [toString] |
---|
[219] | 342 | |
---|
| 343 | template <> |
---|
| 344 | StdString CAttributeTemplate<bool>::toString(void) const; |
---|
| 345 | |
---|
| 346 | //--------------------------------------------------------------- |
---|
| 347 | |
---|
[775] | 348 | // Spécialisations des templates pour la fonction [fromString] |
---|
[219] | 349 | |
---|
| 350 | template <> // Chaîne de caractÚres. |
---|
| 351 | void CAttributeTemplate<StdString>::fromString(const StdString & str); |
---|
| 352 | |
---|
| 353 | template <> // Entier |
---|
| 354 | void CAttributeTemplate<int>::fromString(const StdString & str); |
---|
| 355 | |
---|
| 356 | template <> // Booléen |
---|
| 357 | void CAttributeTemplate<bool>::fromString(const StdString & str); |
---|
| 358 | |
---|
| 359 | template <> // Double |
---|
| 360 | void CAttributeTemplate<double>::fromString(const StdString & str); |
---|
| 361 | |
---|
| 362 | template<> // Tableau |
---|
| 363 | void CAttributeTemplate<ARRAY(double, 1)>::fromString(const StdString & str); |
---|
| 364 | |
---|
| 365 | //--------------------------------------------------------------- |
---|
| 366 | |
---|
[369] | 367 | // Spécialisations des templates pour la fonction [toBinary] // |
---|
[219] | 368 | |
---|
| 369 | template <> // Chaîne de caractÚres. |
---|
| 370 | void CAttributeTemplate<StdString>::toBinary (StdOStream & os) const; |
---|
| 371 | |
---|
| 372 | template <> // Entier |
---|
| 373 | void CAttributeTemplate<int>::toBinary(StdOStream & os) const; |
---|
| 374 | |
---|
| 375 | template <> // Booléen |
---|
| 376 | void CAttributeTemplate<bool>::toBinary(StdOStream & os) const; |
---|
[775] | 377 | |
---|
[219] | 378 | template <> // Double |
---|
| 379 | void CAttributeTemplate<double>::toBinary(StdOStream & os) const; |
---|
| 380 | |
---|
| 381 | //--------------------------------------------------------------- |
---|
| 382 | |
---|
[369] | 383 | // Spécialisations des templates pour la fonction [fromBinary] // |
---|
[219] | 384 | |
---|
| 385 | template <> // Chaîne de caractÚres. |
---|
| 386 | void CAttributeTemplate<StdString>::fromBinary(StdIStream & is); |
---|
| 387 | |
---|
| 388 | template <> // Entier |
---|
| 389 | void CAttributeTemplate<int>::fromBinary(StdIStream & is); |
---|
| 390 | |
---|
| 391 | template <> // Booléen |
---|
| 392 | void CAttributeTemplate<bool>::fromBinary(StdIStream & is); |
---|
[775] | 393 | |
---|
[219] | 394 | template <> // Double |
---|
| 395 | void CAttributeTemplate<double>::fromBinary(StdIStream & is); |
---|
| 396 | |
---|
| 397 | ///-------------------------------------------------------------- |
---|
[775] | 398 | */ |
---|
[335] | 399 | } // namespace xios |
---|
[219] | 400 | |
---|
[591] | 401 | #endif // __XIOS_CAttributeTemplate_impl__ |
---|