NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_delegator_storage.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_DELEGATOR_STORAGE_H
2#define NE_STL_INCLUDENE_STL_DELEGATOR_STORAGE_H
3
4#include <algorithm>
5#include "ne_stl_bind.h"
7#include "ne_stl_spinlock.h"
8namespace nstd {
9
13public:
16 void clear() {
17 nstd::vector<unregister_delegate_t> _temp_callback_list;
18 {
20 _temp_callback_list = callback_list_;
22 }
23 for (auto& iter : _temp_callback_list) {
24 if (iter != nullptr) {
25 (iter)();
26 }
27 }
28 }
29 void add_delegate(const unregister_delegate_t& _delegate) {
31 callback_list_.append(_delegate);
32 }
33
34private:
37};
39
40template <typename t_delegator>
42public:
45 inline void clear() {
46 nstd::lock_guard auto_lock(lock_);
47 element_list_.clear();
48 }
49 inline unregister_delegate_t add_delegate(const t_delegator& _delegate, bool exclusive = false) {
50 auto _new_delegate = nstd::make_shared<t_delegator>(_delegate);
51 size_t _new_delegate_id = (size_t)(_new_delegate.get());
52 {
53 nstd::lock_guard auto_lock(lock_);
54 if (exclusive)
55 element_list_.clear();
56 element_list_.append(make_pair(_new_delegate_id, _new_delegate));
57 }
58 return ([this, _weak_flag = weak_flag_.get_weak_flag(), _new_delegate_id]() {
59 if (!_weak_flag.expired()) {
60 nstd::lock_guard auto_lock(lock_);
61 auto it = std::find_if(element_list_.begin(), element_list_.end(), [&](const pair<size_t, nstd::shared_ptr<t_delegator>>& item) {
62 return item.first == _new_delegate_id;
63 });
64 if (it != element_list_.end())
65 element_list_.erase(it);
66 }
67 });
68 }
69 inline operator bool() const {
70 nstd::lock_guard auto_lock(lock_);
71 return !element_list_.empty();
72 }
73 inline bool operator==(std::nullptr_t) const { return !operator bool(); }
74 inline bool operator!=(std::nullptr_t) const { return operator bool(); }
75 template <typename... t_params>
76 inline void operator()(t_params... params) {
78 {
79 nstd::lock_guard auto_lock(lock_);
80 _element_list = element_list_;
81 }
82 for (auto& it : _element_list) {
83 if (it.second != nullptr) {
84 (*it.second)(params...);
85 }
86 }
87 }
88
89private:
93};
94template <typename t_delegator>
96
97} // namespace nstd
98
99#endif // !NE_STL_INCLUDENE_STL_DELEGATOR_STORAGE_H
Definition ne_stl_continuous_container.h:15
void clear()
Definition ne_stl_continuous_container.h:444
Definition ne_stl_delegator_storage.h:41
void operator()(t_params... params)
Definition ne_stl_delegator_storage.h:76
bool operator==(std::nullptr_t) const
Definition ne_stl_delegator_storage.h:73
void clear()
Definition ne_stl_delegator_storage.h:45
nstd::spinlock lock_
Definition ne_stl_delegator_storage.h:90
bool operator!=(std::nullptr_t) const
Definition ne_stl_delegator_storage.h:74
_delegator_storage()
Definition ne_stl_delegator_storage.h:43
~_delegator_storage()
Definition ne_stl_delegator_storage.h:44
unregister_delegate_t add_delegate(const t_delegator &_delegate, bool exclusive=false)
Definition ne_stl_delegator_storage.h:49
nstd::vector< pair< size_t, nstd::shared_ptr< t_delegator > > > element_list_
Definition ne_stl_delegator_storage.h:91
nstd::weak_semantics_supporter weak_flag_
Definition ne_stl_delegator_storage.h:92
Definition ne_stl_spinlock.h:61
Definition ne_stl_spinlock.h:44
Definition ne_stl_delegator_storage.h:12
nstd::vector< unregister_delegate_t > callback_list_
Definition ne_stl_delegator_storage.h:36
void add_delegate(const unregister_delegate_t &_delegate)
Definition ne_stl_delegator_storage.h:29
~_unregister_delegator()
Definition ne_stl_delegator_storage.h:15
void clear()
Definition ne_stl_delegator_storage.h:16
nstd::spinlock callback_list_lock_
Definition ne_stl_delegator_storage.h:35
Definition ne_stl_any.h:7
pair< first_type, second_type > make_pair(const first_type &_first, const second_type &_second)
Definition ne_stl_value_def.h:65
unsigned char bool
Definition stdbool.h:25
Definition ne_stl_bind.h:11
Definition ne_stl_bind.h:13
nstd::weak_ptr< nstd::weak_flag > get_weak_flag()
Definition ne_stl_bind.h:15
Definition ne_stl_bind.h:132