28 #include <unordered_map>
32 #include "observable.h"
34 std::string ptr_key(
const void* ptr) {
35 std::ostringstream oss;
42 ListenersByKey& ListenersByKey::GetInstance(
const std::string& key) {
43 static std::unordered_map<std::string, ListenersByKey> instances;
44 static std::mutex s_mutex;
46 std::lock_guard<std::mutex> lock(s_mutex);
47 if (instances.find(key) == instances.end()) {
50 return instances[key];
55 using ev_pair = std::pair<wxEvtHandler*, wxEventType>;
57 void Observable::Listen(wxEvtHandler* listener, wxEventType ev_type) {
58 std::lock_guard<std::mutex> lock(m_mutex);
59 const auto& listeners = m_list.listeners;
61 ev_pair key_pair(listener, ev_type);
62 auto found = std::find(listeners.begin(), listeners.end(), key_pair);
63 assert((found == listeners.end()) &&
"Duplicate listener");
64 m_list.listeners.push_back(key_pair);
68 std::lock_guard<std::mutex> lock(m_mutex);
69 auto& listeners = m_list.listeners;
71 ev_pair key_pair(listener, ev_type);
72 auto found = std::find(listeners.begin(), listeners.end(), key_pair);
73 if (found == listeners.end())
return false;
74 listeners.erase(found);
79 const std::string& s,
int num,
81 std::lock_guard<std::mutex> lock(m_mutex);
82 auto& listeners = m_list.listeners;
84 for (
auto l = listeners.begin(); l != listeners.end(); l++) {
86 evt->SetSharedPtr(ptr);
87 evt->SetClientData(client_data);
88 evt->SetString(s.c_str());
90 wxQueueEvent(l->first, evt);
100 if (key !=
"") Unlisten();
107 void ObservableListener::Listen() {
114 void ObservableListener::Unlisten() {
void Listen(const std::string &key, wxEvtHandler *listener, wxEventType evt)
Set object to send wxEventType ev to listener on changes in key.
The observable notify/listen basic nuts and bolts.
bool Unlisten(wxEvtHandler *listener, wxEventType ev)
Remove window listening to ev from list of listeners.
virtual const void Notify()
Notify all listeners about variable change.
Adds a std::shared<void> element to wxCommandEvent.