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 { 28 ABSL_NAMESPACE_BEGIN 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> 44 struct detector : std::false_type {}; 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 54 using HasInvokeMock = typename detector<invoke_mock_t, void, URBG>::type; 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 92 ABSL_NAMESPACE_END 93 } // namespace absl 94 95 #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_ 96