Leosac  0.8.0
Open Source Access Control
IVisitable.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "tools/Visitor.hpp"
23 #include <cassert>
24 
25 namespace Leosac
26 {
27 namespace Tools
28 {
29 
44 {
45  public:
49  virtual void accept(::Leosac::Tools::BaseVisitor &) = 0;
50 
54  virtual void accept(::Leosac::Tools::BaseVisitor &) const = 0;
55 
56  protected:
57  template <class T>
58  static bool visitor_dispatch(T &visited, BaseVisitor &visitor,
59  bool abort_on_failure)
60  {
61  using VisitedT = std::remove_reference_t<std::remove_const_t<T>>;
62  if (Visitor<VisitedT> *p = dynamic_cast<Visitor<VisitedT> *>(&visitor))
63  {
64  p->visit(visited);
65  return true;
66  }
67  else
68  {
69  if (abort_on_failure)
70  {
71  visitor.cannot_visit(static_cast<const IVisitable &>(visited));
72  }
73  return false;
74  }
75  }
76 };
77 
85 #define MAKE_VISITABLE() \
86  virtual void accept(::Leosac::Tools::BaseVisitor &v) override \
87  { \
88  visitor_dispatch(*this, v, true); \
89  } \
90  virtual void accept(::Leosac::Tools::BaseVisitor &v) const override \
91  { \
92  visitor_dispatch(*this, v, true); \
93  }
94 
104 #define MAKE_VISITABLE_FALLBACK(PARENT_CLASS) \
105  virtual void accept(::Leosac::Tools::BaseVisitor &v) override \
106  { \
107  if (!visitor_dispatch(*this, v, false)) \
108  { \
109  PARENT_CLASS::accept(v); \
110  } \
111  } \
112  virtual void accept(::Leosac::Tools::BaseVisitor &v) const override \
113  { \
114  if (!visitor_dispatch(*this, v, false)) \
115  { \
116  PARENT_CLASS::accept(v); \
117  } \
118  }
119 }
120 }
Leosac::Tools::BaseVisitor::cannot_visit
virtual void cannot_visit(const IVisitable &)
Invoked when the visitable cannot be visited by the visitor.
Definition: Visitor.cpp:30
Leosac::Tools::BaseVisitor
Base class for visitor, should not be used directly.
Definition: Visitor.hpp:37
Leosac::Tools::Visitor
A Visitor object.
Definition: Visitor.hpp:64
Leosac::Tools::IVisitable::accept
virtual void accept(::Leosac::Tools::BaseVisitor &)=0
Accept a visitor that may mutate this.
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Tools::IVisitable::visitor_dispatch
static bool visitor_dispatch(T &visited, BaseVisitor &visitor, bool abort_on_failure)
Definition: IVisitable.hpp:58
Visitor.hpp
Leosac::Tools::IVisitable
Base class to make an object visitable.
Definition: IVisitable.hpp:43