source: XIOS3/trunk/src/type/enum_ref_impl.hpp

Last change on this file was 2629, checked in by jderouillat, 2 months ago

Delete boost dependencies, the few features used are replaced by functions stored in extern/boost_extraction

  • 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: 8.2 KB
Line 
1#ifndef __XIOS_ENUM_REF_IMPL__
2#define __XIOS_ENUM_REF_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#include <string>
10
11namespace xios
12{
13 
14   
15  using namespace std;
16 
17  template <typename T>
18  CEnum_ref<T>::CEnum_ref(void)
19  {
20    empty=true ;
21  }
22   
23  template <typename T>
24  CEnum_ref<T>::CEnum_ref(T_enum& val)
25  {
26    empty=true ;
27    set_ref(val) ;
28  }
29 
30  template <typename T>
31  CEnum_ref<T>::CEnum_ref(CEnum<T>& type)
32  {
33    empty=true ;
34    set_ref(type) ;
35  }
36
37  template <typename T>
38  CEnum_ref<T>::CEnum_ref(const CEnum_ref<T>& type)
39  {
40    empty=true ;
41    set_ref(type) ;
42  } 
43
44 
45  template <typename T>
46  void CEnum_ref<T>::set_ref(T_enum& val)
47  {
48    ptrValue=&val ;
49    empty=false ;
50  }
51
52  template <typename T>
53  void CEnum_ref<T>::set_ref(CEnum<T>& type)
54  {
55    ptrValue=&type.get() ;
56    empty=false ;
57  }
58
59  template <typename T>
60  void CEnum_ref<T>::set_ref(const CEnum_ref<T>& type)
61  {
62    ptrValue=type.ptrValue ;
63    empty=type.empty ;
64  }
65
66  template <typename T>
67  void CEnum_ref<T>::set(const T_enum& val) const
68  {
69    checkEmpty() ;
70    *ptrValue=val ;
71  }
72
73  template <typename T>
74  void CEnum_ref<T>::set(const CEnum<T>& type) const
75  {
76    checkEmpty() ;
77    *ptrValue=type.get() ;
78  }
79
80  template <typename T>
81  void CEnum_ref<T>::set(const CEnum_ref<T>& type) const
82  {
83    checkEmpty() ;
84    *ptrValue=type.get() ;
85  }
86 
87  template <typename T>
88  typename T::t_enum & CEnum_ref<T>::get(void) const
89  {
90    checkEmpty() ;
91    return *ptrValue ;
92  }
93 
94  template <typename T>
95  const CEnum_ref<T>& CEnum_ref<T>::operator = (T_enum& val) const
96  {
97    set(val) ;
98    return *this ;
99  }
100
101  template <typename T>
102  const CEnum_ref<T>& CEnum_ref<T>::operator = (CEnum<T>& type) const
103  {
104    set(type) ;
105    return *this ;
106  }
107   
108  template <typename T>
109  const CEnum_ref<T>& CEnum_ref<T>::operator = (const CEnum_ref<T>& type) const
110  {
111    set(type) ;
112    return *this ;
113  }
114 
115  template <typename T>
116  CEnum_ref<T>::operator T_enum&() const
117  {
118    checkEmpty() ;
119    return *ptrValue ;
120  }
121
122  template <typename T>
123  CEnum_ref<T>* CEnum_ref<T>::_clone(void) const
124  {
125    checkEmpty() ;
126    return new CEnum_ref<T>(*this) ;
127  }
128   
129  template <typename T>
130  void CEnum_ref<T>::_fromString(const string& str) const
131  {
132    istringstream iss(str);
133    checkEmpty() ;
134    iss>>*ptrValue ;
135  }
136 
137  template <typename T>
138  void CEnum_ref<T>::_fromString(const string& str)
139  {
140    checkEmpty() ;
141    string tmpStr=xios_to_lower_copy(xios_trim_copy(str)) ;
142   
143    bool found=false ;
144    for(int i=0;i<T::getSize();i++)
145    {
146      if (xios_to_lower_copy(string(T::getStr()[i]))==tmpStr)
147      {
148        *ptrValue=(T_enum) i ;
149        return ;
150      }
151    }
152   
153    ostringstream strList ;
154    for(int i=0;i<T::getSize();i++) 
155    {
156      if (i>0) strList<<", " ;
157      strList<<xios_to_lower_copy(string(T::getStr()[i])) ;
158    }
159   
160    ERROR("template <typename T> void CEnum_ref<T>::_fromString(const string& str)",
161          << tmpStr << " cannot be converted in a valid enumeration, possibilities are: " << strList.str());
162  }
163 
164  template <typename T>
165  string CEnum_ref<T>::_toString(void) const
166  {
167    if (empty) return string("empty"); 
168    return string((T::getStr())[(int)(*ptrValue)]) ;
169  }
170
171   
172  template <typename T>
173  bool CEnum_ref<T>::_toBuffer(CBufferOut& buffer) const
174  {
175    checkEmpty() ;
176    if (sizeof(*ptrValue)==sizeof(short int)) return buffer.put((short int) *ptrValue) ;
177    else if (sizeof(*ptrValue)==sizeof(int)) return buffer.put((int) *ptrValue) ;
178    else if (sizeof(*ptrValue)==sizeof(long int)) return buffer.put((long int) *ptrValue) ;
179    else ERROR("template <typename T> bool CEnum_ref<T>::_toBuffer(CBufferOut& buffer) const",
180               << "incompatibility between enumeration and standard integer type.");
181    return false ;
182  }
183 
184  template <typename T>
185  bool CEnum_ref<T>::_fromBuffer(CBufferIn& buffer)
186  {
187    checkEmpty() ;
188    bool ret ;
189    if (sizeof(*ptrValue)==sizeof(short int)) 
190    {
191      short int val ;
192      ret=buffer.get(val) ;
193      if (ret) *ptrValue = (T_enum) val ;
194    }
195    else if (sizeof(*ptrValue)==sizeof(int)) 
196    {
197      int val ;
198      ret=buffer.get(val) ;
199      if (ret) *ptrValue = (T_enum) val ;
200    }
201    else if (sizeof(*ptrValue)==sizeof(long int)) 
202    {
203      long int val ;
204      ret=buffer.get(val) ;
205      if (ret) *ptrValue = (T_enum) val ;
206    }
207    else ERROR("template <typename T> bool CEnum_ref<T>::_fromBuffer(CBufferIn& buffer)",
208               << "incompatibility between enumeration and standard integer type.");
209    return ret ;
210  }
211 
212  template <typename T>
213  bool CEnum_ref<T>::_fromBuffer(CBufferIn& buffer) const
214  {
215    checkEmpty() ;
216    bool ret ;
217    if (sizeof(*ptrValue)==sizeof(short int)) 
218    {
219      short int val ;
220      ret=buffer.get(val) ;
221      if (ret) *ptrValue = (T_enum) val ;
222    }
223    else if (sizeof(*ptrValue)==sizeof(int)) 
224    {
225      int val ;
226      ret=buffer.get(val) ;
227      if (ret) *ptrValue = (T_enum) val ;
228    }
229    else if (sizeof(*ptrValue)==sizeof(long int)) 
230    {
231      long int val ;
232      ret=buffer.get(val) ;
233      if (ret) *ptrValue = (T_enum) val ;
234    }
235    else ERROR("template <typename T> bool CEnum_ref<T>::_fromBuffer(CBufferIn& buffer)",
236               << "incompatibility between enumeration and standard integer type");
237  }
238 
239  template <typename T>
240  size_t CEnum_ref<T>::_size(void) const
241  {
242    return sizeof(T_enum) ;
243  }
244 
245  template <typename T>
246  bool CEnum_ref<T>::_isEmpty(void) const
247  {
248    return empty ;
249  }
250   
251  template <typename T>
252  void CEnum_ref<T>::_reset(void)
253  {
254      empty=true ;
255  }
256 
257  template <typename T>
258  void CEnum_ref<T>::checkEmpty(void) const
259  {
260    if (empty) ERROR("template <typename T> void CEnum_ref<T>::checkEmpty(void)",
261                     << "Enum reference is not initialized.") ;
262  }
263                     
264  template <typename T> 
265  bool operator== (const CEnum_ref<T>& lhs, const typename T::t_enum& rhs)
266  {
267     if (lhs.isEmpty()) return false;
268     return (lhs.get() == rhs);
269  }
270
271  template <typename T> 
272  bool operator== (const typename T::t_enum& lhs, const CEnum_ref<T>& rhs)
273  {
274    return rhs == lhs;
275  }
276
277  template <typename T> 
278  bool operator== (const CEnum_ref<T>& lhs, const CEnum_ref<T>& rhs)
279  {
280    if ((lhs.isEmpty() && !rhs.isEmpty()) || (!lhs.isEmpty() && rhs.isEmpty())) return false;
281    if (lhs.isEmpty() && rhs.isEmpty()) return true;
282    return (lhs.get() == rhs.get());
283  }
284 
285  template <typename T>
286  CBufferOut& operator<<(CBufferOut& buffer, const CEnum_ref<T>& type)
287  {
288    if (!type.toBuffer(buffer)) ERROR("template <typename T> CBufferOut& operator<<(CBufferOut& buffer, const CEnum_ref<T>& type)",
289                                      << "Not enough free space in buffer to queue the enum.");
290    return buffer ;
291  }
292
293  template <typename T>
294  CBufferOut& operator<<(CBufferOut& buffer, typename T::t_enum& type)
295  {
296    if (!CEnum_ref<T>(type).toBuffer(buffer)) ERROR("template <typename T> CBufferOut& operator<<(CBufferOut& buffer, const typename T::t_enum& type)",
297                                                    << "Not enough free space in buffer to queue the enum.");
298    return buffer ;
299  }
300
301  template <typename T>
302  CBufferIn& operator>>(CBufferIn& buffer, typename T::t_enum& type)
303  {
304    if (!CEnum_ref<T>(type).fromBuffer(buffer)) ERROR("template <typename T>  CBufferIn& operator>>(CBufferIn& buffer, typename T::t_enum& type)",
305                                                      << "Not enough data in buffer to unqueue the enum.");
306    return buffer ;
307  }
308
309  template <typename T>
310  CBufferIn& operator>>(CBufferIn& buffer, const CEnum_ref<T>& type)
311  {
312    if (!type.fromBuffer(buffer)) ERROR("template <typename T> CBufferIn& operator>>(CBufferIn& buffer, const CEnum_ref<T>& type) ",
313                                        << "Not enough data in buffer to unqueue the enum.");
314    return buffer ;
315    return buffer ;
316  }
317
318
319/*
320  template <typename T>
321  CMessage& operator<<(CMessage& msg, const CEnum_ref<T>& type)
322  {
323    msg.push(*type.clone()) ;
324    return msg ;
325  }
326*/
327
328  template <typename T>
329  CMessage& operator<<(CMessage& msg, typename T::t_enum & type)
330  {
331    msg.push(*CEnum_ref<T>(type).clone()) ;
332    return msg ;
333  }
334 
335}
336
337#endif
338
Note: See TracBrowser for help on using the repository browser.