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")))
49 std::string ptr_key(
const void* ptr);
57 virtual std::string GetKey()
const = 0;
78 std::vector<std::pair<wxEvtHandler*, wxEventType>> listeners;
87 :
key(_key), m_list(ListenersByKey::GetInstance(_key)) {}
92 virtual const void Notify();
94 const void Notify(std::shared_ptr<const void> p) {
Notify(p,
"", 0, 0); }
100 bool Unlisten(wxEvtHandler* listener, wxEventType ev);
102 std::string GetKey()
const {
return key; }
113 const void Notify(std::shared_ptr<const void> ptr,
const std::string& s,
114 int num,
void* client_data);
116 const void Notify(
const std::string& s,
void* client_data) {
117 Notify(
nullptr, s, 0, client_data);
123 void Listen(wxEvtHandler* listener, wxEventType ev_type);
127 mutable std::mutex m_mutex;
140 : key(k), listener(l), ev_type(e) {
149 : key(other.key), listener(other.listener), ev_type(other.ev_type) {
160 void Listen(
const std::string& key, wxEvtHandler* listener, wxEventType evt);
162 void Listen(
const KeyProvider& kp, wxEvtHandler* l, wxEventType evt) {
163 Listen(kp.GetKey(), l, evt);
171 wxEvtHandler* listener;
229 const wxEventTypeTag<ObservedEvt> EvtObs(wxNewEventType());
231 m_listener.
Listen(kp,
this, EvtObs);
232 Bind(EvtObs, action);
241 template <
typename T>
242 std::shared_ptr<const T> UnpackEvtPointer(
ObservedEvt ev) {
243 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.
Keeps listening over it's lifespan, removes itself on destruction.
ObservableListener(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.