DirectZ
Loading...
Searching...
No Matches
size_ptr.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstddef>
8#include <cstdlib>
9#include <functional>
10
11namespace dz
12{
17 {
18 static void call(void* p) {}
19 };
20
25 {
26 template <typename T>
27 static void call(void* p) { delete static_cast<T*>(p); }
28 };
29
34 {
35 template <typename T>
36 static void call(void* p) { delete[] static_cast<T*>(p); }
37 };
38
43 {
44 static void call(void* p) { free(p); }
45 };
46
52 template <typename T>
53 struct size_ptr
54 {
55 T* ptr = nullptr;
56 size_t* size = nullptr;
57 size_t* ref_c = nullptr;
58 void(*deleter)(void*) = nullptr;
59
63 size_ptr() = default;
64
72 size_ptr(T* ptr, size_t size = 1, void(*deleter)(void*) = &default_delete_single::call<T>) :
73 ptr(ptr),
74 size(new size_t(size)),
75 ref_c(new size_t(1)),
77 {
78 }
79
85 size_ptr(const size_ptr& other) :
86 ptr(other.ptr),
87 size(other.size),
88 ref_c(other.ref_c),
89 deleter(other.deleter)
90 {
91 (*ref_c)++;
92 }
93
101 {
102 if (this != &other)
103 {
104 reset();
105 ptr = other.ptr;
106 size = other.size;
107 ref_c = other.ref_c;
108 deleter = other.deleter;
109 (*ref_c)++;
110 }
111 return *this;
112 }
113
118 {
119 reset();
120 }
121
125 void reset()
126 {
127 if (ref_c)
128 {
129 (*ref_c)--;
130 if (!(*ref_c))
131 {
132 if (deleter) deleter(static_cast<void*>(ptr));
133 delete size;
134 delete ref_c;
135 }
136 }
137 ptr = nullptr;
138 size = nullptr;
139 ref_c = nullptr;
140 deleter = nullptr;
141 }
142
148 T* operator->() { return ptr; }
149
155 T& operator*() { return *ptr; }
156
162 const T* get() const { return ptr; }
163
169 size_t get_size() const { return size ? *size : 0; }
170 };
171} // namespace dz
Definition DirectZ.hpp:39
@ T
Definition D7Stream.hpp:19
@ p
Definition Window.hpp:177
Default deleter for arrays.
Definition size_ptr.hpp:34
static void call(void *p)
Definition size_ptr.hpp:36
Default deleter for single heap-allocated objects.
Definition size_ptr.hpp:25
static void call(void *p)
Definition size_ptr.hpp:27
Default deleter using free().
Definition size_ptr.hpp:43
static void call(void *p)
Definition size_ptr.hpp:44
Default no-op deleter.
Definition size_ptr.hpp:17
static void call(void *p)
Definition size_ptr.hpp:18
size_t get_size() const
Gets the number of elements pointed to.
Definition size_ptr.hpp:169
T & operator*()
Dereference operator.
Definition size_ptr.hpp:155
T * operator->()
Pointer access operator.
Definition size_ptr.hpp:148
size_ptr & operator=(const size_ptr &other)
Copy assignment operator.
Definition size_ptr.hpp:100
const T * get() const
Gets the raw pointer.
Definition size_ptr.hpp:162
size_t * ref_c
Definition size_ptr.hpp:57
size_t * size
Definition size_ptr.hpp:56
void reset()
Reset the smart pointer, decrementing the reference count and deleting resources if needed.
Definition size_ptr.hpp:125
size_ptr()=default
Default constructor.
size_ptr(const size_ptr &other)
Copy constructor (increments reference count).
Definition size_ptr.hpp:85
~size_ptr()
Destructor. Decrements reference count and frees if zero.
Definition size_ptr.hpp:117
size_ptr(T *ptr, size_t size=1, void(*deleter)(void *)=&default_delete_single::call< T >)
Construct with data pointer, size, and deleter.
Definition size_ptr.hpp:72
char * ptr
Definition size_ptr.hpp:55
void(* deleter)(void *)
Definition size_ptr.hpp:58