SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Visit.h
Go to the documentation of this file.
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
11 #ifndef smtk_common_Visit_h
12 #define smtk_common_Visit_h
13 
14 #include <type_traits>
15 #include <utility>
16 
17 namespace smtk
18 {
19 namespace common
20 {
21 
23 enum class Visit
24 {
25  Continue,
26  Halt
27 };
28 
35 enum class Visited
36 {
37  All,
38  Some,
39  Empty
40 };
41 
55 template<typename Functor>
57 {
58  VisitorFunctor(Functor f)
59  : m_functor(f)
60  {
61  }
62 
64  template<class... Types>
65  auto invokeVisitor(Types&&... args) const -> typename std::
66  enable_if<std::is_same<decltype(std::declval<Functor>()(args...)), Visit>::value, Visit>::type
67  {
68  return m_functor(std::forward<Types>(args)...);
69  }
70 
73  template<class... Types>
74  auto invokeVisitor(Types&&... args) const -> typename std::
75  enable_if<!std::is_same<decltype(std::declval<Functor>()(args...)), Visit>::value, Visit>::type
76  {
77  m_functor(std::forward<Types>(args)...);
78  return Visit::Continue;
79  }
80 
82  template<class... Types>
83  Visit operator()(Types&&... args) const
84  {
85  return this->invokeVisitor(std::forward<Types>(args)...);
86  }
87 
88  Functor m_functor;
89 };
90 
91 } // namespace common
92 } // namespace smtk
93 
94 #endif // smtk_common_Visit_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::common::Visit::Halt
@ Halt
Stop visiting items immediately.
smtk::common::Visited::All
@ All
The visitor was invoked on every item exhaustively.
smtk::common::Visited::Empty
@ Empty
The were no values to visit.
smtk::common::VisitorFunctor::operator()
Visit operator()(Types &&... args) const
The parenthesis operation simply forwards arguments to whichever method above is enabled.
Definition: Visit.h:83
smtk::common::VisitorFunctor::invokeVisitor
auto invokeVisitor(Types &&... args) const -> typename std::enable_if< std::is_same< decltype(std::declval< Functor >()(args...)), Visit >::value, Visit >::type
For functors that return a Visit enumerant, simply invoke them:
Definition: Visit.h:65
smtk::common::Visit
Visit
Return values common to most visitor methods.
Definition: Visit.h:23
smtk::common::Visit::Continue
@ Continue
Continue to visit items.
smtk::common::VisitorFunctor::invokeVisitor
auto invokeVisitor(Types &&... args) const -> typename std::enable_if<!std::is_same< decltype(std::declval< Functor >()(args...)), Visit >::value, Visit >::type
For functors that do not return a Visit enumerant (i.e., void), invoke them but always return Visit::...
Definition: Visit.h:74
smtk::common::Visited
Visited
Return value for functions/methods that accept visitors.
Definition: Visit.h:35
smtk::common::Visited::Some
@ Some
A visitor signaled early termination.
smtk::common::VisitorFunctor
A template for accepting visitors with different return types.
Definition: Visit.h:56