StormByte-Database 3.0.0
C++26 database module of the StormByte suite
Loading...
Searching...
No Matches
value.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Database.
5 *
6 * StormByte-Database original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Database source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte-Logger tree and
23 * the PostgreSQL, MariaDB and SQLite trees), which remain under their own
24 * licenses.
25 *
26 * Neither license grants any patent rights. Any patent licenses required
27 * to use this software or third-party components must be obtained separately
28 * from the patent holders.
29 *
30 * StormByte-Database is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public License
36 * version 3 along with StormByte-Database. If not, see
37 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
38 *
39 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
40 */
41
42#pragma once
43
47#include <StormByte/safe/binary.hxx>
48#include <StormByte/safe/variant.hxx>
49#include <StormByte/type_traits.hxx>
50
51#include <cmath>
52#include <cstdint>
53#include <limits>
54#include <string_view>
55#include <type_traits>
56
61namespace StormByte {
66 namespace Database {
72 public:
77 enum class Type : unsigned short {
78 Null = 0,
79 Integer,
80 UnsignedInteger,
81 LongInteger,
82 UnsignedLongInteger,
83 Double,
84 Text,
85 Blob,
86 Boolean
87 };
88
96 Value() : m_value(StormByte::Safe::Monostate{}), m_type(Type::Null) {}
97
102 Value(int value) : m_value(value), m_type(Type::Integer) {}
103
108 Value(unsigned int value) : m_value(value), m_type(Type::UnsignedInteger) {}
109
114 Value(long int value) : m_value(static_cast<long long int>(value)), m_type(Type::LongInteger) {}
115
120 Value(unsigned long int value) : m_value(static_cast<unsigned long long int>(value)), m_type(Type::UnsignedLongInteger) {}
121
126 Value(long long int value) : m_value(value), m_type(Type::LongInteger) {}
127
132 Value(unsigned long long int value) : m_value(value), m_type(Type::UnsignedLongInteger) {}
133
138 Value(double value) : m_value(value), m_type(Type::Double) {}
139
144 Value(std::string_view value);
145
151
157
162 template <typename T>
164 Value(T &&value) : m_value(value), m_type(Type::Boolean) {}
166
170 Value(const Value &other);
171
175 Value(Value &&other) noexcept;
176
180 Value &operator=(const Value &other);
181
185 Value &operator=(Value &&other) noexcept;
186
192 inline bool operator==(const Value &other) const {
193 if (m_type == Type::Null && other.m_type == Type::Null)
194 return true;
195 return m_value == other.m_value;
196 }
197
203 inline bool operator!=(const Value &other) const {
204 return !(*this == other);
205 }
206
210 virtual ~Value() noexcept;
211
218 template <typename T>
219 requires (ValuesVariant::AlternativeIndex<std::decay_t<T>>() != ValuesVariant::npos)
220 std::decay_t<T> Get() const {
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>;
224 return StormByte::Safe::visit([](auto &&val) -> To {
225 using From = std::decay_t<decltype(val)>;
227 throw WrongValueType("Requested type does not match stored type (null).");
228 } else if constexpr (StormByte::Type::SameAs<From, To>) {
229 return val;
231 return convert_numeric<To, From>(val);
232 } else {
233 throw WrongValueType("Requested type does not match stored type.");
234 }
235 },
236 m_value);
237 }
238
243 inline Type Type() const noexcept {
244 return m_type;
245 }
246
251 inline bool IsNull() const noexcept {
252 return m_type == Type::Null;
253 }
254
255 private:
264 template <typename To, typename From>
266 static To convert_numeric(const From &val) {
268 if constexpr (StormByte::Type::Signed<From>) {
269 std::intmax_t from = static_cast<std::intmax_t>(val);
270 if constexpr (StormByte::Type::Signed<To>) {
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);
274 } else {
275 if (from < 0)
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);
280 }
281 } else {
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);
287 } else {
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);
291 }
292 }
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.");
305 } else {
306 if (d < 0)
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.");
310 }
311 return static_cast<To>(d);
312 } else if constexpr (StormByte::Type::FloatingPoint<From> && StormByte::Type::FloatingPoint<To>) {
313 return static_cast<To>(val);
314 } else {
315 throw WrongValueType("Unsupported numeric conversion.");
316 }
317 }
318
319 ValuesVariant m_value;
320 enum Type m_type;
321 };
322 }
323}
324
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.
STL namespace.
#define STORMBYTE_DATABASE_PUBLIC
Definition visibility.h:54