DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
ensure.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_ENSURE_H
13 #define DOCWIRE_ENSURE_H
14 
15 #include <initializer_list>
16 #include <cassert>
17 #include "source_location.h"
18 #include "throw_if.h"
19 #include <vector>
20 
21 namespace docwire
22 {
23 
54 template<typename T>
55 class [[nodiscard]] ensure
56 {
57 public:
63  explicit ensure(const T& value, const source_location& loc = source_location::current())
64  : m_value(value), m_location(loc)
65 #ifndef NDEBUG
66  , m_comparison_performed(false)
67 #endif
68  {}
69 
70  ~ensure()
71  {
72  // In debug builds, assert that a comparison operator was used. This prevents incorrect
73  // usage like `ensure(a == b);` from silently passing. The assert macro is automatically
74  // compiled out in release builds (when NDEBUG is defined), resulting in zero overhead.
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.");
77  }
78 
83  template<typename U>
84  void operator==(const U& other) const
85  {
86  set_comparison_performed();
87  DOCWIRE_THROW_IF_AT_LOCATION(!(m_value == other), m_location, m_value, other);
88  }
89 
94  template<typename U>
95  void operator!=(const U& other) const
96  {
97  set_comparison_performed();
98  DOCWIRE_THROW_IF_AT_LOCATION(!(m_value != other), m_location, m_value, other);
99  }
100 
105  template<typename U>
106  void operator>(const U& other) const
107  {
108  set_comparison_performed();
109  DOCWIRE_THROW_IF_AT_LOCATION(!(m_value > other), m_location, m_value, other);
110  }
111 
116  template<typename U>
117  void operator>=(const U& other) const
118  {
119  set_comparison_performed();
120  DOCWIRE_THROW_IF_AT_LOCATION(!(m_value >= other), m_location, m_value, other);
121  }
122 
127  template<typename U>
128  void operator<(const U& other) const
129  {
130  set_comparison_performed();
131  DOCWIRE_THROW_IF_AT_LOCATION(!(m_value < other), m_location, m_value, other);
132  }
133 
138  template<typename U>
139  void operator<=(const U& other) const
140  {
141  set_comparison_performed();
142  DOCWIRE_THROW_IF_AT_LOCATION(!(m_value <= other), m_location, m_value, other);
143  }
144 
151  template<typename U>
152  requires string_like<T> && string_like<U>
153  void contains(const U& substring) const
154  {
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);
157  }
158 
167  void is_one_of(std::initializer_list<T> expected_values) const
168  {
169  set_comparison_performed();
170  for (const auto& expected : expected_values)
171  {
172  if (m_value == expected)
173  {
174  return; // Match found, success.
175  }
176  }
177  DOCWIRE_THROW_IF_AT_LOCATION(true, m_location, m_value, std::vector<T>(expected_values));
178  }
179 private:
185  void set_comparison_performed() const
186  {
187 #ifndef NDEBUG
188  m_comparison_performed = true;
189 #endif
190  }
191 
192  const T& m_value;
193  docwire::source_location m_location;
194 #ifndef NDEBUG
195  mutable bool m_comparison_performed;
196 #endif
197 };
198 
204 template<typename T>
206 
207 } // namespace docwire
208 
209 #endif // DOCWIRE_ENSURE_H
A utility for creating expressive, exception-throwing assertions in a fluent style.
Definition: ensure.h:56
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:167
void operator==(const U &other) const
Performs an equality check (==). Throws if m_value != other.
Definition: ensure.h:84
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.
Definition: ensure.h:153
void operator!=(const U &other) const
Performs an inequality check (!=). Throws if m_value == other.
Definition: ensure.h:95
void operator<=(const U &other) const
Performs a less-than-or-equal-to check (<=). Throws if m_value > other.
Definition: ensure.h:139
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:63
void operator<(const U &other) const
Performs a less-than check (<). Throws if m_value >= other.
Definition: ensure.h:128
void operator>=(const U &other) const
Performs a greater-than-or-equal-to check (>=). Throws if m_value < other.
Definition: ensure.h:117
void operator>(const U &other) const
Performs a greater-than check (>). Throws if m_value <= other.
Definition: ensure.h:106
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19
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.