44#if defined(_MSC_VER) || defined(__CYGWIN__)
45#define DECL_EXP __declspec(dllexport)
46#elif defined(__GNUC__) || defined(__clang__)
47#define DECL_EXP __attribute__((visibility("default")))
54std::string
ptr_key(
const void* ptr);
76 [[nodiscard]]
virtual std::string
GetKey()
const = 0;
96 std::vector<std::pair<wxEvtHandler*, wxEventType>> listeners;
105 :
key(_key), m_list(ListenersByKey::GetInstance(_key)) {}
117 void Notify(
const std::shared_ptr<const void>& p) {
Notify(p,
"", 0,
nullptr); }
123 bool Unlisten(wxEvtHandler* listener, wxEventType ev);
136 void Notify(
const std::shared_ptr<const void>& ptr,
const std::string& s,
137 int num,
void* client_data);
139 void Notify(
const std::string& s,
void* client_data) {
140 Notify(
nullptr, s, 0, client_data);
145 void Listen(wxEvtHandler* listener, wxEventType ev_type);
149 mutable std::mutex m_mutex;
164 : key(std::move(k)), listener(l), ev_type(e) {
174 listener = other.listener;
175 ev_type = other.ev_type;
183 listener = other.listener;
184 ev_type = other.ev_type;
196 void Listen(
const std::string& key, wxEvtHandler* listener, wxEventType evt);
200 Listen(kp.
GetKey(), l, evt);
208 wxEvtHandler* listener;
264 m_listener.Unlisten();
265 Unbind(other.m_obs_evt, other.m_action);
266 m_action = other.m_action;
267 Bind(m_obs_evt, m_action);
268 m_listener.
Listen(other.m_listener.key,
this, m_obs_evt);
272 m_listener.Unlisten();
273 Unbind(other.m_obs_evt, other.m_action);
274 m_action = other.m_action;
275 Bind(m_obs_evt, m_action);
276 m_listener.
Listen(other.m_listener.key,
this, m_obs_evt);
285 const std::function<
void(
ObservedEvt& ev)>& action)
286 : m_obs_evt(wxNewEventType()) {
296 const std::function<
void(
ObservedEvt& ev)>& action) {
298 const wxEventTypeTag<ObservedEvt> EvtObs(wxNewEventType());
300 m_listener.
Listen(kp,
this, EvtObs);
301 Bind(EvtObs, action);
307 const wxEventTypeTag<ObservedEvt> m_obs_evt;
313 return std::static_pointer_cast<const T>(ev.
GetSharedPtr());
Interface implemented by classes which listens.
virtual ~KeyProvider()=default
Destroy the Key Provider object.
virtual std::string GetKey() const =0
Get the Key object from the Key Provider.
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, const std::function< void()> &action)
Create object which invokes action when kp is notified.
void Init(const KeyProvider &kp, const std::function< void(ObservedEvt &ev)> &action)
Initiate an object yet not listening.
ObsListener(const KeyProvider &kp, const std::function< void(ObservedEvt &ev)> &action)
Create object which invokes action when kp is notified.
ObsListener(ObsListener &&other) noexcept
ObsListener can only be assigned using std::move.
Keeps listening over its lifespan, removes itself on destruction.
ObservableListener(ObservableListener &&other) noexcept
A listener can only be transferred using std::move().
ObservableListener(std::string k, wxEvtHandler *l, wxEventType e)
Construct a listening object.
void Listen(const std::string &key, wxEvtHandler *listener, wxEventType evt)
Set object to send wxEventType ev to listener on changes in key.
void Listen(const KeyProvider &kp, wxEvtHandler *l, wxEventType evt)
Set object to send wxEventType ev to listener on changes in key.
ObservableListener & operator=(ObservableListener &&other) noexcept
A listener can only be transferred using std::move().
ObservableListener()
Default constructor, does not listen to anything.
The observable notify/listen basic nuts and bolts.
~Observable() override=default
Destroy the Observable object.
virtual void Notify()
Notify all listeners about variable change.
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.
std::string GetKey() const override
Get the Key object from the Key Provider.
Custom event class for OpenCPN's notification system.
std::shared_ptr< const void > GetSharedPtr() const
Gets the event's payload data.
std::shared_ptr< const T > UnpackEvtPointer(const ObservedEvt &ev)
Shorthand for accessing ObservedEvt.SharedPtr().
std::string ptr_key(const void *ptr)
Return address as printable string.
wxCommandEvt subclass which can carry also a shared_ptr<void>