A utility for creating expressive, exception-throwing assertions in a fluent style.
More...
|
| | 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. More...
|
| |
| template<typename U > |
| void | operator== (const U &other) const |
| | Performs an equality check (==). Throws if m_value != other. More...
|
| |
| template<typename U > |
| void | operator!= (const U &other) const |
| | Performs an inequality check (!=). Throws if m_value == other. More...
|
| |
| template<typename U > |
| void | operator> (const U &other) const |
| | Performs a greater-than check (>). Throws if m_value <= other. More...
|
| |
| template<typename U > |
| void | operator>= (const U &other) const |
| | Performs a greater-than-or-equal-to check (>=). Throws if m_value < other. More...
|
| |
| template<typename U > |
| void | operator< (const U &other) const |
| | Performs a less-than check (<). Throws if m_value >= other. More...
|
| |
| template<typename U > |
| void | operator<= (const U &other) const |
| | Performs a less-than-or-equal-to check (<=). Throws if m_value > other. More...
|
| |
| template<typename U > |
| 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. More...
|
| |
| 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. More...
|
| |
template<typename T>
class docwire::ensure< T >
A utility for creating expressive, exception-throwing assertions in a fluent style.
This class is the core of a fluent validation API. It is designed to be constructed with a value to be tested, and then used with a comparison operator to check a condition. If the condition fails, it throws a docwire::error with rich diagnostic information, including the source location of the check and the values involved.
The intended usage is natural and expressive:
ensure(const T &, const docwire::source_location &) -> ensure< T >
Deduction guide for the ensure class template.
To prevent accidental misuse where a check is written but no comparison is performed (e.g., docwire::ensure(a == b);), the class is marked [[nodiscard]] to encourage compiler warnings. More importantly, in debug builds, its destructor will assert if no comparison operator was called, immediately flagging the bug at runtime. This runtime check is compiled out in release builds for zero performance overhead.
- Performance
- In release builds (when
NDEBUG is defined), this class is a true zero-cost abstraction. Compilers like GCC, Clang, and MSVC with standard optimizations (e.g., -O2 or /O2) will inline the constructor and operators, completely optimizing away the temporary ensure object. The resulting machine code for a check like ensure(a) == b; is identical to a handwritten if (!(a == b)) throw ...;, imposing no overhead on the success path.
- Template Parameters
-
| T | The type of the value being held for comparison. |
Definition at line 55 of file ensure.h.