SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
SmartPointer.hpp
Go to the documentation of this file.
1#ifndef _SMART_POINTER_H_
2#define _SMART_POINTER_H_
3
4#include <cstddef>
6#include <iostream>
7
8namespace Sleak {
9
10 // Forward declaration of derived classes
11 template <typename T>
12 class ObjectPtr;
13
14 template <typename T>
15 class RefPtr;
16
17 template <typename T>
18 class WeakPtr;
19
20 /// Common base for RefPtr/ObjectPtr/WeakPtr: holds the raw pointer and the
21 /// dereference/validity operators shared by every ownership model.
22 /// @ingroup memory
23 template <typename T>
25 protected:
26 T* ptr; // Raw pointer to the managed object
27
28 // Protected constructor for derived classes
29 explicit SmartPointer(T* p = nullptr) : ptr(p) {}
30
31 public:
32 // Virtual destructor for proper cleanup
33 virtual ~SmartPointer() {}
34
35 // Dereference operators
36 T& operator*() const { return *ptr; }
37 T* operator->() const { return ptr; }
38 /// Raw pointer access; throws NullPointerException if unset.
39 T* get() const
40 {
41 if(ptr == nullptr)
42 throw Sleak::NullPointerException("Requested object is nullptr!");
43
44 return ptr;
45 }
46
47 /// True if the pointer is non-null.
48 virtual bool IsValid() const {
49 return ptr != nullptr;
50 }
51
52 // Reset the pointer (to be overridden by derived classes)
53 virtual void reset(T* newPtr = nullptr) = 0;
54
55 // Check if the pointer is valid
56 explicit operator bool() const { return IsValid(); }
57
58 // Disallow copying (to be overridden by derived classes)
59 SmartPointer(const SmartPointer&) = delete;
61 };
62
63} // namespace Sleak
64
65#endif // _SMART_POINTER_H_
virtual bool IsValid() const
True if the pointer is non-null.
SmartPointer(const SmartPointer &)=delete
SmartPointer(T *p=nullptr)
T * get() const
Raw pointer access; throws NullPointerException if unset.
SmartPointer & operator=(const SmartPointer &)=delete
virtual void reset(T *newPtr=nullptr)=0
Root namespace for everything the engine exposes.
Definition Camera.hpp:10