1 // 2 // Copyright 2019 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 #ifndef ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_ 17 #define ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_ 18 19 #include <tuple> 20 #include <type_traits> 21 22 #include "absl/base/internal/fast_type_id.h" 23 #include "absl/types/optional.h" 24 25 namespace absl { 26 ABSL_NAMESPACE_BEGIN 27 namespace random_internal { 28 29 // MockHelpers works in conjunction with MockOverloadSet, MockingBitGen, and 30 // BitGenRef to enable the mocking capability for absl distribution functions. 31 // 32 // MockingBitGen registers mocks based on the typeid of a mock signature, KeyT, 33 // which is used to generate a unique id. 34 // 35 // KeyT is a signature of the form: 36 // result_type(discriminator_type, std::tuple<args...>) 37 // The mocked function signature will be composed from KeyT as: 38 // result_type(args...) 39 // 40 class MockHelpers { 41 using IdType = ::absl::base_internal::FastTypeIdType; 42 43 // Given a key signature type used to index the mock, extract the components. 44 // KeyT is expected to have the form: 45 // result_type(discriminator_type, arg_tuple_type) 46 template <typename KeyT> 47 struct KeySignature; 48 49 template <typename ResultT, typename DiscriminatorT, typename ArgTupleT> 50 struct KeySignature<ResultT(DiscriminatorT, ArgTupleT)> { 51 using result_type = ResultT; 52 using discriminator_type = DiscriminatorT; 53 using arg_tuple_type = ArgTupleT; 54 }; 55 56 // Detector for InvokeMock. 57 template <class T> 58 using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock( 59 std::declval<IdType>(), std::declval<void*>(), std::declval<void*>())); 60 61 // Empty implementation of InvokeMock. 62 template <typename KeyT, typename ReturnT, typename ArgTupleT, typename URBG, 63 typename... Args> 64 static absl::optional<ReturnT> InvokeMockImpl(char, URBG*, Args&&...) { 65 return absl::nullopt; 66 } 67 68 // Non-empty implementation of InvokeMock. 69 template <typename KeyT, typename ReturnT, typename ArgTupleT, typename URBG, 70 typename = invoke_mock_t<URBG>, typename... Args> 71 static absl::optional<ReturnT> InvokeMockImpl(int, URBG* urbg, 72 Args&&... args) { 73 ArgTupleT arg_tuple(std::forward<Args>(args)...); 74 ReturnT result; 75 if (urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple, 76 &result)) { 77 return result; 78 } 79 return absl::nullopt; 80 } 81 82 public: 83 // InvokeMock is private; this provides access for some specialized use cases. 84 template <typename URBG> 85 static inline bool PrivateInvokeMock(URBG* urbg, IdType type, 86 void* args_tuple, void* result) { 87 return urbg->InvokeMock(type, args_tuple, result); 88 } 89 90 // Invoke a mock for the KeyT (may or may not be a signature). 91 // 92 // KeyT is used to generate a typeid-based lookup key for the mock. 93 // KeyT is a signature of the form: 94 // result_type(discriminator_type, std::tuple<args...>) 95 // The mocked function signature will be composed from KeyT as: 96 // result_type(args...) 97 // 98 // An instance of arg_tuple_type must be constructable from Args..., since 99 // the underlying mechanism requires a pointer to an argument tuple. 100 template <typename KeyT, typename URBG, typename... Args> 101 static auto MaybeInvokeMock(URBG* urbg, Args&&... args) 102 -> absl::optional<typename KeySignature<KeyT>::result_type> { 103 // Use function overloading to dispatch to the implemenation since 104 // more modern patterns (e.g. require + constexpr) are not supported in all 105 // compiler configurations. 106 return InvokeMockImpl<KeyT, typename KeySignature<KeyT>::result_type, 107 typename KeySignature<KeyT>::arg_tuple_type, URBG>( 108 0, urbg, std::forward<Args>(args)...); 109 } 110 111 // Acquire a mock for the KeyT (may or may not be a signature). 112 // 113 // KeyT is used to generate a typeid-based lookup for the mock. 114 // KeyT is a signature of the form: 115 // result_type(discriminator_type, std::tuple<args...>) 116 // The mocked function signature will be composed from KeyT as: 117 // result_type(args...) 118 template <typename KeyT, typename MockURBG> 119 static auto MockFor(MockURBG& m) 120 -> decltype(m.template RegisterMock< 121 typename KeySignature<KeyT>::result_type, 122 typename KeySignature<KeyT>::arg_tuple_type>( 123 m, std::declval<IdType>())) { 124 return m.template RegisterMock<typename KeySignature<KeyT>::result_type, 125 typename KeySignature<KeyT>::arg_tuple_type>( 126 m, ::absl::base_internal::FastTypeId<KeyT>()); 127 } 128 }; 129 130 } // namespace random_internal 131 ABSL_NAMESPACE_END 132 } // namespace absl 133 134 #endif // ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_ 135