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

Last change on this file since 532 was 532, checked in by rlacroix, 10 years ago

Add a new attribute type for dates and use it for the context's start_date and time_origin.

The "xios_date" type should now be used to get/set date attributes through the Fortran interface. This avoids using strings to manipulate dates.

  • 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: 14.9 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#include "date.hpp"
12 
13namespace xios
14{
15
16// template specialization for bool
17
18  template <> void CType<bool>::_fromString(const string& str)
19  {
20    string tmpStr=boost::to_lower_copy(boost::trim_copy(str)) ;
21    if (tmpStr=="true" || tmpStr==".true." || tmpStr=="yes" || tmpStr=="y") set(true) ;
22    else if (tmpStr=="false" || tmpStr==".false." || tmpStr=="no" || tmpStr=="n") set(false) ;
23    else ERROR("template <> CType<bool>::fromString(const string& str)",<< tmpStr << " cannot be converted in a boolean value") ;
24  }
25 
26  template <> string CType<bool>::_toString(void) const
27  {
28    if (get()) return string("true") ;
29    else return string("false") ;
30  }
31 
32  template <> void CType_ref<bool>::_fromString(const string& str) const
33  {
34    string tmpStr=boost::to_lower_copy(boost::trim_copy(str)) ;
35    if (tmpStr=="true" || tmpStr==".true." || tmpStr=="yes" || tmpStr=="y") set(true) ;
36    else if (tmpStr=="false" || tmpStr==".false." || tmpStr=="no" || tmpStr=="n") set(false) ;
37    else ERROR("template <> CType<bool>::fromString(const string& str)",<< tmpStr << " cannot be converted in a boolean value") ;
38  }
39 
40  template <> void CType_ref<bool>::_fromString(const string& str)
41  {
42    string tmpStr=boost::to_lower_copy(boost::trim_copy(str)) ;
43    if (tmpStr=="true" || tmpStr==".true." || tmpStr=="yes" || tmpStr=="y") set(true) ;
44    else if (tmpStr=="false" || tmpStr==".false." || tmpStr=="no" || tmpStr=="n") set(false) ;
45    else ERROR("template <> CType<bool>::fromString(const string& str)",<< tmpStr << " cannot be converted in a boolean value") ;
46  }
47 
48  template <> string CType_ref<bool>::_toString(void) const
49  {
50    if (get()) return string("true") ;
51    else return string("false") ;
52  }
53
54// template specialization for string
55
56  template <>
57  size_t CType<string>::_size() const
58  {
59    size_t typeSize=0 ;
60    typeSize+=sizeof(size_t) ;
61    typeSize+=ptrValue->size() ;
62    return typeSize ;
63  }
64
65  template <>
66  size_t CType_ref<string>::_size() const
67  {
68    size_t typeSize=0 ;
69    typeSize+=sizeof(size_t) ;
70    typeSize+=ptrValue->size() ;
71    return typeSize ;
72  }
73 
74  template <>
75  void CType<string>::_fromString(const string& str)
76  {
77    allocate() ;
78    *ptrValue=str ;
79  } 
80
81  template <>
82  void CType_ref<string>::_fromString(const string& str)
83  {
84    checkEmpty() ;
85    *ptrValue=str ;
86  } 
87
88  template <>
89  void CType_ref<string>::_fromString(const string& str) const
90  {
91    checkEmpty() ;
92    *ptrValue=str ;
93  } 
94 
95  template <>
96  bool CType<string>::_toBuffer(CBufferOut& buffer) const
97  {
98    if (buffer.remain()<size()) return false ;
99    else
100    {
101      bool ret=true ;
102      if (ret) ret&=buffer.put(ptrValue->size()) ;
103      if (ret) ret&=buffer.put(ptrValue->data(),ptrValue->size()) ;
104      return ret ;
105    }
106  }
107 
108  template <>
109  bool CType_ref<string>::_toBuffer(CBufferOut& buffer) const
110  {
111    if (buffer.remain()<size()) return false ;
112    else
113    {
114      bool ret=true ;
115      if (ret) ret&=buffer.put(ptrValue->size()) ;
116      if (ret) ret&=buffer.put(ptrValue->data(),ptrValue->size()) ;
117      return ret ;
118    }
119  }
120 
121 
122 
123  template <>
124  bool CType<string>::_fromBuffer(CBufferIn& buffer)
125  {
126    allocate() ;
127    bool ret=true ;
128    size_t typeSize ;
129    if (ret) ret&=buffer.get(typeSize) ;
130   
131    char* str;
132    str= (char*) buffer.ptr() ;
133    if (ret) buffer.advance(typeSize) ;
134    if (ret) *ptrValue=string(str,typeSize) ;
135   
136    return ret ;
137  } 
138
139  template <>
140  bool CType_ref<string>::_fromBuffer(CBufferIn& buffer) const
141  {
142    bool ret=true ;
143    size_t typeSize ;
144    if (ret) ret&=buffer.get(typeSize) ;
145   
146    char* str;
147    str= (char*) buffer.ptr() ;
148    if (ret) buffer.advance(typeSize) ;
149    if (ret) *ptrValue=string(str,typeSize) ;
150   
151    return ret ;
152  }   
153
154  template <>
155  bool CType_ref<string>::_fromBuffer(CBufferIn& buffer)
156  {
157    bool ret=true ;
158    size_t typeSize ;
159    if (ret) ret&=buffer.get(typeSize) ;
160   
161    char* str;
162    str= (char*) buffer.ptr() ;
163    if (ret) buffer.advance(typeSize) ;
164    if (ret) *ptrValue=string(str,typeSize) ;
165   
166    return ret ;
167  }     
168
169// template specializations for CDate
170
171  template <>
172  size_t CType<CDate>::_size() const
173  {
174    return 6 * sizeof(int);
175  }
176
177  template <>
178  size_t CType_ref<CDate>::_size() const
179  {
180    return 6 * sizeof(int);
181  }
182 
183  /*template <>
184  void CType<CDate>::_fromString(const string& str)
185  {
186    allocate() ;
187    *ptrValue=str ;
188  } 
189
190  template <>
191  void CType_ref<CDate>::_fromString(const string& str)
192  {
193    checkEmpty() ;
194    *ptrValue=str ;
195  } 
196
197  template <>
198  void CType_ref<CDate>::_fromString(const string& str) const
199  {
200    checkEmpty() ;
201    *ptrValue=str ;
202  }*/ 
203 
204  template <>
205  bool CType<CDate>::_toBuffer(CBufferOut& buffer) const
206  {
207    if (buffer.remain() < size()) return false;
208    else
209    {
210      bool ret = true;
211      if (ret) ret &= buffer.put(ptrValue->getYear());
212      if (ret) ret &= buffer.put(ptrValue->getMonth());
213      if (ret) ret &= buffer.put(ptrValue->getDay());
214      if (ret) ret &= buffer.put(ptrValue->getHour());
215      if (ret) ret &= buffer.put(ptrValue->getMinute());
216      if (ret) ret &= buffer.put(ptrValue->getSecond());
217      return ret;
218    }
219  }
220 
221  template <>
222  bool CType_ref<CDate>::_toBuffer(CBufferOut& buffer) const
223  {
224    if (buffer.remain() < size()) return false;
225    else
226    {
227      bool ret = true;
228      if (ret) ret &= buffer.put(ptrValue->getYear());
229      if (ret) ret &= buffer.put(ptrValue->getMonth());
230      if (ret) ret &= buffer.put(ptrValue->getDay());
231      if (ret) ret &= buffer.put(ptrValue->getHour());
232      if (ret) ret &= buffer.put(ptrValue->getMinute());
233      if (ret) ret &= buffer.put(ptrValue->getSecond());
234      return ret;
235    }
236  }
237
238  template <>
239  bool CType<CDate>::_fromBuffer(CBufferIn& buffer)
240  {
241    allocate();
242    bool ret = true;
243
244    int year, month, day, hour, min, sec;
245    if (ret) ret &= buffer.get(year);
246    if (ret) ret &= buffer.get(month);
247    if (ret) ret &= buffer.get(day);
248    if (ret) ret &= buffer.get(hour);
249    if (ret) ret &= buffer.get(min);
250    if (ret) ret &= buffer.get(sec);
251    if (ret)
252    {
253      ptrValue->setYear(year);
254      ptrValue->setMonth(month);
255      ptrValue->setDay(day);
256      ptrValue->setHour(hour);
257      ptrValue->setMinute(min);
258      ptrValue->setSecond(sec);
259    }
260
261    return ret;
262  }
263
264  template <>
265  bool CType_ref<CDate>::_fromBuffer(CBufferIn& buffer) const
266  {
267    bool ret = true;
268   
269    int year, month, day, hour, min, sec;
270    if (ret) ret &= buffer.get(year);
271    if (ret) ret &= buffer.get(month);
272    if (ret) ret &= buffer.get(day);
273    if (ret) ret &= buffer.get(hour);
274    if (ret) ret &= buffer.get(min);
275    if (ret) ret &= buffer.get(sec);
276    if (ret)
277    {
278      ptrValue->setYear(year);
279      ptrValue->setMonth(month);
280      ptrValue->setDay(day);
281      ptrValue->setHour(hour);
282      ptrValue->setMinute(min);
283      ptrValue->setSecond(sec);
284    }
285
286    return ret;
287  }
288
289  template <>
290  bool CType_ref<CDate>::_fromBuffer(CBufferIn& buffer)
291  {
292    bool ret = true;
293   
294    int year, month, day, hour, min, sec;
295    if (ret) ret &= buffer.get(year);
296    if (ret) ret &= buffer.get(month);
297    if (ret) ret &= buffer.get(day);
298    if (ret) ret &= buffer.get(hour);
299    if (ret) ret &= buffer.get(min);
300    if (ret) ret &= buffer.get(sec);
301    if (ret)
302    {
303      ptrValue->setYear(year);
304      ptrValue->setMonth(month);
305      ptrValue->setDay(day);
306      ptrValue->setHour(hour);
307      ptrValue->setMinute(min);
308      ptrValue->setSecond(sec);
309    }
310
311    return ret;
312  }
313
314  // template specialisation for CArray
315/*
316  template<>
317  size_t CType< ARRAY(int, 1)>::size() const
318  {
319     return (*(this->ptrValue))->getSize() ;
320  }
321 
322  template <>
323  bool CType<ARRAY(int, 1)>::toBuffer(CBufferOut& buffer) const
324  {
325      return (*(this->ptrValue))->toBuffer(buffer) ;
326  }
327
328  template <>
329  bool CType<ARRAY(int, 1)>::fromBuffer(CBufferIn& buffer) const
330  {
331    return (*(this->ptrValue))->fromBuffer(buffer) ;
332  }
333
334  template <>
335  void CType<ARRAY(int, 1)>::fromString(const string& str) const
336  {
337 // to implement
338  }
339
340  template <>
341  string CType<ARRAY(int, 1)>::toString(void) const
342  {
343 // to implement
344   return string("") ;
345 
346  }
347*/
348
349
350/*
351template<size_t numDim>
352boost::detail::multi_array::extent_gen<numDim> getExtentNull(void) { return getExtentNull<numDim-1>()[0];}
353
354template<>
355boost::detail::multi_array::extent_gen<1> getExtentNull<1>(void) { return extents[0]; }
356*/
357/*
358#define CTYPE_ARRAY(ValueType,NumsDims)                                \
359                                                                       \
360  template <>                                                          \
361  void CType<ARRAY(ValueType,NumsDims)>::allocate(void)  \
362  {                                                                    \
363    if (empty) ptrValue=new shared_ptr<CArray<ValueType, NumsDims> >(new  CArray<ValueType, NumsDims>()) ;\
364    empty=false ;                                                      \
365  }                                                                    \
366                                                                        \   
367  template<>                                                           \
368  size_t CType< ARRAY(ValueType,NumsDims)>::_size() const               \
369  {                                                                    \
370     return (*(this->ptrValue))->getSize() ;                           \
371  }                                                                    \
372                                                                       \
373  template<>                                                           \
374  size_t CType_ref< ARRAY(ValueType,NumsDims)>::_size() const           \
375  {                                                                    \
376     return (*(this->ptrValue))->getSize() ;                           \
377  }                                                                    \
378                                                                       \
379  template <>                                                          \
380  bool CType<ARRAY(ValueType,NumsDims)>::_toBuffer(CBufferOut& buffer) const        \
381  {                                                                    \
382      return (*(this->ptrValue))->toBuffer(buffer) ;                   \
383  }                                                                    \
384                                                                       \
385  template <>                                                          \
386  bool CType_ref<ARRAY(ValueType,NumsDims)>::_toBuffer(CBufferOut& buffer) const        \
387  {                                                                    \
388      return (*(this->ptrValue))->toBuffer(buffer) ;                   \
389  }                                                                    \
390                                                                       \
391  template <>                                                          \
392  bool CType<ARRAY(ValueType,NumsDims)>::_fromBuffer(CBufferIn& buffer)       \
393  {                                                                    \
394    allocate();                                                        \
395    return (*(ptrValue))->fromBuffer(buffer) ;                         \
396  }                                                                    \
397                                                                       \
398  template <>                                                          \
399  bool CType_ref<ARRAY(ValueType,NumsDims)>::_fromBuffer(CBufferIn& buffer) const  \
400  {                                                                    \
401    checkEmpty() ;                                                     \
402    return (*(this->ptrValue))->fromBuffer(buffer) ;                   \
403  }                                                                    \
404                                                                       \
405 template <>                                                          \
406  bool CType_ref<ARRAY(ValueType,NumsDims)>::_fromBuffer(CBufferIn& buffer)   \
407  {                                                                    \
408    shared_ptr<CArray<ValueType, NumsDims> > tmp(new CArray<ValueType, NumsDims>() ) ; \
409    *(this->ptrValue)=tmp ;\
410    return (*(this->ptrValue))->fromBuffer(buffer) ;                   \
411  }                                                                    \
412                                                                       \
413                                                                       \
414  template <>                                                          \
415  void CType<ARRAY(ValueType,NumsDims)>::_fromString(const string& str)       \
416  {                                                                    \
417                                      \
418  }                                                                    \
419                                                                       \
420  template <>                                                          \
421  void CType_ref<ARRAY(ValueType,NumsDims)>::_fromString(const string& str) const \
422  {                                                                    \
423                                        \
424  }                                                                    \
425  template <>                                                          \
426  void CType_ref<ARRAY(ValueType,NumsDims)>::_fromString(const string& str) \
427  {                                                                    \
428                                                \
429  }                                                                    \
430                                                                          \
431  template <>                                                          \
432  string CType<ARRAY(ValueType,NumsDims)>::_toString(void) const                    \
433  {                                                                    \
434                               \
435   return string("") ;                                                 \
436  }                                                                    \
437                                                                          \
438  template <>                                                          \
439  string CType_ref<ARRAY(ValueType,NumsDims)>::_toString(void) const                    \
440  {                                                                    \
441                                      \
442   return string("") ;                                                 \
443  }
444//CTYPE_ARRAY(double,1)
445
446//CTYPE_ARRAY(double,2)
447
448
449CTYPE_ARRAY(int,1)
450CTYPE_ARRAY(int,2)
451CTYPE_ARRAY(int,3)
452CTYPE_ARRAY(bool,1)
453CTYPE_ARRAY(bool,2)
454CTYPE_ARRAY(bool,3)
455CTYPE_ARRAY(double,1)
456CTYPE_ARRAY(double,2)
457CTYPE_ARRAY(double,3)
458CTYPE_ARRAY(float,1)
459CTYPE_ARRAY(float,2)
460CTYPE_ARRAY(float,3)
461*/
462} 
463
464#endif 
Note: See TracBrowser for help on using the repository browser.