1#ifndef NE_STL_INCLUDENE_STL_SHARED_PTR_H
2#define NE_STL_INCLUDENE_STL_SHARED_PTR_H
11typedef struct _cblock {
18void cblock_release(cblock*& ptr) {
22void object_release(TObj*& ptr) {
49 cblock_ = memory::new_obj<cblock>();
57 cblock* temp_cblock = sp.
cblock_;
59 int current_count = temp_cblock->shared_count.load();
60 while (current_count > 0) {
61 if (temp_cblock->shared_count.compare_exchange(current_count, current_count + 1)) {
68 current_count = temp_cblock->shared_count.load();
78 cblock* temp_cblock = sp.
cblock_;
80 int current_count = temp_cblock->shared_count.load();
81 while (current_count > 0) {
82 if (temp_cblock->shared_count.compare_exchange(current_count, current_count + 1)) {
89 current_count = temp_cblock->shared_count.load();
103 cblock_->shared_count.operator++();
116 cblock_->shared_count.operator++();
124 inline operator bool()
const {
return data_ !=
nullptr; }
148 cblock_ = memory::new_obj<cblock>();
156 int new_count =
cblock_->shared_count.operator--();
157 if (new_count == 0) {
160 int weak_count_val =
cblock_->weak_count.load();
187 cblock_->weak_count.operator++();
196 cblock_->weak_count.operator++();
219 int new_weak_count =
cblock_->weak_count.operator--();
220 int shared_count_val =
cblock_->shared_count.load();
221 if (shared_count_val <= 0 && new_weak_count == 0 &&
release_block_ !=
nullptr) {
237template <
class T,
class... TArgs>
241template <
class T1,
class T2>
Definition: ne_stl_smart_ptr.h:31
_shared_ptr(const _weak_ptr< T > &wp)
Definition: ne_stl_smart_ptr.h:97
_shared_ptr & operator=(const _shared_ptr &sp)
Definition: ne_stl_smart_ptr.h:107
_shared_ptr(std::nullptr_t)
Definition: ne_stl_smart_ptr.h:52
void dec_shared_count()
Definition: ne_stl_smart_ptr.h:152
nstd::cblock * cblock_
Definition: ne_stl_smart_ptr.h:169
void(* release_)(T *&pdata)
Definition: ne_stl_smart_ptr.h:170
void reset(T *data)
Definition: ne_stl_smart_ptr.h:145
T * get() const
Definition: ne_stl_smart_ptr.h:130
void reset()
Definition: ne_stl_smart_ptr.h:139
T * data_
Definition: ne_stl_smart_ptr.h:168
_shared_ptr(const _shared_ptr &sp)
Definition: ne_stl_smart_ptr.h:56
long use_count() const
Definition: ne_stl_smart_ptr.h:128
~_shared_ptr()
Definition: ne_stl_smart_ptr.h:106
_shared_ptr(const _shared_ptr< D > &sp)
Definition: ne_stl_smart_ptr.h:77
T & operator*() const
Definition: ne_stl_smart_ptr.h:134
bool operator!=(std::nullptr_t) const
Definition: ne_stl_smart_ptr.h:126
bool operator==(std::nullptr_t) const
Definition: ne_stl_smart_ptr.h:125
_shared_ptr(T *data)
Definition: ne_stl_smart_ptr.h:44
std::remove_extent_t< T > element_type
Definition: ne_stl_smart_ptr.h:38
void(* release_block_)(nstd::cblock *&pdata)
Definition: ne_stl_smart_ptr.h:171
T * operator->() const
Definition: ne_stl_smart_ptr.h:132
_shared_ptr()
Definition: ne_stl_smart_ptr.h:39
_shared_ptr & operator=(std::nullptr_t)
Definition: ne_stl_smart_ptr.h:120
Definition: ne_stl_smart_ptr.h:174
T * data_
Definition: ne_stl_smart_ptr.h:228
_weak_ptr(const _shared_ptr< T > &sp)
Definition: ne_stl_smart_ptr.h:190
nstd::cblock * cblock_
Definition: ne_stl_smart_ptr.h:229
~_weak_ptr()
Definition: ne_stl_smart_ptr.h:199
_weak_ptr(const _weak_ptr &wp)
Definition: ne_stl_smart_ptr.h:181
void(* release_block_)(nstd::cblock *&pdata)
Definition: ne_stl_smart_ptr.h:231
void reset()
Definition: ne_stl_smart_ptr.h:207
void dec_weak_count()
Definition: ne_stl_smart_ptr.h:215
_weak_ptr & operator=(const _weak_ptr &wp)
Definition: ne_stl_smart_ptr.h:200
bool expired() const
Definition: ne_stl_smart_ptr.h:206
_shared_ptr< T > lock() const
Definition: ne_stl_smart_ptr.h:205
void(* release_)(T *&pdata)
Definition: ne_stl_smart_ptr.h:230
_weak_ptr()
Definition: ne_stl_smart_ptr.h:176
Definition: ne_stl_any.h:10
nstd::shared_ptr< T > make_shared(TArgs &&... args)
Definition: ne_stl_smart_ptr.h:238
shared_ptr< T1 > dynamic_pointer_cast(const shared_ptr< T2 > &other) noexcept
Definition: ne_stl_smart_ptr.h:242
nstd::atomic_int shared_count
Definition: ne_stl_smart_ptr.h:15
nstd::atomic_int weak_count
Definition: ne_stl_smart_ptr.h:14
unsigned char bool
Definition: stdbool.h:27
static bool delete_obj(TObject *&object)
Definition: ne_stl_memory.h:47