userver: userver/utils/not_null.hpp Source File
Loading...
Searching...
No Matches
not_null.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/not_null.hpp
4/// @brief @copybrief utils::NotNull
5
6#include <functional>
7#include <memory>
8#include <type_traits>
9#include <utility>
10
11#include <userver/utils/assert.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utils {
16
17/// @ingroup userver_universal userver_containers
18///
19/// @brief Restricts a pointer or smart pointer to only hold non-null values.
20template <typename T>
21class NotNull {
22 static_assert(!std::is_reference_v<T>, "NotNull does not work with references");
23 static_assert(!std::is_const_v<T>);
24
25public:
26 constexpr explicit NotNull() = delete;
27
28 constexpr explicit NotNull(const T& u) : ptr_(u) { UASSERT_MSG(ptr_, "Trying to construct NotNull from null"); }
29
30 constexpr explicit NotNull(T&& u) : ptr_(std::move(u)) {
31 UASSERT_MSG(ptr_, "Trying to construct NotNull from null");
32 }
33
34 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
35 constexpr explicit NotNull(U&& u) : ptr_(std::forward<U>(u)) {
36 UASSERT_MSG(ptr_, "Trying to construct NotNull from null");
37 }
38
39 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T>>>
40 constexpr /*implicit*/ NotNull(U& u) : ptr_(std::addressof(u)) {}
41
42 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
43 constexpr NotNull(const NotNull<U>& other) : ptr_(other.GetBase()) {
44 UASSERT_MSG(ptr_, "Trying to construct NotNull from null (moved-from) NotNull");
45 }
46
47 template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
48 constexpr NotNull(NotNull<U>&& other) : ptr_(std::move(other).GetBase()) {
49 UASSERT_MSG(ptr_, "Trying to construct NotNull from null (moved-from) NotNull");
50 }
51
52 constexpr NotNull(std::nullptr_t) = delete;
53
54 NotNull(const NotNull& other) noexcept = default;
55 NotNull(NotNull&& other) noexcept = default;
56
57 NotNull& operator=(const NotNull& other) noexcept = default;
58 NotNull& operator=(NotNull&& other) noexcept = default;
59
60 constexpr NotNull& operator=(std::nullptr_t) = delete;
61
62 constexpr const T& GetBase() const& {
63 UASSERT_MSG(ptr_, "Trying to access a null (moved-from) NotNull");
64 return ptr_;
65 }
66
67 constexpr T&& GetBase() && {
68 UASSERT_MSG(ptr_, "Trying to access a null (moved-from) NotNull");
69 return std::move(ptr_);
70 }
71
72 constexpr /*implicit*/ operator const T&() const& { return GetBase(); }
73
74 constexpr /*implicit*/ operator bool() = delete;
75
76 constexpr decltype(auto) operator->() const& { return GetBase(); }
77
78 constexpr decltype(auto) operator*() const& { return *GetBase(); }
79
80 template <typename U>
81 constexpr bool operator==(const NotNull<U>& other) const& {
82 return GetBase() == other.GetBase();
83 }
84
85 template <typename U>
86 constexpr bool operator!=(const NotNull<U>& other) const& {
87 return GetBase() != other.GetBase();
88 }
89
90private:
91 T ptr_;
92};
93
94/// @ingroup userver_universal userver_containers
95///
96/// @brief A `std::shared_ptr` that is guaranteed to be not-null.
97/// @see MakeSharedRef
98template <typename U>
99using SharedRef = NotNull<std::shared_ptr<U>>;
100
101/// @ingroup userver_universal userver_containers
102///
103/// @brief A `std::unique_ptr` that is guaranteed to be not-null.
104/// @see MakeUniqueRef
105template <typename U>
107
108/// @brief An equivalent of `std::make_shared` for SharedRef.
109template <typename U, typename... Args>
111 return SharedRef<U>{std::make_shared<U>(std::forward<Args>(args)...)};
112}
113
114/// @brief An equivalent of `std::make_unique` for UniqueRef.
115template <typename U, typename... Args>
117 return UniqueRef<U>{std::make_unique<U>(std::forward<Args>(args)...)};
118}
119
120} // namespace utils
121
122USERVER_NAMESPACE_END
123
124template <typename T>
125// NOLINTNEXTLINE(cert-dcl58-cpp)
126struct std::hash<USERVER_NAMESPACE::utils::NotNull<T>> : public std::hash<T> {
127 using std::hash<T>::hash;
128
129 auto operator()(const USERVER_NAMESPACE::utils::NotNull<T>& value) const
130 noexcept(std::is_nothrow_invocable_v<const std::hash<T>&, const T&>) {
131 return this->std::hash<T>::operator()(value.GetBase());
132 }
133};