blob: d9b57c38dbf319dd915daaa8160ad1d403cb6f66 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#pragma once
#include "shared_ptr.hpp"
#include "common/memory.hpp"
#include <stdlib.h>
namespace common
{
template <class Target>
SharedPtr<Target>::SharedPtr() noexcept
: _reference_cnt(malloc_s<unsigned int>(sizeof(unsigned int)))
{
(*_reference_cnt) = 0;
}
template <class Target>
SharedPtr<Target>::SharedPtr(nullptr_t) noexcept
: _reference_cnt(malloc_s<unsigned int>(sizeof(unsigned int)))
{
(*_reference_cnt) = 0;
}
template <class Target>
SharedPtr<Target>::SharedPtr(Target *target) noexcept
: _target(target), _reference_cnt(malloc_s<unsigned int>(sizeof(unsigned int)))
{
(*_reference_cnt) = 0;
}
/**
* Copy constructor
*/
template <class Target>
SharedPtr<Target>::SharedPtr(const SharedPtr &shared_ptr) noexcept
: _target(shared_ptr._target), _reference_cnt(shared_ptr._reference_cnt)
{
(*_reference_cnt)++;
}
/**
* Move constructor
*/
template <class Target>
SharedPtr<Target>::SharedPtr(SharedPtr &&shared_ptr) noexcept
: _target(shared_ptr._target), _reference_cnt(shared_ptr._reference_cnt)
{
shared_ptr._target = nullptr;
}
template <class Target>
SharedPtr<Target>::~SharedPtr() noexcept
{
if ((*_reference_cnt) != 0U)
{
(*_reference_cnt)--;
}
if ((*_reference_cnt) == 0U)
{
delete _target;
free(_reference_cnt);
_reference_cnt = nullptr;
}
}
template <class Target>
unsigned int SharedPtr<Target>::reference_cnt() const noexcept
{
if (_reference_cnt == nullptr)
{
return 0;
}
return *_reference_cnt;
}
template <class Target>
bool SharedPtr<Target>::is_disposable() const noexcept
{
return _reference_cnt == nullptr || (*_reference_cnt) == 0U;
}
/**
* Copy assignment operator
*/
template <class Target>
SharedPtr<Target> &SharedPtr<Target>::operator=(const SharedPtr &rhs) noexcept
{
if (&rhs != this)
{
if (is_disposable())
{
delete _target;
free(_reference_cnt);
}
_target = nullptr;
_target = rhs._target;
_reference_cnt = nullptr;
_reference_cnt = rhs._reference_cnt;
(*_reference_cnt)++;
}
return *this;
}
/**
* Move assignment operator
*/
template <class Target>
SharedPtr<Target> &SharedPtr<Target>::operator=(SharedPtr &&rhs) noexcept
{
if (&rhs != this)
{
if (is_disposable())
{
delete _target;
free(_reference_cnt);
}
_target = rhs._target;
rhs._target = nullptr;
_reference_cnt = rhs._reference_cnt;
rhs._reference_cnt = nullptr;
}
return *this;
}
template <class Target>
Target &SharedPtr<Target>::operator*() const noexcept
{
return *(_target);
}
template <class Target>
Target *SharedPtr<Target>::operator->() const noexcept
{
return _target;
}
template <class Target, typename... Args>
SharedPtr<Target> make_shared(Args &&...args) noexcept
{
return SharedPtr<Target>(new Target(args...));
}
} // namespace common
|