NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_smart_ptr.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_SHARED_PTR_H
2#define NE_STL_INCLUDENE_STL_SHARED_PTR_H
3
4#include "ne_stl_build.h"
5#include "ne_stl_memory.h"
6
7namespace nstd {
8
9namespace {
10
11typedef struct _cblock {
12 _cblock() {}
15
16} cblock;
17void cblock_release(cblock*& ptr) {
19}
20template <class TObj>
21void object_release(TObj*& ptr) {
23}
24} // namespace
25
26template <class T>
27class _weak_ptr;
28
29template <class T>
31 template <class TObj>
32 friend class _weak_ptr;
33 template <class TObj>
34 friend class _shared_ptr;
35
36public:
37 using element_type = std::remove_extent_t<T>;
39 : data_(nullptr)
40 , release_(object_release<T>)
41 , release_block_(cblock_release) {}
42
43 explicit _shared_ptr(T* data)
44 : data_(data)
45 , release_(object_release<T>)
46 , release_block_(cblock_release) {
47 if (data_) {
48 cblock_ = memory::new_obj<cblock>();
49 }
50 }
51 _shared_ptr(std::nullptr_t)
52 : data_(nullptr)
53 , release_(object_release<T>)
54 , release_block_(cblock_release) {}
56 : data_(sp.data_)
57 , cblock_(sp.cblock_)
58 , release_(sp.release_)
60 if (cblock_) {
61 cblock_->shared_count.operator++();
62 }
63 }
64 template <class D>
66 : data_(dynamic_cast<T*>(sp.data_))
67 , cblock_(sp.cblock_)
68 , release_(reinterpret_cast<void (*)(T*&)>(sp.release_))
70 if (cblock_) {
71 cblock_->shared_count.operator++();
72 }
73 }
75 : data_(wp.data_)
76 , cblock_(wp.cblock_)
77 , release_(wp.release_)
78 , release_block_(cblock_release) {
79 if (cblock_) {
80 cblock_->shared_count.operator++();
81 }
82 }
85 if (this == &sp)
86 return *this;
87 data_ = sp.data_;
88 cblock_ = sp.cblock_;
89 release_ = sp.release_;
91 if (cblock_) {
92 cblock_->shared_count.operator++();
93 }
94 return *this;
95 }
96 _shared_ptr& operator=(std::nullptr_t) {
97 reset();
98 return *this;
99 }
100 inline operator bool() const { return data_ != nullptr; }
101 inline bool operator==(std::nullptr_t) const { return !this->operator bool(); }
102 inline bool operator!=(std::nullptr_t) const { return this->operator bool(); }
103 T* get() const { return data_; }
104
105 T* operator->() const { return data_; }
106
107 T& operator*() const {
108 assert(*this);
109 return *data_;
110 }
111
112 void reset() {
114 data_ = nullptr;
115 cblock_ = nullptr;
116 }
117
118 void reset(T* data) {
120 data_ = data;
121 cblock_ = memory::new_obj<cblock>();
122 }
123
124private:
125 inline void dec_shared_count() {
126 if (!cblock_) {
127 return;
128 }
129 cblock_->shared_count.operator--();
130 if (cblock_->shared_count <= 0) {
131 if (release_ != nullptr)
133 if (cblock_->weak_count <= 0 && release_block_ != nullptr) {
135 }
136 }
137 }
138
139private:
140 T* data_{nullptr};
141 nstd::cblock* cblock_{nullptr};
142 void (*release_)(T*& pdata){nullptr};
143 void (*release_block_)(nstd::cblock*& pdata){nullptr};
144};
145template <class T>
147public:
149 : data_(nullptr)
150 , cblock_(nullptr)
151 , release_(nullptr)
152 , release_block_(nullptr) {}
154 : data_(wp.data_)
155 , cblock_(wp.cblock_)
156 , release_(wp.release_)
158 if (cblock_) {
159 cblock_->weak_count.operator++();
160 }
161 }
163 : data_(sp.data_)
164 , cblock_(sp.cblock_)
165 , release_(sp.release_)
167 if (cblock_) {
168 cblock_->weak_count.operator++();
169 }
170 }
173 _weak_ptr tmp(wp);
174 swap(tmp);
175 return *this;
176 }
177 inline _shared_ptr<T> lock() const { return expired() ? _shared_ptr<T>(nullptr) : _shared_ptr<T>(*this); }
178 inline bool expired() const { return cblock_ == nullptr || cblock_->shared_count <= 0; }
179 inline void reset() {
181 data_ = nullptr;
182 cblock_ = nullptr;
183 release_block_ = nullptr;
184 }
185
186private:
187 inline void dec_weak_count() {
188 if (cblock_ == nullptr) {
189 return;
190 }
191 cblock_->weak_count.operator--();
192 if (cblock_->shared_count <= 0 && cblock_->weak_count <= 0 && release_block_ != nullptr) {
194 cblock_ = nullptr;
195 }
196 }
197
198private:
200 nstd::cblock* cblock_;
201 void (*release_)(T*& pdata);
202 void (*release_block_)(nstd::cblock*& pdata);
203 template <class Y>
204 friend class _shared_ptr;
205};
206template <class T>
208template <class T, class... TArgs>
210 return nstd::shared_ptr<T>(memory::new_obj<T>(args...));
211}
212template <class T1, class T2>
214 // dynamic_cast for shared_ptr that properly respects the reference count control block
215 const auto ptr = dynamic_cast<typename shared_ptr<T1>::element_type*>(other.get());
216 if (ptr) {
217 return shared_ptr<T1>(other);
218 }
219 return (shared_ptr<T1>());
220}
221template <class T>
223} // namespace nstd
224
225#endif // !NE_STL_INCLUDENE_STL_SHARED_PTR_H
Definition: ne_stl_smart_ptr.h:30
_shared_ptr(const _weak_ptr< T > &wp)
Definition: ne_stl_smart_ptr.h:74
_shared_ptr & operator=(const _shared_ptr &sp)
Definition: ne_stl_smart_ptr.h:84
_shared_ptr(std::nullptr_t)
Definition: ne_stl_smart_ptr.h:51
void dec_shared_count()
Definition: ne_stl_smart_ptr.h:125
nstd::cblock * cblock_
Definition: ne_stl_smart_ptr.h:141
void(* release_)(T *&pdata)
Definition: ne_stl_smart_ptr.h:142
void reset(T *data)
Definition: ne_stl_smart_ptr.h:118
T * get() const
Definition: ne_stl_smart_ptr.h:103
void reset()
Definition: ne_stl_smart_ptr.h:112
T * data_
Definition: ne_stl_smart_ptr.h:140
_shared_ptr(const _shared_ptr &sp)
Definition: ne_stl_smart_ptr.h:55
~_shared_ptr()
Definition: ne_stl_smart_ptr.h:83
_shared_ptr(const _shared_ptr< D > &sp)
Definition: ne_stl_smart_ptr.h:65
T & operator*() const
Definition: ne_stl_smart_ptr.h:107
bool operator!=(std::nullptr_t) const
Definition: ne_stl_smart_ptr.h:102
bool operator==(std::nullptr_t) const
Definition: ne_stl_smart_ptr.h:101
_shared_ptr(T *data)
Definition: ne_stl_smart_ptr.h:43
std::remove_extent_t< T > element_type
Definition: ne_stl_smart_ptr.h:37
void(* release_block_)(nstd::cblock *&pdata)
Definition: ne_stl_smart_ptr.h:143
T * operator->() const
Definition: ne_stl_smart_ptr.h:105
_shared_ptr()
Definition: ne_stl_smart_ptr.h:38
_shared_ptr & operator=(std::nullptr_t)
Definition: ne_stl_smart_ptr.h:96
Definition: ne_stl_smart_ptr.h:146
T * data_
Definition: ne_stl_smart_ptr.h:199
_weak_ptr(const _shared_ptr< T > &sp)
Definition: ne_stl_smart_ptr.h:162
nstd::cblock * cblock_
Definition: ne_stl_smart_ptr.h:200
~_weak_ptr()
Definition: ne_stl_smart_ptr.h:171
_weak_ptr(const _weak_ptr &wp)
Definition: ne_stl_smart_ptr.h:153
void(* release_block_)(nstd::cblock *&pdata)
Definition: ne_stl_smart_ptr.h:202
void reset()
Definition: ne_stl_smart_ptr.h:179
void dec_weak_count()
Definition: ne_stl_smart_ptr.h:187
_weak_ptr & operator=(const _weak_ptr &wp)
Definition: ne_stl_smart_ptr.h:172
bool expired() const
Definition: ne_stl_smart_ptr.h:178
_shared_ptr< T > lock() const
Definition: ne_stl_smart_ptr.h:177
void(* release_)(T *&pdata)
Definition: ne_stl_smart_ptr.h:201
_weak_ptr()
Definition: ne_stl_smart_ptr.h:148
Definition: ne_stl_any.h:7
nstd::shared_ptr< T > make_shared(TArgs &&... args)
Definition: ne_stl_smart_ptr.h:209
shared_ptr< T1 > dynamic_pointer_cast(const shared_ptr< T2 > &other) noexcept
Definition: ne_stl_smart_ptr.h:213
nstd::atomic_int shared_count
Definition: ne_stl_smart_ptr.h:14
nstd::atomic_int weak_count
Definition: ne_stl_smart_ptr.h:13
unsigned char bool
Definition: stdbool.h:25
static bool delete_obj(TObject *&object)
Definition: ne_stl_memory.h:47