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{
20 void operator()(void* ptr) const;
21};
22
32template<class T>
33using OwnPtr = std::unique_ptr<T, PtrDeleter>;
34
42template<class T>
44{
45 OwnPtr<T> m_ptr;
46 size_t m_size = 0;
47
48public:
50 constexpr OwnArray(std::nullptr_t = nullptr) {}
51
53 constexpr explicit OwnArray(T* ptr, size_t size)
54 : m_ptr(ptr)
55 , m_size(size)
56 {
57 }
58
60 constexpr explicit OwnArray(T* ptr)
61 : m_ptr(ptr)
62 {
63 if (ptr) {
64 auto endPtr = ptr;
65 while (*endPtr) ++endPtr;
66 m_size = endPtr - ptr;
67 }
68 }
69
71 constexpr explicit operator bool() const { return bool(m_ptr); }
72
74 constexpr bool empty() const { return !m_ptr; }
75
77 constexpr T* data() { return m_ptr.get(); }
78
80 constexpr const T* data() const { return m_ptr.get(); }
81
83 constexpr size_t size() const { return m_size; }
84
91 T* release() { return m_ptr.release(); }
92
99 void reset(T* newValue = nullptr)
100 {
101 m_ptr.reset(newValue);
102 if (newValue) {
103 auto endPtr = newValue;
104 while (*endPtr) ++endPtr;
105 m_size = endPtr - newValue;
106 }
107 }
108
115 void reset(T* newValue, size_t size)
116 {
117 m_ptr.reset(newValue);
118 m_size = size;
119 }
120
122 constexpr T& operator[](size_t i) { return m_ptr.get()[i]; }
123
125 constexpr const T& operator[](size_t i) const { return m_ptr.get()[i]; }
126
128 T* begin() { return data(); }
129
131 const T* begin() const { return data(); }
132
134 const T* cbegin() const { return begin(); }
135
137 T* end() { return begin() + size(); }
138
140 const T* end() const { return begin() + size(); }
141
143 const T* cend() const { return begin() + size(); }
144
146 T& front() { return *data(); }
147
149 T& back() { return begin()[size()]; }
150};
151
164template<class T>
165using RefArray = std::span<T>;
166
168
169} // namespace SDL
170#endif /* SDL3PP_OWN_PTR_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
constexpr T & operator[](size_t i)
Access index.
Definition: SDL3pp_ownPtr.h:122
void reset(T *newValue, size_t size)
Reset object.
Definition: SDL3pp_ownPtr.h:115
constexpr bool empty() const
True if size() == 0.
Definition: SDL3pp_ownPtr.h:74
T * begin()
Get iterator to first element.
Definition: SDL3pp_ownPtr.h:128
const T * cend() const
Get iterator to one past end element.
Definition: SDL3pp_ownPtr.h:143
T * release()
Release control on object.
Definition: SDL3pp_ownPtr.h:91
constexpr const T * data() const
Data.
Definition: SDL3pp_ownPtr.h:80
const T * begin() const
Get iterator to first element.
Definition: SDL3pp_ownPtr.h:131
constexpr OwnArray(std::nullptr_t=nullptr)
Constructor.
Definition: SDL3pp_ownPtr.h:50
constexpr OwnArray(T *ptr)
Constructor.
Definition: SDL3pp_ownPtr.h:60
T & front()
Return first element.
Definition: SDL3pp_ownPtr.h:146
const T * cbegin() const
Get iterator to first element.
Definition: SDL3pp_ownPtr.h:134
T * end()
Get iterator to one past end element.
Definition: SDL3pp_ownPtr.h:137
constexpr const T & operator[](size_t i) const
Access index.
Definition: SDL3pp_ownPtr.h:125
void reset(T *newValue=nullptr)
Reset object.
Definition: SDL3pp_ownPtr.h:99
constexpr size_t size() const
Size.
Definition: SDL3pp_ownPtr.h:83
const T * end() const
Get iterator to one past end element.
Definition: SDL3pp_ownPtr.h:140
T & back()
Return last element.
Definition: SDL3pp_ownPtr.h:149
constexpr T * data()
Data.
Definition: SDL3pp_ownPtr.h:77
constexpr OwnArray(T *ptr, size_t size)
Constructor.
Definition: SDL3pp_ownPtr.h:53
std::span< T > RefArray
Handle to an owned SDL memory allocated array.
Definition: SDL3pp_ownPtr.h:165
std::unique_ptr< T, PtrDeleter > OwnPtr
Handle to an owned SDL memory allocated pointer.
Definition: SDL3pp_ownPtr.h:33
Main include header for the SDL3pp library.
Calls SDL_free()
Definition: SDL3pp_ownPtr.h:18