DF-0
DF-0: Data processing Framework with Zero-overhead
Toggle main menu visibility
Loading...
Searching...
No Matches
ensure.h
1
/**************************************************************************/
2
/* */
3
/* DF-0: Data processing Framework with Zero-overhead */
4
/* */
5
/* Copyright (c) DF-0 Team https://github.com/df0-io/df0 */
6
/* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DF0-Commercial */
7
/* */
8
/**************************************************************************/
9
10
#ifndef DF0_ENSURE_H
11
#define DF0_ENSURE_H
12
13
#include <initializer_list>
14
#include <cassert>
15
#include "source_location.h"
16
#include "throw_if.h"
17
#include <vector>
18
19
namespace
df0
20
{
21
52
template
<
typename
T>
53
class
[[nodiscard]]
ensure
54
{
55
public
:
61
explicit
ensure
(
const
T& value,
const
source_location& loc = source_location::current())
62
: m_value(value), m_location(loc)
63
#ifndef NDEBUG
64
, m_comparison_performed(false)
65
#endif
66
{}
67
68
~ensure
()
69
{
70
// In debug builds, assert that a comparison operator was used. This prevents incorrect
71
// usage like `ensure(a == b);` from silently passing. The assert macro is automatically
72
// compiled out in release builds (when NDEBUG is defined), resulting in zero overhead.
73
assert(m_comparison_performed &&
"df0::ensure() was called without a comparison operator (e.g., ==, !=, <, etc.). "
74
"This is a bug in the calling code, not a runtime error."
);
75
}
76
81
template
<
typename
U>
82
void
operator==
(
const
U& other)
const
83
{
84
set_comparison_performed();
85
DF0_THROW_IF_AT_LOCATION(!(m_value == other), m_location, m_value, other);
86
}
87
92
template
<
typename
U>
93
void
operator!=
(
const
U& other)
const
94
{
95
set_comparison_performed();
96
DF0_THROW_IF_AT_LOCATION(!(m_value != other), m_location, m_value, other);
97
}
98
103
template
<
typename
U>
104
void
operator>
(
const
U& other)
const
105
{
106
set_comparison_performed();
107
DF0_THROW_IF_AT_LOCATION(!(m_value > other), m_location, m_value, other);
108
}
109
114
template
<
typename
U>
115
void
operator>=
(
const
U& other)
const
116
{
117
set_comparison_performed();
118
DF0_THROW_IF_AT_LOCATION(!(m_value >= other), m_location, m_value, other);
119
}
120
125
template
<
typename
U>
126
void
operator<
(
const
U& other)
const
127
{
128
set_comparison_performed();
129
DF0_THROW_IF_AT_LOCATION(!(m_value < other), m_location, m_value, other);
130
}
131
136
template
<
typename
U>
137
void
operator<=
(
const
U& other)
const
138
{
139
set_comparison_performed();
140
DF0_THROW_IF_AT_LOCATION(!(m_value <= other), m_location, m_value, other);
141
}
142
149
template
<
typename
U>
150
requires
string_like<T>
&&
string_like<U>
151
void
contains
(
const
U& substring)
const
152
{
153
set_comparison_performed();
154
DF0_THROW_IF_AT_LOCATION(std::string_view(m_value).find(substring) == std::string_view::npos, m_location, m_value, substring);
155
}
156
165
void
is_one_of
(std::initializer_list<T> expected_values)
const
166
{
167
set_comparison_performed();
168
for
(
const
auto
& expected : expected_values)
169
{
170
if
(m_value == expected)
171
{
172
return
;
// Match found, success.
173
}
174
}
175
DF0_THROW_IF_AT_LOCATION(
true
, m_location, m_value, std::vector<T>(expected_values));
176
}
177
private
:
183
void
set_comparison_performed()
const
184
{
185
#ifndef NDEBUG
186
m_comparison_performed =
true
;
187
#endif
188
}
189
190
const
T& m_value;
191
df0::source_location m_location;
192
#ifndef NDEBUG
193
mutable
bool
m_comparison_performed;
194
#endif
195
};
196
202
template
<
typename
T>
203
ensure
(
const
T&,
const
df0::source_location&) ->
ensure<T>
;
204
205
}
// namespace df0
206
207
#endif
// DF0_ENSURE_H
df0::ensure
A utility for creating expressive, exception-throwing assertions in a fluent style.
Definition
ensure.h:54
df0::ensure::ensure
ensure(const T &value, const source_location &loc=source_location::current())
Constructs an ensure object, capturing a value and the source location of the call.
Definition
ensure.h:61
df0::ensure::operator<=
void operator<=(const U &other) const
Performs a less-than-or-equal-to check (<=). Throws if m_value > other.
Definition
ensure.h:137
df0::ensure::operator<
void operator<(const U &other) const
Performs a less-than check (<). Throws if m_value >= other.
Definition
ensure.h:126
df0::ensure::operator>=
void operator>=(const U &other) const
Performs a greater-than-or-equal-to check (>=). Throws if m_value < other.
Definition
ensure.h:115
df0::ensure::operator>
void operator>(const U &other) const
Performs a greater-than check (>). Throws if m_value <= other.
Definition
ensure.h:104
df0::ensure::operator==
void operator==(const U &other) const
Performs an equality check (==). Throws if m_value != other.
Definition
ensure.h:82
df0::ensure::operator!=
void operator!=(const U &other) const
Performs an inequality check (!=). Throws if m_value == other.
Definition
ensure.h:93
df0::ensure::is_one_of
void is_one_of(std::initializer_list< T > expected_values) const
Checks if the held value is present in a given set of values. Throws if it is not.
Definition
ensure.h:165
df0::ensure::contains
void contains(const U &substring) const
Checks if the held string-like value contains a substring. Throws if it does not.
Definition
ensure.h:151
df0::string_like
Concept for string-like types that can be converted to a string view.
Definition
concepts_string.h:30
ensure.h
Generated by
1.18.0