Skip to content

Commit fb5dc6d

Browse files
committed
Listener cleanup.
1 parent 94a5d32 commit fb5dc6d

File tree

11 files changed

+55
-42
lines changed

11 files changed

+55
-42
lines changed

SerialPrograms/Source/CommonFramework/Environment/SystemSleep.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ SystemSleepController::SystemSleepController()
2929
{}
3030

3131
void SystemSleepController::add_listener(Listener& listener){
32-
std::lock_guard<std::mutex> lg(m_lock);
33-
m_listeners.insert(&listener);
32+
m_listeners.add(listener);
3433
}
3534
void SystemSleepController::remove_listener(Listener& listener){
36-
std::lock_guard<std::mutex> lg(m_lock);
37-
m_listeners.erase(&listener);
35+
m_listeners.remove(listener);
3836
}
3937
void SystemSleepController::notify_listeners(SleepSuppress state){
40-
for (Listener* listener : m_listeners){
41-
listener->sleep_suppress_state_changed(state);
42-
}
38+
m_listeners.run_method_unique(&Listener::sleep_suppress_state_changed, state);
4339
}
4440

4541
SleepSuppress SystemSleepController::current_state() const{

SerialPrograms/Source/CommonFramework/Environment/SystemSleep.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
#ifndef PokemonAutomation_SystemSleep_H
88
#define PokemonAutomation_SystemSleep_H
99

10-
#include <memory>
1110
#include <set>
1211
#include <atomic>
1312
#include <mutex>
14-
#include "Common/Compiler.h"
13+
#include "Common/Cpp/ListenerSet.h"
1514

1615
#if _WIN32
1716
#define PA_ENABLE_SLEEP_SUPPRESS
@@ -76,7 +75,7 @@ class SystemSleepController{
7675
std::atomic<SleepSuppress> m_state;
7776

7877
std::mutex m_lock;
79-
std::set<Listener*> m_listeners;
78+
ListenerSet<Listener> m_listeners;
8079
};
8180

8281

SerialPrograms/Source/CommonFramework/ProgramSession.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ namespace PokemonAutomation{
1717

1818

1919
void ProgramSession::add_listener(Listener& listener){
20-
std::lock_guard<std::mutex> lg(m_lock);
21-
m_listeners.insert(&listener);
20+
m_listeners.add(listener);
2221
}
2322
void ProgramSession::remove_listener(Listener& listener){
24-
std::lock_guard<std::mutex> lg(m_lock);
25-
m_listeners.erase(&listener);
23+
m_listeners.remove(listener);
2624
}
2725

2826

@@ -99,19 +97,17 @@ void ProgramSession::set_state(ProgramState state){
9997
break;
10098
}
10199
m_state.store(state, std::memory_order_relaxed);
102-
for (Listener* listener : m_listeners){
103-
listener->state_change(state);
104-
}
100+
m_listeners.run_method_unique(&Listener::state_change, state);
105101
}
106102
void ProgramSession::push_stats(){
107-
for (Listener* listener : m_listeners){
108-
listener->stats_update(m_current_stats.get(), m_historical_stats.get());
109-
}
103+
m_listeners.run_method_unique(
104+
&Listener::stats_update,
105+
m_current_stats.get(),
106+
m_historical_stats.get()
107+
);
110108
}
111109
void ProgramSession::push_error(const std::string& message){
112-
for (Listener* listener : m_listeners){
113-
listener->error(message);
114-
}
110+
m_listeners.run_method_unique(&Listener::error, message);
115111
}
116112

117113
void ProgramSession::load_historical_stats(){

SerialPrograms/Source/CommonFramework/ProgramSession.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#ifndef PokemonAutomation_CommonFramework_ProgramSession_H
1717
#define PokemonAutomation_CommonFramework_ProgramSession_H
1818

19-
#include <set>
2019
#include <mutex>
2120
#include <atomic>
2221
#include <thread>
2322
#include "Common/Cpp/Time.h"
23+
#include "Common/Cpp/ListenerSet.h"
2424
#include "CommonFramework/Globals.h"
2525
#include "CommonFramework/Logging/Logger.h"
2626
#include "Integrations/ProgramTrackerInterfaces.h"
@@ -138,7 +138,7 @@ class ProgramSession : public TrackableProgram{
138138
std::unique_ptr<StatsTracker> m_current_stats;
139139
// CancellableScope* m_scope = nullptr;
140140

141-
std::set<Listener*> m_listeners;
141+
ListenerSet<Listener> m_listeners;
142142
};
143143

144144

SerialPrograms/Source/CommonFramework/VideoPipeline/VideoOverlayScopes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class OverlayBoxScope : public OverlayBox{
5757

5858

5959

60-
// A text as part of the video overlay.
61-
// It handles its own life time on video overlay: once it's destroyed, it removes itself from VideoOverlay.
60+
// A text as part of the video overlay.
61+
// It handles its own life time on video overlay: once it's destroyed, it removes itself from VideoOverlay.
6262
class OverlayTextScope : public OverlayText{
6363
OverlayTextScope(const OverlayTextScope&) = delete;
6464
void operator=(const OverlayTextScope&) = delete;

SerialPrograms/Source/CommonFramework/VideoPipeline/VideoOverlaySession.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ namespace PokemonAutomation{
1717
void VideoOverlaySession::add_listener(Listener& listener){
1818
WriteSpinLock lg(m_lock);
1919
m_listeners.insert(&listener);
20+
listener.update_stats(&m_stats_order);
2021
}
2122
void VideoOverlaySession::remove_listener(Listener& listener){
2223
WriteSpinLock lg(m_lock);
24+
// listener.update_stats(nullptr);
2325
m_listeners.erase(&listener);
2426
}
2527

@@ -111,7 +113,8 @@ void VideoOverlaySession::push_box_update(){
111113
return;
112114
}
113115

114-
// We create a newly allocated Box vector to avoid listeners accessing `m_boxes` asynchronously.
116+
// We create a newly allocated Box vector to avoid listeners accessing
117+
// `m_boxes` asynchronously.
115118
std::shared_ptr<std::vector<OverlayBox>> ptr = std::make_shared<std::vector<OverlayBox>>();
116119
for (const auto& item : m_boxes){
117120
ptr->emplace_back(*item);
@@ -146,7 +149,8 @@ void VideoOverlaySession::push_text_update(){
146149
return;
147150
}
148151

149-
// We create a newly allocated Box vector to avoid listeners accessing `m_texts` asynchronously.
152+
// We create a newly allocated Box vector to avoid listeners accessing
153+
// `m_texts` asynchronously.
150154
std::shared_ptr<std::vector<OverlayText>> ptr = std::make_shared<std::vector<OverlayText>>();
151155
for (const auto& item : m_texts){
152156
ptr->emplace_back(*item);
@@ -187,7 +191,8 @@ void VideoOverlaySession::push_log_text_update(){
187191
return;
188192
}
189193

190-
// We create a newly allocated Box vector to avoid listeners accessing `m_log_texts` asynchronously.
194+
// We create a newly allocated Box vector to avoid listeners accessing
195+
// `m_log_texts` asynchronously.
191196
std::shared_ptr<std::vector<OverlayLogLine>> ptr = std::make_shared<std::vector<OverlayLogLine>>();
192197
for(const auto& item : m_log_texts){
193198
ptr->emplace_back(item);

SerialPrograms/Source/CommonFramework/VideoPipeline/VideoOverlaySession.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
#include <set>
2020
#include <map>
2121
#include <deque>
22-
#include "Common/Compiler.h"
2322
#include "Common/Cpp/Color.h"
2423
#include "Common/Cpp/Concurrency/SpinLock.h"
25-
#include "CommonFramework/ImageTools/ImageBoxes.h"
2624
#include "VideoOverlay.h"
2725
#include "VideoOverlayOption.h"
2826

@@ -56,14 +54,17 @@ class VideoOverlaySession : public VideoOverlay{
5654
// change the structure of the list itself, you must first call this
5755
// with null to remove it from all the listeners. Then add the updated
5856
// one back when you're done.
57+
// This is called immediately when attaching a listener to give the
58+
// current stats. The listener must drop all references to the stats
59+
// before detaching.
5960
virtual void update_stats(const std::list<OverlayStat*>* stats){}
6061

6162
};
6263

63-
// Add a UI class to listen to any overlay change. The UI class needs to inherit Listener.
64-
// Must call `remove_listener()` before listener is destroyed.
64+
// Add a UI class to listen to any overlay change. The UI class needs to inherit Listener.
65+
// Must call `remove_listener()` before listener is destroyed.
6566
void add_listener(Listener& listener);
66-
// remove a UI class that listens to the overlay change, added by `add_listener()`.
67+
// Remove a UI class that listens to the overlay change, added by `add_listener()`.
6768
void remove_listener(Listener& listener);
6869

6970
public:

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchProgramSession.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ MultiSwitchProgramSession::MultiSwitchProgramSession(MultiSwitchProgramOption& o
4646
, m_scope(nullptr)
4747
, m_sanitizer("MultiSwitchProgramSession")
4848
{
49-
WriteSpinLock lg(m_lock);
50-
m_system.add_listener(*this);
49+
// WriteSpinLock lg(m_lock);
5150
m_option.instance().update_active_consoles(option.system().count());
51+
m_system.add_listener(*this);
5252
}
5353

5454
MultiSwitchProgramSession::~MultiSwitchProgramSession(){

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchSystemSession.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace NintendoSwitch{
1515
void MultiSwitchSystemSession::add_listener(Listener& listener){
1616
std::lock_guard<std::mutex> lg(m_lock);
1717
m_listeners.insert(&listener);
18+
listener.startup(m_consoles.size());
1819
}
1920
void MultiSwitchSystemSession::remove_listener(Listener& listener){
2021
std::lock_guard<std::mutex> lg(m_lock);

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_MultiSwitchSystemSession.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class MultiSwitchSystemSession{
3131
virtual void shutdown() = 0;
3232

3333
// Sent after new Switches are started up.
34+
// This is called immediately when attaching a listener to give the
35+
// current switch count. The listener must drop all references to the
36+
// switch sessions before detaching.
3437
virtual void startup(size_t switch_count) = 0;
3538
};
3639
void add_listener(Listener& listener);

0 commit comments

Comments
 (0)