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

Wrapper for configuration variables which lives in a wxBaseConfig object. More...

#include <observable_confvar.h>

Inheritance diagram for ConfigVar< T >:
Observable KeyProvider

Public Member Functions

 ConfigVar (const std::string &section_, const std::string &key_, wxConfigBase *cb)
 Create a ConfigVar given section, key and a configuration. More...
 
void Set (const T &arg)
 Set value to given argument and notify listeners. More...
 
const T Get (const T &default_val)
 Get current 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 = std::string>
class ConfigVar< T >

Wrapper for configuration variables which lives in a wxBaseConfig object.

Supports int, bool, double, std::string and wxString. Besides basic set()/get() also provides notification events when value changes.

Client usage when reading, setting a value and notifying listeners:

ConfigVar<bool> expert("/PlugIns", "CatalogExpert", &g_pConfig);
bool old_value = expert.Get(false);
expert.Set(false);

Client usage, listening to value changes.

class Foo: public wxEventHandler {
public:
  Foo(...) {
    ConfigVar<bool> expert("/PlugIns", "CatalogExpert", &g_pConfig);

    // expert sends a wxCommandEvent of type EVT_FOO to this on change:
    wxDEFINE_EVENT(EVT_FOO, wxCommandEvent);
    expert_listener.Listen(expert, this, EVT_FOO);

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

Definition at line 69 of file observable_confvar.h.

Constructor & Destructor Documentation

◆ ConfigVar()

template<typename T >
ConfigVar< T >::ConfigVar ( const std::string &  section_,
const std::string &  key_,
wxConfigBase *  cb 
)

Create a ConfigVar given section, key and a configuration.

Definition at line 47 of file observable_confvar.cpp.

Member Function Documentation

◆ Get()

template<typename T >
const T ConfigVar< T >::Get ( const T &  default_val)

Get current value.

Returns
value from configuration if it exists, else default_val

Definition at line 55 of file observable_confvar.cpp.

◆ Set()

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

Set value to given argument and notify listeners.

Definition at line 66 of file observable_confvar.cpp.


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