LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
visitor.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include <boost/variant.hpp>
33 #include "void.h"
34 #include "util.h"
35 #include "typelist.h"
36 #include "typelevel.h"
37 #include "detector.h"
38 
39 namespace LeechCraft
40 {
41 namespace Util
42 {
43  namespace detail
44  {
45  template<typename... Bases>
46  struct VisitorBase : std::decay_t<Bases>...
47  {
48  VisitorBase (Bases&&... bases)
49  : std::decay_t<Bases> { std::forward<Bases> (bases) }...
50  {
51  }
52 
53  using std::decay_t<Bases>::operator()...;
54  };
55 
56  template<typename R, typename... Args>
57  struct Visitor : boost::static_visitor<R>
58  , VisitorBase<Args...>
59  {
60  using VisitorBase<Args...>::VisitorBase;
61  };
62 
63  template<typename T>
64  constexpr T Declval () { throw "shall not be called"; }
65 
66  template<typename... Args>
67  constexpr bool AllLValueRefs = AllOf<std::is_lvalue_reference, Args...>;
68 
69  template<typename... Args>
70  constexpr bool AllRValueRefs = AllOf<std::is_rvalue_reference, Args...>;
71 
72  template<typename... Args>
73  constexpr bool AllConsts = AllOf<std::is_const, Args...>;
74 
75  template<typename... Args>
76  constexpr bool AllConstsWithoutRefs = AllOf<std::is_const, std::remove_reference_t<Args>...>;
77 
78  template<typename R, typename... Args>
79  constexpr decltype (auto) FixCommonType ()
80  {
81  if constexpr (AllConsts<Args...>)
82  return Declval<std::add_const_t<R>> ();
83  else if constexpr (AllLValueRefs<Args...>)
84  {
85  if constexpr (AllConstsWithoutRefs<Args...>)
86  return Declval<std::add_lvalue_reference_t<std::add_const_t<R>>> ();
87  else
88  return Declval<std::add_lvalue_reference_t<R>> ();
89  }
90  else if constexpr (AllRValueRefs<Args...>)
91  {
92  static_assert (!AllConstsWithoutRefs<Args...>, "const rvalue references are useless");
93  return Declval<std::add_rvalue_reference_t<R>> ();
94  }
95  else
96  return Declval<R> ();
97  }
98 
99  template<typename R, typename... Args>
100  using FixCommonType_t = decltype (FixCommonType<R, Args...> ());
101 
102  template<typename... Vars,
103  typename... Args,
104  typename Common = std::common_type_t<std::result_of_t<detail::VisitorBase<Args...> (Vars&)>...>,
105  typename Res = FixCommonType_t<Common, std::result_of_t<detail::VisitorBase<Args...> (Vars&)>...>>
107  }
108 
109  template<typename... Vars, typename... Args>
110  decltype (auto) Visit (const boost::variant<Vars...>& v, Args&&... args)
111  {
112  using R_t = decltype (detail::DetectCommonType (Typelist<Vars...> {}, Typelist<Args...> {}));
113 
114  detail::Visitor<R_t, Args...> visitor { std::forward<Args> (args)... };
115  return boost::apply_visitor (visitor, v);
116  }
117 
118  template<typename... Vars, typename... Args>
119  decltype (auto) Visit (boost::variant<Vars...>& v, Args&&... args)
120  {
121  using R_t = decltype (detail::DetectCommonType (Typelist<Vars...> {}, Typelist<Args...> {}));
122 
123  detail::Visitor<R_t, Args...> visitor { std::forward<Args> (args)... };
124  return boost::apply_visitor (visitor, v);
125  }
126 
127  namespace detail
128  {
129  struct VisitorFinallyTag {};
130  }
131 
132  template<typename FinallyFunc, typename... Args>
133  class Visitor
134  {
135  detail::VisitorBase<Args...> Base_;
136 
137  FinallyFunc Finally_;
138  public:
139  Visitor (Args&&... args)
140  : Base_ { std::forward<Args> (args)... }
141  {
142  }
143 
144  Visitor (const detail::VisitorFinallyTag&, Args&&... args, FinallyFunc&& func)
145  : Base_ { std::forward<Args> (args)... }
146  , Finally_ { std::forward<FinallyFunc> (func) }
147  {
148  }
149 
150  template<typename T>
151  decltype (auto) operator() (const T& var) const
152  {
153  if constexpr (std::is_same_v<FinallyFunc, Void>)
154  return Visit (var, Base_);
155  else
156  {
157  const auto guard = MakeScopeGuard (Finally_);
158  return Visit (var, Base_);
159  }
160  }
161 
162  template<typename F>
163  Visitor<F, detail::VisitorBase<Args...>> Finally (F&& func)
164  {
165  return { detail::VisitorFinallyTag {}, std::move (Base_), std::forward<F> (func) };
166  }
167  };
168 
169  template<typename... Args>
170  Visitor (Args&&...) -> Visitor<Void, Args...>;
171 
172  template<typename T, typename... Args>
173  auto InvokeOn (T&& t, Args&&... args)
174  {
175  return detail::VisitorBase<Args...> { std::forward<Args> (args)... } (std::forward<T> (t));
176  }
177 }
178 }
Definition: prelude.h:39
decltype(FixCommonType< R, Args... >()) FixCommonType_t
Definition: visitor.h:100
decltype(auto) constexpr FixCommonType()
Definition: visitor.h:79
STL namespace.
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:157
constexpr Res DetectCommonType(Typelist< Vars... >, Typelist< Args... >)
VisitorBase(Bases &&... bases)
Definition: visitor.h:48
constexpr bool AllLValueRefs
Definition: visitor.h:67
constexpr bool AllConstsWithoutRefs
Definition: visitor.h:76
constexpr auto AllOf
Definition: typelevel.h:55
constexpr bool AllRValueRefs
Definition: visitor.h:70
Visitor(Args &&... args)
Definition: visitor.h:139
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:209
Visitor(Args &&...) -> Visitor< Void, Args... >
Visitor(const detail::VisitorFinallyTag &, Args &&... args, FinallyFunc &&func)
Definition: visitor.h:144
constexpr bool AllConsts
Definition: visitor.h:73
constexpr T Declval()
Definition: visitor.h:64
auto InvokeOn(T &&t, Args &&... args)
Definition: visitor.h:173
Visitor< F, detail::VisitorBase< Args... > > Finally(F &&func)
Definition: visitor.h:163