OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
TreeTraversalVisitor.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_TreeTraversalVisitor_h_GUID_A75E39C9_AB5B_4494_0BF6_E8C3BA1BCB34
26 #define INCLUDED_TreeTraversalVisitor_h_GUID_A75E39C9_AB5B_4494_0BF6_E8C3BA1BCB34
27 
28 // Internal Includes
29 #include <osvr/Util/TreeNode_fwd.h>
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <utility>
36 
37 namespace osvr {
38 namespace util {
39  namespace tree {
42  template <typename F> class TreeTraversalWrapper {
43  public:
44  explicit TreeTraversalWrapper(F &&functor)
45  : m_functor(std::move(functor)) {}
46 
49  operator=(TreeTraversalWrapper const &) = delete;
50 
51  template <typename T> void operator()(TreeNode<T> &node) {
52  m_functor(node);
53  node.visitChildren(*this);
54  }
55  template <typename T> void operator()(TreeNode<T> const &node) {
56  m_functor(node);
57  node.visitConstChildren(*this);
58  }
59 
60  private:
61  F m_functor;
62  };
63 
66  template <typename T, typename F>
67  inline void traverseWith(T &node, F &&functor) {
68  TreeTraversalWrapper<F> funcWrap{std::forward<F>(functor)};
69  funcWrap(node);
70  }
71  } // namespace tree
72  using tree::traverseWith;
73 } // namespace util
74 } // namespace osvr
75 #endif // INCLUDED_TreeTraversalVisitor_h_GUID_A75E39C9_AB5B_4494_0BF6_E8C3BA1BCB34
void visitConstChildren(F &visitor) const
Generic constant visitation method that calls a functor on each of the children (as const) in an unde...
Definition: TreeNode.h:152
A node in a generic tree, which can contain an object by value.
Definition: TreeNode.h:72
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
void traverseWith(T &node, F &&functor)
A method to handle visiting every node in a tree with a lambda or other by-value function object...
void visitChildren(F &visitor)
Generic visitation method that calls a functor on each of the children in an undefined order...
Definition: TreeNode.h:141
A wrapper for pre-order traversal of a TreeNode-based tree with something like a lambda.