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
not_null.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_NOT_NULL_H
13 #define DOCWIRE_NOT_NULL_H
14 
15 #include "safety_policy.h"
16 #include "enforce.h"
17 #include <utility>
18 #include <type_traits>
19 
20 namespace docwire
21 {
22 
24 struct guaranteed_t {};
25 
27 constexpr inline guaranteed_t guaranteed;
28 
39 template <typename Ptr, safety_policy safety_level = default_safety_level>
40 class not_null
41 {
42 public:
46  not_null(Ptr p) : m_ptr(std::move(p))
47  {
48  enforce<safety_level>(m_ptr != nullptr, "not_null constructed with a null pointer.");
49  }
50 
55  not_null(Ptr p, guaranteed_t) : m_ptr(std::move(p)) {}
56 
57  // Generic forwarding constructor for constructing the underlying pointer.
58  // This is constrained to avoid interfering with copy/move constructors.
59  template <typename... Args,
60  typename = std::enable_if_t<std::is_constructible_v<Ptr, Args...> && (sizeof...(Args) > 1)>>
61  not_null(Args&&... args)
62  : m_ptr(std::forward<Args>(args)...)
63  {
64  enforce<safety_level>(m_ptr != nullptr, "not_null constructed with a null pointer.");
65  }
66 
67  // Deleted constructors to prevent creation from nullptr.
68  not_null(std::nullptr_t) = delete;
69  not_null& operator=(std::nullptr_t) = delete;
70 
72  auto get() const requires requires(const Ptr& p) { p.get(); } { return m_ptr.get(); }
73  auto get() requires requires(Ptr& p) { p.get(); } { return m_ptr.get(); }
74 
75  // Dereference operators for smart-pointer-like behavior.
76  auto& operator*() const { return *m_ptr; }
77  auto& operator*() { return *m_ptr; }
78 
79  auto operator->() const
80  {
81  return &this->operator*();
82  }
83  auto operator->()
84  {
85  return &this->operator*();
86  }
87 
88  explicit operator const Ptr&() const { return m_ptr; }
89 
90 private:
91  Ptr m_ptr;
92 };
93 
101 template <typename Ptr>
103 {
104  return not_null<std::remove_cvref_t<Ptr>>(std::forward<Ptr>(ptr), guaranteed);
105 }
106 
107 } // namespace docwire
108 
109 #endif // DOCWIRE_NOT_NULL_H
A wrapper for pointer-like types that enforces a non-null invariant.
Definition: not_null.h:41
auto get() const requires requires(const Ptr &p)
Returns the raw pointer.
Definition: not_null.h:72
not_null(Ptr p, guaranteed_t)
Unchecked constructor for when the pointer is guaranteed to be non-null. This is an optimization to a...
Definition: not_null.h:55
not_null(Ptr p)
Constructs from a pointer, enforcing the non-null invariant in strict mode.
Definition: not_null.h:46
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19
not_null< std::remove_cvref_t< Ptr > > assume_not_null(Ptr &&ptr)
Wraps a pointer-like object in a not_null, bypassing the runtime check.
Definition: not_null.h:102
requires(!string_method_equipped< T >) struct stringifier< T >
Specialization for types that are streamable to std::ostream.
constexpr guaranteed_t guaranteed
A constant to use with the unchecked not_null constructor, e.g., not_null(ptr, guaranteed).
Definition: not_null.h:27
A tag to indicate that a pointer is guaranteed to be non-null, bypassing the runtime check in not_nul...
Definition: not_null.h:24