SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_ownPtr.h
1#ifndef SDL3PP_OWN_PTR_H_
2#define SDL3PP_OWN_PTR_H_
3
4#include <memory>
5#include <span>
6
7namespace SDL {
8
18{
19 void operator()(void* ptr) const;
20};
21
31template<class T>
32using OwnPtr = std::unique_ptr<T, PtrDeleter>;
33
41template<class T>
43{
44 OwnPtr<T> m_ptr;
45 size_t m_size = 0;
46
47public:
48 constexpr OwnArray(std::nullptr_t = nullptr) {}
49
50 constexpr explicit OwnArray(T* ptr, size_t size)
51 : m_ptr(ptr)
52 , m_size(size)
53 {
54 }
55
56 constexpr explicit OwnArray(T* ptr)
57 : m_ptr(ptr)
58 {
59 if (ptr) {
60 auto endPtr = ptr;
61 while (*endPtr) ++endPtr;
62 m_size = endPtr - ptr;
63 }
64 }
65
67 constexpr explicit operator bool() const { return bool(m_ptr); }
68
70 constexpr bool empty() const { return !m_ptr; }
71
73 constexpr T* data() { return m_ptr.get(); }
74
76 constexpr const T* data() const { return m_ptr.get(); }
77
79 constexpr size_t size() const { return m_size; }
80
87 T* release() { return m_ptr.release(); }
88
95 void reset(T* newValue = nullptr)
96 {
97 m_ptr.reset(newValue);
98 if (newValue) {
99 auto endPtr = newValue;
100 while (*endPtr) ++endPtr;
101 m_size = endPtr - newValue;
102 }
103 }
104
111 void reset(T* newValue, size_t size)
112 {
113 m_ptr.reset(newValue);
114 m_size = size;
115 }
116
118 constexpr T& operator[](size_t i) { return m_ptr.get()[i]; }
119
121 constexpr const T& operator[](size_t i) const { return m_ptr.get()[i]; }
122
128 T* begin() { return data(); }
129 const T* begin() const { return data(); }
130 const T* cbegin() const { return begin(); }
132
138 T* end() { return begin() + size(); }
139 const T* end() const { return begin() + size(); }
140 const T* cend() const { return begin() + size(); }
142
144 T& front() { return *data(); }
145
147 T& back() { return begin()[size()]; }
148};
149
162template<class T>
163using RefArray = std::span<T>;
164
166
167} // namespace SDL
168#endif /* SDL3PP_OWN_PTR_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
constexpr T & operator[](size_t i)
Access index.
Definition SDL3pp_ownPtr.h:118
void reset(T *newValue, size_t size)
Reset object.
Definition SDL3pp_ownPtr.h:111
constexpr bool empty() const
True if size() == 0.
Definition SDL3pp_ownPtr.h:70
T * begin()
Get iterator to first element.
Definition SDL3pp_ownPtr.h:128
T * release()
Release control on object.
Definition SDL3pp_ownPtr.h:87
constexpr const T * data() const
Data.
Definition SDL3pp_ownPtr.h:76
T & front()
Return first element.
Definition SDL3pp_ownPtr.h:144
T * end()
Get iterator to one past end element.
Definition SDL3pp_ownPtr.h:138
constexpr const T & operator[](size_t i) const
Access index.
Definition SDL3pp_ownPtr.h:121
void reset(T *newValue=nullptr)
Reset object.
Definition SDL3pp_ownPtr.h:95
constexpr size_t size() const
Size.
Definition SDL3pp_ownPtr.h:79
T & back()
Return last element.
Definition SDL3pp_ownPtr.h:147
constexpr T * data()
Data.
Definition SDL3pp_ownPtr.h:73
std::span< T > RefArray
Handle to an owned SDL memory allocated array.
Definition SDL3pp_ownPtr.h:163
std::unique_ptr< T, PtrDeleter > OwnPtr
Handle to an owned SDL memory allocated pointer.
Definition SDL3pp_ownPtr.h:32
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Calls SDL_free()
Definition SDL3pp_ownPtr.h:18