abseil-cpp/absl/random/internal/distribution_caller.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
18 #define ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
19 
20 #include <utility>
21 #include <type_traits>
22 
23 #include "absl/base/config.h"
24 #include "absl/base/internal/fast_type_id.h"
25 #include "absl/utility/utility.h"
26 
27 namespace absl {
29 namespace random_internal {
30 
31 // DistributionCaller provides an opportunity to overload the general
32 // mechanism for calling a distribution, allowing for mock-RNG classes
33 // to intercept such calls.
34 template <typename URBG>
35 struct DistributionCaller {
36  static_assert(!std::is_pointer<URBG>::value,
37  "You must pass a reference, not a pointer.");
38  // SFINAE to detect whether the URBG type includes a member matching
39  // bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
40  //
41  // These live inside BitGenRef so that they have friend access
42  // to MockingBitGen. (see similar methods in DistributionCaller).
43  template <template <class...> class Trait, class AlwaysVoid, class... Args>
45  template <template <class...> class Trait, class... Args>
46  struct detector<Trait, absl::void_t<Trait<Args...>>, Args...>
47  : std::true_type {};
48 
49  template <class T>
50  using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
51  std::declval<::absl::base_internal::FastTypeIdType>(),
52  std::declval<void*>(), std::declval<void*>()));
53 
55 
56  // Default implementation of distribution caller.
57  template <typename DistrT, typename... Args>
58  static typename DistrT::result_type Impl(std::false_type, URBG* urbg,
59  Args&&... args) {
60  DistrT dist(std::forward<Args>(args)...);
61  return dist(*urbg);
62  }
63 
64  // Mock implementation of distribution caller.
65  // The underlying KeyT must match the KeyT constructed by MockOverloadSet.
66  template <typename DistrT, typename... Args>
67  static typename DistrT::result_type Impl(std::true_type, URBG* urbg,
68  Args&&... args) {
69  using ResultT = typename DistrT::result_type;
70  using ArgTupleT = std::tuple<absl::decay_t<Args>...>;
71  using KeyT = ResultT(DistrT, ArgTupleT);
72 
73  ArgTupleT arg_tuple(std::forward<Args>(args)...);
74  ResultT result;
75  if (!urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
76  &result)) {
77  auto dist = absl::make_from_tuple<DistrT>(arg_tuple);
78  result = dist(*urbg);
79  }
80  return result;
81  }
82 
83  // Default implementation of distribution caller.
84  template <typename DistrT, typename... Args>
85  static typename DistrT::result_type Call(URBG* urbg, Args&&... args) {
86  return Impl<DistrT, Args...>(HasInvokeMock{}, urbg,
87  std::forward<Args>(args)...);
88  }
89 };
90 
91 } // namespace random_internal
93 } // namespace absl
94 
95 #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
absl::random_internal::DistributionCaller::detector
Definition: abseil-cpp/absl/random/internal/distribution_caller.h:44
absl::random_internal::DistributionCaller::Call
static DistrT::result_type Call(URBG *urbg, Args &&... args)
Definition: abseil-cpp/absl/random/internal/distribution_caller.h:85
google::protobuf.internal::true_type
integral_constant< bool, true > true_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:89
google::protobuf.internal::false_type
integral_constant< bool, false > false_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:90
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
hpack_encoder_fixtures::Args
Args({0, 16384})
result_type
const typedef int * result_type
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:4325
absl::random_internal::DistributionCaller::Impl
static DistrT::result_type Impl(std::true_type, URBG *urbg, Args &&... args)
Definition: abseil-cpp/absl/random/internal/distribution_caller.h:67
value
const char * value
Definition: hpack_parser_table.cc:165
absl::void_t
typename type_traits_internal::VoidTImpl< Ts... >::type void_t
Definition: abseil-cpp/absl/meta/type_traits.h:218
absl::random_internal::DistributionCaller::Impl
static DistrT::result_type Impl(std::false_type, URBG *urbg, Args &&... args)
Definition: abseil-cpp/absl/random/internal/distribution_caller.h:58
absl::random_internal::DistributionCaller::HasInvokeMock
typename detector< invoke_mock_t, void, URBG >::type HasInvokeMock
Definition: abseil-cpp/absl/random/internal/distribution_caller.h:54
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::random_internal::DistributionCaller::invoke_mock_t
decltype(std::declval< T * >() ->InvokeMock(std::declval<::absl::base_internal::FastTypeIdType >(), std::declval< void * >(), std::declval< void * >())) invoke_mock_t
Definition: abseil-cpp/absl/random/internal/distribution_caller.h:52


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:16