source: XIOS/dev/branch_openmp/src/type/type_impl.hpp @ 1482

Last change on this file since 1482 was 1482, checked in by yushan, 6 years ago

Branch EP merged with Dev_cmip6 @r1481

  • 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: 5.0 KB
RevLine 
[308]1#ifndef __XIOS_TYPE_IMPL__
2#define __XIOS_TYPE_IMPL__
3
[591]4#include "xios_spl.hpp"
[308]5#include "exception.hpp"
6#include "buffer_in.hpp"
7#include "buffer_out.hpp"
8#include "message.hpp"
9
10
11
[369]12
[335]13namespace xios
[308]14{
[748]15
[308]16  using namespace std;
[748]17
[308]18  template <typename T>
[369]19  CType<T>::CType(void)
20  {
21    empty=true ;
22  }
[748]23
[369]24  template <typename T>
[308]25  CType<T>::CType(const T& val)
26  {
[369]27    empty=true ;
28    set(val) ;
[308]29  }
[748]30
[308]31  template <typename T>
[369]32  CType<T>::CType(const CType<T>& type)
[308]33  {
[369]34    empty=true ;
35    set(type) ;
[308]36  }
37
38  template <typename T>
[369]39  CType<T>::CType(const CType_ref<T>& type)
[308]40  {
[369]41    empty=true ;
42    set(type) ;
[748]43  }
44
[369]45  template <typename T>
46  void CType<T>::set(const T& val)
47  {
[748]48    if (empty)
49    {
[369]50      ptrValue = new T(val) ;
51      empty=false ;
52    }
53    else *ptrValue = val ;
54  }
55
56  template <typename T>
57  void CType<T>::set(const CType<T>& type)
58  {
59    if (type.isEmpty()) reset() ;
60    else
[308]61    {
[369]62      if (empty)
[748]63      {
[369]64        ptrValue = new T(*type.ptrValue) ;
65        empty=false ;
66      }
67      else *ptrValue = *type.ptrValue ;
[308]68    }
[369]69  }
70
71  template <typename T>
72  void CType<T>::set(const CType_ref<T>& type)
73  {
74    if (type.isEmpty()) reset() ;
75    else
[308]76    {
[369]77      if (empty)
[748]78      {
[369]79        ptrValue = new T(*type.ptrValue) ;
80        empty=false ;
81      }
82      else *ptrValue = *type.ptrValue ;
[308]83    }
84  }
[369]85
86  template <typename T>
87  T& CType<T>::get(void)
88  {
[1482]89    this->checkEmpty();
90    return *ptrValue ;
[369]91  }
92
93  template <typename T>
94  const T& CType<T>::get(void) const
95  {
[1482]96    this->checkEmpty();
[369]97    return *ptrValue ;
98  }
[748]99
[369]100  template <typename T>
101  CType<T>& CType<T>::operator = (const T& val)
102  {
103    set(val) ;
104    return *this ;
105  }
[748]106
[369]107  template <typename T>
108  CType<T>& CType<T>::operator = (const CType<T>& type)
109  {
110    set(type) ;
111    return *this ;
112  }
[748]113
[369]114  template <typename T>
115  CType<T>& CType<T>::operator = (const CType_ref<T>& type)
116  {
117    set(type) ;
118    return *this ;
119  }
[748]120
[369]121   template <typename T>
122   CType<T>::operator T&()
123   {
[1482]124     this->checkEmpty();
125     return *ptrValue ;
[369]126   }
[308]127
[369]128   template <typename T>
[748]129   CType<T>::operator const T&() const
130   {
[1482]131     this->checkEmpty();
132     return *ptrValue ;
[748]133   }
134
135   template <typename T>
[369]136   CType<T>* CType<T>::_clone(void) const
137   {
[1482]138     this->checkEmpty();
[369]139     return new CType(*this) ;
140   }
141
[748]142
[308]143  template <typename T>
[369]144  void CType<T>::_fromString(const string& str)
[308]145  {
146    istringstream iss(str);
[369]147    allocate() ;
[308]148    iss>>*ptrValue ;
149  }
150
151  template <typename T>
[369]152  size_t CType<T>::_size(void) const
[308]153  {
154    return sizeof(T) ;
155  }
[748]156
[308]157  template <typename T>
[369]158  bool CType<T>::_isEmpty(void) const
[308]159  {
[369]160    return empty ;
161  }
[748]162
[369]163  template <typename T>
164  string CType<T>::_toString(void) const
165  {
[308]166    ostringstream oss;
[1482]167    this->checkEmpty();
[308]168    oss<<*ptrValue ;
169    return oss.str() ;
170  }
[748]171
[308]172  template <typename T>
[369]173  bool CType<T>::_toBuffer(CBufferOut& buffer) const
[308]174  {
[1482]175    this->checkEmpty();
[308]176    return buffer.put(*ptrValue) ;
177  }
[748]178
[308]179  template <typename T>
[369]180  bool CType<T>::_fromBuffer(CBufferIn& buffer)
[308]181  {
[369]182    allocate() ;
[308]183    return buffer.get(*ptrValue) ;
184  }
[369]185
[748]186
[308]187  template <typename T>
[369]188  void CType<T>::allocate(void)
[308]189  {
[748]190    if (empty)
[369]191    {
192      ptrValue = new T ;
193      empty=false ;
194    }
[308]195  }
[748]196
[308]197  template <typename T>
[369]198  void CType<T>::_reset(void)
[308]199  {
[748]200    if (!empty)
[369]201    {
202      delete ptrValue ;
203      empty=true ;
204    }
[308]205  }
[748]206
[308]207  template <typename T>
[1482]208  void CType<T>::_checkEmpty(void) const
[308]209  {
[680]210    if (empty) ERROR("template <typename T> void CType<T>::checkEmpty(void) const", << "Data is not initialized") ;
[748]211  }
[308]212
[1105]213  template <typename T>
214  bool operator==(const CType<T>& lhs, const T& rhs)
215  {
216    if (lhs.isEmpty()) return false;
217    return (*lhs.ptrValue == rhs);   
218  }
[748]219
[308]220  template <typename T>
[1105]221  bool operator==(const T& lhs, const CType<T>& rhs)
222  {
223    return (rhs == lhs);
224  }
225
226  template <typename T>
227  bool operator==(const CType<T>& lhs, const CType<T>& rhs)
228  {
229    if ((lhs.isEmpty() && !rhs.isEmpty()) || (!lhs.isEmpty() && rhs.isEmpty())) return false;
230    if (lhs.isEmpty() && rhs.isEmpty()) return true;
231   
232    return (*lhs.ptrValue == *rhs.ptrValue);
233  }
234
235  template <typename T>
[308]236  CBufferOut& operator<<(CBufferOut& buffer, const CType<T>& type)
237  {
238    if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)",
[680]239                                      << "Not enough free space in buffer to queue the data.");
[308]240    return buffer ;
241  }
242
243
244  template <typename T>
245  CBufferOut& operator<<(CBufferOut& buffer, const T& type)
246  {
247    if (!CType<T>(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, const T& type)",
[680]248                                                << "Not enough free space in buffer to queue the data.");
[308]249    return buffer ;
250  }
[680]251
[308]252  template <typename T>
[369]253  CBufferIn& operator>>(CBufferIn& buffer, CType<T>& type)
[308]254  {
[369]255    if (! type.fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)",
[680]256                                         << "Not enough data in buffer to unqueue the data.");
[308]257    return buffer ;
258  }
[680]259
[308]260  template <typename T>
261  CMessage& operator<<(CMessage& msg, const T& type)
262  {
[401]263    msg.push(CType<T>(type)) ;
[308]264    return msg ;
265  }
266
267}
268
269#endif
Note: See TracBrowser for help on using the repository browser.