1010#include < map>
1111#include < atomic>
1212#include " Common/Cpp/Concurrency/SpinLock.h"
13+ #include " Common/Cpp/LifetimeSanitizer.h"
1314
1415// #include <iostream>
1516// using std::cout;
1617// using std::endl;
1718
19+ #define PA_DEBUG_ListenerSet
20+
21+
1822namespace PokemonAutomation {
1923
2024
@@ -30,12 +34,18 @@ class ListenerSet{
3034 }
3135
3236 void add (ListenerType& listener){
37+ #ifdef PA_DEBUG_ListenerSet
38+ auto scope = m_sanitizer.check_scope ();
39+ #endif
3340 WriteSpinLock lg (m_lock);
3441// std::lock_guard<std::mutex> lg(m_lock);
3542 m_listeners[&listener]++;
3643 m_count.store (m_listeners.size (), std::memory_order_relaxed);
3744 }
3845 void remove (ListenerType& listener){
46+ #ifdef PA_DEBUG_ListenerSet
47+ auto scope = m_sanitizer.check_scope ();
48+ #endif
3949 WriteSpinLock lg (m_lock);
4050// std::lock_guard<std::mutex> lg(m_lock);
4151 auto iter = m_listeners.find (&listener);
@@ -50,6 +60,9 @@ class ListenerSet{
5060
5161 template <typename Lambda>
5262 void run_lambda_unique (Lambda&& lambda){
63+ #ifdef PA_DEBUG_ListenerSet
64+ auto scope = m_sanitizer.check_scope ();
65+ #endif
5366 if (empty ()){
5467 return ;
5568 }
@@ -61,6 +74,9 @@ class ListenerSet{
6174 }
6275 template <typename Lambda>
6376 void run_lambda_with_duplicates (Lambda&& lambda){
77+ #ifdef PA_DEBUG_ListenerSet
78+ auto scope = m_sanitizer.check_scope ();
79+ #endif
6480 if (empty ()){
6581 return ;
6682 }
@@ -77,6 +93,9 @@ class ListenerSet{
7793
7894 template <typename Function, class ... Args>
7995 void run_method_unique (Function function, Args&&... args){
96+ #ifdef PA_DEBUG_ListenerSet
97+ auto scope = m_sanitizer.check_scope ();
98+ #endif
8099 if (empty ()){
81100 return ;
82101 }
@@ -88,6 +107,9 @@ class ListenerSet{
88107 }
89108 template <typename Function, class ... Args>
90109 void run_method_with_duplicates (Function function, Args&&... args){
110+ #ifdef PA_DEBUG_ListenerSet
111+ auto scope = m_sanitizer.check_scope ();
112+ #endif
91113 if (empty ()){
92114 return ;
93115 }
@@ -110,6 +132,10 @@ class ListenerSet{
110132 mutable SpinLock m_lock;
111133// mutable std::mutex m_lock;
112134 std::map<ListenerType*, size_t > m_listeners;
135+
136+ #ifdef PA_DEBUG_ListenerSet
137+ LifetimeSanitizer m_sanitizer;
138+ #endif
113139};
114140
115141
0 commit comments