BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
INode.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Node/INode.h
6 //! @brief Defines interface INode.
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 USER_API
16 #ifndef BORNAGAIN_PARAM_NODE_INODE_H
17 #define BORNAGAIN_PARAM_NODE_INODE_H
18 
19 #include <limits>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 const double INF = std::numeric_limits<double>::infinity();
27 
28 //! Metadata of one model parameter.
29 struct ParaMeta {
30  std::string name;
31  std::string unit;
32  std::string tooltip;
33  double vMin;
34  double vMax;
35  double vDefault;
36 };
37 
38 //! Base class for tree-like structures containing parameterized objects.
39 
40 class INode {
41 public:
42  INode() = default;
43  INode(std::vector<double> PValues);
44 
45  virtual ~INode() = default;
46 
47  //! Returns the class name, to be hard-coded in each leaf class that inherits from INode.
48  virtual std::string className() const = 0;
49 
50  //! Returns the parameter definitions, to be hard-coded in each leaf class.
51  virtual std::vector<ParaMeta> parDefs() const { return {}; }
52 
53  //! Raises exception if a parameter value is invalid.
54  void checkNodeArgs() const;
55 
56  //! Returns all children.
57  virtual std::vector<const INode*> nodeChildren() const;
58 
59  //! Returns all descendants.
60  std::vector<const INode*> nodeOffspring() const;
61 
62 protected:
63  std::vector<double> m_P;
64 };
65 
66 // ************************************************************************************************
67 // vector concatenation, for use in nodeChildren functions
68 // ************************************************************************************************
69 
70 #ifndef SWIG
71 
72 template <class T>
73 std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node,
74  const std::unique_ptr<T>& node)
75 {
76  if (node)
77  v_node.push_back(node.get());
78  return v_node;
79 }
80 
81 template <class T>
82 std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node,
83  const std::unique_ptr<T>& node)
84 {
85  if (node)
86  v_node.push_back(node.get());
87  return v_node;
88 }
89 
90 inline std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node, const INode* node)
91 {
92  v_node.push_back(node);
93  return v_node;
94 }
95 
96 inline std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node, const INode* node)
97 {
98  v_node.push_back(node);
99  return v_node;
100 }
101 
102 inline std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node,
103  const std::vector<const INode*>& other)
104 {
105  v_node.insert(v_node.end(), other.begin(), other.end());
106  return v_node;
107 }
108 
109 inline std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node,
110  const std::vector<const INode*>& other)
111 {
112  v_node.insert(v_node.end(), other.begin(), other.end());
113  return v_node;
114 }
115 
116 #endif // SWIG
117 
118 #endif // BORNAGAIN_PARAM_NODE_INODE_H
119 #endif // USER_API
std::vector< const INode * > & operator<<(std::vector< const INode * > &v_node, const std::unique_ptr< T > &node)
Definition: INode.h:73
const double INF
Definition: INode.h:26
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:40
INode()=default
virtual std::vector< const INode * > nodeChildren() const
Returns all children.
Definition: INode.cpp:56
std::vector< const INode * > nodeOffspring() const
Returns all descendants.
Definition: INode.cpp:61
virtual std::vector< ParaMeta > parDefs() const
Returns the parameter definitions, to be hard-coded in each leaf class.
Definition: INode.h:51
void checkNodeArgs() const
Raises exception if a parameter value is invalid.
Definition: INode.cpp:27
std::vector< double > m_P
Definition: INode.h:63
virtual std::string className() const =0
Returns the class name, to be hard-coded in each leaf class that inherits from INode.
virtual ~INode()=default
Metadata of one model parameter.
Definition: INode.h:29
double vMin
Definition: INode.h:33
std::string unit
Definition: INode.h:31
std::string tooltip
Definition: INode.h:32
double vDefault
Definition: INode.h:35
double vMax
Definition: INode.h:34
std::string name
Definition: INode.h:30