NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/nostd/function_ref.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <iosfwd>
7 #include <memory> // IWYU pragma: keep
8 #include <type_traits>
9 #include <utility>
10 
11 #include "opentelemetry/version.h"
12 
14 namespace nostd
15 {
16 template <class Sig>
17 class function_ref; // IWYU pragma: keep
18 
27 template <class R, class... Args>
28 class function_ref<R(Args...)>
29 {
30  void *callable_ = nullptr;
31  R (*invoker_)(void *, Args...) = nullptr;
32 
33  template <class F>
34  using FunctionPointer = decltype(std::addressof(std::declval<F &>()));
35 
36  template <class F>
37  void BindTo(F &f) noexcept
38  {
39  callable_ = static_cast<void *>(std::addressof(f));
40  invoker_ = [](void *callable, Args... args) -> R {
41  return (*static_cast<FunctionPointer<F>>(callable))(std::forward<Args>(args)...);
42  };
43  }
44 
45  template <class R_in, class... Args_in>
46  void BindTo(R_in (*f)(Args_in...)) noexcept
47  {
48  using F = decltype(f);
49  if (f == nullptr)
50  {
51  return BindTo(nullptr);
52  }
53  callable_ = reinterpret_cast<void *>(f);
54  invoker_ = [](void *callable, Args... args) -> R {
55  return (F(callable))(std::forward<Args>(args)...);
56  };
57  }
58 
59  void BindTo(std::nullptr_t) noexcept
60  {
61  callable_ = nullptr;
62  invoker_ = nullptr;
63  }
64 
65 public:
66  template <
67  class F,
68  typename std::enable_if<!std::is_same<function_ref, typename std::decay<F>::type>::value,
69  int>::type = 0,
70  typename std::enable_if<
71 #if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG > 201402))
72  // std::result_of deprecated in C++17, removed in C++20
73  std::is_convertible<typename std::invoke_result<F, Args...>::type, R>::value,
74 #else
75  // std::result_of since C++11
76  std::is_convertible<typename std::result_of<F &(Args...)>::type, R>::value,
77 #endif
78  int>::type = 0>
79  function_ref(F &&f)
80  {
81  BindTo(f); // not forward
82  }
83 
84  function_ref(std::nullptr_t) {}
85 
86  function_ref(const function_ref &) noexcept = default;
87  function_ref(function_ref &&) noexcept = default;
88 
89  R operator()(Args... args) const { return invoker_(callable_, std::forward<Args>(args)...); }
90 
91  explicit operator bool() const { return invoker_; }
92 };
93 } // namespace nostd
nostd::function_ref
Definition: includes/opentelemetry/nostd/function_ref.h:17
nostd
Definition: includes/opentelemetry/nostd/function_ref.h:14
nostd::function_ref< R(Args...)>::function_ref
function_ref(F &&f)
Definition: includes/opentelemetry/nostd/function_ref.h:79
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
absl::type_traits_internal::result_of
std::result_of< F > result_of
Definition: includes/opentelemetry/nostd/internal/absl/meta/type_traits.h:617
nostd::invoke_result
detail::invoke_result< void, F, Args... > invoke_result
Definition: includes/opentelemetry/nostd/detail/invoke.h:128
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
nostd::function_ref< R(Args...)>::function_ref
function_ref(std::nullptr_t)
Definition: includes/opentelemetry/nostd/function_ref.h:84