summaryrefslogtreecommitdiff
path: root/louloulibs
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2016-06-14 03:02:36 +0200
committerlouiz’ <louiz@louiz.org>2016-06-14 03:02:36 +0200
commit46ff73662cc94220c5ee962b591c8ee327de6f85 (patch)
tree482596a4c341477eee53456aac5e7b4746733630 /louloulibs
parent550ab46f02511614d7ce7b46a4b4f63beae1aebc (diff)
downloadbiboumi-46ff73662cc94220c5ee962b591c8ee327de6f85.tar.gz
biboumi-46ff73662cc94220c5ee962b591c8ee327de6f85.tar.bz2
biboumi-46ff73662cc94220c5ee962b591c8ee327de6f85.tar.xz
biboumi-46ff73662cc94220c5ee962b591c8ee327de6f85.zip
Clean the Config module, use static things instead of a stupid singleton
Diffstat (limited to 'louloulibs')
-rw-r--r--louloulibs/config/config.cpp66
-rw-r--r--louloulibs/config/config.hpp28
-rw-r--r--louloulibs/utils/reload.cpp4
3 files changed, 36 insertions, 62 deletions
diff --git a/louloulibs/config/config.cpp b/louloulibs/config/config.cpp
index c785632..967f581 100644
--- a/louloulibs/config/config.cpp
+++ b/louloulibs/config/config.cpp
@@ -6,22 +6,21 @@
#include <stdlib.h>
std::string Config::filename{};
-bool Config::file_must_exist = false;
+std::map<std::string, std::string> Config::values{};
+std::vector<t_config_changed_callback> Config::callbacks{};
std::string Config::get(const std::string& option, const std::string& def)
{
- Config* self = Config::instance().get();
- auto it = self->values.find(option);
+ auto it = Config::values.find(option);
- if (it == self->values.end())
+ if (it == Config::values.end())
return def;
return it->second;
}
int Config::get_int(const std::string& option, const int& def)
{
- Config* self = Config::instance().get();
- std::string res = self->get(option, "");
+ std::string res = Config::get(option, "");
if (!res.empty())
return atoi(res.c_str());
else
@@ -30,65 +29,48 @@ int Config::get_int(const std::string& option, const int& def)
void Config::set(const std::string& option, const std::string& value, bool save)
{
- Config* self = Config::instance().get();
- self->values[option] = value;
+ Config::values[option] = value;
if (save)
{
- self->save_to_file();
- self->trigger_configuration_change();
+ Config::save_to_file();
+ Config::trigger_configuration_change();
}
}
void Config::connect(t_config_changed_callback callback)
{
- Config* self = Config::instance().get();
- self->callbacks.push_back(callback);
+ Config::callbacks.push_back(callback);
}
-void Config::close()
+void Config::clear()
{
- Config* self = Config::instance().get();
- self->values.clear();
- Config::instance().reset();
+ Config::values.clear();
}
/**
* Private methods
*/
-
void Config::trigger_configuration_change()
{
std::vector<t_config_changed_callback>::iterator it;
- for (it = this->callbacks.begin(); it < this->callbacks.end(); ++it)
+ for (it = Config::callbacks.begin(); it < Config::callbacks.end(); ++it)
(*it)();
}
-std::unique_ptr<Config>& Config::instance()
+bool Config::read_conf(const std::string& name)
{
- static std::unique_ptr<Config> instance;
+ if (!name.empty())
+ Config::filename = name;
- if (!instance)
- {
- instance = std::make_unique<Config>();
- instance->read_conf();
- }
- return instance;
-}
-
-bool Config::read_conf()
-{
- std::ifstream file;
- file.open(filename.data());
+ std::ifstream file(Config::filename.data());
if (!file.is_open())
{
- if (Config::file_must_exist)
- {
- perror(("Error while opening file " + filename + " for reading.").c_str());
- file.exceptions(std::ifstream::failbit);
- }
+ perror(("Error while opening file " + filename + " for reading.").c_str());
return false;
}
+ Config::clear();
+
std::string line;
size_t pos;
std::string option;
@@ -103,20 +85,18 @@ bool Config::read_conf()
continue ;
option = line.substr(0, pos);
value = line.substr(pos+1);
- this->values[option] = value;
+ Config::values[option] = value;
}
- return true;
}
-void Config::save_to_file() const
+void Config::save_to_file()
{
- std::ofstream file(this->filename.data());
+ std::ofstream file(Config::filename.data());
if (file.fail())
{
std::cerr << "Could not save config file." << std::endl;
return ;
}
- for (auto& it: this->values)
+ for (const auto& it: Config::values)
file << it.first << "=" << it.second << '\n';
- file.close();
}
diff --git a/louloulibs/config/config.hpp b/louloulibs/config/config.hpp
index 72620c0..b46768e 100644
--- a/louloulibs/config/config.hpp
+++ b/louloulibs/config/config.hpp
@@ -60,38 +60,34 @@ public:
* Destroy the instance, forcing it to be recreated (with potentially
* different parameters) the next time it’s needed.
*/
- static void close();
+ static void clear();
/**
- * Set the value of the filename to use, before calling any method.
+ * Read the configuration file at the given path.
*/
- static std::string filename;
+ static bool read_conf(const std::string& name="");
/**
- * Set to true if you want an exception to be raised if the file does not
- * exist when reading it.
+ * Get the filename
*/
- static bool file_must_exist;
+ static const std::string& get_filename()
+ { return Config::filename; }
private:
/**
- * Get the singleton instance
- */
- static std::unique_ptr<Config>& instance();
- /**
- * Read the configuration file at the given path.
+ * Set the value of the filename to use, before calling any method.
*/
- bool read_conf();
+ static std::string filename;
/**
* Write all the config values into the configuration file
*/
- void save_to_file() const;
+ static void save_to_file();
/**
* Call all the callbacks previously registered using connect().
* This is used to notify any class that a configuration change occured.
*/
- void trigger_configuration_change();
+ static void trigger_configuration_change();
- std::map<std::string, std::string> values;
- std::vector<t_config_changed_callback> callbacks;
+ static std::map<std::string, std::string> values;
+ static std::vector<t_config_changed_callback> callbacks;
};
diff --git a/louloulibs/utils/reload.cpp b/louloulibs/utils/reload.cpp
index 6600c75..bddfd86 100644
--- a/louloulibs/utils/reload.cpp
+++ b/louloulibs/utils/reload.cpp
@@ -3,9 +3,7 @@
void reload_process()
{
- // Closing the config will just force it to be reopened the next time
- // a configuration option is needed
- Config::close();
+ Config::read_conf();
// Destroy the logger instance, to be recreated the next time a log
// line needs to be written
Logger::instance().reset();