1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
13 // Non-owning alternative for std::function
16 template <typename> class FuncRef;
18 template <typename R, typename... Args>
19 class FuncRef<R(Args...)> {
22 FuncRef(const Func& func) :
24 _impl(&caller<typename std::remove_reference<Func>::type>) {
25 using actual_result_type = typename std::result_of<Func(Args...)>::type;
27 !std::is_reference<R>::value || std::is_reference<actual_result_type>::value,
28 "Mismatch between Func and FuncRef prototype");
31 FuncRef(const FuncRef&) = delete;
32 FuncRef& operator=(const FuncRef&) = delete;
34 FuncRef(FuncRef&&) = delete;
35 FuncRef& operator=(FuncRef&&) = delete;
37 R operator()(Args... args) const {
38 return _impl(_realFuncPtr, std::forward<Args>(args)...);
43 static R caller(const void* realFuncPtr, Args... args) {
44 const auto& realFunc = *static_cast<const Func*>(realFuncPtr);
45 return realFunc(std::forward<Args>(args)...);
49 using ImplFunc = R(*)(const void*, Args...);
51 const void* _realFuncPtr = nullptr;
52 ImplFunc _impl = nullptr;