46#if defined(_MSC_VER) || defined(__CYGWIN__)
47#define DECL_EXP __declspec(dllexport)
48#elif defined(__GNUC__) || defined(__clang__)
49#define DECL_EXP __attribute__((visibility("default")))
61std::string
PtrKey(
const void* ptr);
76 [[nodiscard]]
virtual std::string
GetKey()
const = 0;
96 std::vector<std::pair<wxEvtHandler*, wxEventType>> listeners;
101 friend class ::ObservableListener;
122 virtual void Notify(
const std::shared_ptr<const void>& p) {
123 Notify(p,
"", 0,
nullptr);
130 bool Unlisten(wxEvtHandler* listener, wxEventType ev);
144 void Notify(
const std::shared_ptr<const void>& ptr,
const std::string& s,
145 int num,
void* client_data);
152 void Notify(
const std::string& s,
void* client_data){
153 Notify(
nullptr, s, 0, client_data);
158 void Listen(wxEvtHandler* listener, wxEventType ev_type);
162 mutable std::mutex m_mutex;
176 friend class obs::Listener;
189 : m_key(std::move(k)), m_listener(l), m_ev_type(e) {
207 m_listener = other.m_listener;
208 m_ev_type = other.m_ev_type;
216 m_listener = other.m_listener;
217 m_ev_type = other.m_ev_type;
230 void Listen(
const std::string& key, wxEvtHandler* listener, wxEventType evt);
234 Listen(kp.
GetKey(), l, evt);
242 wxEvtHandler* m_listener;
243 wxEventType m_ev_type;
293class Listener :
public wxEvtHandler {
296 Listener() : m_obs_evt(wxNewEventType()) {}
299 Listener(Listener&& other)
noexcept: m_obs_evt(wxNewEventType()) {
300 m_listener.Unlisten();
301 Unbind(other.m_obs_evt, other.m_action);
302 m_action = other.m_action;
303 Bind(m_obs_evt, m_action);
304 m_listener.Listen(other.m_listener.m_key,
this, m_obs_evt);
308 Listener& operator=(Listener&& other)
noexcept {
309 m_listener.Unlisten();
310 Unbind(other.m_obs_evt, other.m_action);
311 m_action = other.m_action;
312 Bind(m_obs_evt, m_action);
313 m_listener.Listen(other.m_listener.m_key,
this, m_obs_evt);
318 Listener(
const Listener&) =
delete;
321 Listener& operator=(Listener&) =
delete;
324 Listener(
const KeyProvider& kp,
325 const std::function<
void(
ObservedEvt& ev)>& action)
326 : m_obs_evt(wxNewEventType()) {
331 Listener(
const KeyProvider& kp,
const std::function<
void()>& action)
338 void Init(
const KeyProvider& kp,
339 const std::function<
void(
ObservedEvt& ev)>& action) {
341 const wxEventTypeTag<ObservedEvt> EvtObs(wxNewEventType());
343 m_listener.Listen(kp,
this, EvtObs);
344 Bind(EvtObs, action);
350 const wxEventTypeTag<ObservedEvt> m_obs_evt;
355std::shared_ptr<const T> UnpackEvtPointer(
const ObservedEvt& ev) {
356 return std::static_pointer_cast<const T>(ev.
GetSharedPtr());
Manages listening to an Observable instance.
ObservableListener(ObservableListener &&other) noexcept
std::move() support.
ObservableListener(const obs::KeyProvider &kp, wxEvtHandler *l, wxEventType e)
Construct a listening object.
ObservableListener(std::string k, wxEvtHandler *l, wxEventType e)
Construct a listening object.
ObservableListener & operator=(ObservableListener &)=delete
A listener is non copyable, can only be transferred using std::move().
void Listen(const obs::KeyProvider &kp, wxEvtHandler *l, wxEventType evt)
Set object to send wxEventType ev to listeners on changes in key.
ObservableListener(const ObservableListener &other)=delete
A listener is non copyable, can only be transferred using std::move().
ObservableListener & operator=(ObservableListener &&other) noexcept
std::move() support.
ObservableListener()
Default constructor, does not listen to anything.
Custom event class for OpenCPN's notification system.
std::shared_ptr< const void > GetSharedPtr() const
Gets the event's payload data.
Interface implemented by classes which listens.
virtual std::string GetKey() const =0
Return string used by Notify() and Listen().
The observable notify/listen basic nuts and bolts.
const std::string key
The key used to create and clone.
Observable(const KeyProvider &kp)
Initiate an object which can apply Notify() and Listen() to given key.
virtual void Notify()
Notify all listeners to configured key about variable change.
virtual void Notify(const std::shared_ptr< const void > &p)
Notify all listeners to configured key about variable change.
void Notify(const std::string &s, void *client_data)
Notify all listeners: send them a 'type' ObservedEvt message as defined by Listen() with optional dat...
Observable(const std::string &_key)
Initiate an object which can apply Notify() and Listen() to given key.
std::string GetKey() const override
Return the key given to the constructor.
bool Unlisten(wxEvtHandler *listener, wxEventType ev)
Remove window listening to ev from list of listeners.
wxCommandEvt subclass which can carry also a shared_ptr<void>
std::string PtrKey(const void *ptr)
Return address as printable string.