settings_manager.h
1 /*
2  * <one line to give the program's name and a brief idea of what it does.>
3  * Copyright (C) 2023 Jose F Rivas C <email>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef NAF_TOOLS_SETTINGSMANAGER
20 #define NAF_TOOLS_SETTINGSMANAGER
21 
22 
23 #include <iostream>
24 #include <memory>
25 #include <string>
26 #include <mutex>
27 #include <algorithm>
28 
29 #include "tools/dvalue.h"
30 #include "yaml-cpp/node/detail/iterator_fwd.h"
31 #include "yaml-cpp/node/node.h"
32 #include "yaml-cpp/yaml.h"
33 #include "Poco/Exception.h"
34 
35 #include "tools/dvalue.h"
36 
37 namespace NAF
38 {
39  namespace Tools
40  {
41  class SettingsManager;
42  }
43 }
44 
45 using namespace Poco;
46 
47 
49 {
50  public:
51  struct Setting
52  {
53  Setting(std::string name, Tools::DValue::Type type, Tools::DValue value) :
54  name(name)
55  ,type(type)
56  ,value(value)
57  {
58 
59  }
60  std::string name;
61  Tools::DValue::Type type;
62  Tools::DValue value;
63  };
64 
66 
67  static std::vector<Setting>& get_settings()
68  {
69  auto& var = settings_;
70  return var;
71  }
72  static std::string get_config_path() { return config_path_; }
73 
74  static void set_config_path(std::string config_path) { config_path_ = config_path; }
75 
76  static std::vector<Setting>::iterator GetSetting_(std::string setting_name);
77  static std::string GetSetting_(std::string setting_name, const char* another_value);
78  static std::string GetSetting_(std::string setting_name, std::string another_value);
79  static int GetSetting_(std::string setting_name, int another_value);
80  static float GetSetting_(std::string setting_name, float another_value);
81  static bool GetSetting_(std::string setting_name, bool another_value);
82  static void AddSetting_(std::string name, Tools::DValue::Type type, Tools::DValue value);
83  static void AddBasicSettings_();
84  static void ReadSettings_();
85 
86  protected:
87  static bool VerifyYAMLScalarNode_(YAML::Node& node);
88  static bool VerifyYAMLMapNode_(YAML::Node& node);
89  static void PrintError_(std::string function, std::string variable);
90 
91  private:
92  static std::mutex mutex_;
93  static std::vector<Setting> settings_;
94  static std::string config_path_;
95 };
96 
97 #endif // NAF_TOOLS_SETTINGSMANAGER
Definition: dvalue.h:48
Definition: settings_manager.h:49
Definition: settings_manager.h:52