12 #ifndef DOCWIRE_ENSURE_H
13 #define DOCWIRE_ENSURE_H
15 #include <initializer_list>
17 #include "source_location.h"
64 : m_value(value), m_location(loc)
66 , m_comparison_performed(false)
75 assert(m_comparison_performed &&
"docwire::ensure() was called without a comparison operator (e.g., ==, !=, <, etc.). "
76 "This is a bug in the calling code, not a runtime error.");
86 set_comparison_performed();
87 DOCWIRE_THROW_IF_AT_LOCATION(!(m_value == other), m_location, m_value, other);
97 set_comparison_performed();
98 DOCWIRE_THROW_IF_AT_LOCATION(!(m_value != other), m_location, m_value, other);
108 set_comparison_performed();
109 DOCWIRE_THROW_IF_AT_LOCATION(!(m_value > other), m_location, m_value, other);
119 set_comparison_performed();
120 DOCWIRE_THROW_IF_AT_LOCATION(!(m_value >= other), m_location, m_value, other);
130 set_comparison_performed();
131 DOCWIRE_THROW_IF_AT_LOCATION(!(m_value < other), m_location, m_value, other);
141 set_comparison_performed();
142 DOCWIRE_THROW_IF_AT_LOCATION(!(m_value <= other), m_location, m_value, other);
152 requires string_like<T> && string_like<U>
155 set_comparison_performed();
156 DOCWIRE_THROW_IF_AT_LOCATION(std::string_view(m_value).find(substring) == std::string_view::npos, m_location, m_value, substring);
167 void is_one_of(std::initializer_list<T> expected_values)
const
169 set_comparison_performed();
170 for (
const auto& expected : expected_values)
172 if (m_value == expected)
177 DOCWIRE_THROW_IF_AT_LOCATION(
true, m_location, m_value, std::vector<T>(expected_values));
185 void set_comparison_performed()
const
188 m_comparison_performed =
true;
195 mutable bool m_comparison_performed;
A utility for creating expressive, exception-throwing assertions in a fluent style.
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.
void operator==(const U &other) const
Performs an equality check (==). Throws if m_value != other.
requires string_like< T > &&string_like< U > void contains(const U &substring) const
Checks if the held string-like value contains a substring. Throws if it does not.
void operator!=(const U &other) const
Performs an inequality check (!=). Throws if m_value == other.
void operator<=(const U &other) const
Performs a less-than-or-equal-to check (<=). Throws if m_value > other.
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.
void operator<(const U &other) const
Performs a less-than check (<). Throws if m_value >= other.
void operator>=(const U &other) const
Performs a greater-than-or-equal-to check (>=). Throws if m_value < other.
void operator>(const U &other) const
Performs a greater-than check (>). Throws if m_value <= other.
The main namespace for the DocWire SDK.
ensure(const T &, const docwire::source_location &) -> ensure< T >
Deduction guide for the ensure class template.
requires(!string_method_equipped< T >) struct stringifier< T >
Specialization for types that are streamable to std::ostream.
A fallback implementation of source_location for compilers that do not support std::source_location.