FastLED 3.9.3
Loading...
Searching...
No Matches
scoped_ptr.h
1
2#pragma once
3
4
5#include <stddef.h>
6
7#include "namespace.h"
8
9FASTLED_NAMESPACE_BEGIN
10
11
12template<typename T>
14 void operator()(T* ptr) {
15 delete[] ptr;
16 }
17};
18
19template<typename T>
21 void operator()(T* ptr) {
22 delete ptr;
23 }
24};
25
26template <typename T, typename Deleter = PointerDeleter<T>>
28 public:
29 // Constructor
30 explicit scoped_ptr(T *ptr = nullptr, Deleter deleter = Deleter())
31 : ptr_(ptr), deleter_(deleter) {}
32
33 // Destructor
34 ~scoped_ptr() { deleter_(ptr_); }
35
36 // Disable copy semantics (no copying allowed)
37 scoped_ptr(const scoped_ptr &) = delete;
38 scoped_ptr &operator=(const scoped_ptr &) = delete;
39
40 // Move constructor
41 scoped_ptr(scoped_ptr &&other) noexcept
42 : ptr_(other.ptr_), deleter_(other.deleter_) {
43 other.ptr_ = nullptr;
44 other.deleter_ = {};
45 }
46
47 // Move assignment operator
48 scoped_ptr &operator=(scoped_ptr &&other) noexcept {
49 if (this != &other) {
50 reset(other.ptr_);
51 deleter_ = other.deleter_;
52 other.ptr_ = nullptr;
53 other.deleter_ = {};
54 }
55 return *this;
56 }
57
58 // Access the managed object
59 T *operator->() const { return ptr_; }
60
61 // Dereference the managed object
62 T &operator*() const { return *ptr_; }
63
64 // Get the raw pointer
65 T *get() const { return ptr_; }
66
67 // Boolean conversion operator
68 explicit operator bool() const noexcept { return ptr_ != nullptr; }
69
70 // Logical NOT operator
71 bool operator!() const noexcept { return ptr_ == nullptr; }
72
73 // Release the managed object and reset the pointer
74 void reset(T *ptr = nullptr) {
75 if (ptr_ != ptr) {
76 deleter_(ptr_);
77 ptr_ = ptr;
78 }
79 }
80
81 T* release() {
82 T* tmp = ptr_;
83 ptr_ = nullptr;
84 return tmp;
85 }
86
87 private:
88 T *ptr_; // Managed pointer
89 Deleter deleter_; // Custom deleter
90};
91
92template <typename T, typename Deleter=ArrayDeleter<T>> class scoped_array {
93 public:
94 // Constructor
95 explicit scoped_array(T *arr = nullptr) : arr_(arr) {}
96 scoped_array(T *arr, Deleter deleter) : arr_(arr), deleter_(deleter) {}
97
98 // Destructor
99 ~scoped_array() { deleter_(arr_); }
100
101 // Disable copy semantics (no copying allowed)
102 scoped_array(const scoped_array &) = delete;
103 scoped_array &operator=(const scoped_array &) = delete;
104
105 // Move constructor
106 scoped_array(scoped_array &&other) noexcept : arr_(other.arr_), deleter_(other.deleter_) {
107 other.arr_ = nullptr;
108 other.deleter_ = {};
109 }
110
111 // Move assignment operator
112 scoped_array &operator=(scoped_array &&other) noexcept {
113 if (this != &other) {
114 reset(other.arr_);
115 other.arr_ = nullptr;
116 }
117 return *this;
118 }
119
120 // Array subscript operator
121 T &operator[](size_t i) const { return arr_[i]; }
122
123 // Get the raw pointer
124 T *get() const { return arr_; }
125
126 // Boolean conversion operator
127 explicit operator bool() const noexcept { return arr_ != nullptr; }
128
129 // Logical NOT operator
130 bool operator!() const noexcept { return arr_ == nullptr; }
131
132 // Release the managed array and reset the pointer
133 void reset(T *arr = nullptr) {
134 if (arr_ == arr) {
135 return;
136 }
137 deleter_(arr_);
138 arr_ = arr;
139 }
140
141 T* release() {
142 T* tmp = arr_;
143 arr_ = nullptr;
144 return tmp;
145 }
146
147 private:
148 T *arr_; // Managed array pointer
149 Deleter deleter_ = {};
150};
151
152
153FASTLED_NAMESPACE_END