47#include <StormByte/safe/binary.hxx>
48#include <StormByte/safe/variant.hxx>
49#include <StormByte/type_traits.hxx>
77 enum class Type :
unsigned short {
102 Value(
int value) : m_value(value), m_type(
Type::Integer) {}
108 Value(
unsigned int value) : m_value(value), m_type(
Type::UnsignedInteger) {}
114 Value(
long int value) : m_value(static_cast<long long int>(value)), m_type(
Type::LongInteger) {}
120 Value(
unsigned long int value) : m_value(static_cast<unsigned long long int>(value)), m_type(
Type::UnsignedLongInteger) {}
126 Value(
long long int value) : m_value(value), m_type(
Type::LongInteger) {}
132 Value(
unsigned long long int value) : m_value(value), m_type(
Type::UnsignedLongInteger) {}
138 Value(
double value) : m_value(value), m_type(
Type::Double) {}
162 template <
typename T>
164 Value(T &&value) : m_value(value), m_type(
Type::Boolean) {}
195 return m_value == other.m_value;
204 return !(*
this == other);
218 template <typename T>
221 if (m_value.valueless_by_exception())
222 throw WrongValueType(
"Requested type does not match stored type (null).");
223 using To = std::decay_t<T>;
225 using From = std::decay_t<
decltype(val)>;
227 throw WrongValueType(
"Requested type does not match stored type (null).");
231 return convert_numeric<To, From>(val);
233 throw WrongValueType(
"Requested type does not match stored type.");
264 template <
typename To,
typename From>
266 static To convert_numeric(
const From &val) {
269 std::intmax_t from =
static_cast<std::intmax_t
>(val);
271 if (from <
static_cast<std::intmax_t
>(std::numeric_limits<To>::lowest()) || from >
static_cast<std::intmax_t
>(std::numeric_limits<To>::max()))
272 throw WrongValueType(
"Integer conversion would overflow/narrow.");
273 return static_cast<To
>(from);
276 throw WrongValueType(
"Negative value cannot be converted to unsigned.");
277 if (
static_cast<std::uintmax_t
>(from) >
static_cast<std::uintmax_t
>(std::numeric_limits<To>::max()))
278 throw WrongValueType(
"Integer conversion would overflow/narrow.");
279 return static_cast<To
>(from);
282 std::uintmax_t from =
static_cast<std::uintmax_t
>(val);
283 if constexpr (StormByte::Type::Signed<To>) {
284 if (from >
static_cast<std::uintmax_t
>(std::numeric_limits<To>::max()))
285 throw WrongValueType(
"Integer conversion would overflow/narrow.");
286 return static_cast<To
>(from);
288 if (from >
static_cast<std::uintmax_t
>(std::numeric_limits<To>::max()))
289 throw WrongValueType(
"Integer conversion would overflow/narrow.");
290 return static_cast<To
>(from);
293 }
else if constexpr (StormByte::Type::Integral<From> && StormByte::Type::FloatingPoint<To>) {
294 return static_cast<To
>(val);
295 }
else if constexpr (StormByte::Type::FloatingPoint<From> && StormByte::Type::Integral<To>) {
296 long double d =
static_cast<long double>(val);
297 if (!std::isfinite(d))
298 throw WrongValueType(
"Non-finite floating conversion to integer.");
299 if (std::trunc(d) != d)
300 throw WrongValueType(
"Floating value has fractional part; would lose data.");
301 const long double upper_bound = std::ldexp(1.0L, std::numeric_limits<To>::digits);
302 if constexpr (StormByte::Type::Signed<To>) {
303 if (d < -upper_bound || d >= upper_bound)
304 throw WrongValueType(
"Floating to integer conversion would overflow/narrow.");
307 throw WrongValueType(
"Negative value cannot be converted to unsigned.");
308 if (d >= upper_bound)
309 throw WrongValueType(
"Floating to integer conversion would overflow/narrow.");
311 return static_cast<To
>(d);
312 }
else if constexpr (StormByte::Type::FloatingPoint<From> && StormByte::Type::FloatingPoint<To>) {
313 return static_cast<To
>(val);
315 throw WrongValueType(
"Unsupported numeric conversion.");
Type-erased SQL value (NULL, integers, double, text, blob, bool).
Definition value.hxx:71
Value(StormByte::Safe::Binary &&value)
Move a blob into this value.
Value(double value)
From a double.
Definition value.hxx:138
Value(unsigned int value)
From an unsigned int.
Definition value.hxx:108
Value(long int value)
From a signed long int, stored as long long int.
Definition value.hxx:114
Value(Value &&other) noexcept
Move constructor.
bool IsNull() const noexcept
Whether the value is SQL NULL.
Definition value.hxx:251
Value(T &&value)
From a bool.
Definition value.hxx:164
bool operator!=(const Value &other) const
Inequality.
Definition value.hxx:203
Value(std::string_view value)
Copy UTF-8 text into a String-owned value.
Value()
Default constructor.
Definition value.hxx:96
std::decay_t< T > Get() const
Stored value as T, with safe numeric conversions.
Definition value.hxx:220
Value(long long int value)
From a signed long long int.
Definition value.hxx:126
@ Null
SQL NULL.
Definition value.hxx:78
bool operator==(const Value &other) const
Equality of the stored alternatives.
Definition value.hxx:192
Value(const StormByte::Safe::Binary &value)
Copy a blob into Base-owned storage.
Value(const Value &other)
Copy constructor.
Value(unsigned long long int value)
From an unsigned long long int.
Definition value.hxx:132
Type Type() const noexcept
Discriminator of the stored alternative.
Definition value.hxx:243
virtual ~Value() noexcept
Destructor.
Value & operator=(Value &&other) noexcept
Move assignment.
Value & operator=(const Value &other)
Copy assignment.
Value(int value)
From a signed int.
Definition value.hxx:102
Value(unsigned long int value)
From an unsigned long int, stored as unsigned long long int.
Definition value.hxx:120
Value accessed as the wrong type.
Definition exception.hxx:213
STORMBYTE_DECLARE_MAYBE_SAFE(StormByte::Logger::Exception)
Database module of the StormByte suite.
StormByte::Safe::Variant< StormByte::Safe::Monostate, int, unsigned int, long long int, unsigned long long int, double, StormByte::Safe::String, bool, StormByte::Safe::Binary > ValuesVariant
Column alternatives stored on Base's heap.
Definition typedefs.hxx:66
decltype(auto) visit(Visitor &&visitor, Variant< Ts... > &value)
Root namespace of the StormByte C++ suite.
#define STORMBYTE_DATABASE_PUBLIC
Definition visibility.h:54