BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
NodeProgeny.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Core/Export/NodeProgeny.h
6 //! @brief Defines namespace node_progeny.
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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_CORE_EXPORT_NODEPROGENY_H
21 #define BORNAGAIN_CORE_EXPORT_NODEPROGENY_H
22 
23 #include "Param/Node/INode.h"
24 
25 namespace node_progeny {
26 
27 template <typename T> std::vector<const T*> ChildNodesOfType(const INode& node)
28 {
29  std::vector<const T*> result;
30  for (const auto& p_child : node.getChildren()) {
31  if (const auto* t = dynamic_cast<const T*>(p_child))
32  result.push_back(t);
33  }
34  return result;
35 }
36 
37 template <typename T> const T* OnlyChildOfType(const INode& node)
38 {
39  const auto list = ChildNodesOfType<T>(node);
40  if (list.size() != 1)
41  return nullptr;
42  return list.front();
43 }
44 
45 template <typename T> std::vector<const T*> AllDescendantsOfType(const INode& node)
46 {
47  std::vector<const T*> result;
48  for (const auto* p_child : node.getChildren()) {
49  if (const auto* t = dynamic_cast<const T*>(p_child))
50  result.push_back(t);
51  for (const auto* t : AllDescendantsOfType<T>(*p_child))
52  result.push_back(t);
53  }
54  return result;
55 }
56 
57 } // namespace node_progeny
58 
59 #endif // BORNAGAIN_CORE_EXPORT_NODEPROGENY_H
60 #endif // USER_API
Defines interface INode.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
virtual std::vector< const INode * > getChildren() const
Returns a vector of children.
Definition: INode.cpp:63
std::vector< const T * > AllDescendantsOfType(const INode &node)
Definition: NodeProgeny.h:45
std::vector< const T * > ChildNodesOfType(const INode &node)
Definition: NodeProgeny.h:27
const T * OnlyChildOfType(const INode &node)
Definition: NodeProgeny.h:37