attribute_template_impl.hpp 11 KB

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