The Pedigree Project
0.1
src
system
include
pedigree
kernel
utilities
Pointers.h
1
/*
2
* Copyright (c) 2008-2014, Pedigree Developers
3
*
4
* Please see the CONTRIB file in the root of the source tree for a full
5
* list of contributors.
6
*
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
10
*
11
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
*/
19
20
#ifndef POINTERS_H
21
#define POINTERS_H
22
23
#include "pedigree/kernel/Log.h"
24
#include "pedigree/kernel/processor/types.h"
25
26
template
<
class
T>
27
class
UniqueCommon
{
28
public
:
29
UniqueCommon
() : m_Pointer(
nullptr
) {}
30
31
virtual
~UniqueCommon
() {
32
reset();
33
}
34
35
T* operator*()
const
{
36
return
get();
37
}
38
39
operator
void
*()
const
{
40
return
get();
41
}
42
43
T* get()
const
{
44
return
m_Pointer;
45
}
46
47
void
reset()
noexcept
{
48
if
(m_Pointer) {
49
destroy();
50
m_Pointer = 0;
51
}
52
}
53
54
NOT_COPYABLE_OR_ASSIGNABLE(
UniqueCommon
);
55
56
protected
:
57
UniqueCommon
(T* p) : m_Pointer(p) {}
58
59
virtual
void
destroy()
noexcept
{
60
delete
m_Pointer;
61
}
62
63
void
setPointer(T* p)
noexcept
{
64
m_Pointer = p;
65
}
66
68
void
release
() noexcept {
69
m_Pointer =
nullptr
;
70
}
71
72
T* m_Pointer;
73
};
74
78
template
<
class
T>
79
class
UniquePointer
:
public
UniqueCommon
<T> {
80
public
:
81
UniquePointer
() =
default
;
82
83
virtual
~UniquePointer
() {
84
this->reset();
85
}
86
87
// move constructor
88
UniquePointer
(
UniquePointer<T>
&& p)
noexcept
{
89
move_from(p);
90
}
91
92
// no copy construction permitted
93
NOT_COPYABLE_OR_ASSIGNABLE(
UniquePointer
);
94
95
UniquePointer<T>
& operator=(
UniquePointer<T>
&& p)
noexcept
{
96
move_from(p);
97
return
*
this
;
98
}
99
101
static
UniquePointer<T>
adopt
(T* pointer) {
102
return
UniquePointer<T>
(pointer);
103
}
104
106
T*
releaseOwnership
() noexcept {
107
T* pointer = this->get();
108
this->
release
();
109
return
pointer;
110
}
111
112
template
<
class
... Args>
113
static
UniquePointer<T>
allocate(Args&&... args) {
114
return
UniquePointer<T>
(
new
T(args...));
115
}
116
117
private
:
118
UniquePointer
(T* p) :
UniqueCommon
<T>(p) {}
119
120
void
move_from(
UniquePointer<T>
& p)
noexcept
{
121
T* ptr = p.get();
122
p.release();
123
124
this->reset();
125
this->setPointer(ptr);
126
}
127
};
128
130
template
<
class
T>
131
class
UniqueArray
:
public
UniqueCommon
<T> {
132
public
:
133
UniqueArray
() =
default
;
134
135
virtual
~UniqueArray
() {
136
this->reset();
137
}
138
139
// move constructor
140
UniqueArray
(
UniqueArray<T>
&& p)
noexcept
{
141
move_from(p);
142
}
143
144
// no copy construction permitted
145
NOT_COPYABLE_OR_ASSIGNABLE(
UniqueArray
);
146
147
UniqueArray<T>
& operator=(
UniqueArray<T>
&& p)
noexcept
{
148
move_from(p);
149
return
*
this
;
150
}
151
152
static
UniqueArray<T>
allocate(
size_t
count) {
153
return
UniqueArray<T>
(
new
T[count]);
154
}
155
156
protected
:
157
virtual
void
destroy()
noexcept
override
{
158
T* ptr = this->get();
159
delete
[] ptr;
160
}
161
162
private
:
163
UniqueArray
(T* p) :
UniqueCommon<T>
(p) {}
164
165
void
move_from(
UniqueArray<T>
& p)
noexcept
{
166
T* ptr = p.get();
167
p.release();
168
169
this->reset();
170
this->setPointer(ptr);
171
}
172
};
173
174
#endif
UniqueArray
Definition
Pointers.h:131
UniqueCommon
Definition
Pointers.h:27
UniqueCommon::release
void release() noexcept
Definition
Pointers.h:68
UniquePointer
Definition
Pointers.h:79
UniquePointer::releaseOwnership
T * releaseOwnership() noexcept
Definition
Pointers.h:106
UniquePointer::adopt
static UniquePointer< T > adopt(T *pointer)
Definition
Pointers.h:101
Generated on Tue Sep 29 2026 04:30:12 for The Pedigree Project by
1.9.8