• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "absl/base/config.h"
23 #include "absl/base/internal/fast_type_id.h"
24 #include "absl/utility/utility.h"
25 
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 namespace random_internal {
29 
30 // DistributionCaller provides an opportunity to overload the general
31 // mechanism for calling a distribution, allowing for mock-RNG classes
32 // to intercept such calls.
33 template <typename URBG>
34 struct DistributionCaller {
35   // SFINAE to detect whether the URBG type includes a member matching
36   // bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
37   //
38   // These live inside BitGenRef so that they have friend access
39   // to MockingBitGen. (see similar methods in DistributionCaller).
40   template <template <class...> class Trait, class AlwaysVoid, class... Args>
41   struct detector : std::false_type {};
42   template <template <class...> class Trait, class... Args>
43   struct detector<Trait, absl::void_t<Trait<Args...>>, Args...>
44       : std::true_type {};
45 
46   template <class T>
47   using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
48       std::declval<::absl::base_internal::FastTypeIdType>(),
49       std::declval<void*>(), std::declval<void*>()));
50 
51   using HasInvokeMock = typename detector<invoke_mock_t, void, URBG>::type;
52 
53   // Default implementation of distribution caller.
54   template <typename DistrT, typename... Args>
55   static typename DistrT::result_type Impl(std::false_type, URBG* urbg,
56                                            Args&&... args) {
57     DistrT dist(std::forward<Args>(args)...);
58     return dist(*urbg);
59   }
60 
61   // Mock implementation of distribution caller.
62   // The underlying KeyT must match the KeyT constructed by MockOverloadSet.
63   template <typename DistrT, typename... Args>
64   static typename DistrT::result_type Impl(std::true_type, URBG* urbg,
65                                            Args&&... args) {
66     using ResultT = typename DistrT::result_type;
67     using ArgTupleT = std::tuple<absl::decay_t<Args>...>;
68     using KeyT = ResultT(DistrT, ArgTupleT);
69 
70     ArgTupleT arg_tuple(std::forward<Args>(args)...);
71     ResultT result;
72     if (!urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
73                           &result)) {
74       auto dist = absl::make_from_tuple<DistrT>(arg_tuple);
75       result = dist(*urbg);
76     }
77     return result;
78   }
79 
80   // Default implementation of distribution caller.
81   template <typename DistrT, typename... Args>
82   static typename DistrT::result_type Call(URBG* urbg, Args&&... args) {
83     return Impl<DistrT, Args...>(HasInvokeMock{}, urbg,
84                                  std::forward<Args>(args)...);
85   }
86 };
87 
88 }  // namespace random_internal
89 ABSL_NAMESPACE_END
90 }  // namespace absl
91 
92 #endif  // ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
93