BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
OptionContainer.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
21 #define BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
22 
23 #include "Fit/Tools/MultiOption.h"
24 #include <map>
25 #include <memory>
26 #include <stdexcept>
27 #include <vector>
28 
29 //! Stores multi option (int,double,string) in a container.
30 
32 public:
33  using option_t = std::shared_ptr<MultiOption>;
34  using container_t = std::vector<option_t>;
35  using iterator = container_t::iterator;
36  using const_iterator = container_t::const_iterator;
37 
38  OptionContainer() = default;
39  OptionContainer(const OptionContainer& other);
41 
42  template <class T>
43  option_t addOption(const std::string& optionName, T value, const std::string& description = "");
44 
45  option_t option(const std::string& optionName);
46  option_t option(const std::string& optionName) const;
47 
48  template <class T>
49  T optionValue(const std::string& optionName) const;
50 
51  //! Sets the value of option. Option should hold same value type already.
52  template <class T>
53  void setOptionValue(const std::string& optionName, T value);
54 
55  iterator begin() { return m_options.begin(); }
56  const_iterator begin() const { return m_options.begin(); }
57 
58  iterator end() { return m_options.end(); }
59  const_iterator end() const { return m_options.end(); }
60 
61  size_t size() const { return m_options.size(); }
62  bool empty() const { return size() == 0; }
63 
64 protected:
65  bool exists(const std::string& name);
66  void swapContent(OptionContainer& other);
68 };
69 
70 template <class T>
71 OptionContainer::option_t OptionContainer::addOption(const std::string& optionName, T value,
72  const std::string& description)
73 {
74  if (exists(optionName))
75  throw std::runtime_error("OptionContainer::addOption() -> Error. Option '" + optionName
76  + "' exists.");
77 
78  option_t result(new MultiOption(optionName, value, description));
79  m_options.push_back(result);
80  return result;
81 }
82 
83 template <class T>
84 T OptionContainer::optionValue(const std::string& optionName) const
85 {
86  return option(optionName)->get<T>();
87 }
88 
89 template <class T>
90 void OptionContainer::setOptionValue(const std::string& optionName, T value)
91 {
92  option(optionName)->value() = value;
93  if (option(optionName)->value().index() != option(optionName)->defaultValue().index())
94  throw std::runtime_error(
95  "OptionContainer::setOptionValue() -> Error. Attempt to set different"
96  "type to option '"
97  + optionName + "'");
98 }
99 
100 #endif // BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
101 #endif // USER_API
Declares class MultiOption.
Stores a single option for minimization algorithm. Int, double, string values are available.
Definition: MultiOption.h:28
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
OptionContainer()=default
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)