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
9
10#include "libhatchet.h"
11
12// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
13#if HX_USE_MACROS_WITH_MODULE
14#error Header does not provide macros alone.
15#endif
16
17#include "hxoptional.hpp"
18
19HX_NS_BEGIN_
20
22template<typename T_> class hxref;
23
24namespace hxdetail_ {
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 { };
29}
31
41template<typename T_>
42class hxref {
43public:
44 // Use hxremove_reference_t<T> if needed.
45 static_assert(!hxis_reference<T_>::value, "hxref does not support reference types");
46
49 using value_type = T_&;
50
52 hxref(void) : m_value_(hxnull) { }
53
56 hxref(hxnullopt_t) : m_value_(hxnull) { }
57
61 template<typename U_, hxenable_if_t<
62 !hxdetail_::hxis_hxref_<hxremove_cvref_t<U_>>::value &&
63 !hxdetail_::hxis_hxoptional_<hxremove_cvref_t<U_>>::value, bool> = true>
64 hxref(U_& value_);
65
69 template<typename U_, hxenable_if_t<
70 !hxis_same<U_, T_>::value, bool> = true>
71 hxref(const hxref<U_>& other_);
72
77 template<typename U_>
79
81 hxattr_nodiscard T_& operator*(void) const;
82
85
87 hxattr_nodiscard operator bool(void) const { return m_value_ != hxnull; }
88
91 hxref& operator=(hxnullopt_t) { m_value_ = hxnull; return *this; }
92
95 template<typename U_, hxenable_if_t<
96 !hxdetail_::hxis_hxref_<hxremove_cvref_t<U_>>::value &&
97 !hxdetail_::hxis_hxoptional_<hxremove_cvref_t<U_>>::value, bool> = true>
98 hxref& operator=(U_& value_);
99
103 hxattr_nodiscard bool operator==(const hxref& rhs_) const;
104
107 hxattr_nodiscard bool operator==(hxnullopt_t) const { return m_value_ == hxnull; }
108
111 hxattr_nodiscard bool operator==(const T_& value_) const;
112
113#if HX_CPLUSPLUS < 202002L // C++20 defaults != from ==.
116 hxattr_nodiscard bool operator!=(const hxref& rhs_) const { return !(*this == rhs_); }
117
120 hxattr_nodiscard bool operator!=(hxnullopt_t nullopt_) const { return !(*this == nullopt_); }
121
125 hxattr_nodiscard bool operator!=(const T_& value_) const { return !(*this == value_); }
126#endif
127
133 template<typename function_t_>
134 hxattr_nodiscard auto and_then(function_t_&& callable_) const
136
140 template<typename U_>
141 T_& emplace(U_& value_);
142
144 hxattr_nodiscard bool has_value(void) const { return m_value_ != hxnull; }
145
150 template<typename function_t_>
151 hxattr_nodiscard hxref or_else(function_t_&& callable_) const;
152
154 void reset(void) { m_value_ = hxnull; }
155
158 void swap(hxref& other_) noexcept;
159
161 hxattr_nodiscard T_& value(void) const;
162
166 template<typename U_=hxremove_cv_t<T_>>
167 hxattr_nodiscard hxremove_cv_t<T_> value_or(U_&& default_value_) const;
168
169private:
170 T_* m_value_;
171};
172
176template<typename T_>
178 return ref_.has_value() ? hxkey_hash(*ref_) : hxhash_t{1u};
179}
180
181#include "detail/hxref.inl"
182HX_NS_END_
hxoptional<T> - Holds either a value of type T or nothing.
Definition hxoptional.hpp:52
hxref<T> - Holds a reference to a value of type T, or nothing.
Definition hxref.hpp:42
bool operator==(hxnullopt_t) const
Returns true if this reference is disengaged.
Definition hxref.hpp:107
bool operator==(const hxref &rhs_) const
Returns true if both references are disengaged or both reference equal values.
hxremove_cv_t< T > value_or(U_ &&default_value_) const
Returns the referenced value if engaged, otherwise returns default_value forwarded and converted to T...
hxref & operator=(U_ &value_)
Rebinds the reference to value.
T * operator->(void) const
Returns a pointer to the referenced value. The reference must be engaged.
hxref(const hxref< U_ > &other_)
Binds a reference by converting the referent of other.
T & value(void) const
Returns a reference to the referenced value. The reference must be engaged.
T & emplace(U_ &value_)
Binds the reference to value, engaging the reference.
void reset(void)
Disengages the reference. The referent is not affected.
Definition hxref.hpp:154
hxref(U_ &value_)
Binds a reference to value.
bool operator==(const T &value_) const
Returns true if this reference is engaged and its referent equals value.
hxref & operator=(hxnullopt_t)
Disengages the reference.
Definition hxref.hpp:91
auto and_then(function_t_ &&callable_) const -> hxremove_cvref_t< decltype(hxforward< function_t_ >(callable_)(hxdeclval< T & >()))>
Returns the result of calling callable with the referenced value if engaged, otherwise returns a dise...
hxref(hxoptional< U_ > &other_)
Binds a reference by converting the value of other.
hxref(hxnullopt_t)
Constructs a disengaged hxref from hxnullopt.
Definition hxref.hpp:56
hxref or_else(function_t_ &&callable_) const
Returns a copy of this reference if engaged, otherwise returns the result of calling callable.
void swap(hxref &other_) noexcept
Exchanges the bound pointers with other.
hxref(void)
Constructs a disengaged hxref.
Definition hxref.hpp:52
T & value_type
value_type - Publishes the referenced value type.
Definition hxref.hpp:49
T & operator*(void) const
Returns a reference to the referenced value. The reference must be engaged.
bool has_value(void) const
Returns true if the reference is engaged.
Definition hxref.hpp:144
An optional value type.
hxhash_t hxkey_hash(const hxref< T > &ref_)
hxkey_hash(hxref<T>) - Returns the hash of the referenced value if engaged, otherwise 1u.
Definition hxref.hpp:177
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
typename hxremove_cv_< T >::type hxremove_cv_t
Internal.
Definition hxutility.h:92
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:115
T && hxdeclval(void) noexcept
hxdeclval - Implements std::declval.
Provides core macros, memory management and feature detection.
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric constant 0.
Definition libhatchet.h:277
uint32_t hxhash_t
hxhash_t - Unsigned 32-bit hash value. Expect collisions.
Definition libhatchet.h:212
static constexpr bool value
Definition hxutility.h:124
hxnullopt_t - A tag type used to construct a disengaged hxoptional.
Definition hxoptional.hpp:34