32#include <jsonifier/Index.hpp>
44 template<
typename value_type,
typename deleter = std::default_delete<value_type>>
class unique_ptr :
protected deleter {
46 using element_type = std::remove_extent_t<value_type>;
47 using pointer = element_type*;
48 using deleter_type = deleter;
49 using reference = element_type&;
51 DCA_INLINE unique_ptr& operator=(
const unique_ptr&) =
delete;
52 DCA_INLINE
unique_ptr(
const unique_ptr&) =
delete;
60 template<jsonifier::concepts::unique_ptr_t value_type_new> DCA_INLINE unique_ptr&
operator=(value_type_new&& other) {
61 if (
this !=
static_cast<void*
>(&other)) {
75 template<jsonifier::concepts::unique_ptr_t value_type_new> DCA_INLINE
unique_ptr(value_type_new&& other) {
76 *
this = std::move(other);
82 template<jsonifier::concepts::po
inter_t value_type_new> DCA_INLINE unique_ptr&
operator=(value_type_new newPtr) {
95 template<jsonifier::concepts::po
inter_t value_type_new> DCA_INLINE
unique_ptr(value_type_new newPtr) {
101 DCA_INLINE pointer
get()
const {
107 DCA_INLINE
operator bool()
const {
108 return ptr !=
nullptr;
115 throw dca_exception{
"Sorry, but you attempted to access a unique_ptr that is nullptr." };
124 throw dca_exception{
"Sorry, but you attempted to access a unique_ptr that is nullptr." };
132 pointer releasedPtr =
ptr;
139 DCA_INLINE
void reset(pointer newPtr =
nullptr) {
140 pointer oldPtr = std::exchange(
ptr, newPtr);
148 DCA_INLINE
void swap(unique_ptr& other) {
149 std::swap(
ptr, other.
ptr);
163 pointer tempPtr = other;
170 return *
static_cast<deleter_type*
>(
this);
177 template<
typename value_type,
typename deleter>
class unique_ptr<value_type[], deleter> :
public deleter {
179 using element_type = std::remove_extent_t<value_type>;
180 using pointer = element_type*;
181 using deleter_type = deleter;
182 using reference = element_type&;
184 DCA_INLINE unique_ptr& operator=(
const unique_ptr&) =
delete;
185 DCA_INLINE
unique_ptr(
const unique_ptr&) =
delete;
193 template<jsonifier::concepts::unique_ptr_t value_type_new> DCA_INLINE unique_ptr&
operator=(value_type_new&& other) {
194 if (
this !=
static_cast<void*
>(&other)) {
208 template<jsonifier::concepts::unique_ptr_t value_type_new> DCA_INLINE
unique_ptr(value_type_new&& other) {
209 *
this = std::move(other);
215 template<jsonifier::concepts::po
inter_t value_type_new> DCA_INLINE unique_ptr&
operator=(value_type_new newPtr) {
228 template<jsonifier::concepts::po
inter_t value_type_new> DCA_INLINE
unique_ptr(value_type_new newPtr) {
234 DCA_INLINE pointer
get()
const {
241 DCA_INLINE reference
operator[](std::ptrdiff_t index)
const {
247 DCA_INLINE
operator bool()
const {
248 return ptr !=
nullptr;
255 throw dca_exception{
"Sorry, but you attempted to access a unique_ptr that is nullptr." };
264 throw dca_exception{
"Sorry, but you attempted to access a unique_ptr that is nullptr." };
272 pointer releasedPtr =
ptr;
279 DCA_INLINE
void reset(pointer newPtr =
nullptr) {
280 pointer oldPtr = std::exchange(
ptr, newPtr);
288 DCA_INLINE
void swap(unique_ptr& other) {
289 std::swap(
ptr, other.
ptr);
303 pointer tempPtr = other;
310 return *
static_cast<deleter_type*
>(
this);
320 template<
typename value_type,
typename deleter = std::default_delete<value_type>,
typename... arg_types, std::enable_if_t<!std::is_array_v<value_type>,
int32_t> = 0>
330 template<
typename value_type,
typename deleter = std::default_delete<value_type>, std::enable_if_t<std::is_array_v<value_type> && std::extent_v<value_type> == 0,
int32_t> = 0>
332 using element_type = std::remove_extent_t<value_type>;
337 template<
typename value_type,
typename... arg_types, std::enable_if_t<std::extent_v<value_type> != 0, int32_t> = 0> DCA_INLINE
void makeUnique(arg_types&&...) =
delete;
A smart pointer class that provides unique ownership semantics.
DCA_INLINE unique_ptr & operator=(value_type_new newPtr)
Move assignment operator for raw pointer.
DCA_INLINE unique_ptr & operator=(value_type_new &&other)
Move assignment operator for related pointers.
DCA_INLINE pointer release()
Releases the managed pointer.
DCA_INLINE void reset(pointer newPtr=nullptr)
Resets the managed pointer and invokes the deleter.
DCA_INLINE unique_ptr(value_type_new &&other)
Move constructor for related pointers.
DCA_INLINE pointer operator->() const
Member access operator.
DCA_INLINE deleter_type & getDeleter()
Gets the deleter associated with the managed object.
DCA_INLINE pointer get() const
Gets the managed raw pointer.
DCA_INLINE unique_ptr()
Default constructor.
DCA_INLINE pointer get() const
Gets the managed raw pointer.
DCA_INLINE pointer release()
Releases the managed pointer.
DCA_INLINE reference operator*() const
Dereference operator.
DCA_INLINE pointer operator->() const
Member access operator.
DCA_INLINE unique_ptr()
Default constructor.
DCA_INLINE void commit(pointer other)
Commits a new pointer value and resets the current one.
DCA_INLINE ~unique_ptr()
Destructor that releases the managed object.
DCA_INLINE unique_ptr(value_type_new newPtr)
Constructor from a raw pointer.
DCA_INLINE unique_ptr & operator=(value_type_new &&other)
Move assignment operator for related pointers.
DCA_INLINE unique_ptr(value_type_new &&other)
Move constructor for related pointers.
DCA_INLINE reference operator*() const
Dereference operator.
DCA_INLINE void reset(pointer newPtr=nullptr)
Resets the managed pointer and invokes the deleter.
DCA_INLINE void swap(unique_ptr &other)
Swaps the contents of two unique_ptr objects.
DCA_INLINE unique_ptr & operator=(value_type_new newPtr)
Move assignment operator for raw pointer.
DCA_INLINE deleter_type & getDeleter()
Gets the deleter associated with the managed object.
pointer ptr
The stored object.
DCA_INLINE unique_ptr(value_type_new newPtr)
Constructor from a raw pointer.
DCA_INLINE void swap(unique_ptr &other)
Swaps the contents of two unique_ptr objects.
DCA_INLINE reference operator[](std::ptrdiff_t index) const
Square bracket operator for accessing elements of this array.
DCA_INLINE void commit(pointer other)
Commits a new pointer value and resets the current one.
DCA_INLINE ~unique_ptr()
Destructor that releases the managed object.
DCA_INLINE unique_ptr< value_type, deleter > makeUnique(arg_types &&... args)
Helper function to create a unique_ptr for a non-array object.
The main namespace for the forward-facing interfaces.