wex v24.10.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/core/config.h>

Public Types

typedef wex::strings_t strings_t
 Type for keeping the string values. We use the same as core types.
 
typedef wex::ints_t ints_t
 Type for keeping the int values. We use the same as core types.
 
typedef std::vector< std::tuple< std::string, strings_t, int > > statusbar_t
 Type to hold statusbar panes setup as a vector of tuples:
 

Public Member Functions

 config (const std::string &item=std::string())
 Default constructor.
 
 config (const std::string &parent, const std::string &child)
 Constructor for one child item (calling child_start not necessary).
 
 ~config ()
 Destructor, calls child_end.
 
bool change_path (const wex::path &p)
 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.
 
bool child_start ()
 Starts setting child values for this item.
 
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.
 
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 strings_t get (const strings_t &def) const
 Returns a strings type for item.
 
const ints_t get (const ints_t &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_first_of (const std::string &def=std::string()) 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.
 
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 strings_t &v)
 Sets value from a strings type.
 
void set (const ints_t &v)
 Sets value from a ints type.
 
void set (const statusbar_t &r)
 Sets value from a statusbar_t.
 
const std::string set_first_of (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.
 
bool toggle (bool def=false)
 If this item is a bool, toggles value and returns new value.
 

Static Public Member Functions

static const wex::path dir ()
 Returns the config path for user data files.
 
static void discard ()
 Do not save current config file on exit.
 
static void on_exit ()
 Saves changes to store (unless discard was invoked), and frees objects.
 
static void on_init ()
 Initializes the store, and reads previous file.
 
static const wex::path path ()
 Returns the current config path.
 
static void read ()
 Reads current config file.
 
static void save ()
 Saves current config file.
 
static void set_path (const wex::path &p)
 Sets the config path to use.
 
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())
explicit

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 i(config("x.y.z").get(9));
Offers a configuration using key value pairs with defaults.
Definition config.h:26
const std::string get(const std::string &def=std::string()) const
Getter methods.
config(const std::string &item=std::string())
Default constructor.
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.

◆ 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_first_of()

const std::string wex::config::set_first_of ( 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.

◆ set_path()

static void wex::config::set_path ( const wex::path & p)
static

Sets the config path to use.

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