YggdrasilWM  0.1.1
A tiny window manager coded in C++
ConfigHandler Class Reference

ConfigHandler class This class is responsible for handling the configuration It can be created with a file name or not, if not config files will be looked up in default path defined in ConfigFileHandler ConfigHandler is a Singleton class and should be created after the Logger class and before the WindowManager class. More...

#include <ConfigHandler.hpp>

Collaboration diagram for ConfigHandler:
Collaboration graph

Public Member Functions

 ConfigHandler (const ConfigHandler &)=delete
 
ConfigHandleroperator= (const ConfigHandler &)=delete
 
template<typename T >
std::shared_ptr< T > getConfigData ()
 Get the ConfigData object of type T. More...
 
template<typename T >
void addConfigData (std::shared_ptr< T > configData)
 
void configInit ()
 Initialize the ConfigHandler Read the configuration file and store the JSon root object in Json::Value root_ Get the configFilePath form which the config has been read. More...
 
 ~ConfigHandler ()
 

Static Public Member Functions

static void Create (const std::string &configPath)
 
static void Create ()
 
static ConfigHandlerGetInstance ()
 Get the ConfigHandler object Throws an exception if the object is not created. More...
 
static void Destroy ()
 Destroy the ConfigHandler object. More...
 
static unsigned long colorCodeToULong (const std::string &colorCode)
 Convert a string containing a color code to unsigned long. More...
 

Private Member Functions

 ConfigHandler ()
 Construct a new ConfigHandler object without a path Instanciate a ConfigFileHandler Object without a path. More...
 
 ConfigHandler (const std::string &configPath)
 

Private Attributes

std::string configPath_
 
std::unordered_map< std::type_index, std::shared_ptr< ConfigDataBase > > configMap_
 

Static Private Attributes

static ConfigHandlerinstance_ = nullptr
 

Detailed Description

ConfigHandler class This class is responsible for handling the configuration It can be created with a file name or not, if not config files will be looked up in default path defined in ConfigFileHandler ConfigHandler is a Singleton class and should be created after the Logger class and before the WindowManager class.

See also
ConfigFileHandler
ConfigDataBase

Constructor & Destructor Documentation

◆ ConfigHandler() [1/3]

ConfigHandler::ConfigHandler ( const ConfigHandler )
delete

◆ ~ConfigHandler()

ConfigHandler::~ConfigHandler ( )
default

◆ ConfigHandler() [2/3]

ConfigHandler::ConfigHandler ( )
private

Construct a new ConfigHandler object without a path Instanciate a ConfigFileHandler Object without a path.

58  :
59  configPath_(){
60 }
std::string configPath_
Definition: ConfigHandler.hpp:123

Referenced by Create().

Here is the caller graph for this function:

◆ ConfigHandler() [3/3]

ConfigHandler::ConfigHandler ( const std::string &  configPath)
explicitprivate
61  :
62  configPath_(configPath){
63 }

Member Function Documentation

◆ addConfigData()

template<typename T >
template< typename T > void ConfigHandler::addConfigData ( std::shared_ptr< T >  configData)
inline
Template Parameters
TType of the ConfigData object you want to add to the ConfigHandler
Parameters
configDatathe ConfigData object you want to add
Todo:
modify to use smart pointers
80  {
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  }
std::unordered_map< std::type_index, std::shared_ptr< ConfigDataBase > > configMap_
Definition: ConfigHandler.hpp:125

References configMap_.

Referenced by configInit().

Here is the caller graph for this function:

◆ colorCodeToULong()

unsigned long ConfigHandler::colorCodeToULong ( const std::string &  colorCode)
static

Convert a string containing a color code to unsigned long.

Parameters
colorCode
Returns
unsigned long color code
94  {
95  if (colorCode.size() != 7 || colorCode[0] != '#' || !isxdigit(colorCode[1])) {
96  throw std::runtime_error("Invalid color code");
97  }
98  std::istringstream iss(colorCode.substr(1));
99  unsigned long colorValue;
100  iss >> std::hex >> colorValue;
101  if (iss.fail()) { throw std::runtime_error("Invalid color code");}
102  return colorValue;
103 }

Referenced by ConfigDataBar::configInit(), ConfigDataGroup::configInit(), and ConfigDataWidget::configInit().

Here is the caller graph for this function:

◆ configInit()

void ConfigHandler::configInit ( )

Initialize the ConfigHandler Read the configuration file and store the JSon root object in Json::Value root_ Get the configFilePath form which the config has been read.

65  {
66  std::unique_ptr<ConfigFileHandler> configFileHandler_;
67  if (configPath_.empty()) {
68  configFileHandler_ = std::make_unique<ConfigFileHandler>(ConfigFileHandler());
69  } else {
70  configFileHandler_ = std::make_unique<ConfigFileHandler>(ConfigFileHandler(configPath_));
71  }
72  Logger::GetInstance()->Log("===================Loading Config===================",L_INFO);
73  configFileHandler_->readConfig();
74  configPath_ = configFileHandler_->getConfigPath();
75  const Json::Value &root_ = configFileHandler_->getRoot();
76  if (root_.empty() || !root_.isObject()) {
77  throw std::runtime_error("Config file is empty or not an object");
78  }
79  Json::Value Groups = root_["Groups"];
80  Json::Value Bars = root_["Bars"];
81  Json::Value Bindings = root_["Bindings"];
82  if (Groups.empty() || !Groups.isArray() || Bars.empty() || !Bars.isArray() || Bindings.empty() || !Bindings.isObject()){
83  throw std::runtime_error("Config file is missing Groups, Bars or Bindings See Documentation for more information");
84  }
85  addConfigData(std::make_shared<ConfigDataGroups>());
86  addConfigData(std::make_shared<ConfigDataBars>());
87  addConfigData(std::make_shared<ConfigDataBindings>());
88  getConfigData<ConfigDataGroups>()->configInit(Groups);
89  getConfigData<ConfigDataBars>()->configInit(Bars);
90  getConfigData<ConfigDataBindings>()->configInit(Bindings);
91  Logger::GetInstance()->Log("===================Succefully Loaded Config===================\n\n",L_INFO);
92 }
@ L_INFO
Definition: Logger.hpp:49
Class to manage the bars This class is a singleton and is responsible for managing the bars instancia...
Definition: Bars.hpp:56
Handle file I/O and JSON parsing for the configuration file. If no path is provided,...
Definition: ConfigFileHandler.hpp:40
void addConfigData(std::shared_ptr< T > configData)
Definition: ConfigHandler.hpp:80
virtual void Log(const std::string &message, LogLevel level) const
Log a message This method logs a message to the log file or stream. The message is only logged if the...
Definition: Logger.cpp:73
static Logger * GetInstance()
Get the Logger object.
Definition: Logger.cpp:41

References addConfigData(), configPath_, Logger::GetInstance(), L_INFO, and Logger::Log().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create() [1/2]

void ConfigHandler::Create ( )
static
41  {
42  if (instance_ == nullptr) {
43  instance_ = new ConfigHandler();
44  }
45 }
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

References ConfigHandler(), and instance_.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create() [2/2]

void ConfigHandler::Create ( const std::string &  configPath)
static
36  {
37  if (instance_ == nullptr) {
38  instance_ = new ConfigHandler(configPath);
39  }
40 }

References ConfigHandler(), and instance_.

Here is the call graph for this function:

◆ Destroy()

static void ConfigHandler::Destroy ( )
static

Destroy the ConfigHandler object.

52  {
53  if (instance_ != nullptr) {
54  delete instance_;
55  instance_ = nullptr;
56  }
57 }

References instance_.

Referenced by main().

Here is the caller graph for this function:

◆ getConfigData()

template<typename T >
template< typename T > T * ConfigHandler::getConfigData ( )
inline

Get the ConfigData object of type T.

Template Parameters
TType of the ConfigData object
Returns
a pointer to an object form a subclass of ConfigData
Todo:
modify to use smart pointers
66  {
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  }

References configMap_.

Referenced by WindowManager::addGroupsFromConfig(), Client::frame(), EventHandler::handleKeyPress(), EventHandler::handleUnmapNotify(), and WindowManager::init().

Here is the caller graph for this function:

◆ GetInstance()

static ConfigHandler & ConfigHandler::GetInstance ( )
static

Get the ConfigHandler object Throws an exception if the object is not created.

46  {
47  if (instance_ == nullptr) {
48  throw std::runtime_error("ConfigHandler instance not created");
49  }
50  return *instance_;
51 }

References instance_.

Referenced by WindowManager::addGroupsFromConfig(), WindowManager::createBars(), Client::frame(), EventHandler::handleKeyPress(), EventHandler::handleUnmapNotify(), WindowManager::init(), and main().

Here is the caller graph for this function:

◆ operator=()

ConfigHandler& ConfigHandler::operator= ( const ConfigHandler )
delete

Member Data Documentation

◆ configMap_

std::unordered_map<std::type_index, std::shared_ptr<ConfigDataBase> > ConfigHandler::configMap_
private

Referenced by addConfigData(), and getConfigData().

◆ configPath_

std::string ConfigHandler::configPath_
private

Referenced by configInit().

◆ instance_

ConfigHandler * ConfigHandler::instance_ = nullptr
staticprivate

Referenced by Create(), Destroy(), and GetInstance().


The documentation for this class was generated from the following files: