BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
INodeUtils.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Core/Export/INodeUtils.h
6 //! @brief Defines namespace INodeUtils.
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_EXPORT_INODEUTILS_H
16 #define BORNAGAIN_CORE_EXPORT_INODEUTILS_H
17 
18 #include "Param/Node/INode.h"
19 
20 namespace INodeUtils
21 {
22 template <typename T> std::vector<const T*> ChildNodesOfType(const INode& node)
23 {
24  std::vector<const T*> result;
25  for (const auto& p_child : node.getChildren()) {
26  if (const auto* t = dynamic_cast<const T*>(p_child))
27  result.push_back(t);
28  }
29  return result;
30 }
31 
32 template <typename T> const T* OnlyChildOfType(const INode& node)
33 {
34  const auto list = ChildNodesOfType<T>(node);
35  if (list.size() != 1)
36  return nullptr;
37  return list.front();
38 }
39 
40 template <typename T> std::vector<const T*> AllDescendantsOfType(const INode& node)
41 {
42  std::vector<const T*> result;
43  for (const auto* p_child : node.getChildren()) {
44  if (const auto* t = dynamic_cast<const T*>(p_child))
45  result.push_back(t);
46  for (const auto* t : AllDescendantsOfType<T>(*p_child))
47  result.push_back(t);
48  }
49  return result;
50 }
51 } // namespace INodeUtils
52 
53 #endif // BORNAGAIN_CORE_EXPORT_INODEUTILS_H
Defines class 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 (const).
Definition: INode.cpp:64
const T * OnlyChildOfType(const INode &node)
Definition: INodeUtils.h:32
std::vector< const T * > AllDescendantsOfType(const INode &node)
Definition: INodeUtils.h:40
std::vector< const T * > ChildNodesOfType(const INode &node)
Definition: INodeUtils.h:22