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

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