36#include "observable_evt.h"
39#if defined(_MSC_VER) || defined(__CYGWIN__)
40#define DECL_EXP __declspec(dllexport)
41#elif defined(__GNUC__) || defined(__clang__)
42#define DECL_EXP __attribute__((visibility("default")))
49std::string ptr_key(
const void* ptr);
57 virtual std::string GetKey()
const = 0;
77 std::vector<std::pair<wxEvtHandler*, wxEventType>> listeners;
86 :
key(_key), m_list(ListenersByKey::GetInstance(_key)) {}
91 virtual const void Notify();
93 const void Notify(std::shared_ptr<const void> p) {
Notify(p,
"", 0, 0); }
99 bool Unlisten(wxEvtHandler* listener, wxEventType ev);
101 std::string GetKey()
const {
return key; }
112 const void Notify(std::shared_ptr<const void> ptr,
const std::string& s,
113 int num,
void* client_data);
115 const void Notify(
const std::string& s,
void* client_data) {
116 Notify(
nullptr, s, 0, client_data);
121 void Listen(wxEvtHandler* listener, wxEventType ev_type);
125 mutable std::mutex m_mutex;
138 : key(k), listener(l), ev_type(e) {
147 : key(other.key), listener(other.listener), ev_type(other.ev_type) {
155 listener = other.listener;
156 ev_type = other.ev_type;
168 void Listen(
const std::string& key, wxEvtHandler* listener, wxEventType evt);
170 void Listen(
const KeyProvider& kp, wxEvtHandler* l, wxEventType evt) {
171 Listen(kp.GetKey(), l, evt);
179 wxEvtHandler* listener;
235 this->m_listener = std::move(other.m_listener);
238 m_listener = std::move(other.m_listener);
257 const wxEventTypeTag<ObservedEvt> EvtObs(wxNewEventType());
259 m_listener.
Listen(kp,
this, EvtObs);
260 Bind(EvtObs, action);
269std::shared_ptr<const T> UnpackEvtPointer(
ObservedEvt ev) {
270 return std::static_pointer_cast<const T>(ev.GetSharedPtr());
Interface implemented by classes which listens.
Define an action to be performed when a KeyProvider is notified.
ObsListener()
Create an object which does not listen until Init();.
ObsListener(const KeyProvider &kp, std::function< void(ObservedEvt &ev)> action)
Create object which invokes action when kp is notified.
ObsListener(const KeyProvider &kp, std::function< void()> action)
Create object which invokes action when kp is notified.
void Init(const KeyProvider &kp, std::function< void(ObservedEvt &ev)> action)
Initiate an object yet not listening.
ObsListener(ObsListener &&other)
ObsListener can only be assigned using std::move.
Keeps listening over it's lifespan, removes itself on destruction.
ObservableListener(ObservableListener &&other)
A listener can only be transferred using std::move().
ObservableListener & operator=(ObservableListener &&other)
A listener can only be transferred using std::move().
void Listen(const std::string &key, wxEvtHandler *listener, wxEventType evt)
Set object to send wxEventType ev to listener on changes in key.
ObservableListener(const std::string &k, wxEvtHandler *l, wxEventType e)
Construct a listening object.
ObservableListener()
Default constructor, does not listen to anything.
The observable notify/listen basic nuts and bolts.
const std::string key
The key used to create and clone.
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.