BornAgain  1.19.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 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 //! @ingroup fitting_internal
31 
33 public:
34  using option_t = std::shared_ptr<MultiOption>;
35  using container_t = std::vector<option_t>;
36  using iterator = container_t::iterator;
37  using const_iterator = container_t::const_iterator;
38 
40  OptionContainer(const OptionContainer& other);
42 
43  template <class T>
44  option_t addOption(const std::string& optionName, T value, const std::string& description = "");
45 
46  option_t option(const std::string& optionName);
47  const option_t option(const std::string& optionName) const;
48 
49  template <class T> 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> void setOptionValue(const std::string& optionName, T value);
53 
54  iterator begin() { return m_options.begin(); }
55  const_iterator begin() const { return m_options.begin(); }
56 
57  iterator end() { return m_options.end(); }
58  const_iterator end() const { return m_options.end(); }
59 
60  size_t size() const { return m_options.size(); }
61  bool empty() const { return size() == 0; }
62 
63 protected:
64  bool exists(const std::string& name);
65  void swapContent(OptionContainer& other);
67 };
68 
69 template <class T>
70 OptionContainer::option_t OptionContainer::addOption(const std::string& optionName, T value,
71  const std::string& description)
72 {
73  if (exists(optionName))
74  throw std::runtime_error("OptionContainer::addOption() -> Error. Option '" + optionName
75  + "' exists.");
76 
77  option_t result(new MultiOption(optionName, value, description));
78  m_options.push_back(result);
79  return result;
80 }
81 
82 template <class T> T OptionContainer::optionValue(const std::string& optionName) const
83 {
84  return option(optionName)->get<T>();
85 }
86 
87 template <class T> void OptionContainer::setOptionValue(const std::string& optionName, T value)
88 {
89  option(optionName)->value() = value;
90  if (option(optionName)->value().which() != option(optionName)->defaultValue().which())
91  throw std::runtime_error(
92  "OptionContainer::setOptionValue() -> Error. Attempt to set different"
93  "type to option '"
94  + optionName + "'");
95 }
96 
97 #endif // BORNAGAIN_FIT_TOOLS_OPTIONCONTAINER_H
98 #endif // USER_API
Declares class MultiOption.
Stores a single option for minimization algorithm.
Definition: MultiOption.h:30
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)
QString const & name(EShape k)
Definition: particles.cpp:21