Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
visitable.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//visitable.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//visitable.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with visitable.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
27
28#ifndef BERTINI_DETAIL_VISITABLE_HPP
29#define BERTINI_DETAIL_VISITABLE_HPP
30
33
34namespace bertini{
35
36 namespace policy{
37
45 template<class VisitedT, typename RetT>
47 {
49 static RetT OnUnknownVisitor(VisitedT&a, VisitorBase&) // VisitorBase is inherited from using CRTP -- it's not a template parameter to this function, so its typeid name is useless.
50 {
51 std::cout << "unknown visitor: " << a << " of type " << typeid(VisitedT).name() << ". Make sure you've added it in all three places! two in the class definition (type inheritance listing as visiting, and virtual function declaration), and one in cpp source (function definition)." << std::endl;
52 return VisitedT();
53 }
54 };
55
57 template<class VisitedT, typename RetT>
59 {
61 static RetT OnUnknownVisitor(VisitedT&a, VisitorBase&)
62 {
63 std::stringstream err_msg;
64 err_msg << "unknown visitor: " << a << " of type " << typeid(VisitedT).name() << ". Make sure you've added it in all three places! two in the class definition (type inheritance listing as visiting, and virtual function declaration), and one in cpp source (function definition)." << std::endl;
65 throw std::runtime_error(err_msg.str());
66 }
67 };
68
71 template<class VisitedT, typename RetT>
73 } // namespace policy
74
75
76
77
85 template< typename RetT = void, template<class,class> class CatchAll = policy::DefaultCatchAll>
87 {
88 public:
89 typedef RetT VisitReturnType;
90 virtual ~VisitableBase() = default;
92 virtual VisitReturnType Accept(VisitorBase&) = 0; // the implementation will either be provided by a macro, or by hand, for each class which is visitable.
93
94 protected:
95
106 template<typename T>
107 static
109 {
110 if (auto p = dynamic_cast<Visitor<T>*>(&guest))
111 return p->Visit(visited);
112 else
113 return CatchAll<T,RetT>::OnUnknownVisitor(visited, guest);
114 }
115 };
116
117
121 #define BERTINI_DEFAULT_VISITABLE() \
122 virtual VisitReturnType Accept(VisitorBase& guest) override \
123 { return AcceptBase(*this, guest); }
124
125} // namespace bertini
126
127
128#endif
The base class for visitable types.
Definition visitable.hpp:87
virtual VisitReturnType Accept(VisitorBase &)=0
Accept a visitor (dispatch to the visitor's Visit for this concrete type).
virtual ~VisitableBase()=default
static VisitReturnType AcceptBase(T &visited, VisitorBase &guest)
Abstract method for how to accept a visitor.
Definition visitable.hpp:108
RetT VisitReturnType
The type returned when a visitor accepts this object.
Definition visitable.hpp:89
A strawman class for implementing the visitor pattern.
Definition visitor.hpp:42
The first non-trivial visitor class.
Definition visitor.hpp:58
Contains the detail/events base types.
Definition visitable.hpp:36
RaiseExceptionWithTypeNamesInMessage< VisitedT, RetT > DefaultCatchAll
The default policy for what to do when a visitable is visited by an unknown visitor.
Definition visitable.hpp:72
Definition algorithm_builder.hpp:36
A policy for what to do when a visited type is encoutered by a visitor it doesn't know.
Definition visitable.hpp:47
static RetT OnUnknownVisitor(VisitedT &a, VisitorBase &)
Default behaviour on an unknown visitor: warn and return a default-constructed value.
Definition visitable.hpp:49
A policy that throws (with the offending type names) when an unknown visitor is encountered.
Definition visitable.hpp:59
static RetT OnUnknownVisitor(VisitedT &a, VisitorBase &)
Behaviour on an unknown visitor: throw a std::runtime_error naming the visited type.
Definition visitable.hpp:61
Contains the visitor base types.