BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
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 
20 #include "Param/Node/INodeVisitor.h" // not forward declared because used by all children
21 #include <limits>
22 #include <memory>
23 #include <vector>
24 
25 const double INF = std::numeric_limits<double>::infinity();
26 
27 //! Metadata of one model parameter.
28 struct ParaMeta {
29  std::string name;
30  std::string unit;
31  std::string tooltip;
32  double vMin;
33  double vMax;
34  double vDefault;
35 };
36 
37 //! Metadata of one model node.
38 struct NodeMeta {
39  std::string className;
40  std::string tooltip;
41  std::vector<ParaMeta> paraMeta;
42 };
43 
44 NodeMeta nodeMetaUnion(const std::vector<ParaMeta>& base, const NodeMeta& other);
45 
46 //! Base class for tree-like structures containing parameterized objects.
47 //! @ingroup tools_internal
48 
49 class INode : public IParametricComponent {
50 public:
51  INode() : m_NP{0} {}
52  INode(const NodeMeta& meta, const std::vector<double>& PValues);
53 
54  virtual ~INode() {}
55 
56  //! Calls the INodeVisitor's visit method
57  virtual void accept(INodeVisitor* visitor) const = 0;
58 
59  //! Returns multiline string representing tree structure below the node.
60  virtual std::string treeToString() const;
61 
62  void registerChild(INode* node);
63 
64  //! Returns a vector of children
65  virtual std::vector<const INode*> getChildren() const;
66  //! Returns a vector of all descendants
67  std::vector<const INode*> progeny() const;
68 
69  virtual void setParent(const INode* newParent);
70  const INode* parent() const;
71  INode* parent();
72 
73  //! Returns copyNumber of child, which takes into account existence of children with same name.
74  int copyNumber(const INode* node) const;
75 
76  //! Returns display name, composed from the name of node and it's copy number.
77  std::string displayName() const;
78 
79  //! Creates new parameter pool, with all local parameters and those of its children.
81 
82 private:
83  const INode* m_parent{nullptr};
84  // const std::string m_className;
85  // const std::string m_tooltip;
86 
87 protected:
88  const size_t m_NP;
89  std::vector<double> m_P;
90 };
91 
92 template <class T>
93 std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node,
94  const std::unique_ptr<T>& node)
95 {
96  if (node)
97  v_node.push_back(node.get());
98  return v_node;
99 }
100 
101 template <class T>
102 std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node,
103  const std::unique_ptr<T>& node)
104 {
105  if (node)
106  v_node.push_back(node.get());
107  return v_node;
108 }
109 
110 inline std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node, const INode* node)
111 {
112  v_node.push_back(node);
113  return v_node;
114 }
115 
116 inline std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node, const INode* node)
117 {
118  v_node.push_back(node);
119  return v_node;
120 }
121 
122 inline std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node,
123  const std::vector<const INode*>& other)
124 {
125  v_node.insert(v_node.end(), other.begin(), other.end());
126  return v_node;
127 }
128 
129 inline std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node,
130  const std::vector<const INode*>& other)
131 {
132  v_node.insert(v_node.end(), other.begin(), other.end());
133  return v_node;
134 }
135 
136 #endif // BORNAGAIN_PARAM_NODE_INODE_H
137 #endif // USER_API
Defines interface class INodeVisitor.
const double INF
Definition: INode.h:25
NodeMeta nodeMetaUnion(const std::vector< ParaMeta > &base, const NodeMeta &other)
Definition: INode.cpp:23
Defines interface IParametricComponent.
std::ostream & operator<<(std::ostream &os, const BasicVector3D< T > &a)
Output to stream.
Visitor interface to visit ISampleNode objects.
Definition: INodeVisitor.h:146
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
const INode * m_parent
Definition: INode.h:83
virtual std::string treeToString() const
Returns multiline string representing tree structure below the node.
Definition: INode.cpp:52
std::string displayName() const
Returns display name, composed from the name of node and it's copy number.
Definition: INode.cpp:115
virtual void setParent(const INode *newParent)
Definition: INode.cpp:79
INode()
Definition: INode.h:51
std::vector< const INode * > progeny() const
Returns a vector of all descendants.
Definition: INode.cpp:68
const INode * parent() const
Definition: INode.cpp:84
virtual ~INode()
Definition: INode.h:54
virtual std::vector< const INode * > getChildren() const
Returns a vector of children.
Definition: INode.cpp:63
ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.
Definition: INode.cpp:126
virtual void accept(INodeVisitor *visitor) const =0
Calls the INodeVisitor's visit method.
std::vector< double > m_P
Definition: INode.h:89
void registerChild(INode *node)
Definition: INode.cpp:57
const size_t m_NP
Definition: INode.h:88
int copyNumber(const INode *node) const
Returns copyNumber of child, which takes into account existence of children with same name.
Definition: INode.cpp:94
Manages a local parameter pool, and a tree of child pools.
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29
Metadata of one model node.
Definition: INode.h:38
std::string className
Definition: INode.h:39
std::string tooltip
Definition: INode.h:40
std::vector< ParaMeta > paraMeta
Definition: INode.h:41
Metadata of one model parameter.
Definition: INode.h:28
double vMin
Definition: INode.h:32
std::string unit
Definition: INode.h:30
std::string tooltip
Definition: INode.h:31
double vDefault
Definition: INode.h:34
double vMax
Definition: INode.h:33
std::string name
Definition: INode.h:29