visitor.h Source File

CPP API: visitor.h Source File
visitor.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef VISITOR_H
21 #define VISITOR_H
22 
23 namespace mio
24 {
25 
51 template <typename... Types>
52 struct Visitor;
53 
54 template <typename T>
55 struct Visitor<T> {
56  virtual void visit(T& t) = 0;
57 
58  virtual ~Visitor() = default;
59 };
60 
61 template <typename T, typename... Types>
62 struct Visitor<T, Types...> : public Visitor<Types...> // Recursive
63 {
64  using Visitor<Types...>::visit;
65  virtual void visit(T& t) = 0;
66 
67  virtual ~Visitor() = default;
68 };
69 
70 template <typename... Types>
71 struct ConstVisitor;
72 
73 template <typename T>
74 struct ConstVisitor<T> {
75  virtual void visit(const T& t) = 0;
76 
77  virtual ~ConstVisitor() = default;
78 };
79 
80 template <typename T, typename... Types>
81 struct ConstVisitor<T, Types...> : public ConstVisitor<Types...> // Recursive
82 {
83  using ConstVisitor<Types...>::visit;
84  virtual void visit(const T& t) = 0;
85 
86  virtual ~ConstVisitor() = default;
87 };
88 
89 template <class Derived, class Base, class Visitor, class ConstVisitor>
90 struct Visitable : public Base {
91  using Base::Base;
92 
93  void accept(Visitor& visitor)
94  {
95  visitor.visit(*static_cast<Derived*>(this));
96  }
97  void accept(ConstVisitor& visitor) const
98  {
99  visitor.visit(*static_cast<Derived const*>(this));
100  }
101 };
102 
103 } // namespace mio
104 
105 #endif // VISITOR_H
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
virtual void visit(const T &t)=0
virtual void visit(const T &t)=0
virtual ~ConstVisitor()=default
Definition: visitor.h:71
Definition: visitor.h:90
void accept(ConstVisitor &visitor) const
Definition: visitor.h:97
void accept(Visitor &visitor)
Definition: visitor.h:93
virtual ~Visitor()=default
virtual void visit(T &t)=0
virtual ~Visitor()=default
virtual void visit(T &t)=0
A generic visitor inspired by Fedor Pikus.
Definition: visitor.h:52