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