source: XIOS/trunk/src/type/enum_ref_impl.hpp @ 369

Last change on this file since 369 was 369, checked in by ymipsl, 12 years ago

Major Update

  • redesign Type and attribute manipulation
  • add enumerate type and attribute
  • use blitz class array instead of boost class array

YM

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