ConfigUtility now supports recursion; is a Singleton

If you have "config.next" set, the config system will load that as another
config file. Higher config files have a higher precedence over subfiles
when conflicting keys are encountered.

* Added singleton.hpp, containing Singleton<T>
* ConfigUtility now inherits from Singleton
* Tweaked timer.*pp layouts
This commit is contained in:
Kayne Ruse
2014-08-03 23:14:56 +10:00
parent 10c89970cc
commit c830fa0537
5 changed files with 135 additions and 28 deletions
+34 -10
View File
@@ -26,11 +26,22 @@
#include <stdexcept>
void ConfigUtility::Load(std::string fname) {
//TODO: recursive rerouting?
//clear the stored configuration
configMap.clear();
//pass to the recursive method
configMap = Read(fname);
}
ConfigUtility::table_t ConfigUtility::Read(std::string fname) {
//read in and return this file's data
table_t retTable;
std::ifstream is(fname);
if (!is.is_open()) {
throw(std::runtime_error("Failed to open config file"));
std::string msg;
msg += "Failed to open a config file: ";
msg += fname;
throw(std::runtime_error(msg));
}
std::string key, val;
@@ -71,35 +82,48 @@ void ConfigUtility::Load(std::string fname) {
}
//save the pair
table[key] = val;
retTable[key] = val;
}
is.close();
//load in any subordinate config files
//TODO: Possibility of nesting config levels?
if (retTable.find("config.next") != retTable.end()) {
table_t subTable = Read(retTable["config.next"]);
retTable.insert(subTable.begin(), subTable.end());
}
return retTable;
}
//-------------------------
//Convert to a type
//-------------------------
std::string& ConfigUtility::String(std::string s) {
return table[s];
return configMap[s];
}
int ConfigUtility::Integer(std::string s) {
std::map<std::string, std::string>::iterator it = table.find(s);
if (it == table.end()) {
table_t::iterator it = configMap.find(s);
if (it == configMap.end()) {
return 0;
}
return atoi(it->second.c_str());
}
double ConfigUtility::Double(std::string s) {
std::map<std::string, std::string>::iterator it = table.find(s);
if (it == table.end()) {
table_t::iterator it = configMap.find(s);
if (it == configMap.end()) {
return 0.0;
}
return atof(it->second.c_str());
}
bool ConfigUtility::Boolean(std::string s) {
std::map<std::string, std::string>::iterator it = table.find(s);
if (it == table.end()) {
table_t::iterator it = configMap.find(s);
if (it == configMap.end()) {
return false;
}
return it->second == "true";
+14 -10
View File
@@ -22,14 +22,13 @@
#ifndef CONFIGUTILITY_HPP_
#define CONFIGUTILITY_HPP_
#include "singleton.hpp"
#include <map>
#include <string>
class ConfigUtility {
class ConfigUtility : public Singleton<ConfigUtility> {
public:
ConfigUtility() = default;
ConfigUtility(std::string s) { Load(s); }
void Load(std::string fname);
//convert to a type
@@ -39,16 +38,21 @@ public:
bool Boolean(std::string);
//shorthand
inline std::string& operator[](std::string s) { return String(s); }
inline std::string& operator[](std::string s) { return configMap[s]; }
inline int Int(std::string s) { return Integer(s); }
inline bool Bool(std::string s) { return Boolean(s); }
//OO breaker
std::map<std::string, std::string>* GetMap() {
return &table;
}
private:
std::map<std::string, std::string> table;
typedef std::map<std::string, std::string> table_t;
friend Singleton<ConfigUtility>;
ConfigUtility() = default;
~ConfigUtility() = default;
table_t Read(std::string fname);
table_t configMap;
};
#endif
+63
View File
@@ -0,0 +1,63 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef SINGLETON_HPP_
#define SINGLETON_HPP_
#include <stdexcept>
template<typename T>
class Singleton {
public:
static T& GetSingleton() {
if (!ptr) {
throw(std::logic_error("This singleton has not been created"));
}
return *ptr;
}
static void Create() {
if (ptr) {
throw(std::logic_error("This singleton has already been created"));
}
ptr = new T();
}
static void Delete() {
if (!ptr) {
throw(std::logic_error("A non-existant singleton cannot be deleted"));
}
delete ptr;
ptr = nullptr;
}
protected:
Singleton() = default;
Singleton(Singleton const&) = default;
Singleton(Singleton&&) = default;
~Singleton() = default;
private:
static T* ptr;
};
template<typename T>
T* Singleton<T>::ptr = nullptr;
#endif