Publishing 2019 R3 content
[platform/upstream/dldt.git] / inference-engine / src / vpu / common / include / vpu / utils / func_ref.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <utility>
8 #include <type_traits>
9
10 namespace vpu {
11
12 //
13 // Non-owning alternative for std::function
14 //
15
16 template <typename> class FuncRef;
17
18 template <typename R, typename... Args>
19 class FuncRef<R(Args...)> {
20 public:
21     template <class Func>
22     FuncRef(const Func& func) :
23             _realFuncPtr(&func),
24             _impl(&caller<typename std::remove_reference<Func>::type>) {
25         using actual_result_type = typename std::result_of<Func(Args...)>::type;
26         static_assert(
27             !std::is_reference<R>::value || std::is_reference<actual_result_type>::value,
28             "Mismatch between Func and FuncRef prototype");
29     }
30
31     FuncRef(const FuncRef&) = delete;
32     FuncRef& operator=(const FuncRef&) = delete;
33
34     FuncRef(FuncRef&&) = delete;
35     FuncRef& operator=(FuncRef&&) = delete;
36
37     R operator()(Args... args) const {
38         return _impl(_realFuncPtr, std::forward<Args>(args)...);
39     }
40
41 private:
42     template <class Func>
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)...);
46     }
47
48 private:
49     using ImplFunc = R(*)(const void*, Args...);
50
51     const void* _realFuncPtr = nullptr;
52     ImplFunc _impl = nullptr;
53 };
54
55 }  // namespace vpu