galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
galaxy::core::Config Class Referencefinal

Allows you to read, write and manipulate JSON config files. More...

#include <Config.hpp>

+ Collaboration diagram for galaxy::core::Config:

Public Member Functions

 Config () noexcept
 Constructor.
 
 Config (std::string_view file) noexcept
 Argument constructor.
 
 ~Config () noexcept
 Destructor.
 
void load (std::string_view file)
 Checks if config exists and flags if a config needs to be created.
 
void save ()
 Save the config file. Must be opened first.
 
template<meta::standard_type Value>
void set (const std::string &key, const Value &value) noexcept
 Set a new key-value pair for the config.
 
template<meta::standard_type Value>
void set (const std::string &key, const Value &value, const std::string &section, const std::string &delim=".")
 Sets a new key-value pair in a section. Can use a delimiter to seperate sections.
 
template<meta::standard_type Value>
void restore (const std::string &key, const Value &value) noexcept
 Sets a new setting, only if key or value is missing.
 
template<meta::standard_type Value>
void restore (const std::string &key, const Value &value, const std::string &section, const std::string &delim=".")
 Sets a new setting, only if key or value is missing.
 
bool has (const std::string &key) noexcept
 Check if the config file actually has a value.
 
bool has (const std::string &key, const std::string &section, const std::string &delim=".")
 Check if the config file actually has a value.
 
template<meta::standard_type Value>
Value get (const std::string &key)
 Retrieve a root config value.
 
template<meta::standard_type Value>
Value get (const std::string &key, const std::string &section, const std::string &delim=".")
 Retrieve a config value in a section.
 
bool empty () const
 Is the config file blank.
 
void raw (const nlohmann::json &json) noexcept
 Set as external json object.
 
const nlohmann::json & raw () const noexcept
 Get raw json object.
 

Private Member Functions

 Config (const Config &)=delete
 Copy constructor.
 
 Config (Config &&)=delete
 Move constructor.
 
Configoperator= (const Config &)=delete
 Copy assignment operator.
 
Configoperator= (Config &&)=delete
 Move assignment operator.
 

Private Attributes

bool m_loaded
 Config loaded flag.
 
nlohmann::json m_config
 Config file as JSON.
 
std::string m_path
 Filepath.
 

Detailed Description

Allows you to read, write and manipulate JSON config files.

Definition at line 24 of file Config.hpp.

Constructor & Destructor Documentation

◆ Config() [1/4]

galaxy::core::Config::Config ( )
noexcept

Constructor.

Definition at line 16 of file Config.cpp.

◆ Config() [2/4]

galaxy::core::Config::Config ( std::string_view file)
noexcept

Argument constructor.

Calls load().

Parameters
filePath to config to open / use (does not matter if does not exist). This is relative to the app.

Definition at line 22 of file Config.cpp.

◆ ~Config()

galaxy::core::Config::~Config ( )
noexcept

Destructor.

Definition at line 30 of file Config.cpp.

◆ Config() [3/4]

galaxy::core::Config::Config ( const Config & )
privatedelete

Copy constructor.

◆ Config() [4/4]

galaxy::core::Config::Config ( Config && )
privatedelete

Move constructor.

Member Function Documentation

◆ load()

void galaxy::core::Config::load ( std::string_view file)

Checks if config exists and flags if a config needs to be created.

Also sets up filepath and json.

Parameters
filePath to config to open / use (does not matter if does not exist). This is relative to the app.

Definition at line 36 of file Config.cpp.

◆ save()

void galaxy::core::Config::save ( )

Save the config file. Must be opened first.

Definition at line 75 of file Config.cpp.

+ Here is the caller graph for this function:

◆ set() [1/2]

template<meta::standard_type Value>
void galaxy::core::Config::set ( const std::string & key,
const Value & value )
inlinenoexcept

Set a new key-value pair for the config.

Will create if not exist, otherwise update.

Template Parameters
ValueStandard JSON type to use.
Parameters
keyName of the variable.
valueThe variable value to set.

Definition at line 224 of file Config.hpp.

+ Here is the caller graph for this function:

◆ set() [2/2]

template<meta::standard_type Value>
void galaxy::core::Config::set ( const std::string & key,
const Value & value,
const std::string & section,
const std::string & delim = "." )
inline

Sets a new key-value pair in a section. Can use a delimiter to seperate sections.

Will create if not exist, otherwise update.

Template Parameters
ValueStandard JSON type to use.
Parameters
keyName of the variable.
valueThe variable value to set.
sectionSection of the config file to use. Can use a delimiter to seperate sections.
delimDelimiter to seperate sections with. Optional.

Definition at line 237 of file Config.hpp.

+ Here is the call graph for this function:

◆ restore() [1/2]

template<meta::standard_type Value>
void galaxy::core::Config::restore ( const std::string & key,
const Value & value )
inlinenoexcept

Sets a new setting, only if key or value is missing.

Template Parameters
ValueStandard JSON type to use.
Parameters
keyName of the variable.
valueThe variable value to set.

Definition at line 271 of file Config.hpp.

+ Here is the caller graph for this function:

◆ restore() [2/2]

template<meta::standard_type Value>
void galaxy::core::Config::restore ( const std::string & key,
const Value & value,
const std::string & section,
const std::string & delim = "." )
inline

Sets a new setting, only if key or value is missing.

Template Parameters
ValueStandard JSON type to use.
Parameters
keyName of the variable.
valueThe variable value to set.
sectionSection of the config file to use. Can use a delimiter to seperate sections.
delimDelimiter to seperate sections with. Optional.

Definition at line 280 of file Config.hpp.

+ Here is the call graph for this function:

◆ has() [1/2]

bool galaxy::core::Config::has ( const std::string & key)
nodiscardnoexcept

Check if the config file actually has a value.

Parameters
keyName of the value to check.
Returns
True if the value exists.

Definition at line 98 of file Config.cpp.

+ Here is the caller graph for this function:

◆ has() [2/2]

bool galaxy::core::Config::has ( const std::string & key,
const std::string & section,
const std::string & delim = "." )
nodiscard

Check if the config file actually has a value.

Parameters
keyKey to lookup value.
sectionDelimiter seperated sections.
delimDelimiter to use. Optional.
Returns
True if the value exists.

Definition at line 108 of file Config.cpp.

+ Here is the call graph for this function:

◆ get() [1/2]

template<meta::standard_type Value>
Value galaxy::core::Config::get ( const std::string & key)
inlinenodiscard

Retrieve a root config value.

Template Parameters
ValueStandard JSON type to use.
Parameters
keyName of the value to retrieve.
Returns
Returns the value retrieved from the key.

Definition at line 289 of file Config.hpp.

+ Here is the caller graph for this function:

◆ get() [2/2]

template<meta::standard_type Value>
Value galaxy::core::Config::get ( const std::string & key,
const std::string & section,
const std::string & delim = "." )
inlinenodiscard

Retrieve a config value in a section.

Template Parameters
ValueStandard JSON type to use.
Parameters
keyKey to lookup value.
sectionDelimiter seperated sections.
delimDelimiter to use. Optional.
Returns
Returns the value retrieved from the key.

Definition at line 311 of file Config.hpp.

+ Here is the call graph for this function:

◆ empty()

bool galaxy::core::Config::empty ( ) const
nodiscard

Is the config file blank.

Returns
True if blank.

Definition at line 144 of file Config.cpp.

+ Here is the caller graph for this function:

◆ raw() [1/2]

void galaxy::core::Config::raw ( const nlohmann::json & json)
noexcept

Set as external json object.

Parameters
jsonNew json config data to set.

Definition at line 154 of file Config.cpp.

◆ raw() [2/2]

const nlohmann::json & galaxy::core::Config::raw ( ) const
nodiscardnoexcept

Get raw json object.

Returns
Const reference to internal json object.

Definition at line 160 of file Config.cpp.

◆ operator=() [1/2]

Config & galaxy::core::Config::operator= ( const Config & )
privatedelete

Copy assignment operator.

◆ operator=() [2/2]

Config & galaxy::core::Config::operator= ( Config && )
privatedelete

Move assignment operator.

Member Data Documentation

◆ m_loaded

bool galaxy::core::Config::m_loaded
private

Config loaded flag.

Definition at line 210 of file Config.hpp.

◆ m_config

nlohmann::json galaxy::core::Config::m_config
private

Config file as JSON.

Definition at line 215 of file Config.hpp.

◆ m_path

std::string galaxy::core::Config::m_path
private

Filepath.

Definition at line 220 of file Config.hpp.


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