abseil-cpp/absl/random/bit_gen_ref.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 // File: bit_gen_ref.h
18 // -----------------------------------------------------------------------------
19 //
20 // This header defines a bit generator "reference" class, for use in interfaces
21 // that take both Abseil (e.g. `absl::BitGen`) and standard library (e.g.
22 // `std::mt19937`) bit generators.
23 
24 #ifndef ABSL_RANDOM_BIT_GEN_REF_H_
25 #define ABSL_RANDOM_BIT_GEN_REF_H_
26 
27 #include <limits>
28 #include <type_traits>
29 #include <utility>
30 
31 #include "absl/base/internal/fast_type_id.h"
32 #include "absl/base/macros.h"
33 #include "absl/meta/type_traits.h"
34 #include "absl/random/internal/distribution_caller.h"
35 #include "absl/random/internal/fast_uniform_bits.h"
36 
37 namespace absl {
39 namespace random_internal {
40 
41 template <typename URBG, typename = void, typename = void, typename = void>
43 
44 template <typename URBG>
45 struct is_urbg<
46  URBG,
47  absl::enable_if_t<std::is_same<
48  typename URBG::result_type,
49  typename std::decay<decltype((URBG::min)())>::type>::value>,
50  absl::enable_if_t<std::is_same<
51  typename URBG::result_type,
52  typename std::decay<decltype((URBG::max)())>::type>::value>,
53  absl::enable_if_t<std::is_same<
54  typename URBG::result_type,
55  typename std::decay<decltype(std::declval<URBG>()())>::type>::value>>
56  : std::true_type {};
57 
58 template <typename>
60 class MockHelpers;
61 
62 } // namespace random_internal
63 
64 // -----------------------------------------------------------------------------
65 // absl::BitGenRef
66 // -----------------------------------------------------------------------------
67 //
68 // `absl::BitGenRef` is a type-erasing class that provides a generator-agnostic
69 // non-owning "reference" interface for use in place of any specific uniform
70 // random bit generator (URBG). This class may be used for both Abseil
71 // (e.g. `absl::BitGen`, `absl::InsecureBitGen`) and Standard library (e.g
72 // `std::mt19937`, `std::minstd_rand`) bit generators.
73 //
74 // Like other reference classes, `absl::BitGenRef` does not own the
75 // underlying bit generator, and the underlying instance must outlive the
76 // `absl::BitGenRef`.
77 //
78 // `absl::BitGenRef` is particularly useful when used with an
79 // `absl::MockingBitGen` to test specific paths in functions which use random
80 // values.
81 //
82 // Example:
83 // void TakesBitGenRef(absl::BitGenRef gen) {
84 // int x = absl::Uniform<int>(gen, 0, 1000);
85 // }
86 //
87 class BitGenRef {
88  // SFINAE to detect whether the URBG type includes a member matching
89  // bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
90  //
91  // These live inside BitGenRef so that they have friend access
92  // to MockingBitGen. (see similar methods in DistributionCaller).
93  template <template <class...> class Trait, class AlwaysVoid, class... Args>
95  template <template <class...> class Trait, class... Args>
96  struct detector<Trait, absl::void_t<Trait<Args...>>, Args...>
97  : std::true_type {};
98 
99  template <class T>
100  using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
101  std::declval<base_internal::FastTypeIdType>(), std::declval<void*>(),
102  std::declval<void*>()));
103 
104  template <typename T>
106 
107  public:
108  BitGenRef(const BitGenRef&) = default;
109  BitGenRef(BitGenRef&&) = default;
110  BitGenRef& operator=(const BitGenRef&) = default;
111  BitGenRef& operator=(BitGenRef&&) = default;
112 
113  template <typename URBG, typename absl::enable_if_t<
116  !HasInvokeMock<URBG>::value)>* = nullptr>
117  BitGenRef(URBG& gen) // NOLINT
118  : t_erased_gen_ptr_(reinterpret_cast<uintptr_t>(&gen)),
120  generate_impl_fn_(ImplFn<URBG>) {}
121 
122  template <typename URBG,
125  HasInvokeMock<URBG>::value)>* = nullptr>
126  BitGenRef(URBG& gen) // NOLINT
127  : t_erased_gen_ptr_(reinterpret_cast<uintptr_t>(&gen)),
128  mock_call_(&MockCall<URBG>),
129  generate_impl_fn_(ImplFn<URBG>) {}
130 
132 
133  static constexpr result_type(min)() {
135  }
136 
137  static constexpr result_type(max)() {
139  }
140 
142 
143  private:
146  void*);
147 
148  template <typename URBG>
150  // Ensure that the return values from operator() fill the entire
151  // range promised by result_type, min() and max().
153  return fast_uniform_bits(*reinterpret_cast<URBG*>(ptr));
154  }
155 
156  // Get a type-erased InvokeMock pointer.
157  template <typename URBG>
159  void* result, void* arg_tuple) {
160  return reinterpret_cast<URBG*>(gen_ptr)->InvokeMock(type, result,
161  arg_tuple);
162  }
163  static bool NotAMock(uintptr_t, base_internal::FastTypeIdType, void*, void*) {
164  return false;
165  }
166 
167  inline bool InvokeMock(base_internal::FastTypeIdType type, void* args_tuple,
168  void* result) {
169  if (mock_call_ == NotAMock) return false; // avoids an indirect call.
170  return mock_call_(t_erased_gen_ptr_, type, args_tuple, result);
171  }
172 
176 
177  template <typename>
178  friend struct ::absl::random_internal::DistributionCaller; // for InvokeMock
179  friend class ::absl::random_internal::MockHelpers; // for InvokeMock
180 };
181 
183 } // namespace absl
184 
185 #endif // ABSL_RANDOM_BIT_GEN_REF_H_
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
absl::BitGenRef::BitGenRef
BitGenRef(const BitGenRef &)=default
absl::BitGenRef::result_type
uint64_t result_type
Definition: abseil-cpp/absl/random/bit_gen_ref.h:131
bool
bool
Definition: setup_once.h:312
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::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: abseil-cpp/absl/meta/type_traits.h:631
absl::BitGenRef::detector
Definition: abseil-cpp/absl/random/bit_gen_ref.h:94
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::BitGenRef::ImplFn
static result_type ImplFn(uintptr_t ptr)
Definition: abseil-cpp/absl/random/bit_gen_ref.h:149
absl::BitGenRef::generate_impl_fn_
impl_fn generate_impl_fn_
Definition: abseil-cpp/absl/random/bit_gen_ref.h:175
absl::BitGenRef::operator=
BitGenRef & operator=(const BitGenRef &)=default
absl::random_internal::DistributionCaller
Definition: abseil-cpp/absl/random/bit_gen_ref.h:59
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::BitGenRef::max
static constexpr result_type() max()
Definition: abseil-cpp/absl/random/bit_gen_ref.h:137
hpack_encoder_fixtures::Args
Args({0, 16384})
absl::BitGenRef::mock_call_fn
bool(*)(uintptr_t, base_internal::FastTypeIdType, void *, void *) mock_call_fn
Definition: abseil-cpp/absl/random/bit_gen_ref.h:146
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
absl::BitGenRef::impl_fn
result_type(*)(uintptr_t) impl_fn
Definition: abseil-cpp/absl/random/bit_gen_ref.h:144
absl::BitGenRef::HasInvokeMock
typename detector< invoke_mock_t, void, T >::type HasInvokeMock
Definition: abseil-cpp/absl/random/bit_gen_ref.h:105
absl::base_internal::FastTypeIdType
const void * FastTypeIdType
Definition: abseil-cpp/absl/base/internal/fast_type_id.h:39
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
absl::BitGenRef
Definition: abseil-cpp/absl/random/bit_gen_ref.h:87
gen
OPENSSL_EXPORT GENERAL_NAME * gen
Definition: x509v3.h:495
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
absl::BitGenRef::operator()
result_type operator()()
Definition: abseil-cpp/absl/random/bit_gen_ref.h:141
min
#define min(a, b)
Definition: qsort.h:83
absl::BitGenRef::InvokeMock
bool InvokeMock(base_internal::FastTypeIdType type, void *args_tuple, void *result)
Definition: abseil-cpp/absl/random/bit_gen_ref.h:167
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::BitGenRef::NotAMock
static bool NotAMock(uintptr_t, base_internal::FastTypeIdType, void *, void *)
Definition: abseil-cpp/absl/random/bit_gen_ref.h:163
absl::BitGenRef::BitGenRef
BitGenRef(URBG &gen)
Definition: abseil-cpp/absl/random/bit_gen_ref.h:117
absl::BitGenRef::min
static constexpr result_type() min()
Definition: abseil-cpp/absl/random/bit_gen_ref.h:133
absl::BitGenRef::MockCall
static bool MockCall(uintptr_t gen_ptr, base_internal::FastTypeIdType type, void *result, void *arg_tuple)
Definition: abseil-cpp/absl/random/bit_gen_ref.h:158
absl::BitGenRef::t_erased_gen_ptr_
uintptr_t t_erased_gen_ptr_
Definition: abseil-cpp/absl/random/bit_gen_ref.h:173
absl::random_internal::MockHelpers
Definition: abseil-cpp/absl/random/internal/mock_helpers.h:41
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
absl::BitGenRef::invoke_mock_t
decltype(std::declval< T * >() ->InvokeMock(std::declval< base_internal::FastTypeIdType >(), std::declval< void * >(), std::declval< void * >())) invoke_mock_t
Definition: abseil-cpp/absl/random/bit_gen_ref.h:102
absl::BitGenRef::mock_call_
mock_call_fn mock_call_
Definition: abseil-cpp/absl/random/bit_gen_ref.h:174
absl::random_internal::is_urbg
Definition: abseil-cpp/absl/random/bit_gen_ref.h:42
absl::random_internal::FastUniformBits
Definition: abseil-cpp/absl/random/internal/fast_uniform_bits.h:89


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