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
Line 
1#ifndef __XIOS_TYPE_IMPL__
2#define __XIOS_TYPE_IMPL__
3
4#include "xios_spl.hpp"
5#include "exception.hpp"
6#include "buffer_in.hpp"
7#include "buffer_out.hpp"
8#include "message.hpp"
9
10
11
12
13namespace xios
14{
15
16  using namespace std;
17
18  template <typename T>
19  CType<T>::CType(void)
20  {
21    empty=true ;
22  }
23
24  template <typename T>
25  CType<T>::CType(const T& val)
26  {
27    empty=true ;
28    set(val) ;
29  }
30
31  template <typename T>
32  CType<T>::CType(const CType<T>& type)
33  {
34    empty=true ;
35    set(type) ;
36  }
37
38  template <typename T>
39  CType<T>::CType(const CType_ref<T>& type)
40  {
41    empty=true ;
42    set(type) ;
43  }
44
45  template <typename T>
46  void CType<T>::set(const T& val)
47  {
48    if (empty)
49    {
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
61    {
62      if (empty)
63      {
64        ptrValue = new T(*type.ptrValue) ;
65        empty=false ;
66      }
67      else *ptrValue = *type.ptrValue ;
68    }
69  }
70
71  template <typename T>
72  void CType<T>::set(const CType_ref<T>& type)
73  {
74    if (type.isEmpty()) reset() ;
75    else
76    {
77      if (empty)
78      {
79        ptrValue = new T(*type.ptrValue) ;
80        empty=false ;
81      }
82      else *ptrValue = *type.ptrValue ;
83    }
84  }
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  }
99
100  template <typename T>
101  CType<T>& CType<T>::operator = (const T& val)
102  {
103    set(val) ;
104    return *this ;
105  }
106
107  template <typename T>
108  CType<T>& CType<T>::operator = (const CType<T>& type)
109  {
110    set(type) ;
111    return *this ;
112  }
113
114  template <typename T>
115  CType<T>& CType<T>::operator = (const CType_ref<T>& type)
116  {
117    set(type) ;
118    return *this ;
119  }
120
121   template <typename T>
122   CType<T>::operator T&()
123   {
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
134    return *ptrValue ;
135   }
136
137   template <typename T>
138   CType<T>::operator const T&() const
139   {
140    checkEmpty();
141    return *ptrValue ;
142   }
143
144   template <typename T>
145   CType<T>* CType<T>::_clone(void) const
146   {
147     checkEmpty();
148     return new CType(*this) ;
149   }
150
151
152  template <typename T>
153  void CType<T>::_fromString(const string& str)
154  {
155    istringstream iss(str);
156    allocate() ;
157    iss>>*ptrValue ;
158  }
159
160  template <typename T>
161  size_t CType<T>::_size(void) const
162  {
163    return sizeof(T) ;
164  }
165
166  template <typename T>
167  bool CType<T>::_isEmpty(void) const
168  {
169    return empty ;
170  }
171
172  template <typename T>
173  string CType<T>::_toString(void) const
174  {
175    ostringstream oss;
176    checkEmpty();
177    oss<<*ptrValue ;
178    return oss.str() ;
179  }
180
181  template <typename T>
182  bool CType<T>::_toBuffer(CBufferOut& buffer) const
183  {
184    checkEmpty();
185    return buffer.put(*ptrValue) ;
186  }
187
188  template <typename T>
189  bool CType<T>::_fromBuffer(CBufferIn& buffer)
190  {
191    allocate() ;
192    return buffer.get(*ptrValue) ;
193  }
194
195
196  template <typename T>
197  void CType<T>::allocate(void)
198  {
199    if (empty)
200    {
201      ptrValue = new T ;
202      empty=false ;
203    }
204  }
205
206  template <typename T>
207  void CType<T>::_reset(void)
208  {
209    if (!empty)
210    {
211      delete ptrValue ;
212      empty=true ;
213    }
214  }
215
216  template <typename T>
217  void CType<T>::checkEmpty(void) const
218  {
219//    if (empty) ERROR("template <typename T> void CType<T>::checkEmpty(void) const", << "Data is not initialized") ;
220    if (empty) throw CException();
221  }
222
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  }
229
230  template <typename T>
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>
246  CBufferOut& operator<<(CBufferOut& buffer, const CType<T>& type)
247  {
248    if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)",
249                                      << "Not enough free space in buffer to queue the data.");
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)",
258                                                << "Not enough free space in buffer to queue the data.");
259    return buffer ;
260  }
261
262  template <typename T>
263  CBufferIn& operator>>(CBufferIn& buffer, CType<T>& type)
264  {
265    if (! type.fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)",
266                                         << "Not enough data in buffer to unqueue the data.");
267    return buffer ;
268  }
269
270  template <typename T>
271  CMessage& operator<<(CMessage& msg, const T& type)
272  {
273    msg.push(CType<T>(type)) ;
274    return msg ;
275  }
276
277}
278
279#endif
Note: See TracBrowser for help on using the repository browser.