blob: fbc07927450baeb9d8e04510935394284970301d (
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
|
#pragma once
#include "shared_pointer.hh"
template <typename T>
SharedPointer<T>::SharedPointer(element_type* p)
{
if (p == nullptr)
{
data_ = nullptr;
count_ = nullptr;
}
else
{
data_ = p;
count_ = new long{ 1 };
}
}
template <typename T>
SharedPointer<T>::~SharedPointer()
{
if (count_ != nullptr)
{
(*count_)--;
if (*count_ == 0)
{
delete count_;
if (data_ != nullptr)
delete data_;
}
}
}
template <typename T>
SharedPointer<T>::SharedPointer(const SharedPointer<element_type>& other)
{
this->~SharedPointer();
data_ = other.data_;
if (data_ == nullptr)
count_ = nullptr;
count_ = other.count_;
if (count_ != nullptr)
(*count_)++;
}
template <typename T>
void SharedPointer<T>::reset(element_type* p)
{
if (data_ == p)
return;
this->~SharedPointer();
data_ = p;
if (p == nullptr)
count_ = nullptr;
else
count_ = new long{ 1 };
}
template <typename T>
SharedPointer<T>& SharedPointer<T>::operator=(const SharedPointer<T>& other)
{
this->~SharedPointer();
data_ = other.data_;
count_ = other.count_;
if (nullptr != other.data_)
{
(*this->count_)++;
}
return *this;
}
template <typename T>
T& SharedPointer<T>::operator*() const
{
return *data_;
}
template <typename T>
T* SharedPointer<T>::operator->() const
{
return data_;
}
template <typename T>
T* SharedPointer<T>::get() const
{
return data_;
}
template <typename T>
long SharedPointer<T>::use_count() const
{
if (count_ == nullptr)
return 0;
return *count_;
}
template <typename T>
template <typename U>
bool SharedPointer<T>::operator==(const SharedPointer<U>& rhs) const
{
if (rhs.data_ == this->data_)
return true;
return false;
}
template <typename T>
template <typename U>
bool SharedPointer<T>::operator!=(const SharedPointer<U>& rhs) const
{
return !(*this == rhs);
}
template <typename T>
bool SharedPointer<T>::operator==(const T* p) const
{
return this->data_ == p;
}
template <typename T>
bool SharedPointer<T>::operator!=(const T* p) const
{
return this->data_ != p;
}
template <typename T>
SharedPointer<T>& SharedPointer<T>::operator=(SharedPointer<T>&& other) noexcept
{
this->~SharedPointer();
data_ = other.data_;
count_ = other.count_;
other.data_ = nullptr;
other.count_ = nullptr;
return *this;
}
template <typename T>
SharedPointer<T>::SharedPointer(SharedPointer&& other)
{
this->~SharedPointer();
data_ = other.data_;
count_ = other.count_;
other.data_ = nullptr;
other.count_ = nullptr;
}
template <typename T>
SharedPointer<T>::operator bool() const
{
return data_ != nullptr;
}
template <typename T>
template <typename U>
bool SharedPointer<T>::is_a() const
{
return dynamic_cast<U*>(data_);
}
|