ORM C++
Toggle main menu visibility
Loading...
Searching...
No Matches
values.cppm
Go to the documentation of this file.
1
module
;
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
15
export
module
orm:foundation_values
;
16
17
export
namespace
orm
18
{
19
/** A calendar date. Database operations validate the calendar and backend range. */
20
using
Date
= std::chrono::year_month_day;
21
22
/** A civil date and time without an associated time zone. */
23
using
LocalDateTime
= std::chrono::local_time<std::chrono::microseconds>;
24
25
/** An instant on the UTC timeline, with microsecond resolution. */
26
using
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). */
29
class
TimeOfDay
30
{
31
public
:
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
65
private
:
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
*/
77
class
Uuid
78
{
79
public
:
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
122
private
:
123
std::array<std::byte, 16> value{};
124
};
125
126
/** Specialize for an application UUID type to convert explicitly to and from Uuid. */
127
template
<
typename
T>
128
struct
UuidAdapter
;
129
130
template
<
typename
T>
131
concept
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
*/
144
template
<
typename
T>
145
struct
ValueConverter
;
146
147
template
<
typename
T>
148
concept
ValueConvertible
=
requires
{
149
typename
ValueConverter<T>::Value
;
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
*/
163
template
<
typename
T>
164
struct
NullableAdapter
;
165
166
template
<
typename
T>
167
concept
NullableAdaptable
=
requires
{
168
typename
NullableAdapter<T>::Value
;
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. */
178
template
<
typename
T>
179
struct
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. */
206
template
<
typename
T>
207
concept
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. */
210
template
<
typename
T>
211
concept
UuidDomainValue
= std::same_as<std::remove_cvref_t<T>, Uuid> || UuidAdaptable<std::remove_cvref_t<T>> ||
212
UuidConvertible<std::remove_cvref_t<T>
>;
213
}
// namespace orm
orm::TimeOfDay::operator==
constexpr auto operator==(const TimeOfDay &other) const noexcept -> bool
Definition
values.cppm:47
orm::TimeOfDay::sinceMidnight
constexpr auto sinceMidnight() const noexcept -> std::chrono::microseconds
Definition
values.cppm:42
orm::TimeOfDay::TimeOfDay
constexpr TimeOfDay() noexcept=default
orm::TimeOfDay::operator<=>
constexpr auto operator<=>(const TimeOfDay &other) const noexcept -> std::strong_ordering
Definition
values.cppm:52
orm::Uuid
Definition
values.cppm:78
orm::Uuid::toString
auto toString() const -> std::string
orm::Uuid::parse
static auto parse(std::string_view text) -> Uuid
orm::Uuid::bytes
constexpr auto bytes() const noexcept -> const std::array< std::byte, 16 > &
Definition
values.cppm:84
orm::Uuid::operator<=>
constexpr auto operator<=>(const Uuid &other) const noexcept -> std::strong_ordering
Definition
values.cppm:106
orm::Uuid::Uuid
constexpr Uuid() noexcept=default
NullableAdaptable
Definition
values.cppm:167
UuidConvertible
Definition
values.cppm:207
UuidDomainValue
Definition
values.cppm:211
orm::UuidAdaptable
Definition
values.cppm:131
orm::ValueConvertible
Definition
values.cppm:148
orm
orm
Definition
BackendRelations.cppm:18
orm::Date
std::chrono::year_month_day Date
Definition
values.cppm:20
orm::LocalDateTime
std::chrono::local_time< std::chrono::microseconds > LocalDateTime
Definition
values.cppm:23
orm::UtcTimestamp
std::chrono::sys_time< std::chrono::microseconds > UtcTimestamp
Definition
values.cppm:26
NullableAdapter< std::optional< T > >::make
static auto make(T &&contained) -> std::optional< T >
Definition
values.cppm:195
NullableAdapter< std::optional< T > >::value
static constexpr auto value(const std::optional< T > &source) -> const T &
Definition
values.cppm:187
NullableAdapter< std::optional< T > >::make
static auto make(const T &contained) -> std::optional< T >
Definition
values.cppm:191
NullableAdapter< std::optional< T > >::none
static auto none() -> std::optional< T >
Definition
values.cppm:199
NullableAdapter< std::optional< T > >::hasValue
static constexpr auto hasValue(const std::optional< T > &value) noexcept -> bool
Definition
values.cppm:183
NullableAdapter< std::optional< T > >::Value
T Value
Definition
values.cppm:181
NullableAdapter
Definition
values.cppm:164
orm::UuidAdapter
Definition
values.cppm:128
orm::ValueConverter
Definition
values.cppm:145
modules
orm
foundation
values.cppm
Generated by
1.18.0