Hyper API for C++  0.0.15888
Hyper client library for C++ applications
Numeric.hpp
Go to the documentation of this file.
1 
5 #ifndef TABLEAU_HYPER_NUMERIC_HPP
6 #define TABLEAU_HYPER_NUMERIC_HPP
7 
8 #include <cstdint>
9 #include <hyperapi/SqlType.hpp>
10 #include <hyperapi/string_view.hpp>
11 #include <string>
12 #include <type_traits>
13 #include <hyperapi/hyperapi.h>
14 
15 class BigNumeric_rawValue_Test;
16 class Numeric_rawValue_Test;
17 
18 namespace std {
20 template <>
21 struct hash<hyper_data128_t> {
23  size_t operator()(hyper_data128_t n) const noexcept {
24  return hash<uint64_t>()(n.data[0]) ^ hash<uint64_t>()(n.data[1]);
25  }
26 };
27 }
28 
29 namespace hyperapi {
31 inline bool operator==(const hyper_data128_t& a, const hyper_data128_t& b) noexcept { return (a.data[0] == b.data[0]) && (a.data[1] == b.data[1]); }
33 inline bool operator>(const hyper_data128_t& a, const hyper_data128_t& b) noexcept {
34  if (a.data[1] != b.data[1]) return (static_cast<int64_t>(a.data[1]) > static_cast<int64_t>(b.data[1]));
35  if (a.data[0] != b.data[0]) return (a.data[0] > b.data[0]);
36  return false;
37 }
38 
42 template <unsigned precision_value, unsigned scale_value>
43 class Numeric final {
44  public:
45  static constexpr unsigned precision = precision_value;
46  static constexpr unsigned scale = scale_value;
47  static_assert(precision >= scale, "The precision of a numeric must be greater or equal than the scale");
48  static_assert(precision <= 38, "Precision must be at most 38");
49  using data_t = typename std::conditional<precision <= 18, int64_t, hyper_data128_t>::type;
50 
54  Numeric() noexcept {}
55 
60  Numeric(short value);
65  Numeric(unsigned short value);
70  Numeric(int value);
75  Numeric(unsigned value);
80  Numeric(long value);
85  Numeric(unsigned long value);
90  Numeric(long long value);
95  Numeric(unsigned long long value);
100  Numeric(float value);
105  Numeric(double value);
110  Numeric(long double value);
111 
116  template <unsigned otherPrecision, unsigned otherScale>
118 
123  explicit Numeric(hyperapi::string_view value);
124 
129  std::string stringValue() const;
130 
135  int64_t intValue() const;
140  operator int64_t() const { return intValue(); }
141 
145  double doubleValue() const noexcept;
149  operator double() const noexcept { return doubleValue(); }
150 
155  std::string toString() const;
156 
160  friend bool operator==(const Numeric& a, const Numeric& b) noexcept { return a.m_value == b.m_value; }
164  friend bool operator>(const Numeric& a, const Numeric& b) noexcept { return a.m_value > b.m_value; }
168  friend bool operator!=(const Numeric& a, const Numeric& b) noexcept { return !(a == b); }
172  friend bool operator<(const Numeric& a, const Numeric& b) noexcept { return (b > a); }
176  friend bool operator<=(const Numeric& a, const Numeric& b) noexcept { return !(a > b); }
180  friend bool operator>=(const Numeric& a, const Numeric& b) noexcept { return !(a < b); }
181 
183  friend std::ostream& operator<<(std::ostream& os, const Numeric& obj) { return os << obj.toString(); }
184 
185  private:
186  friend class ::Numeric_rawValue_Test;
187  friend class ::BigNumeric_rawValue_Test;
188  template <unsigned otherPrecision, unsigned otherScale>
189  friend class Numeric;
190  friend class Inserter;
191  template <typename ReturnType>
192  friend struct internal::ValueAccess;
193  friend struct internal::ValueInserter;
194  template <typename T>
195  friend struct std::hash;
196 
197  struct raw_t {};
198 
204  explicit Numeric(data_t rawNumeric, raw_t) noexcept
205  : m_value(rawNumeric) {
206  }
207 
216  explicit Numeric(int64_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
217 
226  explicit Numeric(hyper_data128_t rawNumeric, unsigned otherPrecision, unsigned otherScale, raw_t);
227 
231  data_t m_value{};
232 };
233 }
234 
235 namespace std {
237 template <unsigned p, unsigned s>
238 struct hash<hyperapi::Numeric<p, s>> {
240  size_t operator()(hyperapi::Numeric<p, s> n) const noexcept {
241  return std::hash<typename hyperapi::Numeric<p, s>::data_t>()(n.m_value);
242  }
243 };
244 }
245 
246 #include <hyperapi/impl/Numeric.impl.hpp>
247 
248 #endif
hyperapi::Inserter
An inserter.
Definition: Inserter.hpp:24
hyperapi::Numeric::doubleValue
double doubleValue() const noexcept
Gets a double representation of this value; may lose accuracy.
string_view.hpp
hyperapi::Numeric::operator!=
friend bool operator!=(const Numeric &a, const Numeric &b) noexcept
Not equal operator.
Definition: Numeric.hpp:168
hyperapi::Numeric::operator<<
friend std::ostream & operator<<(std::ostream &os, const Numeric &obj)
Stream output operator.
Definition: Numeric.hpp:183
hyperapi::Numeric::operator<=
friend bool operator<=(const Numeric &a, const Numeric &b) noexcept
Less than or equal operator.
Definition: Numeric.hpp:176
hyperapi::Numeric::stringValue
std::string stringValue() const
Gets an exact string representation, which is round-trip compatible with the constructor,...
hyperapi::string_view
Describes an object that can refer to a constant, contiguous sequence of char-like objects.
Definition: string_view.hpp:26
hyperapi::operator>
bool operator>(const DatabaseName &a, const DatabaseName &b) noexcept
Greater operator.
Definition: DatabaseName.hpp:55
hyperapi::Numeric::operator<
friend bool operator<(const Numeric &a, const Numeric &b) noexcept
Less than operator.
Definition: Numeric.hpp:172
hyperapi::Numeric::operator>
friend bool operator>(const Numeric &a, const Numeric &b) noexcept
Greater operator.
Definition: Numeric.hpp:164
hyperapi::Numeric::intValue
int64_t intValue() const
Gets an integer representation of this value; if the value has fraction digits, these will be truncat...
hyperapi::Numeric::operator>=
friend bool operator>=(const Numeric &a, const Numeric &b) noexcept
Greater or equal operator.
Definition: Numeric.hpp:180
SqlType.hpp
hyperapi::operator==
bool operator==(const DatabaseName &a, const DatabaseName &b) noexcept
Equality operator.
Definition: DatabaseName.hpp:53
hyperapi::Numeric
A fixed-point numeric data value with scale fraction digits and precision digits overall.
Definition: Numeric.hpp:43
hyperapi
The primary namespace of the Hyper API for C++.
Definition: ByteSpan.hpp:15
hyperapi::Numeric::operator==
friend bool operator==(const Numeric &a, const Numeric &b) noexcept
Equality operator.
Definition: Numeric.hpp:160
std::hash< hyperapi::Numeric< p, s > >::operator()
size_t operator()(hyperapi::Numeric< p, s > n) const noexcept
Calculates the hash value of the given numeric.
Definition: Numeric.hpp:240
hyperapi::Numeric::Numeric
Numeric() noexcept
Default constructor.
Definition: Numeric.hpp:54
std::hash< hyper_data128_t >::operator()
size_t operator()(hyper_data128_t n) const noexcept
Calculates the hash value of the given hyper_data128_t.
Definition: Numeric.hpp:23
hyperapi::Numeric::toString
std::string toString() const
Gets a string representation for debugging.