Skip to content

Commit

Permalink
[#5] Base config class declaration
Browse files Browse the repository at this point in the history
Also const modifiers for some methods
  • Loading branch information
kmicki committed Jun 8, 2022
1 parent 5e81f23 commit efafd39
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
30 changes: 28 additions & 2 deletions inc/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#define _KMICKI_CONFIG_CONFIG_H_

#include <string>
#include <vector>
#include <memory>

namespace kmicki::config
{
struct ConfigItemBase
{
std::string Name;
virtual bool Update(std::string const& value,std::string & message) = 0;
virtual std::string ValToString() = 0;
virtual std::string ValToString() const = 0;
};

template<class T>
Expand All @@ -19,7 +21,31 @@ namespace kmicki::config
ConfigItem(std::string const& name, T const& val);
T Val; //value
bool Update(std::string const& value,std::string & message) override;
std::string ValToString() override;
std::string ValToString() const override;
};

class Config
{
public:
Config() = delete;
Config(std::vector<std::unique_ptr<ConfigItemBase>> & _configData);
std::vector<std::unique_ptr<ConfigItemBase>> & ConfigData();

// called after configData vector is updated externally
virtual void Load() = 0;

// ensure configData vector has up-to-date values
// values in configData can also be changed without this method being called
virtual void Save() = 0;

protected:
std::vector<std::unique_ptr<ConfigItemBase>> & configData;

template<class T>
void SetValue(std::string const& name, T const& value);

template<class T>
T const& GetValue(std::string const& name) const;
};
}

Expand Down
4 changes: 2 additions & 2 deletions inc/config/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace kmicki::config
ConfigFile() = delete;
ConfigFile(std::string const& _filePath);

bool LoadConfig(std::vector<std::unique_ptr<ConfigItemBase>> & configuration);
bool SaveConfig(std::vector<std::unique_ptr<ConfigItemBase>> const& configuration, bool pretty = true);
bool LoadConfig(std::vector<std::unique_ptr<ConfigItemBase>> & configuration) const;
bool SaveConfig(std::vector<std::unique_ptr<ConfigItemBase>> const& configuration, bool pretty = true) const;

private:
static const char cSeparator;
Expand Down
15 changes: 12 additions & 3 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,33 @@ namespace kmicki::config
}

template<>
std::string ConfigItem<std::string>::ValToString()
std::string ConfigItem<std::string>::ValToString() const
{
return Val;
}

template<>
std::string ConfigItem<int>::ValToString()
std::string ConfigItem<int>::ValToString() const
{
return std::to_string(Val);
}

template<>
std::string ConfigItem<bool>::ValToString()
std::string ConfigItem<bool>::ValToString() const
{
return Val?"true":"false";
}

template struct ConfigItem<std::string>;
template struct ConfigItem<int>;
template struct ConfigItem<bool>;

Config::Config(std::vector<std::unique_ptr<ConfigItemBase>> & _configData)
: configData(_configData)
{}

std::vector<std::unique_ptr<ConfigItemBase>> & Config::ConfigData()
{
return configData;
}
}
4 changes: 2 additions & 2 deletions src/config/configfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace kmicki::config

const char ConfigFile::cSeparator = ':';

bool ConfigFile::LoadConfig(std::vector<std::unique_ptr<ConfigItemBase>> & configuration)
bool ConfigFile::LoadConfig(std::vector<std::unique_ptr<ConfigItemBase>> & configuration) const
{
static const int cBufLen = 500;

Expand Down Expand Up @@ -95,7 +95,7 @@ namespace kmicki::config
return true;
}

bool ConfigFile::SaveConfig(std::vector<std::unique_ptr<ConfigItemBase>> const& configuration,bool pretty)
bool ConfigFile::SaveConfig(std::vector<std::unique_ptr<ConfigItemBase>> const& configuration,bool pretty) const
{
{ LogF(LogLevelTrace) << "ConfigFile::SaveConfig: Saving configuration to file: " << filePath; }
std::ofstream file(filePath,std::ios_base::trunc);
Expand Down

0 comments on commit efafd39

Please sign in to comment.