wxWidgets Observable Library
observable_globvar.h
1 /*************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: global variables listen/notify wrapper.
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_GLOBVAR_H
25 #define _OBSERVABLE_GLOBVAR_H
26 
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include <wx/event.h>
32 
33 #include "observable.h"
34 
71 template <typename T>
72 class GlobalVar : public Observable {
73 public:
75  GlobalVar(T* ptr) : Observable(ptr_key(ptr)), variable(ptr) {}
76 
78  void Set(const T& arg) {
79  *variable = arg;
81  }
82 
84  const T Get() { return *variable; }
85 
86 private:
87  GlobalVar(); // not implemented
88 
89  T* const variable;
90 };
91 
92 #endif // _OBSERVABLE_GLOBVAR_H
Wrapper for global variable, supports notification events when value changes.
void Set(const T &arg)
Set variable value and notify all listeners.
const T Get()
Return current variable value.
GlobalVar(T *ptr)
Create a GlobalVar using given pointer (variable address) as key.
The observable notify/listen basic nuts and bolts.
Definition: observable.h:83
virtual const void Notify()
Notify all listeners about variable change.
Definition: observable.cpp:94