source: XIOS/trunk/src/attribute_template_impl.hpp @ 335

Last change on this file since 335 was 335, checked in by ymipsl, 12 years ago

Change namespace xmlioserver -> xios

YM

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