SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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>
5
#include <
Utility/Exception.hpp
>
6
#include <iostream>
7
8
namespace
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>
24
class
SmartPointer
{
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
;
60
SmartPointer
&
operator=
(
const
SmartPointer
&) =
delete
;
61
};
62
63
}
// namespace Sleak
64
65
#endif
// _SMART_POINTER_H_
Exception.hpp
Sleak::NullPointerException
Definition
Exception.hpp:54
Sleak::ObjectPtr
Definition
ObjectPtr.hpp:14
Sleak::RefPtr
Definition
RefPtr.hpp:62
Sleak::SmartPointer::ptr
T * ptr
Definition
SmartPointer.hpp:26
Sleak::SmartPointer::IsValid
virtual bool IsValid() const
True if the pointer is non-null.
Definition
SmartPointer.hpp:48
Sleak::SmartPointer::SmartPointer
SmartPointer(const SmartPointer &)=delete
Sleak::SmartPointer::operator->
T * operator->() const
Definition
SmartPointer.hpp:37
Sleak::SmartPointer::~SmartPointer
virtual ~SmartPointer()
Definition
SmartPointer.hpp:33
Sleak::SmartPointer::SmartPointer
SmartPointer(T *p=nullptr)
Definition
SmartPointer.hpp:29
Sleak::SmartPointer::operator*
T & operator*() const
Definition
SmartPointer.hpp:36
Sleak::SmartPointer::get
T * get() const
Raw pointer access; throws NullPointerException if unset.
Definition
SmartPointer.hpp:39
Sleak::SmartPointer::operator=
SmartPointer & operator=(const SmartPointer &)=delete
Sleak::SmartPointer::reset
virtual void reset(T *newPtr=nullptr)=0
Sleak::WeakPtr
Definition
WeakPtr.hpp:12
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Memory
SmartPointer.hpp
Generated on
for SleakEngine by
1.18.0