BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OptionContainer.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Tools/OptionContainer.h
6 //! @brief Declares class OptionContainer.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #ifndef BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
16 #define BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
17 
18 #include "Fit/Tools/MultiOption.h"
19 #include <map>
20 #include <memory>
21 #include <stdexcept>
22 #include <vector>
23 
24 //! Stores multi option (int,double,string) in a container.
25 //! @ingroup fitting_internal
26 
28 {
29 public:
30  using option_t = std::shared_ptr<MultiOption>;
31  using container_t = std::vector<option_t>;
32  using iterator = container_t::iterator;
33  using const_iterator = container_t::const_iterator;
34 
36  OptionContainer(const OptionContainer& other);
38 
39  template <class T>
40  option_t addOption(const std::string& optionName, T value, const std::string& description = "");
41 
42  option_t option(const std::string& optionName);
43  const option_t option(const std::string& optionName) const;
44 
45  template <class T> T optionValue(const std::string& optionName) const;
46 
47  //! Sets the value of option. Option should hold same value type already.
48  template <class T> void setOptionValue(const std::string& optionName, T value);
49 
50  iterator begin() { return m_options.begin(); }
51  const_iterator begin() const { return m_options.begin(); }
52 
53  iterator end() { return m_options.end(); }
54  const_iterator end() const { return m_options.end(); }
55 
56  size_t size() const { return m_options.size(); }
57  bool empty() const { return size() == 0; }
58 
59 protected:
60  bool exists(const std::string& name);
61  void swapContent(OptionContainer& other);
63 };
64 
65 template <class T>
66 OptionContainer::option_t OptionContainer::addOption(const std::string& optionName, T value,
67  const std::string& description)
68 {
69  if (exists(optionName))
70  throw std::runtime_error("OptionContainer::addOption() -> Error. Option '" + optionName
71  + "' exists.");
72 
73  option_t result(new MultiOption(optionName, value, description));
74  m_options.push_back(result);
75  return result;
76 }
77 
78 template <class T> T OptionContainer::optionValue(const std::string& optionName) const
79 {
80  return option(optionName)->get<T>();
81 }
82 
83 template <class T> void OptionContainer::setOptionValue(const std::string& optionName, T value)
84 {
85  option(optionName)->value() = value;
86  if (option(optionName)->value().which() != option(optionName)->defaultValue().which())
87  throw std::runtime_error(
88  "OptionContainer::setOptionValue() -> Error. Attempt to set different"
89  "type to option '"
90  + optionName + "'");
91 }
92 
93 #endif // BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
Declares class MultiOption.
Stores a single option for minimization algorithm.
Definition: MultiOption.h:26
Stores multi option (int,double,string) in a container.
std::vector< option_t > container_t
const_iterator begin() const
void setOptionValue(const std::string &optionName, T value)
Sets the value of option. Option should hold same value type already.
size_t size() const
const_iterator end() const
container_t::iterator iterator
bool empty() const
T optionValue(const std::string &optionName) const
std::shared_ptr< MultiOption > option_t
OptionContainer & operator=(const OptionContainer &other)
container_t m_options
option_t option(const std::string &optionName)
void swapContent(OptionContainer &other)
iterator begin()
option_t addOption(const std::string &optionName, T value, const std::string &description="")
container_t::const_iterator const_iterator
bool exists(const std::string &name)