source: XIOS/trunk/src/type/type_specialisation.hpp @ 501

Last change on this file since 501 was 501, checked in by ymipsl, 10 years ago

Add licence copyright to all file ond directory src using the command :
svn propset -R copyright -F header_licence src

XIOS is now officialy under CeCILL licence

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 11.5 KB
Line 
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"
8#include "type.hpp"
9#include <string>
10#include <boost/algorithm/string.hpp>
11 
12namespace xios
13{
14
15// template specialization for bool
16
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  }
52
53// template specialization for string
54
55  template <>
56  size_t CType<string>::_size() const
57  {
58    size_t typeSize=0 ;
59    typeSize+=sizeof(size_t) ;
60    typeSize+=ptrValue->size() ;
61    return typeSize ;
62  }
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  }
72 
73  template <>
74  void CType<string>::_fromString(const string& str)
75  {
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  {
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  }
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  } 
137
138  template <>
139  bool CType_ref<string>::_fromBuffer(CBufferIn& buffer) const
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 ;
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  }     
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/*
204template<size_t numDim>
205boost::detail::multi_array::extent_gen<numDim> getExtentNull(void) { return getExtentNull<numDim-1>()[0];}
206
207template<>
208boost::detail::multi_array::extent_gen<1> getExtentNull<1>(void) { return extents[0]; }
209*/
210/*
211#define CTYPE_ARRAY(ValueType,NumsDims)                                \
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                                                                        \   
220  template<>                                                           \
221  size_t CType< ARRAY(ValueType,NumsDims)>::_size() const               \
222  {                                                                    \
223     return (*(this->ptrValue))->getSize() ;                           \
224  }                                                                    \
225                                                                       \
226  template<>                                                           \
227  size_t CType_ref< ARRAY(ValueType,NumsDims)>::_size() const           \
228  {                                                                    \
229     return (*(this->ptrValue))->getSize() ;                           \
230  }                                                                    \
231                                                                       \
232  template <>                                                          \
233  bool CType<ARRAY(ValueType,NumsDims)>::_toBuffer(CBufferOut& buffer) const        \
234  {                                                                    \
235      return (*(this->ptrValue))->toBuffer(buffer) ;                   \
236  }                                                                    \
237                                                                       \
238  template <>                                                          \
239  bool CType_ref<ARRAY(ValueType,NumsDims)>::_toBuffer(CBufferOut& buffer) const        \
240  {                                                                    \
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  {                                                                    \
261    shared_ptr<CArray<ValueType, NumsDims> > tmp(new CArray<ValueType, NumsDims>() ) ; \
262    *(this->ptrValue)=tmp ;\
263    return (*(this->ptrValue))->fromBuffer(buffer) ;                   \
264  }                                                                    \
265                                                                       \
266                                                                       \
267  template <>                                                          \
268  void CType<ARRAY(ValueType,NumsDims)>::_fromString(const string& str)       \
269  {                                                                    \
270                                      \
271  }                                                                    \
272                                                                       \
273  template <>                                                          \
274  void CType_ref<ARRAY(ValueType,NumsDims)>::_fromString(const string& str) const \
275  {                                                                    \
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                               \
288   return string("") ;                                                 \
289  }                                                                    \
290                                                                          \
291  template <>                                                          \
292  string CType_ref<ARRAY(ValueType,NumsDims)>::_toString(void) const                    \
293  {                                                                    \
294                                      \
295   return string("") ;                                                 \
296  }
297//CTYPE_ARRAY(double,1)
298
299//CTYPE_ARRAY(double,2)
300
301
302CTYPE_ARRAY(int,1)
303CTYPE_ARRAY(int,2)
304CTYPE_ARRAY(int,3)
305CTYPE_ARRAY(bool,1)
306CTYPE_ARRAY(bool,2)
307CTYPE_ARRAY(bool,3)
308CTYPE_ARRAY(double,1)
309CTYPE_ARRAY(double,2)
310CTYPE_ARRAY(double,3)
311CTYPE_ARRAY(float,1)
312CTYPE_ARRAY(float,2)
313CTYPE_ARRAY(float,3)
314*/
315} 
316
317#endif 
Note: See TracBrowser for help on using the repository browser.