FastLED 3.9.15
Loading...
Searching...
No Matches
scoped_array.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/allocator.h"
4#include "fl/inplacenew.h"
5#include "fl/namespace.h"
6#include "fl/deprecated.h"
7#include "fl/cstddef.h"
8
9namespace fl {
10
11// Keep existing ArrayDeleter for compatibility with scoped_array
12template <typename T> struct ArrayDeleter {
13 ArrayDeleter() = default;
14 void operator()(T *ptr) { delete[] ptr; }
15};
16
17// Keep existing PointerDeleter for compatibility (though default_delete is preferred)
18template <typename T> struct PointerDeleter {
19 void operator()(T *ptr) { delete ptr; }
20};
21
22// Keep existing scoped_array class unchanged as they serve different purposes than unique_ptr
23template <typename T, typename Deleter = ArrayDeleter<T>> class scoped_array {
24 public:
25 FASTLED_DEPRECATED_CLASS("Use fl::vector<T, fl::allocator_psram<T>> instead");
26 // Constructor
27 explicit scoped_array(T *arr = nullptr) : arr_(arr) {}
28 scoped_array(T *arr, Deleter deleter) : arr_(arr), deleter_(deleter) {}
29
30 // Destructor
32
33 // Disable copy semantics (no copying allowed)
34 scoped_array(const scoped_array &) = delete;
36
37 // Move constructor
38 scoped_array(scoped_array &&other) noexcept
39 : arr_(other.arr_), deleter_(other.deleter_) {
40 other.arr_ = nullptr;
41 other.deleter_ = {};
42 }
43
44 // Move assignment operator
45 scoped_array &operator=(scoped_array &&other) noexcept {
46 if (this != &other) {
47 reset(other.arr_);
48 other.arr_ = nullptr;
49 }
50 return *this;
51 }
52
53 // Array subscript operator
54 T &operator[](fl::size_t i) const { return arr_[i]; }
55
56 // Get the raw pointer
57 T *get() const { return arr_; }
58
59 // Boolean conversion operator
60 explicit operator bool() const noexcept { return arr_ != nullptr; }
61
62 // Logical NOT operator
63 bool operator!() const noexcept { return arr_ == nullptr; }
64
65 // Release the managed array and reset the pointer
66 void reset(T *arr = nullptr) {
67 if (arr_ == arr) {
68 return;
69 }
71 arr_ = arr;
72 }
73
74 void clear() { reset(); }
75
76 T *release() {
77 T *tmp = arr_;
78 arr_ = nullptr;
79 return tmp;
80 }
81
82 void swap(scoped_array &other) noexcept {
83 T *tmp = arr_;
84 arr_ = other.arr_;
85 other.arr_ = tmp;
86 }
87
88 private:
89 T *arr_; // Managed array pointer
90 Deleter deleter_ = {};
91};
92
93// A variant of scoped_ptr where allocation is done completly via a fl::allocator.
94template <typename T, typename Alloc = fl::allocator<T>> class scoped_array2 {
95 public:
96 FASTLED_DEPRECATED_CLASS("Use fl::vector<T, fl::allocator_psram<T>> instead");
97 Alloc mAlloc; // Allocator instance to manage memory allocation
98 // Constructor
100 : arr_(nullptr), size_(size) {
101 if (size > 0) {
102 arr_ = mAlloc.allocate(size);
103 // Default initialize each element
104 for (fl::size_t i = 0; i < size; ++i) {
105 mAlloc.construct(&arr_[i]);
106 }
107 }
108 }
109
110 // Destructor
112 if (arr_) {
113 // Call destructor on each element
114 for (fl::size_t i = 0; i < size_; ++i) {
115 mAlloc.destroy(&arr_[i]);
116 }
117 mAlloc.deallocate(arr_, size_);
118 }
119 }
120
121 // Disable copy semantics (no copying allowed)
122 scoped_array2(const scoped_array2 &) = delete;
124
125 // Move constructor
126 scoped_array2(scoped_array2 &&other) noexcept
127 : arr_(other.arr_), size_(other.size_) {
128 other.arr_ = nullptr;
129 other.size_ = 0;
130 }
131
132 // Move assignment operator
134 if (this != &other) {
135 reset();
136 arr_ = other.arr_;
137 size_ = other.size_;
138 other.arr_ = nullptr;
139 other.size_ = 0;
140 }
141 return *this;
142 }
143
144 // Array subscript operator
145 T &operator[](fl::size_t i) const { return arr_[i]; }
146
147 // Get the raw pointer
148 T *get() const { return arr_; }
149
150 // Get the size of the array
151 fl::size_t size() const { return size_; }
152
153 // Boolean conversion operator
154 explicit operator bool() const noexcept { return arr_ != nullptr; }
155
156 // Logical NOT operator
157 bool operator!() const noexcept { return arr_ == nullptr; }
158
159 // Release the managed array and reset the pointer
160 void reset(fl::size_t new_size = 0) {
161 if (arr_) {
162 // Call destructor on each element
163 for (fl::size_t i = 0; i < size_; ++i) {
164 // arr_[i].~T();
165 mAlloc.destroy(&arr_[i]);
166 }
167 // ::operator delete(arr_);
168 mAlloc.deallocate(arr_, size_);
169 arr_ = nullptr;
170 }
171
172 size_ = new_size;
173 if (new_size > 0) {
174 // arr_ = static_cast<T*>(::operator new(new_size * sizeof(T)));
175 arr_ = mAlloc.allocate(new_size);
176 // Default initialize each element
177 for (fl::size_t i = 0; i < new_size; ++i) {
178 // new (&arr_[i]) T();
179 mAlloc.construct(&arr_[i]);
180 }
181 }
182 }
183
184 // Release ownership of the array
185 T *release() {
186 T *tmp = arr_;
187 arr_ = nullptr;
188 size_ = 0;
189 return tmp;
190 }
191
192 void swap(scoped_array2 &other) noexcept {
193 T *tmp_arr = arr_;
194 fl::size_t tmp_size = size_;
195
196 arr_ = other.arr_;
197 size_ = other.size_;
198
199 other.arr_ = tmp_arr;
200 other.size_ = tmp_size;
201 }
202
203 private:
204 T *arr_ = nullptr; // Managed array pointer
205 fl::size_t size_ = 0; // Size of the array
206};
207
208} // namespace fl
bool operator!() const noexcept
scoped_array2(fl::size_t size=0)
fl::size_t size() const
scoped_array2(const scoped_array2 &)=delete
void reset(fl::size_t new_size=0)
scoped_array2 & operator=(const scoped_array2 &)=delete
FASTLED_DEPRECATED_CLASS("Use fl::vector<T, fl::allocator_psram<T>> instead")
void swap(scoped_array2 &other) noexcept
scoped_array2(scoped_array2 &&other) noexcept
T & operator[](fl::size_t i) const
scoped_array2 & operator=(scoped_array2 &&other) noexcept
void reset(T *arr=nullptr)
scoped_array & operator=(const scoped_array &)=delete
T & operator[](fl::size_t i) const
scoped_array(scoped_array &&other) noexcept
void swap(scoped_array &other) noexcept
bool operator!() const noexcept
T * get() const
FASTLED_DEPRECATED_CLASS("Use fl::vector<T, fl::allocator_psram<T>> instead")
scoped_array(const scoped_array &)=delete
scoped_array(T *arr, Deleter deleter)
scoped_array & operator=(scoped_array &&other) noexcept
scoped_array(T *arr=nullptr)
Implements the FastLED namespace macros.
__SIZE_TYPE__ size_t
Definition cstddef.h:17
IMPORTANT!
Definition crgb.h:20
ArrayDeleter()=default
void operator()(T *ptr)
void operator()(T *ptr)