wex  v21.04.0
Public Types | Public Member Functions | Static Public Member Functions | List of all members
wex::config Class Reference

Offers a configuration using key value pairs with defaults. More...

#include <wex/config.h>

Public Types

typedef std::vector< std::tuple< std::string, std::list< std::string >, int > > statusbar_t
 Type to hold statusbar panes setup as a vector of tuples:
 

Public Member Functions

 config (const std::string &item=std::string())
 other methods More...
 
 config (const std::string &parent, const std::string &child)
 Constructor for one child item (calling child_start not necessary). More...
 
 ~config ()
 Destructor, calls child_end.
 
bool change_file (const std::string &file)
 Saves changes to current config file, and sets and uses new file as config file.
 
bool child_end ()
 Ends setting child values for this item, deletes a possible local store for a child item. More...
 
bool child_start ()
 Starts setting child values for this item. More...
 
size_t children () const
 Returns number of children for this item.
 
bool empty () const
 Returns true if the (string) item is empty.
 
void erase () const
 Deletes the item.
 
bool exists () const
 Returns true if the item exists.
 
const std::string get (const std::string &def=std::string()) const
 Getter methods. More...
 
const std::string get (const char *def) const
 Returns text config value for item.
 
bool get (bool def) const
 Returns boolean config value for item.
 
long get (long def) const
 Returns long config value for item.
 
int get (int def) const
 Returns long config value for item.
 
float get (float def) const
 Returns float config value for item.
 
double get (double def) const
 Returns double config value for item.
 
wxColour get (const wxColour &def) const
 Returns colour config value for item.
 
wxFont get (const wxFont &def) const
 Returns font config value for item.
 
const std::list< std::string > get (const std::list< std::string > &def) const
 Returns a list with strings for item.
 
const std::vector< int > get (const std::vector< int > &def) const
 Returns a vector with ints for item.
 
const statusbar_t get (const statusbar_t &def) const
 Returns a statusbar_t for item.
 
const std::string get_firstof () const
 Returns first of a list of strings from item.
 
bool is_child () const
 Returns true if this item is a child.
 
auto & item () const
 Item access / nested values. More...
 
configitem (const std::string &item)
 Sets the item, and returns config.
 
void set (const std::string &v=std::string())
 Sets value from a string.
 
void set (const char *v)
 Sets value from a char array.
 
void set (bool v)
 Sets value from a bool.
 
void set (long v)
 Sets value from a long.
 
void set (int v)
 Sets value from a int.
 
void set (float v)
 Sets value from a float.
 
void set (double v)
 Sets value from a double.
 
void set (const wxColour &v)
 Sets value from a colour.
 
void set (const wxFont &v)
 Sets value from a font.
 
void set (const std::list< std::string > &v)
 Sets value from a list with strings.
 
void set (const std::vector< int > &v)
 Sets value from a vector with int.
 
void set (const statusbar_t &r)
 Sets value from a statusbar_t.
 
const std::string set_firstof (const std::string &v, size_t max=75)
 Sets first of a list of strings in config key, deletes it if present at other places. More...
 
bool toggle (bool def=false)
 If this item is a bool, toggles value and returns new value.
 

Static Public Member Functions

static const std::string dir ()
 static interface More...
 
static void discard ()
 Do not save current config file on exit.
 
static const std::string file ()
 Returns the current config file.
 
static void on_exit ()
 Saves changes to store (unless discard was invoked), and frees objects. More...
 
static void on_init ()
 Initializes the store, and reads previous file. More...
 
static void read ()
 Reads current config file.
 
static void save ()
 Saves current config file.
 
static void set_file (const std::string &file)
 Sets the config file to use. More...
 
static size_t size ()
 Returns number of top level entries.
 

Detailed Description

Offers a configuration using key value pairs with defaults.

Constructor & Destructor Documentation

◆ config() [1/2]

wex::config::config ( const std::string &  item = std::string())

other methods

Default constructor. Optionally provide the item (key). You can also create a hierarchy using the parent dot child expression:

wex::config("x.y.z").set(8);
const auto j(config("x.y.z").get(9));
Offers a configuration using key value pairs with defaults.
Definition: config.h:24
const std::string get(const std::string &def=std::string()) const
Getter methods.
config(const std::string &item=std::string())
other methods
void set(const std::string &v=std::string())
Sets value from a string.

i will be 8. If you are using children, you can retrieve the value using parent dot child expression:

wex::config c("x");
c.child_start();
c.item("u").set(1);
c.item("v").set(2);
c.item("w").set(3);
c.child_end();
const auto i(c.get("x.u", 9));

i will be 1.

◆ config() [2/2]

wex::config::config ( const std::string &  parent,
const std::string &  child 
)

Constructor for one child item (calling child_start not necessary).

wex::config c("y", "u");
c.item("u").set(1);
c.item("v").set(2);
c.item("w").set(3);
const auto i(c.get("y.u", 9));

Member Function Documentation

◆ child_end()

bool wex::config::child_end ( )

Ends setting child values for this item, deletes a possible local store for a child item.

Returns false if not yet started.

◆ child_start()

bool wex::config::child_start ( )

Starts setting child values for this item.

Returns false if already started.

◆ dir()

static const std::string wex::config::dir ( )
static

static interface

Returns the config dir for user data files.

◆ get()

const std::string wex::config::get ( const std::string &  def = std::string()) const

Getter methods.

Returns text config value for item.

◆ item()

auto& wex::config::item ( ) const
inline

Item access / nested values.

Returns the item.

◆ on_exit()

static void wex::config::on_exit ( )
static

Saves changes to store (unless discard was invoked), and frees objects.

This is done in app::OnExit.

◆ on_init()

static void wex::config::on_init ( )
static

Initializes the store, and reads previous file.

This should be done before first use of config, and is done in app::OnInit.

◆ set_file()

static void wex::config::set_file ( const std::string &  file)
static

Sets the config file to use.

If you do no use this, the default config file is used.

◆ set_firstof()

const std::string wex::config::set_firstof ( const std::string &  v,
size_t  max = 75 
)

Sets first of a list of strings in config key, deletes it if present at other places.

If the list size would be greater than max, the last element is deleted. And returns the value.