SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
ObjectPtr.hpp
Go to the documentation of this file.
1#ifndef _OBJECT_PTR_H_
2#define _OBJECT_PTR_H_
3
4#include "SmartPointer.hpp"
5#include <utility> // For std::move and std::forward
6#include <type_traits> // For std::enable_if_t and std::is_base_of_v
7
8namespace Sleak {
9
10 /// Move-only, exclusive-ownership smart pointer (unique_ptr equivalent).
11 /// Copying is disabled; transfer ownership with std::move.
12 /// @ingroup memory
13 template <typename T>
14 class ObjectPtr : public SmartPointer<T> {
15 public:
16 // Default constructor
17 ObjectPtr() noexcept : SmartPointer<T>(nullptr) {}
18
19 // Constructor that takes a raw pointer
20 explicit ObjectPtr(T* p) noexcept : SmartPointer<T>(p) {}
21
22 // Constructor that takes constructor arguments for T
23 template <typename... Args>
24 explicit ObjectPtr(Args&&... args) : SmartPointer<T>(new T(std::forward<Args>(args)...)) {}
25
26 // Move constructor
27 ObjectPtr(ObjectPtr&& other) noexcept : SmartPointer<T>(other.release()) {}
28
29 // Move assignment
30 ObjectPtr& operator=(ObjectPtr&& other) noexcept {
31 if (this != &other) {
32 reset(other.release());
33 }
34 return *this;
35 }
36
37 // Derived-to-base conversion (move)
38 template <typename U, typename = std::enable_if_t<std::is_base_of_v<T, U>>>
39 ObjectPtr(ObjectPtr<U>&& other) noexcept
40 : SmartPointer<T>(other.release()) {}
41
42 // Destructor
44
45 // Reset the pointer
46 void reset(T* newPtr = nullptr) override {
47 T* oldPtr = this->ptr;
48 this->ptr = newPtr;
49 if (oldPtr) {
50 delete oldPtr;
51 }
52 }
53
54 // Release ownership of the pointer
55 T* release() noexcept {
56 T* temp = this->ptr;
57 this->ptr = nullptr;
58 return temp;
59 }
60
61 // Comparison operators
62 bool operator==(const ObjectPtr& other) const noexcept { return this->ptr == other.ptr; }
63 bool operator!=(const ObjectPtr& other) const noexcept { return this->ptr != other.ptr; }
64 bool operator<(const ObjectPtr& other) const noexcept { return this->ptr < other.ptr; }
65 bool operator<=(const ObjectPtr& other) const noexcept { return this->ptr <= other.ptr; }
66 bool operator>(const ObjectPtr& other) const noexcept { return this->ptr > other.ptr; }
67 bool operator>=(const ObjectPtr& other) const noexcept { return this->ptr >= other.ptr; }
68
69 // Disable copy semantics
70 ObjectPtr(const ObjectPtr&) = delete;
71 ObjectPtr& operator=(const ObjectPtr&) = delete;
72 };
73
74} // namespace Sleak
75
76#endif // _OBJECT_PTR_H_
77
78
79/*
80#ifdef TEMP
81
82#include "SmartPointer.hpp"
83
84namespace Sleak {
85
86 template <typename T>
87 class ObjectPtr : public SmartPointer<T> {
88 public:
89 // Constructor that takes constructor arguments for T
90 template <typename... Args>
91 explicit ObjectPtr(Args&&... args) : SmartPointer<T>(new T(std::forward<Args>(args)...)) {}
92
93 // Constructor that takes a raw pointer (original behavior)
94 explicit ObjectPtr(T* p = nullptr) : SmartPointer<T>(p) {}
95
96 // Copy constructor
97 //ObjectPtr(ObjectPtr& other) noexcept : SmartPointer<T>(other.ptr) {}
98 ObjectPtr(const ObjectPtr&) = delete;
99
100 // Copy assignment
101 //ObjectPtr& operator=(ObjectPtr& other) noexcept {}
102 ObjectPtr& operator=(const ObjectPtr&) = delete;
103
104 // Move constructor
105 ObjectPtr(ObjectPtr&& other) noexcept : SmartPointer<T>(other.ptr) {
106 other.ptr = nullptr;
107 }
108
109 // Move assignment
110 ObjectPtr& operator=(ObjectPtr&& other) noexcept {
111 if (this != &other) {
112 this->reset();
113 this->ptr = other.ptr;
114 other.ptr = nullptr;
115 }
116 return *this;
117 }
118
119 // Derived-to-base conversion (move)
120 template <typename U, typename = std::enable_if_t<std::is_base_of_v<T, U>>>
121 ObjectPtr(ObjectPtr<U>&& other) noexcept
122 : SmartPointer<T>(other.release()) {}
123
124 // Destructor
125 ~ObjectPtr() { this->reset(); }
126
127 // Reset the pointer
128 void reset(T* newPtr = nullptr) override {
129 if (this->ptr != nullptr)
130 delete this->ptr;
131 this->ptr = newPtr;
132 }
133
134 // Release ownership of the pointer
135 T* release() {
136 T* temp = this->ptr;
137 this->ptr = nullptr;
138 return temp;
139 }
140 };
141
142} // namespace Sleak
143
144#endif // _OBJECT_PTR_H_
145
146*/
ObjectPtr(ObjectPtr &&other) noexcept
Definition ObjectPtr.hpp:27
ObjectPtr() noexcept
Definition ObjectPtr.hpp:17
bool operator>(const ObjectPtr &other) const noexcept
Definition ObjectPtr.hpp:66
ObjectPtr(const ObjectPtr &)=delete
bool operator<=(const ObjectPtr &other) const noexcept
Definition ObjectPtr.hpp:65
bool operator<(const ObjectPtr &other) const noexcept
Definition ObjectPtr.hpp:64
ObjectPtr & operator=(ObjectPtr &&other) noexcept
Definition ObjectPtr.hpp:30
ObjectPtr & operator=(const ObjectPtr &)=delete
ObjectPtr(ObjectPtr< U > &&other) noexcept
Definition ObjectPtr.hpp:39
ObjectPtr(Args &&... args)
Definition ObjectPtr.hpp:24
bool operator==(const ObjectPtr &other) const noexcept
Definition ObjectPtr.hpp:62
bool operator!=(const ObjectPtr &other) const noexcept
Definition ObjectPtr.hpp:63
void reset(T *newPtr=nullptr) override
Definition ObjectPtr.hpp:46
ObjectPtr(T *p) noexcept
Definition ObjectPtr.hpp:20
bool operator>=(const ObjectPtr &other) const noexcept
Definition ObjectPtr.hpp:67
T * release() noexcept
Definition ObjectPtr.hpp:55
SmartPointer(T *p=nullptr)
Root namespace for everything the engine exposes.
Definition Camera.hpp:10