source: XIOS/dev/common/src/attribute_template_impl.hpp @ 302

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

nouvelle version de developpement de xios

  • nouvelle interface fortran
  • recodage complet de la couche de communication
  • et bien d'autres choses...

YM

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