FastLED 3.9.15
Loading...
Searching...
No Matches
scoped_ptr.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <stddef.h>
5#include <stdint.h>
6
7#include "fl/namespace.h"
8// #include "fl/warn.h"
9
10namespace fl {
11
12template <typename T> struct ArrayDeleter {
13 ArrayDeleter() = default;
14 void operator()(T *ptr) { delete[] ptr; }
15};
16
17template <typename T> struct PointerDeleter {
18 void operator()(T *ptr) { delete ptr; }
19};
20
21template <typename T, typename Deleter = PointerDeleter<T>> class scoped_ptr {
22 public:
23 // Constructor
24 explicit scoped_ptr(T *ptr = nullptr, Deleter deleter = Deleter())
25 : ptr_(ptr), deleter_(deleter) {}
26
27 // Destructor
29
30 // Disable copy semantics (no copying allowed)
31 scoped_ptr(const scoped_ptr &) = delete;
32 scoped_ptr &operator=(const scoped_ptr &) = delete;
33
34 // Move constructor
35 scoped_ptr(scoped_ptr &&other) noexcept
36 : ptr_(other.ptr_), deleter_(other.deleter_) {
37 other.ptr_ = nullptr;
38 other.deleter_ = {};
39 }
40
41 // // Move assignment operator
42 // scoped_ptr &operator=(scoped_ptr &&other) noexcept {
43 // if (this != &other) {
44 // reset(other.ptr_);
45 // deleter_ = other.deleter_;
46 // other.ptr_ = nullptr;
47 // other.deleter_ = {};
48 // }
49 // return *this;
50 // }
51
52 // Access the managed object
53 T *operator->() const { return ptr_; }
54
55 // Dereference the managed object
56 T &operator*() const { return *ptr_; }
57
58 // Get the raw pointer
59 T *get() const { return ptr_; }
60
61 // Boolean conversion operator
62 explicit operator bool() const noexcept { return ptr_ != nullptr; }
63
64 // Logical NOT operator
65 bool operator!() const noexcept { return ptr_ == nullptr; }
66
67 // Release the managed object and reset the pointer
68 void reset(T *ptr = nullptr) {
69 if (ptr_ != ptr) {
71 ptr_ = ptr;
72 }
73 }
74
75 T *release() {
76 T *tmp = ptr_;
77 ptr_ = nullptr;
78 return tmp;
79 }
80
81 void swap(scoped_ptr &other) noexcept {
82 T *tmp = ptr_;
83 ptr_ = other.ptr_;
84 other.ptr_ = 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
100
101 // Disable copy semantics (no copying allowed)
102 scoped_array(const scoped_array &) = delete;
104
105 // Move constructor
106 scoped_array(scoped_array &&other) noexcept
107 : arr_(other.arr_), deleter_(other.deleter_) {
108 other.arr_ = nullptr;
109 other.deleter_ = {};
110 }
111
112 // Move assignment operator
114 if (this != &other) {
115 reset(other.arr_);
116 other.arr_ = nullptr;
117 }
118 return *this;
119 }
120
121 // Array subscript operator
122 T &operator[](size_t i) const { return arr_[i]; }
123
124 // Get the raw pointer
125 T *get() const { return arr_; }
126
127 // Boolean conversion operator
128 explicit operator bool() const noexcept { return arr_ != nullptr; }
129
130 // Logical NOT operator
131 bool operator!() const noexcept { return arr_ == nullptr; }
132
133 // Release the managed array and reset the pointer
134 void reset(T *arr = nullptr) {
135 if (arr_ == arr) {
136 return;
137 }
138 deleter_(arr_);
139 arr_ = arr;
140 }
141
142 void clear() { reset(); }
143
144 T *release() {
145 T *tmp = arr_;
146 arr_ = nullptr;
147 return tmp;
148 }
149
150 void swap(scoped_array &other) noexcept {
151 T *tmp = arr_;
152 arr_ = other.arr_;
153 other.arr_ = tmp;
154 }
155
156 private:
157 T *arr_; // Managed array pointer
158 Deleter deleter_ = {};
159};
160
161} // namespace fl
void reset(T *arr=nullptr)
Definition scoped_ptr.h:134
scoped_array & operator=(const scoped_array &)=delete
scoped_array(scoped_array &&other) noexcept
Definition scoped_ptr.h:106
void swap(scoped_array &other) noexcept
Definition scoped_ptr.h:150
bool operator!() const noexcept
Definition scoped_ptr.h:131
T & operator[](size_t i) const
Definition scoped_ptr.h:122
T * get() const
Definition scoped_ptr.h:125
scoped_array(const scoped_array &)=delete
scoped_array(T *arr, Deleter deleter)
Definition scoped_ptr.h:96
scoped_array & operator=(scoped_array &&other) noexcept
Definition scoped_ptr.h:113
scoped_array(T *arr=nullptr)
Definition scoped_ptr.h:95
T & operator*() const
Definition scoped_ptr.h:56
void swap(scoped_ptr &other) noexcept
Definition scoped_ptr.h:81
bool operator!() const noexcept
Definition scoped_ptr.h:65
scoped_ptr(scoped_ptr &&other) noexcept
Definition scoped_ptr.h:35
T * operator->() const
Definition scoped_ptr.h:53
scoped_ptr(T *ptr=nullptr, Deleter deleter=Deleter())
Definition scoped_ptr.h:24
scoped_ptr & operator=(const scoped_ptr &)=delete
T * get() const
Definition scoped_ptr.h:59
void reset(T *ptr=nullptr)
Definition scoped_ptr.h:68
Deleter deleter_
Definition scoped_ptr.h:89
scoped_ptr(const scoped_ptr &)=delete
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
ArrayDeleter()=default
void operator()(T *ptr)
Definition scoped_ptr.h:14
void operator()(T *ptr)
Definition scoped_ptr.h:18