OpendTect-6_4  6.4
convert.h
Go to the documentation of this file.
1 #ifndef convert_h
2 #define convert_h
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H. Lammertink
8  Date: 13/01/2005
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "string2.h"
15 #include "undefval.h"
16 #include "fixedstring.h"
17 
18 #ifdef __msvc__
19 # include "msvcdefs.h"
20 #endif
21 
24 namespace Conv
25 {
26 
28 template <class T, class F>
29 inline void set( T& _to, const F& fr )
30  { _to = (T)fr; }
31 
32 template <class T, class F>
33 inline T to( const F& fr )
34 {
35  T ret;
36  Values::setUdf(ret);
37  set<T,F>(ret,fr);
38 
39  return ret;
40 }
41 
42 
44 template <class T, class F>
45 inline void udfset(T& _to, const F& fr, const T& und= Values::Undef<T>::val())
46 {
47  if ( Values::hasUdf<F>() && Values::isUdf(fr) )
48  _to = und;
49  else
50  {
51  set(_to,fr);
52  if ( Values::isUdf(_to) ) _to = und;
53  }
54 }
55 
56 template <class T, class F>
57 inline T udfto( const F& fr, const T& und = Values::Undef<T>::val() )
58 {
59  T ret;
60  Values::setUdf(ret);
61  udfset<T,F>(ret,fr,und);
62 
63  return ret;
64 }
65 
66 
67 //----- specialisations 1: simple types -> const char*
68 
69 template <>
70 inline void set( const char*& _to, const od_int32& i )
71  { _to = toString(i); }
72 
73 template <>
74 inline void set( const char*& _to, const od_uint32& i )
75  { _to = toString(i); }
76 
77 template <>
78 inline void set( const char*& _to, const od_int64& i )
79  { _to = toString(i); }
80 
81 template <>
82 inline void set( const char*& _to, const od_uint64& i )
83  { _to = toString(i); }
84 
85 template <>
86 inline void set( const char*& _to, const bool& b )
87  { _to = toString(b); }
88 
89 template <>
90 inline void set( const char*& _to, const float& f )
91  { _to = toString(f); }
92 
93 template <>
94 inline void set( const char*& _to, const double& d )
95  { _to = toString(d); }
96 
97 template <>
98 inline void set( const char*& _to, const short& i )
99  { _to = toString(i); }
100 
101 template <>
102 inline void set( const char*& _to, const unsigned short& i )
103  { _to = toString(i); }
104 
105 
106 //----- specialisations 2: floating point types -> integer types
107 
108 template <>
109 inline void set( od_int32& _to, const float& f )
110  { _to = mRounded(od_int32,f); }
111 
112 template <>
113 inline void set( od_int64& _to, const float& f )
114  { _to = mRounded(od_int64,f); }
115 
116 template <>
117 inline void set( short& _to, const float& f )
118  { _to = mRounded(short,f); }
119 
120 template <>
121 inline void set( unsigned short& _to, const float& f )
122  { _to = mRounded(od_uint16,f); }
123 
124 template <>
125 inline void set( od_uint32& _to, const float& f )
126  { _to = mRounded(od_uint32,f); }
127 
128 template <>
129 inline void set( od_uint64& _to, const float& f )
130  { _to = mRounded(od_uint64,f); }
131 
132 template <>
133 inline void set( od_int32& _to, const double& f )
134  { _to = mRounded(od_int32,f); }
135 
136 template <>
137 inline void set( od_int64& _to, const double& f )
138  { _to = mRounded(od_int64,f); }
139 
140 template <>
141 inline void set( short& _to, const double& f )
142  { _to = mRounded(short,f); }
143 
144 template <>
145 inline void set( unsigned short& _to, const double& f )
146  { _to = mRounded(od_uint16,f); }
147 
148 template <>
149 inline void set( od_uint32& _to, const double& f )
150  { _to = mRounded(od_uint32,f); }
151 
152 template <>
153 inline void set( od_uint64& _to, const double& f )
154  { _to = mRounded(od_uint64,f); }
155 
156 
157 //----- specialisations 3: strings and simple types -> bool
158 
159 template <>
160 inline void set( bool& _to, const char* const& s )
161  { _to = yesNoFromString(s); }
162 
163 template <>
164 inline void set( bool& _to, const FixedString& s )
165  { _to = yesNoFromString(s.str()); }
166 
167 template <>
168 inline void set( bool& _to, const int& i )
169  { _to = i!=0; }
170 
171 template <>
172 inline void set( bool& _to, const float& f )
173  { _to = !mIsZero(f,mDefEpsF); }
174 
175 template <>
176 inline void set( bool& _to, const double& d )
177  { _to = !mIsZero(d,mDefEpsD); }
178 
179 
180 //----- specialisations 4: strings -> simple types
181 
182 
183 #define mConvDeclFromStrToSimpleType(type) \
184 template <> mGlobal(Basic) void set(type&,const char* const&); \
185 template <> mGlobal(Basic) void set(type&,const FixedString&)
186 
188 mConvDeclFromStrToSimpleType(unsigned short);
195 
196 } // namespace Conv
197 
198 
200 
201 #define mConvDefFromStrToSimpleType(type,function) \
202 namespace Conv \
203 { \
204  template <> void set( type& _to, const char* const& s ) \
205  { \
206  if ( !s || !*s ) { return; } \
207  \
208  char* endptr = 0; \
209  type tmpval = (type) function; \
210  if ( s != endptr ) \
211  _to = (type) tmpval; \
212  else if ( Values::Undef<type>::hasUdf() ) \
213  Values::setUdf( _to ); \
214  } \
215  template <> void set( type& _to, const FixedString& s ) \
216  { \
217  if ( !s ) { return; } \
218  \
219  char* endptr = 0; \
220  type tmpval = (type) function; \
221  if ( s.str() != endptr ) \
222  _to = (type) tmpval; \
223  else if ( Values::Undef<type>::hasUdf() ) \
224  Values::setUdf( _to ); \
225  } \
226 }
227 
228 
229 #endif
T to(const F &fr)
Definition: convert.h:33
T udfto(const F &fr, const T &und=Values::Undef< T >::val())
Definition: convert.h:57
#define mRounded(typ, x)
Definition: commondefs.h:44
#define mIsZero(x, eps)
Definition: commondefs.h:53
OD::String that holds an existing text string.
Definition: fixedstring.h:29
#define od_int64
Definition: plftypes.h:36
bool yesNoFromString(const char *)
#define od_uint32
Definition: plftypes.h:32
#define mDefEpsD
Definition: commondefs.h:57
#define od_uint64
Definition: plftypes.h:37
#define mDefEpsF
Definition: commondefs.h:56
void udfset(T &_to, const F &fr, const T &und=Values::Undef< T >::val())
template based type converstion, with check for undef
Definition: convert.h:45
#define od_int32
Definition: plftypes.h:31
#define mConvDeclFromStrToSimpleType(type)
Definition: convert.h:183
T & setUdf(T &u)
Definition: undefval.h:262
#define od_uint16
Definition: plftypes.h:28
Template based type conversion.
Definition: convert.h:24
bool isUdf(const T &t)
Definition: undefval.h:243
Export_Basic const char * toString(ViewStyle)
Templatized undefined values.
Definition: undefval.h:51

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019