summaryrefslogtreecommitdiff
path: root/configpp.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'configpp.hpp')
-rw-r--r--configpp.hpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/configpp.hpp b/configpp.hpp
new file mode 100644
index 0000000..90038af
--- /dev/null
+++ b/configpp.hpp
@@ -0,0 +1,94 @@
+#pragma once
+
+#include <boost/property_tree/ptree.hpp>
+#include <boost/hana.hpp>
+
+#include <memory>
+#include <type_traits>
+
+struct X {
+ int a;
+ int b;
+};
+
+BOOST_HANA_ADAPT_STRUCT(X, a, b);
+
+struct X2 {
+ int a;
+ int b;
+};
+
+namespace configpp {
+
+ struct ConfigStructure{};
+
+ class Config {
+ Config(std::string subdir, std::string filename = "config.json");
+
+ boost::property_tree::ptree config;
+ std::string subdir;
+ std::string filename;
+
+ static std::unique_ptr<Config> config_ptr;
+
+ void set(std::string const& key, std::string const& value, boost::property_tree::ptree& base) const {
+ base.put(key, value);
+ }
+
+ public:
+
+ Config() = delete;
+ Config(Config const&) = delete;
+ Config(Config&&) = delete;
+ Config& operator=(Config const&) = delete;
+ Config& operator=(Config&&) = delete;
+
+ ~Config();
+
+ static Config& get(std::string subdir, std::string filename = "config.json") {
+ if(!config_ptr) {
+ config_ptr.reset(new Config(subdir, filename));
+ }
+ return *config_ptr;
+ }
+
+ void set(std::string const& key, std::string const& value) {
+ set(key, value, config);
+ }
+
+ template <typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
+ void set(std::string const & key, T const& value) {
+ std::ostringstream oss;
+ oss << value;
+
+ set(key, oss.str());
+ }
+
+ template <typename T, typename std::enable_if<boost::hana::Struct<T>::value>::type* = nullptr>
+ void set(std::string const& key, T const& value) {
+ auto top_element = config.get_child_optional(key);
+
+ if(!top_element) {
+ boost::property_tree::ptree array;
+ config.add_child(key, array);
+ top_element = config.get_child(key);
+ }
+
+ boost::hana::for_each(value, boost::hana::fuse([&top_element, this](auto&& key, auto&& value) {
+ std::ostringstream oss;
+ oss << value;
+
+ set(boost::hana::to<const char*>(key), oss.str(), *top_element);
+ }));
+ }
+
+ template <typename T>
+ void get(std::string key, T const& value) {
+
+ }
+
+ };
+
+} //namespace configpp
+
+//configpp::Config& config = configpp::Config::get({});