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