Coverage Report

Created: 2025-10-09 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/functional/internal/function_ref.h
Line
Count
Source
1
// Copyright 2019 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
16
#define ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
17
18
#include <cassert>
19
#include <functional>
20
#include <type_traits>
21
22
#include "absl/functional/any_invocable.h"
23
#include "absl/meta/type_traits.h"
24
25
namespace absl {
26
ABSL_NAMESPACE_BEGIN
27
namespace functional_internal {
28
29
// Like a void* that can handle function pointers as well. The standard does not
30
// allow function pointers to round-trip through void*, but void(*)() is fine.
31
//
32
// Note: It's important that this class remains trivial and is the same size as
33
// a pointer, since this allows the compiler to perform tail-call optimizations
34
// when the underlying function is a callable object with a matching signature.
35
union VoidPtr {
36
  const void* obj;
37
  void (*fun)();
38
};
39
40
// Chooses the best type for passing T as an argument.
41
// Attempt to be close to SystemV AMD64 ABI. Objects with trivial copy ctor are
42
// passed by value.
43
template <typename T,
44
          bool IsLValueReference = std::is_lvalue_reference<T>::value>
45
struct PassByValue : std::false_type {};
46
47
template <typename T>
48
struct PassByValue<T, /*IsLValueReference=*/false>
49
    : std::integral_constant<bool,
50
                             absl::is_trivially_copy_constructible<T>::value &&
51
                                 absl::is_trivially_copy_assignable<
52
                                     typename std::remove_cv<T>::type>::value &&
53
                                 std::is_trivially_destructible<T>::value &&
54
                                 sizeof(T) <= 2 * sizeof(void*)> {};
55
56
template <typename T>
57
struct ForwardT : std::conditional<PassByValue<T>::value, T, T&&> {};
58
59
// An Invoker takes a pointer to the type-erased invokable object, followed by
60
// the arguments that the invokable object expects.
61
//
62
// Note: The order of arguments here is an optimization, since member functions
63
// have an implicit "this" pointer as their first argument, putting VoidPtr
64
// first allows the compiler to perform tail-call optimization in many cases.
65
template <typename R, typename... Args>
66
using Invoker = R (*)(VoidPtr, typename ForwardT<Args>::type...);
67
68
//
69
// InvokeObject and InvokeFunction provide static "Invoke" functions that can be
70
// used as Invokers for objects or functions respectively.
71
//
72
// static_cast<R> handles the case the return type is void.
73
template <typename Obj, typename R, typename... Args>
74
0
R InvokeObject(VoidPtr ptr, typename ForwardT<Args>::type... args) {
75
0
  using T = std::remove_reference_t<Obj>;
76
0
  return static_cast<R>(std::invoke(
77
0
      std::forward<Obj>(*const_cast<T*>(static_cast<const T*>(ptr.obj))),
78
0
      std::forward<typename ForwardT<Args>::type>(args)...));
79
0
}
Unexecuted instantiation: void absl::functional_internal::InvokeObject<absl::Cord::HashFragmented<absl::hash_internal::MixingHashState>(absl::hash_internal::MixingHashState) const::{lambda(std::__1::basic_string_view<char, std::__1::char_traits<char> >)#1}&, void, std::__1::basic_string_view<char, std::__1::char_traits<char> > >(absl::functional_internal::VoidPtr, absl::functional_internal::ForwardT<std::__1::basic_string_view<char, std::__1::char_traits<char> > >::type)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::InvokeObject<absl::str_format_internal::(anonymous namespace)::BinaryToDecimal::RunConversion(absl::uint128, int, absl::FunctionRef<void (absl::str_format_internal::(anonymous namespace)::BinaryToDecimal)>)::{lambda(absl::Span<unsigned int>)#1}&, void, absl::Span<unsigned int> >(absl::functional_internal::VoidPtr, absl::functional_internal::ForwardT<absl::Span<unsigned int> >::type)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::InvokeObject<absl::str_format_internal::(anonymous namespace)::FormatFPositiveExpSlow(absl::uint128, int, absl::str_format_internal::(anonymous namespace)::FormatState const&)::$_0&, void, absl::str_format_internal::(anonymous namespace)::BinaryToDecimal>(absl::functional_internal::VoidPtr, absl::functional_internal::ForwardT<absl::str_format_internal::(anonymous namespace)::BinaryToDecimal>::type)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::InvokeObject<absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator::RunConversion(absl::uint128, int, absl::FunctionRef<void (absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator)>)::{lambda(absl::Span<unsigned int>)#1}&, void, absl::Span<unsigned int> >(absl::functional_internal::VoidPtr, absl::functional_internal::ForwardT<absl::Span<unsigned int> >::type)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::InvokeObject<absl::str_format_internal::(anonymous namespace)::FormatFNegativeExpSlow(absl::uint128, int, absl::str_format_internal::(anonymous namespace)::FormatState const&)::$_0&, void, absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator>(absl::functional_internal::VoidPtr, absl::functional_internal::ForwardT<absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator>::type)
80
81
template <typename Obj, typename Fun, Fun F, typename R, typename... Args>
82
R InvokeObject(VoidPtr ptr, typename ForwardT<Args>::type... args) {
83
  using T = std::remove_reference_t<Obj>;
84
  Obj&& obj =
85
      std::forward<Obj>(*const_cast<T*>(static_cast<const T*>(ptr.obj)));
86
  // Avoid std::invoke() since the callee is a known function at compile time
87
  if constexpr (std::is_member_function_pointer_v<Fun>) {
88
    return static_cast<R>((std::forward<Obj>(obj).*F)(
89
        std::forward<typename ForwardT<Args>::type>(args)...));
90
  } else {
91
    return static_cast<R>(
92
        F(std::forward<Obj>(obj),
93
          std::forward<typename ForwardT<Args>::type>(args)...));
94
  }
95
}
96
97
template <typename T, typename Fun, Fun F, typename R, typename... Args>
98
R InvokePtr(VoidPtr ptr, typename ForwardT<Args>::type... args) {
99
  T* obj = const_cast<T*>(static_cast<const T*>(ptr.obj));
100
  // Avoid std::invoke() since the callee is a known function at compile time
101
  if constexpr (std::is_member_function_pointer_v<Fun>) {
102
    return static_cast<R>(
103
        (obj->*F)(std::forward<typename ForwardT<Args>::type>(args)...));
104
  } else {
105
    return static_cast<R>(
106
        F(obj, std::forward<typename ForwardT<Args>::type>(args)...));
107
  }
108
}
109
110
template <typename Fun, typename R, typename... Args>
111
R InvokeFunction(VoidPtr ptr, typename ForwardT<Args>::type... args) {
112
  auto f = reinterpret_cast<Fun>(ptr.fun);
113
  return static_cast<R>(
114
      std::invoke(f, std::forward<typename ForwardT<Args>::type>(args)...));
115
}
116
117
template <typename Fun, Fun F, typename R, typename... Args>
118
R InvokeFunction(VoidPtr, typename ForwardT<Args>::type... args) {
119
  return static_cast<R>(
120
      F(std::forward<typename ForwardT<Args>::type>(args)...));
121
}
122
123
template <typename Sig>
124
void AssertNonNull(const std::function<Sig>& f) {
125
  assert(f != nullptr);
126
  (void)f;
127
}
128
129
template <typename Sig>
130
void AssertNonNull(const AnyInvocable<Sig>& f) {
131
  assert(f != nullptr);
132
  (void)f;
133
}
134
135
template <typename F>
136
0
void AssertNonNull(const F&) {}
Unexecuted instantiation: void absl::functional_internal::AssertNonNull<absl::Cord::HashFragmented<absl::hash_internal::MixingHashState>(absl::hash_internal::MixingHashState) const::{lambda(std::__1::basic_string_view<char, std::__1::char_traits<char> >)#1}>(absl::Cord::HashFragmented<absl::hash_internal::MixingHashState>(absl::hash_internal::MixingHashState) const::{lambda(std::__1::basic_string_view<char, std::__1::char_traits<char> >)#1} const&)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::AssertNonNull<absl::str_format_internal::(anonymous namespace)::BinaryToDecimal::RunConversion(absl::uint128, int, absl::FunctionRef<void (absl::str_format_internal::(anonymous namespace)::BinaryToDecimal)>)::{lambda(absl::Span<unsigned int>)#1}>(absl::str_format_internal::(anonymous namespace)::BinaryToDecimal::RunConversion(absl::uint128, int, absl::FunctionRef<void (absl::str_format_internal::(anonymous namespace)::BinaryToDecimal)>)::{lambda(absl::Span<unsigned int>)#1} const&)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::AssertNonNull<absl::str_format_internal::(anonymous namespace)::FormatFPositiveExpSlow(absl::uint128, int, absl::str_format_internal::(anonymous namespace)::FormatState const&)::$_0>(absl::str_format_internal::(anonymous namespace)::FormatFPositiveExpSlow(absl::uint128, int, absl::str_format_internal::(anonymous namespace)::FormatState const&)::$_0 const&)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::AssertNonNull<absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator::RunConversion(absl::uint128, int, absl::FunctionRef<void (absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator)>)::{lambda(absl::Span<unsigned int>)#1}>(absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator::RunConversion(absl::uint128, int, absl::FunctionRef<void (absl::str_format_internal::(anonymous namespace)::FractionalDigitGenerator)>)::{lambda(absl::Span<unsigned int>)#1} const&)
Unexecuted instantiation: float_conversion.cc:void absl::functional_internal::AssertNonNull<absl::str_format_internal::(anonymous namespace)::FormatFNegativeExpSlow(absl::uint128, int, absl::str_format_internal::(anonymous namespace)::FormatState const&)::$_0>(absl::str_format_internal::(anonymous namespace)::FormatFNegativeExpSlow(absl::uint128, int, absl::str_format_internal::(anonymous namespace)::FormatState const&)::$_0 const&)
137
138
template <typename F, typename C>
139
void AssertNonNull(F C::* f) {
140
  assert(f != nullptr);
141
  (void)f;
142
}
143
144
template <bool C>
145
using EnableIf = typename ::std::enable_if<C, int>::type;
146
147
}  // namespace functional_internal
148
ABSL_NAMESPACE_END
149
}  // namespace absl
150
151
#endif  // ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_