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
ranged.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_RANGED_H
13 #define DOCWIRE_RANGED_H
14 
15 #include "safety_policy.h"
16 #include "enforce.h"
17 #include <type_traits>
18 
19 namespace docwire
20 {
21 
25 struct unlimited_t {};
26 
30 inline constexpr unlimited_t unlimited;
31 
44 template <auto Min, auto Max, typename T, safety_policy safety_level = default_safety_level>
45 class ranged
46 {
47  static_assert(!std::is_same_v<std::remove_const_t<decltype(Min)>, unlimited_t> || !std::is_same_v<std::remove_const_t<decltype(Max)>, unlimited_t>,
48  "ranged must have at least one concrete bound; use the raw type instead of ranged<..., unlimited, unlimited>.");
49 
50  static_assert(std::is_same_v<std::remove_const_t<decltype(Min)>, unlimited_t> || std::is_convertible_v<decltype(Min), T>,
51  "The Min bound must be convertible to the ranged type T.");
52 
53  static_assert(std::is_same_v<std::remove_const_t<decltype(Max)>, unlimited_t> || std::is_convertible_v<decltype(Max), T>,
54  "The Max bound must be convertible to the ranged type T.");
55 public:
60  ranged(T value) : m_value(value)
61  {
62  if constexpr (!std::is_same_v<std::remove_const_t<decltype(Min)>, unlimited_t>) {
63  enforce<safety_level>(m_value >= static_cast<T>(Min), "Value is below the expected minimum", "value"_v = m_value, "min"_v = static_cast<T>(Min));
64  }
65  if constexpr (!std::is_same_v<std::remove_const_t<decltype(Max)>, unlimited_t>) {
66  enforce<safety_level>(m_value <= static_cast<T>(Max), "Value is above the expected maximum", "value"_v = m_value, "max"_v = static_cast<T>(Max));
67  }
68  }
69 
71  operator T() const { return m_value; }
72  T get() const { return m_value; }
73 
74 private:
75  T m_value;
76 };
77 
82 template <auto Min, typename T, safety_policy safety_level = default_safety_level>
83 using at_least = ranged<Min, unlimited, T, safety_level>;
84 
89 template <auto Max, typename T, safety_policy safety_level = default_safety_level>
90 using at_most = ranged<unlimited, Max, T, safety_level>;
91 
96 template <auto Value, typename T, safety_policy safety_level = default_safety_level>
97 using exactly = ranged<Value, Value, T, safety_level>;
98 
103 template <typename T, safety_policy safety_level = default_safety_level>
104 using non_negative = at_least<0, T, safety_level>;
105 
106 } // namespace docwire
107 
108 #endif // DOCWIRE_RANGED_H
A wrapper for numeric types that enforces a range [Min, Max].
Definition: ranged.h:46
ranged(T value)
Constructs a ranged value, enforcing the bounds in strict mode.
Definition: ranged.h:60
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19
constexpr unlimited_t unlimited
A convenient instance of the unlimited_t marker.
Definition: ranged.h:30
A marker type to signify an unlimited bound in a ranged type.
Definition: ranged.h:25