BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
OptionContainer Class Reference

Description

Stores multi option (int,double,string) in a container.

Definition at line 31 of file OptionContainer.h.

Inheritance diagram for OptionContainer:
[legend]
Collaboration diagram for OptionContainer:
[legend]

Public Types

using const_iterator = container_t::const_iterator
 
using container_t = std::vector< option_t >
 
using iterator = container_t::iterator
 
using option_t = std::shared_ptr< MultiOption >
 

Public Member Functions

 OptionContainer ()=default
 
 OptionContainer (const OptionContainer &other)
 Returns true if option with such name already exists. More...
 
template<class T >
option_t addOption (const std::string &optionName, T value, const std::string &description="")
 
iterator begin ()
 
const_iterator begin () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
OptionContaineroperator= (const OptionContainer &other)
 
option_t option (const std::string &optionName)
 
option_t option (const std::string &optionName) const
 
template<class T >
optionValue (const std::string &optionName) const
 
template<class T >
void setOptionValue (const std::string &optionName, T value)
 Sets the value of option. Option should hold same value type already. More...
 
size_t size () const
 

Protected Member Functions

bool exists (const std::string &name)
 
void swapContent (OptionContainer &other)
 

Protected Attributes

container_t m_options
 

Member Typedef Documentation

◆ const_iterator

using OptionContainer::const_iterator = container_t::const_iterator

Definition at line 36 of file OptionContainer.h.

◆ container_t

using OptionContainer::container_t = std::vector<option_t>

Definition at line 34 of file OptionContainer.h.

◆ iterator

using OptionContainer::iterator = container_t::iterator

Definition at line 35 of file OptionContainer.h.

◆ option_t

using OptionContainer::option_t = std::shared_ptr<MultiOption>

Definition at line 33 of file OptionContainer.h.

Constructor & Destructor Documentation

◆ OptionContainer() [1/2]

OptionContainer::OptionContainer ( )
default

◆ OptionContainer() [2/2]

OptionContainer::OptionContainer ( const OptionContainer other)

Returns true if option with such name already exists.

Definition at line 19 of file OptionContainer.cpp.

20 {
21  for (const auto& option : other.m_options)
22  m_options.push_back(option_t(new MultiOption(*option)));
23 }
Stores a single option for minimization algorithm. Int, double, string values are available.
Definition: MultiOption.h:28
std::shared_ptr< MultiOption > option_t
container_t m_options
option_t option(const std::string &optionName)

References m_options, and option().

Here is the call graph for this function:

Member Function Documentation

◆ addOption()

template<class T >
OptionContainer::option_t OptionContainer::addOption ( const std::string &  optionName,
value,
const std::string &  description = "" 
)

Definition at line 71 of file OptionContainer.h.

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 }
bool exists(const std::string &name)

References exists(), and m_options.

Referenced by MinimizerAdapter::addOption().

Here is the call graph for this function:

◆ begin() [1/2]

iterator OptionContainer::begin ( )
inline

Definition at line 55 of file OptionContainer.h.

55 { return m_options.begin(); }

References m_options.

◆ begin() [2/2]

const_iterator OptionContainer::begin ( ) const
inline

Definition at line 56 of file OptionContainer.h.

56 { return m_options.begin(); }

References m_options.

◆ empty()

bool OptionContainer::empty ( ) const
inline

Definition at line 62 of file OptionContainer.h.

62 { return size() == 0; }
size_t size() const

References size().

Here is the call graph for this function:

◆ end() [1/2]

iterator OptionContainer::end ( )
inline

Definition at line 58 of file OptionContainer.h.

58 { return m_options.end(); }

References m_options.

◆ end() [2/2]

const_iterator OptionContainer::end ( ) const
inline

Definition at line 59 of file OptionContainer.h.

59 { return m_options.end(); }

References m_options.

◆ exists()

bool OptionContainer::exists ( const std::string &  name)
protected

Definition at line 56 of file OptionContainer.cpp.

57 {
58  for (const auto& option : m_options) {
59  if (option->name() == name)
60  return true;
61  }
62  return false;
63 }

References m_options, and option().

Referenced by addOption().

Here is the call graph for this function:

◆ operator=()

OptionContainer & OptionContainer::operator= ( const OptionContainer other)

Definition at line 25 of file OptionContainer.cpp.

26 {
27  if (this != &other) {
28  OptionContainer tmp(other);
29  tmp.swapContent(*this);
30  }
31  return *this;
32 }
Stores multi option (int,double,string) in a container.

References swapContent().

Here is the call graph for this function:

◆ option() [1/2]

OptionContainer::option_t OptionContainer::option ( const std::string &  optionName)

Definition at line 34 of file OptionContainer.cpp.

35 {
36  for (const auto& option : m_options) {
37  if (option->name() == optionName)
38  return option;
39  }
40 
41  throw std::runtime_error("Configurable::getOption() -> Error. No option with name '"
42  + optionName + "'.");
43 }

References m_options.

Referenced by OptionContainer(), exists(), option(), optionValue(), MinimizerOptions::processCommand(), setOptionValue(), and MinimizerOptions::toOptionString().

◆ option() [2/2]

OptionContainer::option_t OptionContainer::option ( const std::string &  optionName) const

Definition at line 45 of file OptionContainer.cpp.

46 {
47  for (const auto& option : m_options) {
48  if (option->name() == optionName)
49  return option;
50  }
51 
52  throw std::runtime_error("Configurable::getOption() -> Error. No option with name '"
53  + optionName + "'.");
54 }

References m_options, and option().

Here is the call graph for this function:

◆ optionValue()

template<class T >
T OptionContainer::optionValue ( const std::string &  optionName) const

Definition at line 84 of file OptionContainer.h.

85 {
86  return option(optionName)->get<T>();
87 }

References option().

Referenced by MinimizerAdapter::optionValue().

Here is the call graph for this function:

◆ setOptionValue()

template<class T >
void OptionContainer::setOptionValue ( const std::string &  optionName,
value 
)

Sets the value of option. Option should hold same value type already.

Definition at line 90 of file OptionContainer.h.

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 }

References option().

Referenced by MinimizerAdapter::setOptionValue().

Here is the call graph for this function:

◆ size()

size_t OptionContainer::size ( ) const
inline

Definition at line 61 of file OptionContainer.h.

61 { return m_options.size(); }

References m_options.

Referenced by empty().

◆ swapContent()

void OptionContainer::swapContent ( OptionContainer other)
protected

Definition at line 65 of file OptionContainer.cpp.

66 {
67  std::swap(m_options, other.m_options);
68 }

References m_options.

Referenced by operator=().

Member Data Documentation

◆ m_options

container_t OptionContainer::m_options
protected

The documentation for this class was generated from the following files: