libhatchet
Loading...
Searching...
No Matches
hxref.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11static_assert(HX_CPLUSPLUS >= 202302L, "hxref.hpp requires C++23");
12
13// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
14#if HX_USE_MACROS_WITH_MODULE
15#error Header does not provide macros alone.
16#endif
17
18#include "hxexpected.hpp"
19
20HX_NS_BEGIN_
21
23template<typename T_> class hxref;
24
25// Used to prevent the lvalue binding constructor from hijacking converting
26// construction paths.
27template<typename> struct hxis_hxref_ : hxfalse_t { };
28template<typename U_> struct hxis_hxref_<hxref<U_>> : hxtrue_t { };
30
38template<typename T_>
39class hxref {
40public:
41 // Use hxremove_reference_t<T> if needed.
42 static_assert(!hxis_reference<T_>(), "hxref does not support reference types");
43
46 using value_t = T_&;
47
49 hxref(void) : m_value_(hxnull) { }
50
53 hxref(hxnil_t) : m_value_(hxnull) { }
54
58 template<typename U_, hxenable_if_t<
59 !hxis_hxref_<hxremove_cvref_t<U_>>::value &&
60 !hxis_hxexpected_<hxremove_cvref_t<U_>>::value &&
61 hxbinds_directly<T_, U_>(), bool> = true>
62 hxref(U_& value_);
63
67 template<typename E_>
69
72 hxattr_nodiscard T_& operator*(void) const;
73
77
79 hxattr_nodiscard explicit operator bool(void) const { return m_value_ != hxnull; }
80
83 hxref& operator=(hxnil_t) { m_value_ = hxnull; return *this; }
84
87 template<typename U_, hxenable_if_t<
88 !hxis_hxref_<hxremove_cvref_t<U_>>::value &&
89 !hxis_hxexpected_<hxremove_cvref_t<U_>>::value &&
90 hxbinds_directly<T_, U_>(), bool> = true>
91 hxref& operator=(U_& value_);
92
96 hxattr_nodiscard bool operator==(const hxref& x_) const { return this->equal(x_); }
97
100 hxattr_nodiscard bool operator==(hxnil_t) const { return m_value_ == hxnull; }
101
105 hxattr_nodiscard bool operator==(const T_& value_) const;
106
111 template<typename self_t_, typename callable_t_>
112 hxattr_nodiscard auto and_then(this self_t_&& self_, callable_t_&& callable_)
114
118 hxattr_nodiscard bool equal(const hxref& x_) const;
119
121 hxattr_nodiscard bool has_value(void) const { return m_value_ != hxnull; }
122
125
130 template<typename self_t_, typename callable_t_>
131 hxattr_nodiscard hxref or_else(this self_t_&& self_, callable_t_&& callable_);
132
134 void reset(void) { m_value_ = hxnull; }
135
138 void swap(hxref& x_) noexcept;
139
142 hxattr_nodiscard T_& value(void) const;
143
147 template<typename self_t_, typename... args_t_>
149 this self_t_&& self_, args_t_&&... args_);
150
151private:
152 T_* m_value_;
153};
154
158template<typename T_, typename U_=T_, hxenable_if_t<hxbinds_directly<T_, U_>(), bool> = true>
159hxattr_nodiscard hxref<T_> hxmake_ref(U_& value_) { return hxref<T_>(value_); }
160
162template<typename T_>
163class hxkey_equal_t<hxref<T_> > {
164public:
167 const hxref<T_>& x_, const hxref<T_>& y_) const {
168 return x_.equal(y_);
169 }
170};
171
174template<typename T_>
175class hxkey_hash_t<hxref<T_> > {
176public:
178 hxhash_t operator()(const hxref<T_>& ref_) const {
179 return ref_.hash();
180 }
181};
182
183#include "detail/hxref.inl"
184HX_NS_END_
hxexpected<T, E=bool> - Holds either a value of type T or an error of type E.
Definition hxexpected.hpp:67
bool operator()(const hxref< T > &x_, const hxref< T > &y_) const
Definition hxref.hpp:166
hxkey_equal_t<T> - A constexpr callable that returns true if two objects are equivalent.
Definition hxkey.hpp:57
hxhash_t operator()(const hxref< T > &ref_) const
Definition hxref.hpp:178
hxkey_hash_t<T> - A callable that returns the xxhash32 of a numeric value cast to 32 bits.
Definition hxkey.hpp:146
hxnil_t - A class that will only convert to a null T pointer or a null hxhandle_t.
Definition hxutility.h:57
hxref<T> - Holds a reference to a value of type T, or null.
Definition hxref.hpp:39
hxref(hxnil_t)
Constructs a null hxref from hxnil.
Definition hxref.hpp:53
bool operator==(hxnil_t) const
Returns true if this reference is null.
Definition hxref.hpp:100
hxref & operator=(U_ &value_)
Rebinds the reference to value.
T * operator->(void) const
Returns a pointer to the referenced value.
hxref(hxexpected< T, E_ > &x_)
Binds a reference to the value of x.
bool operator==(const hxref &x_) const
Returns true if both references are null or both reference equal values.
Definition hxref.hpp:96
T & value(void) const
Returns a reference to the referenced value.
hxref or_else(this self_t_ &&self_, callable_t_ &&callable_)
Returns a copy of this reference if non-null, otherwise returns the result of calling callable.
void reset(void)
Nulls the reference. The referent is not affected.
Definition hxref.hpp:134
bool equal(const hxref &x_) const
Returns true if both references are null or both reference equal values.
hxremove_cv_t< T > value_or(this self_t_ &&self_, args_t_ &&... args_)
Returns the referenced value if non-null, otherwise returns a T constructed from args.
hxref(U_ &value_)
Binds a reference to value.
void swap(hxref &x_) noexcept
Exchanges the bound pointers with x.
bool operator==(const T &value_) const
Returns true if this reference is non-null and its referent equals value.
T & value_t
value_type - Publishes the referenced value type.
Definition hxref.hpp:46
hxref & operator=(hxnil_t)
Nulls the reference.
Definition hxref.hpp:83
hxref(void)
Constructs a null hxref.
Definition hxref.hpp:49
hxhash_t hash(void) const
Returns the hash of the referenced value if non-null, otherwise 31u.
T & operator*(void) const
Returns a reference to the referenced value.
auto and_then(this self_t_ &&self_, callable_t_ &&callable_) -> hxremove_cvref_t< decltype(hxforward< callable_t_ >(callable_)(hxdeclval< T & >()))>
Returns the result of calling callable with the referenced value if non-null, otherwise returns hxnil...
bool has_value(void) const
Returns true if the reference is non-null.
Definition hxref.hpp:121
An expected value type.
hxref< T > hxmake_ref(U_ &value_)
hxmake_ref<T> - Returns an hxref<T> bound to value.
Definition hxref.hpp:159
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define HX_CPLUSPLUS
HX_CPLUSPLUS - A version of __cplusplus that is defined to 0 when __cplusplus is undefined.
Definition hxsettings.h:31
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:90
typename hxremove_cv_< T >::type hxremove_cv_t
hxremove_cv_t - Removes const and volatile from a type.
Definition hxutility.h:79
hxremove_cv_t< hxremove_reference_t< T > > hxremove_cvref_t
hxremove_cvref_t<T> - Returns T with const, volatile, and references removed.
Definition hxutility.h:103
T && hxdeclval(void) noexcept
hxdeclval - Implements std::declval.
constexpr bool hxis_reference(void)
hxis_reference<T> - Implements std::is_reference.
Definition hxutility.h:228
constexpr bool hxbinds_directly(void)
hxbinds_directly<T, U> - Returns true when an lvalue of type U binds to T& without creating a tempora...
Definition hxutility.h:138
Provides core macros, memory management and feature detection.
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric literal 0.
Definition libhatchet.h:280
uint32_t hxhash_t
hxhash_t - Unsigned 32-bit hash value. Expect collisions.
Definition libhatchet.h:210
hxfalse_t - Implements std::false_type.
Definition hxutility.h:112
hxtrue_t - Implements std::true_type.
Definition hxutility.h:110