Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
function_ref.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef FUB_CORE_FUNCTION_REF
22 #define FUB_CORE_FUNCTION_REF
23 
24 #include "fub/core/type_traits.hpp"
25 
26 #include <cstdint>
27 #include <type_traits>
28 #include <utility>
29 
30 namespace fub {
31 /// An efficient, type-erasing, non-owning reference to a callable. This is
32 /// intended for use as the type of a function parameter that is not used
33 /// after the function in question returns.
34 ///
35 /// This class does not own the callable, so it is not in general safe to store
36 /// a function_ref.
37 template <typename Fn> class function_ref;
38 
39 template <typename F, typename... Args>
40 using IsInvocableT_ = decltype(std::declval<F>()(std::declval<Args>()...));
41 
42 template <typename F, typename R, typename... Args>
43 struct IsInvocable : is_detected_exact<R, IsInvocableT_, F, Args...> {};
44 
45 /// An efficient, type-erasing, non-owning reference to a callable. This is
46 /// intended for use as the type of a function parameter that is not used
47 /// after the function in question returns.
48 ///
49 /// This class does not own the callable, so it is not in general safe to store
50 /// a function_ref.
51 template <typename Ret, typename... Params> class function_ref<Ret(Params...)> {
52  Ret (*callback)(std::intptr_t callable, Params... params);
53  std::intptr_t callable;
54 
55  template <typename Callable>
56  static Ret callback_fn(std::intptr_t callable, Params... params) {
57  return (*reinterpret_cast<Callable*>(callable))(
58  std::forward<Params>(params)...);
59  }
60 
61 public:
62  template <
63  typename Callable,
64  typename = std::enable_if_t<IsInvocable<Callable, Ret, Params...>::value>>
65  function_ref(Callable&& callable,
66  typename std::enable_if<
67  !std::is_same<typename std::remove_reference<Callable>::type,
68  function_ref>::value>::type* = nullptr)
69  : callback(callback_fn<typename std::remove_reference<Callable>::type>),
70  callable(reinterpret_cast<std::intptr_t>(&callable)) {}
71  Ret operator()(Params... params) const {
72  return callback(callable, std::forward<Params>(params)...);
73  }
74 };
75 } // namespace fub
76 
77 #endif
static Ret callback_fn(std::intptr_t callable, Params... params)
Definition: function_ref.hpp:56
function_ref(Callable &&callable, typename std::enable_if< !std::is_same< typename std::remove_reference< Callable >::type, function_ref >::value >::type *=nullptr)
Definition: function_ref.hpp:65
std::intptr_t callable
Definition: function_ref.hpp:53
Ret operator()(Params... params) const
Definition: function_ref.hpp:71
An efficient, type-erasing, non-owning reference to a callable.
Definition: function_ref.hpp:37
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
decltype(std::declval< F >()(std::declval< Args >()...)) IsInvocableT_
Definition: function_ref.hpp:40
Definition: function_ref.hpp:43
This is std::true_type if Op<Args...> is a valid SFINAE expression and the return type is exactly Exp...
Definition: type_traits.hpp:108
This file adds basic type traits utilities which are not yet implemented in all standard libraries.