vsg  1.1.0
VulkanSceneGraph library
visit.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/core/ConstVisitor.h>
16 #include <vsg/core/Visitor.h>
17 
18 namespace vsg
19 {
20 
22  //
23  // vsg::visit<> variants construct a Visitor and pass it to one or more objects accept() method
24  //
25 
28  template<class V, typename I>
29  V visit(I begin, I end)
30  {
31  V visitor;
32  for (I itr = begin; itr != end; ++itr)
33  {
34  (*itr)->accept(visitor);
35  }
36  return visitor;
37  }
38 
41  template<class V, typename P>
42  V visit(vsg::ref_ptr<P> ptr)
43  {
44  V visitor;
45  if (ptr) ptr->accept(visitor);
46  return visitor;
47  }
48 
51  template<class V, typename P>
52  V visit(P* ptr)
53  {
54  V visitor;
55  if (ptr) ptr->accept(visitor);
56  return visitor;
57  }
58 
61  template<class V, typename C>
62  V visit(const C& container)
63  {
64  V visitor;
65  for (const auto& object : container)
66  {
67  object->accept(visitor);
68  }
69  return visitor;
70  }
71 
74  template<class V, typename C>
75  V visit(C& container)
76  {
77  V visitor;
78  for (auto& object : container)
79  {
80  object->accept(visitor);
81  }
82  return visitor;
83  }
84 
86  //
87  // vsg::visit<> variants that pass an existing Visitor to one or more objects accept() method, returning a reference to the visitor
88  //
89 
94  template<class V, typename I>
95  V& visit(V& visitor, I begin, I end)
96  {
97  for (I itr = begin; itr != end; ++itr)
98  {
99  (*itr)->accept(visitor);
100  }
101  return visitor;
102  }
103 
108  template<class V, typename P>
109  V& visit(V& visitor, vsg::ref_ptr<P> ptr)
110  {
111  if (ptr) ptr->accept(visitor);
112  return visitor;
113  }
114 
119  template<class V, typename P>
120  V& visit(V& visitor, P* ptr)
121  {
122  if (ptr) ptr->accept(visitor);
123  return visitor;
124  }
125 
130  template<class V, typename C>
131  V& visit(V& visitor, const C& container)
132  {
133  for (const auto& object : container)
134  {
135  object->accept(visitor);
136  }
137  return visitor;
138  }
139 
144  template<class V, typename C>
145  V& visit(V& visitor, C& container)
146  {
147  for (auto& object : container)
148  {
149  object->accept(visitor);
150  }
151  return visitor;
152  }
153 
154 } // namespace vsg
Definition: ref_ptr.h:22