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 "hxutility.h"
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_>::value, "hxptr does not support array types");
35 static_assert(!hxis_reference<T_>::value, "hxptr does not support reference types");
36 static_assert(!hxis_pointer<T_>::value, "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
50 hxconstexpr hxptr(hxptr&& other_) noexcept;
51
53 hxconstexpr ~hxptr(void) noexcept;
54
59 hxconstexpr hxptr& operator=(hxptr&& other_) noexcept;
60
62 hxattr_nodiscard hxconstexpr T_& operator*(void) const;
63
65 hxattr_nodiscard hxconstexpr T_* operator->(void) const;
66
68 hxattr_nodiscard hxconstexpr operator bool(void) const;
69
72 hxattr_nodiscard hxconstexpr bool operator==(const hxptr& other_) const;
73
76
77#if HX_CPLUSPLUS < 202002L // C++20 defaults != from ==.
80 hxattr_nodiscard hxconstexpr bool operator!=(const hxptr& other_) const;
81
83 hxattr_nodiscard hxconstexpr bool operator!=(hxnullptr_t) const;
84#endif
85
90 template<typename function_t_>
91 hxattr_nodiscard hxconstexpr auto and_then(function_t_&& callable_) const
93
95 hxattr_nodiscard hxconstexpr const deleter_t_& deleter(void) const;
96
99
101 hxattr_nodiscard hxconstexpr T_* get(void) const { return m_ptr_; }
102
107 template<typename function_t_>
108 hxattr_nodiscard hxconstexpr hxptr or_else(function_t_&& callable_) &&;
109
110 // An lvalue `hxptr` cannot yield ownership without leaving a dangling copy.
111 template<typename function_t_>
112 hxptr or_else(function_t_&& callable_) const& = delete;
113
117
121 hxconstexpr void reset(T_* ptr_=hxnull) noexcept;
122
125 hxconstexpr void swap(hxptr& other_) noexcept;
126
130 template<typename U_=hxremove_cv_t<T_>>
132
133private:
134 hxptr(const hxptr&) = delete;
135 hxptr& operator=(const hxptr&) = delete;
136
137 T_* m_ptr_;
138};
139
147template<typename T_, hxsystem_allocator_t allocator_=hxsystem_allocator_current,
148 hxalignment_t align_=hxalignment, typename... args_t_>
149hxattr_nodiscard hxptr<T_> hxmake_ptr(args_t_&&... args_) noexcept {
151}
152
153#include "detail/hxptr.inl"
154HX_NS_END_
hxnullptr_t - A class that will only convert to a null T pointer.
Definition hxutility.h:64
hxptr<T, deleter_t> - A unique owning pointer.
Definition hxptr.hpp:30
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:101
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 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 owned object if non-null, otherwise returns a null hx...
constexpr hxptr or_else(function_t_ &&callable_) &&
Returns this hxptr moved out if non-null, otherwise returns the result of calling callable.
T element_t
Definition hxptr.hpp:39
constexpr deleter_t_ & deleter(void)
Returns a reference to the stored deleter.
constexpr hxptr & operator=(hxptr &&other_) noexcept
Move assignment.
hxptr or_else(function_t_ &&callable_) const &=delete
constexpr void swap(hxptr &other_) noexcept
Exchanges ownership with other.
constexpr hxremove_cv_t< T > value_or(U_ &&default_value_) const
Returns the owned value if non-null, otherwise returns default_value forwarded and converted to T.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:82
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:77
unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:64
T * hxnew(Args_ &&... args_) noexcept
hxnew<T, allocator, align>(...) - Allocates and constructs an object of type T using an optional memo...
Definition hxmemory_manager.h:235
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:69
hxptr< T > hxmake_ptr(args_t_ &&... args_) noexcept
hxmake_ptr<T, allocator, align>(args...) - Allocates and constructs an object of type T and returns i...
Definition hxptr.hpp:149
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxconstexpr
hxconstexpr - Enables C++23 compatable constexpr usage for functions as that has support for destruct...
Definition hxsettings.h:263
A few utility functions and most standard C++ meta programming functions.
typename hxremove_cv_< T >::type hxremove_cv_t
Internal.
Definition hxutility.h:92
constexpr T && hxforward(hxremove_reference_t< T > &&x_)
hxforward - Implements std::forward.
Definition hxutility.h:304
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
static constexpr bool value
Definition hxutility.h:124
hxis_pointer - Returns whether T is a pointer type as hxis_pointer<T>::value.
Definition hxutility.h:206