Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SettingsConsumer.cc
Go to the documentation of this file.
1 ///
2 /// @file SettingsConsumer.cc
3 /// @brief Base class for accessing Hammer settings repository
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
13 
14 using namespace std;
15 
16 namespace Hammer {
17 
18  SettingsConsumer::SettingsConsumer() : _settingHandler{nullptr}, _group{WTerm::COMMON} {
19  }
20 
21  void SettingsConsumer::setPath(const std::string& path) {
22  _settingPath = path;
23  }
24 
25  bool SettingsConsumer::isOn(const std::string& name) const {
26  if (_settingHandler) {
27  bool* val = _settingHandler->getNamedSettingValue<bool>(_settingPath, name, _group);
28  if (val) {
29  return *val;
30  } else {
31  throw invalid_argument("Setting '" + _settingPath + ":" + name + "' not found.");
32  }
33  } else {
34  throw Error("SettingsHandler pointer not set. Handler sought for Irish Setter and English Pointer.");
35  }
36  }
37 
39  _ownedSettings.clear();
42  //addRefs();
43  }
44 
46  }
47 
48  bool SettingsConsumer::isOn(const std::string& otherPath, const std::string& name) const {
49  if (_settingHandler) {
50  bool* val = _settingHandler->getNamedSettingValue<bool>(otherPath, name, _group);
51  if (val) {
52  return *val;
53  } else {
54  throw invalid_argument("Setting '" + otherPath + ":" + name + "' not found.");
55  }
56  } else {
57  throw Error("SettingsHandler pointer not set. Handler sought for Irish Setter and English Pointer.");
58  }
59  }
60 
62  return _settingHandler;
63  }
64 
66  bool needInit = (_settingHandler == nullptr);
67  _settingHandler = &sh;
68  if(needInit) {
69  initSettings();
70  }
71  }
72 
74  bool needInit = (_settingHandler == nullptr);
76  if (needInit) {
77  initSettings();
78  }
79  }
80 
82  WTerm oldgroup = _group;
83  _group = group;
84  return oldgroup;
85  }
86 
87  void SettingsConsumer::removeSetting(const std::string& name) {
88  if (_settingHandler) {
90  } else {
91  throw InitializationError("SettingsHandler pointer not set.");
92  }
93  }
94 
96  if (_settingHandler) {
97  auto defined = _settingHandler->getSettings(_settingPath);
98  if(defined.size() > _ownedSettings.size()) {
99  vector<string> result{defined.size() - _ownedSettings.size()};
100  set_difference(defined.begin(), defined.end(), _ownedSettings.begin(), _ownedSettings.end(), result.begin());
101  for(auto& elem: result) {
102  MSG_ERROR("Setting " + elem + " was defined before the initialization of "
103  + _settingPath + " and is not a valid setting. Removing. Please check syntax for typos.");
104  removeSetting(elem);
105  }
106  }
107  }
108  }
109 
111  return Log::getLog("Hammer.SettingsConsumer");
112  }
113 
114 } // namespace Hammer
virtual void defineSettings()=0
purely virtual function for a class to define new settings
T * getNamedSettingValue(const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
access a specific setting value
virtual void addRefs() const
void setPath(const std::string &path)
provide the basic path for the settings defined by this class, as in &quot;&lt;path&gt;:&lt;setting&gt;&quot; ...
SettingsHandler * _settingHandler
pointer to the settings repository handler.
WTerm setWeightTerm(WTerm group)
std::string _settingPath
path of the settings defined by this class
std::set< std::string > _ownedSettings
Log & getLog() const
logging facility
Base class to access the settings repository.
static Log & getLog(const std::string &name)
Get a logger with the given name.
Definition: Logging.cc:139
virtual void setSettingsHandler(SettingsHandler &sh)
set link to settings repository handler.
Initialization error class.
Definition: Exceptions.hh:69
Logging class.
Definition: Logging.hh:33
void removeSetting(const std::string &name)
Generic error class.
Definition: Exceptions.hh:23
Base class for accessing Hammer settings repository.
void removeSetting(const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
SettingsHandler * getSettingsHandler() const
provide the pointer to the repository handler
Hammer settings manager class.
#define MSG_ERROR(x)
Definition: Logging.hh:367
bool isOn(const std::string &name) const
method to check a boolean setting defined by this class
std::set< std::string > getSettings(const std::string &path="", WTerm group=WTerm::COMMON) const
access the list of registered settings