strusBase  0.17
numericVariant.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
10 #ifndef _STRUS_STORAGE_NUMERIC_VARIANT_TYPE_HPP_INCLUDED
11 #define _STRUS_STORAGE_NUMERIC_VARIANT_TYPE_HPP_INCLUDED
12 #include "strus/base/stdint.h"
13 #include <cstring>
14 #include <stdio.h>
15 #include <limits>
16 
17 namespace strus {
18 
22 {
23 public:
26  NumericVariant( int64_t value)
27  {
28  variant.Int = value;
29  type = Int;
30  }
31 
34  NumericVariant( uint64_t value)
35  {
36  variant.UInt = value;
37  type = UInt;
38  }
39 
42  NumericVariant( double value)
43  {
44  variant.Float = value;
45  type = Float;
46  }
49  NumericVariant( const char* val_)
50  {
51  if (!initFromString( val_)) init();
52  }
55  {
56  init();
57  }
58  static NumericVariant asint( int64_t val)
59  {
60  return NumericVariant( val);
61  }
62  static NumericVariant asuint( uint64_t val)
63  {
64  return NumericVariant( val);
65  }
66  static NumericVariant asdouble( double val)
67  {
68  return NumericVariant( val);
69  }
73  {
74  std::memcpy( this, &o, sizeof(*this));
75  }
76 
77  void init()
78  {
79  std::memset( this, 0, sizeof(*this));
80  }
81 
82  class String
83  {
84  public:
86  {
87  m_buf[0] = '\0';
88  }
89 
90  explicit String( const NumericVariant& val, int precision=-1);
91 
92  operator const char*() const {return m_buf;}
93  const char* c_str() const {return m_buf;}
94 
95  private:
96  char m_buf[ 128];
97  };
98 
99  String tostring( int precision=-1) const
100  {
101  return String( *this, precision);
102  }
103 
107  bool initFromString( const char* src);
108 
111  bool defined() const
112  {
113  return type != Null;
114  }
115 
118  template <typename TYPE>
119  TYPE cast() const
120  {
121  switch (type)
122  {
123  case Null: return TYPE();
124  case Int: return (TYPE)variant.Int;
125  case UInt: return (TYPE)variant.UInt;
126  case Float: return (TYPE)variant.Float;
127  }
128  return TYPE();
129  }
130 
132  operator double() const
133  {
134  return cast<double>();
135  }
137  operator int64_t() const
138  {
139  return cast<int>();
140  }
142  operator uint64_t() const
143  {
144  return cast<uint64_t>();
145  }
146 
148  int64_t toint() const
149  {
150  if (type == Float)
151  {
152  if (variant.Float < 0.0)
153  {
154  return (int64_t)(variant.Float - 2*std::numeric_limits<double>::epsilon());
155  }
156  else if (variant.Float > 0.0)
157  {
158  return (int64_t)(variant.Float + 2*std::numeric_limits<double>::epsilon());
159  }
160  }
161  return cast<int64_t>();
162  }
163 
165  uint64_t touint() const
166  {
167  if (type == Float)
168  {
169  if (variant.Float < 0.0)
170  {
171  return 0;
172  }
173  else if (variant.Float > 0.0)
174  {
175  return (uint64_t)(variant.Float + 2*std::numeric_limits<double>::epsilon());
176  }
177  }
178  return cast<uint64_t>();
179  }
180 
182  double tofloat() const
183  {
184  return cast<double>();
185  }
186 
190  bool operator == (const NumericVariant& o) const
191  {
192  return isequal(o);
193  }
197  bool operator != (const NumericVariant& o) const
198  {
199  return !isequal(o);
200  }
201 
205  bool operator >= (const NumericVariant& o) const
206  {
207  return compare(o) >= 0;
208  }
209 
213  bool operator > (const NumericVariant& o) const
214  {
215  return compare(o) > 0;
216  }
217 
221  bool operator <= (const NumericVariant& o) const
222  {
223  return compare(o) <= 0;
224  }
225 
229  bool operator < (const NumericVariant& o) const
230  {
231  return compare(o) < 0;
232  }
233 
237  bool isequal( const NumericVariant& o) const;
238 
242  int compare( const NumericVariant& o) const;
243 
246  NumericVariant& operator=( int64_t value)
247  {
248  variant.Int = value;
249  type = Int;
250  return *this;
251  }
252 
255  NumericVariant& operator=( uint64_t value)
256  {
257  variant.UInt = value;
258  type = UInt;
259  return *this;
260  }
261 
264  NumericVariant& operator=( double value)
265  {
266  variant.Float = value;
267  type = Float;
268  return *this;
269  }
270 
274  {
275  std::memcpy( this, &o, sizeof(*this));
276  return *this;
277  }
278 
280  enum Type {
282  Int,
285  };
286  typedef int64_t IntType;
287  typedef uint64_t UIntType;
288  typedef double FloatType;
289 
291  static const char* typeName( Type type);
292 
294  union
295  {
296  int64_t Int;
297  uint64_t UInt;
298  double Float;
299  } variant;
300 };
301 
302 }//namespace
303 #endif
signed integer number value
Definition: numericVariant.hpp:282
NumericVariant & operator=(double value)
Assignment operator for a single precision floating point number.
Definition: numericVariant.hpp:264
NumericVariant & operator=(int64_t value)
Assignment operator for a singed integer.
Definition: numericVariant.hpp:246
NumericVariant()
Default constructor (as undefined value)
Definition: numericVariant.hpp:54
double Float
value in case of a floating point number
Definition: numericVariant.hpp:298
void init()
Definition: numericVariant.hpp:77
double FloatType
Definition: numericVariant.hpp:288
bool operator>(const NumericVariant &o) const
Test for greater.
Definition: numericVariant.hpp:213
NumericVariant(const NumericVariant &o)
Copy constructor.
Definition: numericVariant.hpp:72
NumericVariant & operator=(uint64_t value)
Assignment operator for an unsinged integer.
Definition: numericVariant.hpp:255
bool operator<=(const NumericVariant &o) const
Test for smaller or equal.
Definition: numericVariant.hpp:221
uint64_t UIntType
Definition: numericVariant.hpp:287
uint64_t touint() const
Cast to an unsigned integer.
Definition: numericVariant.hpp:165
NumericVariant(const char *val_)
Constructor from a string.
Definition: numericVariant.hpp:49
bool defined() const
Find out if this value is defined.
Definition: numericVariant.hpp:111
NumericVariant(double value)
Constructor from a single precision floating point number.
Definition: numericVariant.hpp:42
uint64_t UInt
value in case of an unsigned integer
Definition: numericVariant.hpp:297
const char * c_str() const
Definition: numericVariant.hpp:93
NumericVariant & operator=(const NumericVariant &o)
Assignment operator.
Definition: numericVariant.hpp:273
static NumericVariant asuint(uint64_t val)
Definition: numericVariant.hpp:62
Type
Enumeration of all types an numeric variant can have.
Definition: numericVariant.hpp:280
String tostring(int precision=-1) const
Definition: numericVariant.hpp:99
Atomic type that can hold numeric values of different type.
Definition: numericVariant.hpp:21
String()
Definition: numericVariant.hpp:85
int compare(const NumericVariant &o) const
Comparison of numbers.
unsigned integer number value
Definition: numericVariant.hpp:283
static NumericVariant asint(int64_t val)
Definition: numericVariant.hpp:58
NumericVariant(uint64_t value)
Constructor from an unsigned integer.
Definition: numericVariant.hpp:34
double tofloat() const
Cast to an unsigned integer.
Definition: numericVariant.hpp:182
NumericVariant(int64_t value)
Constructor from a signed integer.
Definition: numericVariant.hpp:26
static const char * typeName(Type type)
Get the name of a type as string.
bool initFromString(const char *src)
Initialize numeric variant parsed from a Ascii source string.
int64_t IntType
Definition: numericVariant.hpp:286
bool operator>=(const NumericVariant &o) const
Test for greater or equal.
Definition: numericVariant.hpp:205
int64_t Int
value in case of a signed integer
Definition: numericVariant.hpp:296
union strus::NumericVariant::@4 variant
Value of this numeric variant.
uninitialized variant value
Definition: numericVariant.hpp:281
TYPE cast() const
Template for casting to a defined value type.
Definition: numericVariant.hpp:119
Type type
Type of this numeric variant.
Definition: numericVariant.hpp:293
Definition: numericVariant.hpp:82
static NumericVariant asdouble(double val)
Definition: numericVariant.hpp:66
bool operator!=(const NumericVariant &o) const
Test for inequality.
Definition: numericVariant.hpp:197
int64_t toint() const
Cast to a signed integer.
Definition: numericVariant.hpp:148
bool isequal(const NumericVariant &o) const
Test for equality.
bool operator<(const NumericVariant &o) const
Test for smaller.
Definition: numericVariant.hpp:229
bool operator==(const NumericVariant &o) const
Test for equality.
Definition: numericVariant.hpp:190