YggdrasilWM  0.1.1
A tiny window manager coded in C++
ConfigHandler.hpp
Go to the documentation of this file.
1 
29 #ifndef YGGDRASILWM_CONFIGHANDLER_HPP
30 #define YGGDRASILWM_CONFIGHANDLER_HPP
31 #include <iostream>
32 #include <string>
33 #include <fstream>
34 #include <unordered_map>
35 #include "json/json.h"
36 #include "ConfigDataBase.hpp"
37 #include <variant>
38 #include <cctype>
39 #include <typeindex>
40 class ConfigData;
41 
42 class ConfigFileHandler;
55 public:
56  ConfigHandler(const ConfigHandler&) = delete;
65  template <typename T>
66  std::shared_ptr<T> getConfigData() {
67  auto it = configMap_.find(std::type_index(typeid(T)));
68  if (it == configMap_.end()) {
69  throw std::runtime_error("ConfigData not found");
70  }
71  return std::static_pointer_cast<T>(it->second);
72  }
79  template <typename T>
80  void addConfigData(std::shared_ptr<T> configData) {
81  static_assert(std::is_base_of<ConfigDataBase, T>::value, "T must be a subclass of ConfigData");
82  configMap_[std::type_index(typeid(T))] = configData;
83  }
89  static void Create(const std::string& configPath);
94  static void Create();
99  static ConfigHandler& GetInstance();
104  static void Destroy();
105 
112  void configInit();
120  static unsigned long colorCodeToULong(const std::string &colorCode);
121 
122 private:
123  std::string configPath_;
125  std::unordered_map<std::type_index, std::shared_ptr<ConfigDataBase>> configMap_;
131  ConfigHandler();
138  explicit ConfigHandler(const std::string& configPath);
139 };
140 #endif //YGGDRASILWM_CONFIGHANDLER_HPP
ConfigDataBase class header. This class is an interface for the ConfigData classes.
Handle file I/O and JSON parsing for the configuration file. If no path is provided,...
Definition: ConfigFileHandler.hpp:40
ConfigHandler class This class is responsible for handling the configuration It can be created with a...
Definition: ConfigHandler.hpp:54
std::string configPath_
Definition: ConfigHandler.hpp:123
std::unordered_map< std::type_index, std::shared_ptr< ConfigDataBase > > configMap_
Definition: ConfigHandler.hpp:125
static unsigned long colorCodeToULong(const std::string &colorCode)
Convert a string containing a color code to unsigned long.
Definition: ConfigHandler.cpp:94
ConfigHandler(const ConfigHandler &)=delete
static ConfigHandler & GetInstance()
Get the ConfigHandler object Throws an exception if the object is not created.
Definition: ConfigHandler.cpp:46
ConfigHandler()
Construct a new ConfigHandler object without a path Instanciate a ConfigFileHandler Object without a ...
Definition: ConfigHandler.cpp:58
static ConfigHandler * instance_
Definition: ConfigHandler.hpp:124
static void Destroy()
Destroy the ConfigHandler object.
Definition: ConfigHandler.cpp:52
ConfigHandler & operator=(const ConfigHandler &)=delete
std::shared_ptr< T > getConfigData()
Get the ConfigData object of type T.
Definition: ConfigHandler.hpp:66
void addConfigData(std::shared_ptr< T > configData)
Definition: ConfigHandler.hpp:80
void configInit()
Initialize the ConfigHandler Read the configuration file and store the JSon root object in Json::Valu...
Definition: ConfigHandler.cpp:65
static void Create()
Definition: ConfigHandler.cpp:41