ORM C++
Loading...
Searching...
No Matches
values.cppm
Go to the documentation of this file.
1module;
2
3#include <array>
4#include <chrono>
5#include <compare>
6#include <concepts>
7#include <cstddef>
8#include <optional>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12#include <type_traits>
13#include <utility>
14
15export module orm:foundation_values;
16
17export namespace orm
18{
19/** A calendar date. Database operations validate the calendar and backend range. */
20using Date = std::chrono::year_month_day;
21
22/** A civil date and time without an associated time zone. */
23using LocalDateTime = std::chrono::local_time<std::chrono::microseconds>;
24
25/** An instant on the UTC timeline, with microsecond resolution. */
26using UtcTimestamp = std::chrono::sys_time<std::chrono::microseconds>;
27
28/** A time of day in the half-open interval [00:00:00, 24:00:00). */
30{
31public:
32 constexpr TimeOfDay() noexcept = default;
33
34 explicit constexpr TimeOfDay(std::chrono::microseconds valueInit) : value{valueInit}
35 {
36 if (value < std::chrono::microseconds::zero() || value >= std::chrono::hours{24})
37 {
38 throw std::invalid_argument{"Time of day must be between midnight and the end of the day"};
39 }
40 }
41
42 [[nodiscard]] constexpr auto sinceMidnight() const noexcept -> std::chrono::microseconds
43 {
44 return value;
45 }
46
47 constexpr auto operator==(const TimeOfDay& other) const noexcept -> bool
48 {
49 return value.count() == other.value.count();
50 }
51
52 constexpr auto operator<=>(const TimeOfDay& other) const noexcept -> std::strong_ordering
53 {
54 if (value.count() < other.value.count())
55 {
56 return std::strong_ordering::less;
57 }
58 if (value.count() > other.value.count())
59 {
60 return std::strong_ordering::greater;
61 }
62 return std::strong_ordering::equal;
63 }
64
65private:
66 std::chrono::microseconds value{};
67};
68
69/**
70 * A 128-bit UUID in canonical byte order, independent of a database backend.
71 *
72 * Default construction produces the nil UUID. Parsing accepts exactly the
73 * 8-4-4-4-12 hexadecimal format, with either case; formatting uses lowercase.
74 * Every 128-bit pattern is representable. Byte ordering in C++ does not promise
75 * the same order as a database's native UUID comparisons.
76 */
77class Uuid
78{
79public:
80 constexpr Uuid() noexcept = default;
81
82 explicit constexpr Uuid(std::array<std::byte, 16> valueInit) noexcept : value{valueInit} {}
83
84 [[nodiscard]] constexpr auto bytes() const noexcept -> const std::array<std::byte, 16>&
85 {
86 return value;
87 }
88
89 /** @throws std::invalid_argument if the input is not in the required format. */
90 [[nodiscard]] static auto parse(std::string_view text) -> Uuid;
91
92 [[nodiscard]] auto toString() const -> std::string;
93
94 constexpr auto operator==(const Uuid& other) const noexcept -> bool
95 {
96 for (auto index = std::size_t{0}; index < value.size(); ++index)
97 {
98 if (value[index] != other.value[index])
99 {
100 return false;
101 }
102 }
103 return true;
104 }
105
106 constexpr auto operator<=>(const Uuid& other) const noexcept -> std::strong_ordering
107 {
108 for (auto index = std::size_t{0}; index < value.size(); ++index)
109 {
110 if (value[index] < other.value[index])
111 {
112 return std::strong_ordering::less;
113 }
114 if (value[index] > other.value[index])
115 {
116 return std::strong_ordering::greater;
117 }
118 }
119 return std::strong_ordering::equal;
120 }
121
122private:
123 std::array<std::byte, 16> value{};
124};
125
126/** Specialize for an application UUID type to convert explicitly to and from Uuid. */
127template <typename T>
129
130template <typename T>
131concept UuidAdaptable = requires(const T& value, const Uuid& uuid) {
132 { UuidAdapter<T>::toUuid(value) } -> std::same_as<Uuid>;
133 { UuidAdapter<T>::fromUuid(uuid) } -> std::same_as<T>;
134};
135
136/**
137 * Specialize to map a third-party value type to a canonical ORM value.
138 *
139 * `Value` must be one of the canonical ORM scalar values. The ORM derives the
140 * logical column type from it, so the converter cannot disagree with the value
141 * it returns. Converters are opt-in and independent of the built-in scalar
142 * types and of `UuidAdapter`.
143 */
144template <typename T>
146
147template <typename T>
148concept ValueConvertible = requires {
150// requires requires(const T& value, const typename ValueConverter<T>::Value& canonical) {
151// { ValueConverter<T>::toValue(value) } -> std::same_as<typename ValueConverter<T>::Value>;
152// { ValueConverter<T>::fromValue(canonical) } -> std::same_as<T>;
153 };
154};
155
156/**
157 * Specialize for an application nullable wrapper such as `boost::optional<T>`.
158 *
159 * `Value` is the contained value type. `hasValue`, `value`, `make` and `none`
160 * let models, DTOs and parameters treat the wrapper like `std::optional<T>`
161 * without the ORM depending on the wrapper's library.
162 */
163template <typename T>
165
166template <typename T>
167concept NullableAdaptable = requires {
169// requires requires(const T& value, typename NullableAdapter<T>::Value&& contained) {
170// { NullableAdapter<T>::hasValue(value) } -> std::convertible_to<bool>;
171// { NullableAdapter<T>::value(value) } -> std::convertible_to<const typename NullableAdapter<T>::Value&>;
172// { NullableAdapter<T>::make(std::move(contained)) } -> std::same_as<T>;
173// { NullableAdapter<T>::none() } -> std::same_as<T>;
174 };
175};
176
177/** `std::optional` uses the same nullable contract as application wrappers. */
178template <typename T>
179struct NullableAdapter<std::optional<T>>
180{
181 using Value = T;
182
183 static constexpr auto hasValue(const std::optional<T>& value) noexcept -> bool
184 {
185 return value.has_value();
186 }
187 static constexpr auto value(const std::optional<T>& source) -> const T&
188 {
189 return *source;
190 }
191 static auto make(const T& contained) -> std::optional<T>
192 {
193 return std::optional<T>{contained};
194 }
195 static auto make(T&& contained) -> std::optional<T>
196 {
197 return std::optional<T>{std::move(contained)};
198 }
199 static auto none() -> std::optional<T>
200 {
201 return std::nullopt;
202 }
203};
204
205/** True when `T` converts through the canonical `Uuid` value. */
206template <typename T>
207concept UuidConvertible = ValueConvertible<T> && std::same_as<typename ValueConverter<T>::Value, Uuid>;
208
209/** True when `T` carries a UUID domain, whether natively or explicitly converted. */
210template <typename T>
211concept UuidDomainValue = std::same_as<std::remove_cvref_t<T>, Uuid> || UuidAdaptable<std::remove_cvref_t<T>> ||
213} // namespace orm
constexpr auto operator==(const TimeOfDay &other) const noexcept -> bool
Definition values.cppm:47
constexpr auto sinceMidnight() const noexcept -> std::chrono::microseconds
Definition values.cppm:42
constexpr TimeOfDay() noexcept=default
constexpr auto operator<=>(const TimeOfDay &other) const noexcept -> std::strong_ordering
Definition values.cppm:52
auto toString() const -> std::string
static auto parse(std::string_view text) -> Uuid
constexpr auto bytes() const noexcept -> const std::array< std::byte, 16 > &
Definition values.cppm:84
constexpr auto operator<=>(const Uuid &other) const noexcept -> std::strong_ordering
Definition values.cppm:106
constexpr Uuid() noexcept=default
std::chrono::year_month_day Date
Definition values.cppm:20
std::chrono::local_time< std::chrono::microseconds > LocalDateTime
Definition values.cppm:23
std::chrono::sys_time< std::chrono::microseconds > UtcTimestamp
Definition values.cppm:26
static auto make(T &&contained) -> std::optional< T >
Definition values.cppm:195
static constexpr auto value(const std::optional< T > &source) -> const T &
Definition values.cppm:187
static auto make(const T &contained) -> std::optional< T >
Definition values.cppm:191
static auto none() -> std::optional< T >
Definition values.cppm:199
static constexpr auto hasValue(const std::optional< T > &value) noexcept -> bool
Definition values.cppm:183