OpenCPN Partial API docs
Loading...
Searching...
No Matches
GlobalVar< T > Class Template Reference

Wrapper for global variable, supports notification events when value changes. More...

#include <observable_globvar.h>

Inheritance diagram for GlobalVar< T >:
Observable KeyProvider

Public Member Functions

 GlobalVar (T *ptr)
 Create a GlobalVar using given pointer (variable address) as key. More...
 
void Set (const T &arg)
 Set variable value and notify all listeners. More...
 
const T Get ()
 Return current variable value. More...
 
- Public Member Functions inherited from Observable
 Observable (const std::string &_key)
 Create an instance listening to given key. More...
 
 Observable (const KeyProvider &kp)
 Create an instance listening to key provided by kp.GetKey(). More...
 
virtual const void Notify ()
 Notify all listeners about variable change. More...
 
const void Notify (std::shared_ptr< const void > p)
 Notify all listeners about variable change with a shared_ptr payload. More...
 
bool Unlisten (wxEvtHandler *listener, wxEventType ev)
 Remove window listening to ev from list of listeners. More...
 
std::string GetKey () const
 Retrieve the actual listening key: More...
 
virtual std::string GetKey () const =0
 Return key used to listen and notify. More...
 

Additional Inherited Members

- Public Attributes inherited from Observable
const std::string key
 The key used to create and clone. More...
 
- Protected Member Functions inherited from Observable
const void Notify (std::shared_ptr< const void > ptr, const std::string &s, int num, void *client_data)
 Notify all listeners: send them a 'type' ObservedEvt message as defined by listen() with optional data available using GetString(), getInt(), GetSharedPtr() and/or GetClientData(). More...
 
const void Notify (const std::string &s, void *client_data)
 Notify all listeners: send them a 'type' ObservedEvt message as defined by listen() with optional data available using GetString() and/or GetClientData(). More...
 

Detailed Description

template<typename T>
class GlobalVar< T >

Wrapper for global variable, supports notification events when value changes.

Client usage, writing a value + notifying listeners:

GlobalVar<wxString> compat_os(&g_compatOS);
compat_os.Set("ubuntu-gtk3-x86_64");

Client usage, modifying a value + notifying listeners:

GlobalVar<wxString> plugin_array_var(&plugin_array);
plugin_array.Add(new_pic);
plugin_array_var.Notify();

Client usage, listening to value changes:

class Foo: public wxEvtHandler {
public:
  Foo(...) {
    GlobalVar<wxString> compat_os(&g_compatOS);

    // compat_os sends a wxCommandEvent type EVT_FOO to this on change:
    wxDEFINE_EVENT(EVT_FOO, wxCommandEvent);
    compat_os_listener.Listen(compat_os, this, EVT_FOO);)

    // Handle  EVT_FOO as any event when it arrives, for example:
    Bind(EVT_FOO,
         [](wxCommandEvent&) { cout << "value has changed"; });
    ...
  }
private:
  ObservableListener compat_os_listener;
  ...
}

Definition at line 72 of file observable_globvar.h.

Constructor & Destructor Documentation

◆ GlobalVar()

template<typename T >
GlobalVar< T >::GlobalVar ( T *  ptr)
inline

Create a GlobalVar using given pointer (variable address) as key.

Definition at line 75 of file observable_globvar.h.

Member Function Documentation

◆ Get()

template<typename T >
const T GlobalVar< T >::Get ( )
inline

Return current variable value.

Definition at line 84 of file observable_globvar.h.

◆ Set()

template<typename T >
void GlobalVar< T >::Set ( const T &  arg)
inline

Set variable value and notify all listeners.

Definition at line 78 of file observable_globvar.h.


The documentation for this class was generated from the following file: