libhatchet
Loading...
Searching...
No Matches
hxptr.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
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16#include "hxkey.hpp"
17
18HX_NS_BEGIN_
19
29template<typename T_, typename deleter_t_=hxdefault_delete>
30class hxptr : private deleter_t_ {
31public:
32 // No arrays allowed because there is no delete[] equivalent. Use hxref for
33 // pointer and reference types..
34 static_assert(!hxis_array<T_>(), "hxptr does not support array types");
35 static_assert(!hxis_reference<T_>(), "hxptr does not support reference types");
36 static_assert(!hxis_pointer<T_>(), "hxptr does not support pointer types");
37
39 using element_t = T_;
40
45 hxconstexpr explicit hxptr(T_* ptr_=hxnull, deleter_t_ deleter_=deleter_t_()) noexcept;
46
49 hxconstexpr hxptr(hxnil_t) noexcept : hxptr(hxnull) { } // GCOVR_EXCL_LINE. Constant folded.
50
54 hxconstexpr hxptr(hxptr&& x_) noexcept;
55
57 hxconstexpr ~hxptr(void) noexcept;
58
64
67
70
72 hxattr_nodiscard hxconstexpr explicit operator bool(void) const;
73
76 hxattr_nodiscard hxconstexpr bool operator==(const hxptr& x_) const { return this->equal(x_); }
77
80
81#if HX_CPLUSPLUS < 202002L // C++20 defaults != from ==.
84 hxattr_nodiscard hxconstexpr bool operator!=(const hxptr& x_) const;
85
87 hxattr_nodiscard hxconstexpr bool operator!=(hxnil_t) const;
88#endif
89
90#if HX_CPLUSPLUS >= 202302L
95 template<typename self_t_, typename callable_t_>
96 hxattr_nodiscard hxconstexpr auto and_then(this self_t_&& self_, callable_t_&& callable_)
98#endif // HX_CPLUSPLUS >= 202302L
99
101 hxattr_nodiscard hxconstexpr const deleter_t_& deleter(void) const;
102
104
109 hxalignment_t alignment_=hxalignment, typename... args_t_>
110 T_& emplace(args_t_&&... args_) noexcept;
111
115
117 hxattr_nodiscard hxconstexpr T_* get(void) const { return m_ptr_; }
118
121
123 hxattr_nodiscard hxconstexpr bool has_value(void) const { return m_ptr_ != hxnull; }
124
125#if HX_CPLUSPLUS >= 202302L
130 template<typename self_t_, typename callable_t_>
132 this self_t_&& self_, callable_t_&& callable_)
135#endif // HX_CPLUSPLUS >= 202302L
136
140
144 hxconstexpr void reset(T_* ptr_=hxnull) noexcept;
145
148 hxconstexpr void swap(hxptr& x_) noexcept;
149
152
153#if HX_CPLUSPLUS >= 202302L
157 template<typename self_t_, typename... args_t_>
159 this self_t_&& self_, args_t_&&... args_);
160#endif // HX_CPLUSPLUS >= 202302L
161
162private:
163 hxptr(const hxptr&) = delete;
164 hxptr& operator=(const hxptr&) = delete;
165
166 T_* m_ptr_;
167};
168
178template<typename T_, typename deleter_t_=hxdefault_delete,
180 hxalignment_t align_=hxalignment, typename U_=T_>
182 return hxptr<T_, deleter_t_>(::new(hxmalloc_ext(sizeof(T_), allocator_, align_)) T_(value_));
183}
184
186template<typename T_, typename deleter_t_>
187class hxkey_equal_t<hxptr<T_, deleter_t_> > {
188public:
190 const hxptr<T_, deleter_t_>& x_, const hxptr<T_, deleter_t_>& y_) const {
191 return x_.equal(y_);
192 }
193};
194
197template<typename T_, typename deleter_t_>
198class hxkey_hash_t<hxptr<T_, deleter_t_> > {
199public:
202 return ptr_.hash();
203 }
204};
205
206#include "detail/hxptr.inl"
207HX_NS_END_
hxdefault_delete - A callable that deletes objects of type T using hxdelete.
Definition hxmemory_manager.h:265
bool operator()(const hxptr< T, deleter_t_ > &x_, const hxptr< T, deleter_t_ > &y_) const
Definition hxptr.hpp:189
hxkey_equal_t<T> - A constexpr callable that returns true if two objects are equivalent.
Definition hxkey.hpp:57
hxhash_t operator()(const hxptr< T, deleter_t_ > &ptr_) const
Definition hxptr.hpp:201
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
hxptr<T, deleter_t> - A unique owning pointer.
Definition hxptr.hpp:30
constexpr hxremove_cv_t< T > value_or(this self_t_ &&self_, args_t_ &&... args_)
Returns the owned value if non-null, otherwise returns a T constructed from args.
constexpr hxptr(T *ptr_=0, deleter_t_ deleter_=deleter_t_()) noexcept
Constructs an hxptr that takes ownership of ptr with a specific deleter instance.
constexpr T * get(void) const
Returns the owned pointer without releasing ownership.
Definition hxptr.hpp:117
constexpr bool has_value(void) const
Returns true if the owned pointer is non-null.
Definition hxptr.hpp:123
constexpr T & operator*(void) const
Returns a reference to the owned object. The pointer must not be null.
constexpr hxptr or_else(this self_t_ &&self_, callable_t_ &&callable_)
Returns this hxptr moved out if non-null, otherwise returns the result of calling callable.
constexpr ~hxptr(void) noexcept
Destroys the owned object using deleter_t if non-null.
constexpr 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 owned object if non-null, otherwise returns hxnil.
constexpr T * release(void)
Releases ownership and returns the previously owned pointer without invoking the deleter.
constexpr void reset(T *ptr_=0) noexcept
Destroys the currently owned object using deleter_t if non-null, then takes ownership of ptr.
constexpr const deleter_t_ & deleter(void) const
Returns a const reference to the stored deleter.
constexpr hxptr(hxptr &&x_) noexcept
Move constructor.
constexpr void swap(hxptr &x_) noexcept
Exchanges ownership with x.
constexpr bool operator==(const hxptr &x_) const
Returns true if this and x point to the same object.
Definition hxptr.hpp:76
T element_t
Definition hxptr.hpp:39
T & emplace(args_t_ &&... args_) noexcept
Replaces the owned object with one constructed from args and returns a reference to it.
constexpr T & value(void) const
Returns a reference to the owned object without releasing ownership.
constexpr deleter_t_ & deleter(void)
constexpr bool equal(const hxptr &x_) const
Returns true if this and x point to the same object.
constexpr T * operator->(void) const
Returns the owned pointer. The pointer must not be null.
constexpr hxptr & operator=(hxptr &&x_) noexcept
Move assignment.
constexpr bool operator==(hxnil_t) const
Returns true if the owned pointer is null.
hxhash_t hash(void) const
Returns the hash of the owned value if non-null, otherwise 31u.
User-specializable key-equal, key-less, and key-hash callables.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:80
HX_C_BEGIN_ typedef unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:62
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:75
hxattr_noexcept hxsystem_allocator_t allocator_
Definition hxmemory_manager.h:120
hxattr_noexcept hxsystem_allocator_t hxalignment_t alignment_
Definition hxmemory_manager.h:121
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:67
hxptr< T, deleter_t_ > hxmake_ptr(const U_ &value_) noexcept
hxmake_ptr<T, deleter, allocator, align>(value) - Allocates and constructs an object of type T from v...
Definition hxptr.hpp:181
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#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
#define hxconstexpr
hxconstexpr - Enables C++23 compatable constexpr usage for functions as that has support for destruct...
Definition hxsettings.h:266
typename hxremove_cv_< T >::type hxremove_cv_t
hxremove_cv_t - Removes const and volatile from a type.
Definition hxutility.h:79
constexpr bool hxis_rvalue_reference(void)
hxis_rvalue_reference<T> - Implements std::is_rvalue_reference.
Definition hxutility.h:237
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
constexpr bool hxis_array(void)
hxis_array<T> - Implements std::is_array.
Definition hxutility.h:148
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 hxis_pointer(void)
hxis_pointer<T> - Implements std::is_pointer.
Definition hxutility.h:216
constexpr bool hxis_const(void)
hxis_const<T> - Implements std::is_const.
Definition hxutility.h:157
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