Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Setting.hh
Go to the documentation of this file.
1 ///
2 /// @file Setting.hh
3 /// @brief Hammer setting class
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++ -*-
12 #ifndef HAMMER_Setting_HH
13 #define HAMMER_Setting_HH
14 
15 #include <complex>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 #include <type_traits>
22 
23 #include <boost/variant.hpp>
24 
25 #include "Hammer/IndexTypes.hh"
27 
28 #include "yaml-cpp/yaml.h"
29 
30 namespace Hammer {
31 
32  class Log;
33  struct SettingYamlConverter;
34  /// @brief container for an Hammer run option
35  ///
36  /// stores the value, name, owner, description and default value of an Hammer option
37  ///
38  /// @ingroup Handlers
39  class Setting {
40 
41  public:
42  /// default constructor
43  Setting();
44  Setting(const Setting& other) = default;
45  Setting& operator=(const Setting& other);
46  Setting(Setting&& other) = default;
47  Setting& operator=(Setting&& other) = default;
48  ~Setting() = default;
49 
50  Setting(const Serial::FBSetting* msgreader);
51 
52  /// @brief access the setting value
53  /// @return a pointer to the value, `nullptr` if the data type does not match
54  template <typename T>
55  T* getValue();
56 
57  /// @brief access the setting value (`const` version)
58  /// @return a pointer to the value, `nullptr` if the data type does not match
59  template <typename T>
60  const T* getValue() const;
61 
62  /// @brief modify the setting value
63  /// @param[in] value the new value of the setting
64  template <typename T>
65  void setValue(const T& value);
66 
67  /// @brief get the default value of this setting
68  /// @return a pointer to the default value, `nullptr` if the data type does not match
69  template <typename T>
70  T* getDefault();
71 
72  /// @brief get the default value of this setting (`const` version)
73  /// @return a pointer to the default value, `nullptr` if the data type does not match
74  template <typename T>
75  const T* getDefault() const;
76 
77  /// @brief modify the setting default value
78  /// @param[in] value the new default value of the setting
79  template <typename T>
80  void setDefault(const T& value);
81 
82  /// @brief reset the contents of the settings
83  void reset();
84 
85  bool wasChanged() const;
86 
87  bool isSame(const Setting& other) const;
88 
89  void setDefault();
90 
91  void update(const Setting& other);
92 
93  /// @brief properly format the value of the setting (so that can be e.g. displayed on a screen)
94  /// @param[in] useDefault whether to use the current value or the default value
95  /// @return the formatted string
96  std::string toString(const bool useDefault = false) const;
97 
98  using WrittenSettingType = std::pair<flatbuffers::Offset<void>, Serial::FBSettingTypes>;
99 
100  WrittenSettingType write(flatbuffers::FlatBufferBuilder* msgwriter) const;
101 
102  static void setEncodeUseDefault(bool useDefault);
103 
104  private:
105  friend struct SettingEncoder;
106  friend struct SettingChecker;
107  friend class SettingWriter;
108  friend struct SettingStringConverter;
109  friend struct YAML::convert<::Hammer::Setting>;
110 
111  private:
112  using MatrixType = std::vector<std::vector<double>>;
113 
114  /// data type for the setting values
115  using SettingType = boost::variant<boost::blank, bool, int, double, std::string, std::complex<double>,
116  std::vector<std::string>, std::vector<double>, MatrixType>;
117 
118  private:
119  void read(const Serial::FBSetting* msgreader);
120 
121 
122  private:
123  /// setting value
125  /// setting default value
127 
128  static bool _encodeUseDefault;
129  };
130 
131  YAML::Emitter& operator<<(YAML::Emitter& out, const Setting& s);
132 
133 } // namespace Hammer
134 
135 namespace YAML {
136 
137  template <>
138  struct convert<::Hammer::Setting> {
139 
140  static Node encode(const ::Hammer::Setting& value);
141 
142  static bool decode(const Node& node, ::Hammer::Setting& value);
143  };
144 
145 } // namespace YAML
146 
147 #include "Hammer/Tools/SettingDefs.hh"
148 
149 #endif
Setting & operator=(const Setting &other)
Definition: Setting.cc:274
std::vector< std::vector< double >> MatrixType
Definition: Setting.hh:112
void update(const Setting &other)
Definition: Setting.cc:308
Forward declaration of serialization related typedefs and includes.
Hammer data types declarations.
static bool _encodeUseDefault
Definition: Setting.hh:128
T * getDefault()
get the default value of this setting
Definition: SettingDefs.hh:37
void reset()
reset the contents of the settings
Definition: Setting.cc:286
void read(const Serial::FBSetting *msgreader)
Definition: Setting.cc:325
WrittenSettingType write(flatbuffers::FlatBufferBuilder *msgwriter) const
Definition: Setting.cc:320
T * getValue()
access the setting value
Definition: SettingDefs.hh:19
std::pair< flatbuffers::Offset< void >, Serial::FBSettingTypes > WrittenSettingType
Definition: Setting.hh:98
bool wasChanged() const
Definition: Setting.cc:294
boost::variant< boost::blank, bool, int, double, std::string, std::complex< double >, std::vector< std::string >, std::vector< double >, MatrixType > SettingType
data type for the setting values
Definition: Setting.hh:116
void setValue(const T &value)
modify the setting value
Definition: SettingDefs.hh:29
bool isSame(const Setting &other) const
Definition: Setting.cc:299
SettingType _default
setting default value
Definition: Setting.hh:126
container for an Hammer run option
Definition: Setting.hh:39
static void setEncodeUseDefault(bool useDefault)
Definition: Setting.cc:290
void setDefault()
Definition: Setting.cc:304
~Setting()=default
std::string toString(const bool useDefault=false) const
properly format the value of the setting (so that can be e.g.
Definition: Setting.cc:385
SettingType _value
setting value
Definition: Setting.hh:124
Setting()
default constructor
Definition: Setting.cc:268