NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
ConfigTool.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Global.h"
9#include "NAGASH/MSGTool.h"
10
11namespace NAGASH
12{
14 {
15 public:
16 ConfigTool() { msg = std::make_shared<MSGTool>(); };
17 ConfigTool(const char *inputfilename);
18 ConfigTool(const ConfigTool &c) = default;
19 ConfigTool &operator=(const ConfigTool &c) = default;
20 ConfigTool(ConfigTool &&c) = default;
22
23 template <typename T>
24 void BookPar(const TString &name, const T &value);
25 template <class T>
26 T GetPar(const TString &name, bool *doexist = nullptr) const;
27 std::shared_ptr<MSGTool> MSGUser() const;
28 void SetMSG(std::shared_ptr<MSGTool> MSG);
29 void RemovePar(const TString &name);
30
31 private:
32 std::map<TString, std::any> ParMap;
33 std::map<TString, std::any> ParMapFile;
34 std::vector<TString> InputMD5Sum;
35 mutable std::shared_ptr<MSGTool> msg = nullptr;
36 template <typename T>
37 void BookParFile(const TString &name, const T &value);
38 void ReadConfigFromFile(const TString &filename);
39 std::string Exec(std::string command);
40 };
41
44 inline std::shared_ptr<MSGTool> ConfigTool::MSGUser() const { return msg; }
45
48 inline void ConfigTool::SetMSG(std::shared_ptr<MSGTool> MSG) { msg = MSG; }
49
52 inline void ConfigTool::RemovePar(const TString &name)
53 {
54 ParMap.erase(name);
55 ParMapFile.erase(name);
56 }
57
62 template <typename T>
63 inline void ConfigTool::BookPar(const TString &name, const T &value)
64 {
65 static_assert(!std::is_pointer<T>::value, "class ConfigTool : Can not book parameter in pointer type");
66 static_assert(std::is_default_constructible<T>::value, "class ConfigTool : Can not book parameter in not_default_constructible type");
67
68 std::any temp = std::make_any<T>(value);
69 if (ParMap.find(name) != ParMap.end())
70 {
71 MSGUser()->StartTitle("ConfigTool::BookPar");
72 MSGUser()->MSG(MSGLevel::WARNING, "parameter ", name, " has already existed, will be overwritten");
73 MSGUser()->EndTitle();
74 ParMap[name] = temp;
75 }
76 else
77 {
78 ParMap.insert(std::pair<TString, std::any>(name, temp));
79 }
80 }
81
86 template <typename T>
87 inline void ConfigTool::BookParFile(const TString &name, const T &value)
88 {
89 static_assert(!std::is_pointer<T>::value, "class ConfigTool : Can not book parameter in pointer type");
90 static_assert(std::is_default_constructible<T>::value, "class ConfigTool : Can not book parameter in not_default_constructible type");
91
92 std::any temp = std::make_any<T>(value);
93 if (ParMapFile.find(name) != ParMapFile.end())
94 {
95 MSGUser()->StartTitle("ConfigTool::BookPar");
96 MSGUser()->MSG(MSGLevel::WARNING, "parameter ", name, " has already existed, will be overwritten");
97 MSGUser()->EndTitle();
98 ParMapFile[name] = temp;
99 }
100 else
101 {
102 ParMapFile.insert(std::pair<TString, std::any>(name, temp));
103 }
104 }
105
107
128 template <class T>
129 inline T ConfigTool::GetPar(const TString &name, bool *doexist) const
130 {
131 static_assert(std::is_default_constructible<T>::value, "class ConfigTool : Can not get parameter in no_default_constructible type");
132 auto re = ParMap.find(name);
133 auto reF = ParMapFile.find(name);
134 if (reF != ParMapFile.end())
135 {
136 try
137 {
138 auto par = std::any_cast<T>(reF->second);
139 if (doexist != nullptr)
140 *doexist = true;
141 return par;
142 }
143 catch (const std::bad_any_cast &e)
144 {
145 MSGUser()->StartTitle("ConfigTool::GetPar");
146 MSGUser()->MSG(MSGLevel::ERROR, "parameter ", name, " type is not the given type, will return null");
147 MSGUser()->EndTitle();
148 return T();
149 }
150 }
151 else if (re != ParMap.end())
152 {
153 try
154 {
155 auto par = std::any_cast<T>(re->second);
156 if (doexist != nullptr)
157 *doexist = true;
158 return par;
159 }
160 catch (const std::bad_any_cast &e)
161 {
162 MSGUser()->StartTitle("ConfigTool::GetPar");
163 MSGUser()->MSG(MSGLevel::ERROR, "parameter ", name, " type is not the given type, will return null");
164 MSGUser()->EndTitle();
165 return T();
166 }
167 }
168 else
169 {
170 MSGUser()->StartTitle("ConfigTool::GetPar");
171 MSGUser()->MSG(MSGLevel::ERROR, "parameter ", name, " does not exist, will return null");
172 MSGUser()->EndTitle();
173 if (doexist != nullptr)
174 *doexist = false;
175 return T();
176 }
177 };
178
179} // namespace NAGASH
Some global definitions.
provide interface to config objects in NAGASH.
Definition ConfigTool.h:14
ConfigTool(ConfigTool &&c)=default
ConfigTool(const ConfigTool &c)=default
ConfigTool & operator=(const ConfigTool &c)=default
void BookPar(const TString &name, const T &value)
Book parameter with given name and value.
Definition ConfigTool.h:63
ConfigTool & operator=(ConfigTool &&c)=default
void BookParFile(const TString &name, const T &value)
Book parameter from input file.
Definition ConfigTool.h:87
std::string Exec(std::string command)
void SetMSG(std::shared_ptr< MSGTool > MSG)
Set the MSGTool.
Definition ConfigTool.h:48
std::shared_ptr< MSGTool > msg
Definition ConfigTool.h:35
void RemovePar(const TString &name)
Remove the parameter with given name.
Definition ConfigTool.h:52
void ReadConfigFromFile(const TString &filename)
Read config info from given file.
std::shared_ptr< MSGTool > MSGUser() const
Get the MSGTool pointer.
Definition ConfigTool.h:44
std::vector< TString > InputMD5Sum
Definition ConfigTool.h:34
std::map< TString, std::any > ParMapFile
Definition ConfigTool.h:33
std::map< TString, std::any > ParMap
Definition ConfigTool.h:32
T GetPar(const TString &name, bool *doexist=nullptr) const
Get the parameter with given name and type.
Definition ConfigTool.h:129