source: XIOS/trunk/src/type/type_impl.hpp @ 1477

Last change on this file since 1477 was 1477, checked in by oabramkina, 6 years ago

Implementing exception handling for accessing attribute value.

In certain cases attribute's id will be printed in the error message.

  • 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.2 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  {
89    checkEmpty();
90   return *ptrValue ;
91  }
92
93  template <typename T>
94  const T& CType<T>::get(void) const
95  {
96    checkEmpty();
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   {
[1477]124     try
125     {
126       checkEmpty();
127     }
128     catch (xios::CException & exc)
129     {
130       StdString msg("Data is not initialized\n");
131       ERROR("template <typename T> void CType<T>::checkEmpty(void) const", << msg);
132     }
133
[369]134    return *ptrValue ;
135   }
[308]136
[369]137   template <typename T>
[748]138   CType<T>::operator const T&() const
139   {
140    checkEmpty();
141    return *ptrValue ;
142   }
143
144   template <typename T>
[369]145   CType<T>* CType<T>::_clone(void) const
146   {
147     checkEmpty();
148     return new CType(*this) ;
149   }
150
[748]151
[308]152  template <typename T>
[369]153  void CType<T>::_fromString(const string& str)
[308]154  {
155    istringstream iss(str);
[369]156    allocate() ;
[308]157    iss>>*ptrValue ;
158  }
159
160  template <typename T>
[369]161  size_t CType<T>::_size(void) const
[308]162  {
163    return sizeof(T) ;
164  }
[748]165
[308]166  template <typename T>
[369]167  bool CType<T>::_isEmpty(void) const
[308]168  {
[369]169    return empty ;
170  }
[748]171
[369]172  template <typename T>
173  string CType<T>::_toString(void) const
174  {
[308]175    ostringstream oss;
[369]176    checkEmpty();
[308]177    oss<<*ptrValue ;
178    return oss.str() ;
179  }
[748]180
[308]181  template <typename T>
[369]182  bool CType<T>::_toBuffer(CBufferOut& buffer) const
[308]183  {
[369]184    checkEmpty();
[308]185    return buffer.put(*ptrValue) ;
186  }
[748]187
[308]188  template <typename T>
[369]189  bool CType<T>::_fromBuffer(CBufferIn& buffer)
[308]190  {
[369]191    allocate() ;
[308]192    return buffer.get(*ptrValue) ;
193  }
[369]194
[748]195
[308]196  template <typename T>
[369]197  void CType<T>::allocate(void)
[308]198  {
[748]199    if (empty)
[369]200    {
201      ptrValue = new T ;
202      empty=false ;
203    }
[308]204  }
[748]205
[308]206  template <typename T>
[369]207  void CType<T>::_reset(void)
[308]208  {
[748]209    if (!empty)
[369]210    {
211      delete ptrValue ;
212      empty=true ;
213    }
[308]214  }
[748]215
[308]216  template <typename T>
[369]217  void CType<T>::checkEmpty(void) const
[308]218  {
[1477]219//    if (empty) ERROR("template <typename T> void CType<T>::checkEmpty(void) const", << "Data is not initialized") ;
220    if (empty) throw CException();
[748]221  }
[308]222
[1105]223  template <typename T>
224  bool operator==(const CType<T>& lhs, const T& rhs)
225  {
226    if (lhs.isEmpty()) return false;
227    return (*lhs.ptrValue == rhs);   
228  }
[748]229
[308]230  template <typename T>
[1105]231  bool operator==(const T& lhs, const CType<T>& rhs)
232  {
233    return (rhs == lhs);
234  }
235
236  template <typename T>
237  bool operator==(const CType<T>& lhs, const CType<T>& rhs)
238  {
239    if ((lhs.isEmpty() && !rhs.isEmpty()) || (!lhs.isEmpty() && rhs.isEmpty())) return false;
240    if (lhs.isEmpty() && rhs.isEmpty()) return true;
241   
242    return (*lhs.ptrValue == *rhs.ptrValue);
243  }
244
245  template <typename T>
[308]246  CBufferOut& operator<<(CBufferOut& buffer, const CType<T>& type)
247  {
248    if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)",
[680]249                                      << "Not enough free space in buffer to queue the data.");
[308]250    return buffer ;
251  }
252
253
254  template <typename T>
255  CBufferOut& operator<<(CBufferOut& buffer, const T& type)
256  {
257    if (!CType<T>(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, const T& type)",
[680]258                                                << "Not enough free space in buffer to queue the data.");
[308]259    return buffer ;
260  }
[680]261
[308]262  template <typename T>
[369]263  CBufferIn& operator>>(CBufferIn& buffer, CType<T>& type)
[308]264  {
[369]265    if (! type.fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)",
[680]266                                         << "Not enough data in buffer to unqueue the data.");
[308]267    return buffer ;
268  }
[680]269
[308]270  template <typename T>
271  CMessage& operator<<(CMessage& msg, const T& type)
272  {
[401]273    msg.push(CType<T>(type)) ;
[308]274    return msg ;
275  }
276
277}
278
279#endif
Note: See TracBrowser for help on using the repository browser.