BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IParameter.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Param/Base/IParameter.h
6 //! @brief Defines and implements pure virtual base class IParameter<T>.
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_CORE_PARAMETRIZATION_IPARAMETER_H
16 #define BORNAGAIN_CORE_PARAMETRIZATION_IPARAMETER_H
17 
18 #include <functional>
19 #include <stdexcept>
20 #include <string>
21 
22 //! Pure virtual base class for parameter wrapper classes RealParameter, ComponentParameter.
23 //! Holds a pointer to the wrapped parameter, a name, and a callback function to be called
24 //! when the parameter is changed.
25 //! This class is templated on the data type of the wrapped parameter.
26 //! @ingroup tools_internal
27 
28 template <class T> class IParameter
29 {
30 public:
31  IParameter() = delete;
32  IParameter(const std::string& name, T* data, const std::string& parent_name,
33  const std::function<void()>& onChange);
34  virtual ~IParameter() = default;
35 
36  virtual IParameter* clone(const std::string& new_name = "") const = 0;
37 
38  //! Returns true if wrapped parameter was not initialized with proper real value
39  virtual bool isNull() const { return m_data ? false : true; }
40 
41  T& getData() const { return *m_data; }
42  void setData(T& data)
43  {
44  m_data = &data;
45  m_onChange();
46  }
47 
48  bool hasSameData(const IParameter& other);
49  const std::string& getName() const { return m_name; }
50 
51 protected:
52  const std::string m_name;
53  T* m_data;
54  const std::string m_parent_name;
55  const std::function<void()> m_onChange;
56 
57  //! For use in error messages
58  std::string fullName() const { return m_parent_name + "/" + m_name; }
59 };
60 
61 template <class T>
62 IParameter<T>::IParameter(const std::string& name, T* data, const std::string& parent_name,
63  const std::function<void()>& onChange)
64  : m_name(name), m_data(data), m_parent_name(parent_name), m_onChange(onChange)
65 {
66  if (!m_data)
67  throw std::runtime_error("Attempt to construct an IParameter with null data pointer");
68 }
69 
70 //! Returns true if two parameters are pointing to the same raw data.
71 
72 template <class T> bool IParameter<T>::hasSameData(const IParameter<T>& other)
73 {
74  return &getData() == &other.getData();
75 }
76 
77 #endif // BORNAGAIN_CORE_PARAMETRIZATION_IPARAMETER_H
Pure virtual base class for parameter wrapper classes RealParameter, ComponentParameter.
Definition: IParameter.h:29
const std::string & getName() const
Definition: IParameter.h:49
const std::string m_name
Definition: IParameter.h:52
virtual ~IParameter()=default
virtual bool isNull() const
Returns true if wrapped parameter was not initialized with proper real value.
Definition: IParameter.h:39
bool hasSameData(const IParameter &other)
Returns true if two parameters are pointing to the same raw data.
Definition: IParameter.h:72
void setData(T &data)
Definition: IParameter.h:42
T & getData() const
Definition: IParameter.h:41
IParameter()=delete
virtual IParameter * clone(const std::string &new_name="") const =0
const std::function< void()> m_onChange
Definition: IParameter.h:55
IParameter(const std::string &name, T *data, const std::string &parent_name, const std::function< void()> &onChange)
Definition: IParameter.h:62
T * m_data
Definition: IParameter.h:53
const std::string m_parent_name
Definition: IParameter.h:54
std::string fullName() const
For use in error messages.
Definition: IParameter.h:58