wxWidgets Observable Library
observable_evtvar.h
1 /*************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: General observable implementation with several specializations.
5  *
6  * Copyright (C) 2022 Alec Leamas
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  **************************************************************************/
23 
24 #ifndef _OBSERVABLE_EVTVAR_H
25 #define _OBSERVABLE_EVTVAR_H
26 
27 #include <atomic>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <wx/event.h>
33 
34 #include "observable.h"
35 
69 class EventVar : public Observable {
70 public:
71  EventVar() : Observable(Autokey()) {}
72 
74  const void Notify() { Observable::Notify("", 0); }
75 
77  const void Notify(void* data) { Observable::Notify("", data); }
78 
80  const void Notify(const std::string& s) { Observable::Notify(s, 0); }
81 
83  const void Notify(int n, const std::string& s) { Observable::Notify(0, s, n, 0); }
88  const void Notify(std::shared_ptr<void> p, const std::string& s, int n = 0) {
89  Observable::Notify(p, s, n, 0);
90  }
91 
92 private:
93  std::string Autokey() {
94  static std::atomic<unsigned long> last_ix(0);
95  return std::string("!@%/+") + std::to_string(last_ix++);
96  }
97 };
98 
99 #endif // _OBSERVABLE_EVTVAR_H
Generic event handling between between two parties like MVC Model and Controller using a shared Event...
const void Notify(int n, const std::string &s)
Notify all listeners about variable change with a string and an int.
const void Notify(std::shared_ptr< void > p, const std::string &s, int n=0)
Notify all listeners about variable change with shared_ptr, a string and an optional number.
const void Notify()
Notify all listeners, no data supplied.
const void Notify(const std::string &s)
Notify all listeners about variable change with a string.
const void Notify(void *data)
Notify all listeners about variable change with ClientData.
The observable notify/listen basic nuts and bolts.
Definition: observable.h:83
Observable(const std::string &_key)
Create an instance listening to given key.
Definition: observable.h:88
virtual const void Notify()
Notify all listeners about variable change.
Definition: observable.cpp:94