OpendTect  6.6
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  RCS: $Id$
9 ________________________________________________________________________
10 
11 -*/
12 
13 #include "undefval.h"
14 #include "bufstring.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 = sCast(T,fr); }
29 
33 template <class T, class F>
34 inline T to( const F& fr )
35 {
36  T ret;
37  Values::setUdf(ret);
38  set<T,F>(ret,fr);
39 
40  return ret;
41 }
42 
43 
45 template <class T, class F>
46 inline void udfset(T& _to, const F& fr, const T& und= Values::Undef<T>::val())
47 {
48  if ( Values::hasUdf<F>() && Values::isUdf(fr) )
49  _to = und;
50  else
51  {
52  set(_to,fr);
53  if ( Values::isUdf(_to) ) _to = und;
54  }
55 }
56 
57 template <class T, class F>
58 inline T udfto( const F& fr, const T& und = Values::Undef<T>::val() )
59 {
60  T ret;
61  Values::setUdf(ret);
62  udfset<T,F>(ret,fr,und);
63 
64  return ret;
65 }
66 
67 
68 //----- specialisations 1: simple types -> const char*
69 
70 template <>
71 inline void set( const char*& _to, const short& i )
72  { _to = toString(i); }
73 
74 template <>
75 inline void set( const char*& _to, const unsigned short& i )
76  { _to = toString(i); }
77 
78 template <>
79 inline void set( const char*& _to, const od_int32& i )
80  { _to = toString(i); }
81 
82 template <>
83 inline void set( const char*& _to, const od_uint32& i )
84  { _to = toString(i); }
85 
86 template <>
87 inline void set( const char*& _to, const od_int64& i )
88  { _to = toString(i); }
89 
90 template <>
91 inline void set( const char*& _to, const od_uint64& i )
92  { _to = toString(i); }
93 
94 template <>
95 inline void set( const char*& _to, const bool& b )
96  { _to = toString(b); }
97 
98 template <>
99 inline void set( const char*& _to, const float& f )
100  { _to = toString(f); }
101 
102 template <>
103 inline void set( const char*& _to, const double& d )
104  { _to = toString(d); }
105 
106 
107 //----- specialisations 2: floating point types -> integer types
108 
109 template <>
110 inline void set( short& _to, const float& f )
111  { _to = mRounded(short,f); }
112 
113 template <>
114 inline void set( unsigned short& _to, const float& f )
115  { _to = mRounded(od_uint16,f); }
116 
117 template <>
118 inline void set( od_int32& _to, const float& f )
119  { _to = mRounded(od_int32,f); }
120 
121 template <>
122 inline void set( od_uint32& _to, const float& f )
123  { _to = mRounded(od_uint32,f); }
124 
125 template <>
126 inline void set( od_int64& _to, const float& f )
127  { _to = mRounded(od_int64,f); }
128 
129 template <>
130 inline void set( od_uint64& _to, const float& f )
131  { _to = mRounded(od_uint64,f); }
132 
133 template <>
134 inline void set( short& _to, const double& f )
135  { _to = mRounded(short,f); }
136 
137 template <>
138 inline void set( unsigned short& _to, const double& f )
139  { _to = mRounded(od_uint16,f); }
140 
141 template <>
142 inline void set( od_int32& _to, const double& f )
143  { _to = mRounded(od_int32,f); }
144 
145 template <>
146 inline void set( od_uint32& _to, const double& f )
147  { _to = mRounded(od_uint32,f); }
148 
149 template <>
150 inline void set( od_int64& _to, const double& f )
151  { _to = mRounded(od_int64,f); }
152 
153 template <>
154 inline void set( od_uint64& _to, const double& f )
155  { _to = mRounded(od_uint64,f); }
156 
157 
158 //----- specialisations 3: strings and simple types -> bool
159 
160 template <>
161 inline void set( bool& _to, const char* const& s )
162  { _to = yesNoFromString(s); }
163 
164 template <>
165 inline void set( bool& _to, const FixedString& s )
166  { _to = yesNoFromString(s.str()); }
167 
168 template <>
169 inline void set( bool& _to, const BufferString& s )
170  { _to = yesNoFromString(s.str()); }
171 
172 template <>
173 inline void set( bool& _to, const int& i )
174  { _to = i!=0; }
175 
176 template <>
177 inline void set( bool& _to, const float& f )
178  { _to = !mIsZero(f,mDefEpsF); }
179 
180 template <>
181 inline void set( bool& _to, const double& d )
182  { _to = !mIsZero(d,mDefEpsD); }
183 
184 
185 //----- specialisations 4: strings -> simple types
186 
187 
188 #define mConvDeclFromStrToSimpleType(type) \
189 template <> mGlobal(Basic) void set(type&,const char* const&); \
190 template <> mGlobal(Basic) void set(type&,const FixedString&); \
191 template <> mGlobal(Basic) void set(type&,const BufferString&)
192 
201 
202 } // namespace Conv
203 
204 
206 
207 #define mConvDefFromStrToSimpleType(type,function) \
208 namespace Conv \
209 { \
210  template <> void set( type& _to, const char* const& s ) \
211  { \
212  if ( !s || !*s ) { return; } \
213  \
214  char* endptr = nullptr; \
215  type tmpval = sCast(type,function); \
216  if ( s != endptr ) \
217  _to = sCast(type,tmpval); \
218  else if ( Values::Undef<type>::hasUdf() ) \
219  Values::setUdf( _to ); \
220  } \
221  template <> void set( type& _to, const FixedString& s ) \
222  { \
223  if ( s.isEmpty() ) { return; } \
224  \
225  char* endptr = nullptr; \
226  type tmpval = sCast(type,function); \
227  if ( s.str() != endptr ) \
228  _to = sCast(type,tmpval); \
229  else if ( Values::Undef<type>::hasUdf() ) \
230  Values::setUdf( _to ); \
231  } \
232  template <> void set( type& _to, const BufferString& s ) \
233  { \
234  if ( s.isEmpty() ) { return; } \
235  \
236  char* endptr = nullptr; \
237  type tmpval = sCast(type,function); \
238  if ( s.str() != endptr ) \
239  _to = sCast(type,tmpval); \
240  else if ( Values::Undef<type>::hasUdf() ) \
241  Values::setUdf( _to ); \
242  } \
243 }
od_uint64
#define od_uint64
Definition: plftypes.h:36
Conv::to
T to(const F &fr)
Definition: convert.h:34
mDefEpsD
#define mDefEpsD
Definition: commondefs.h:70
od_int64
#define od_int64
Definition: plftypes.h:35
sCast
#define sCast(tp, v)
Definition: commondefs.h:141
Values::setUdf
T & setUdf(T &u)
Definition: undefval.h:264
mRounded
#define mRounded(typ, x)
Definition: commondefs.h:57
mDefEpsF
#define mDefEpsF
Definition: commondefs.h:69
bufstring.h
undefval.h
msvcdefs.h
Values::isUdf
bool isUdf(const T &t)
Definition: undefval.h:245
Conv::udfset
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:46
Conv::udfto
T udfto(const F &fr, const T &und=Values::Undef< T >::val())
Definition: convert.h:58
OD::String::str
const char * str() const
Definition: odstring.h:47
mIsZero
#define mIsZero(x, eps)
Definition: commondefs.h:66
FixedString
OD::String that holds an existing text string.
Definition: fixedstring.h:29
yesNoFromString
bool yesNoFromString(const char *)
od_uint16
#define od_uint16
Definition: plftypes.h:27
Conv::set
void set(T &_to, const F &fr)
template based type conversion
Definition: convert.h:27
BufferString
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size.
Definition: bufstring.h:40
Conv
Template based type conversion.
Definition: convert.h:23
od_int32
#define od_int32
Definition: plftypes.h:30
Values::Undef
Templatized undefined values.
Definition: undefval.h:52
od_uint32
#define od_uint32
Definition: plftypes.h:31
mConvDeclFromStrToSimpleType
#define mConvDeclFromStrToSimpleType(type)
Definition: convert.h:188
toString
BufferString toString(const BufferStringSet &bss)
Definition: bufstringset.h:156

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