Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:37:09

0001 //
0002 // Copyright 2019 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 //      https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015 
0016 #ifndef ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_
0017 #define ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_
0018 
0019 #include <utility>
0020 
0021 #include "absl/base/config.h"
0022 #include "absl/base/internal/fast_type_id.h"
0023 #include "absl/types/optional.h"
0024 
0025 namespace absl {
0026 ABSL_NAMESPACE_BEGIN
0027 namespace random_internal {
0028 
0029 // A no-op validator meeting the ValidatorT requirements for MockHelpers.
0030 //
0031 // Custom validators should follow a similar structure, passing the type to
0032 // MockHelpers::MockFor<KeyT>(m, CustomValidatorT()).
0033 struct NoOpValidator {
0034   // Default validation: do nothing.
0035   template <typename ResultT, typename... Args>
0036   static void Validate(ResultT, Args&&...) {}
0037 };
0038 
0039 // MockHelpers works in conjunction with MockOverloadSet, MockingBitGen, and
0040 // BitGenRef to enable the mocking capability for absl distribution functions.
0041 //
0042 // MockingBitGen registers mocks based on the typeid of a mock signature, KeyT,
0043 // which is used to generate a unique id.
0044 //
0045 // KeyT is a signature of the form:
0046 //   result_type(discriminator_type, std::tuple<args...>)
0047 // The mocked function signature will be composed from KeyT as:
0048 //   result_type(args...)
0049 //
0050 class MockHelpers {
0051   using IdType = ::absl::base_internal::FastTypeIdType;
0052 
0053   // Given a key signature type used to index the mock, extract the components.
0054   // KeyT is expected to have the form:
0055   //   result_type(discriminator_type, arg_tuple_type)
0056   template <typename KeyT>
0057   struct KeySignature;
0058 
0059   template <typename ResultT, typename DiscriminatorT, typename ArgTupleT>
0060   struct KeySignature<ResultT(DiscriminatorT, ArgTupleT)> {
0061     using result_type = ResultT;
0062     using discriminator_type = DiscriminatorT;
0063     using arg_tuple_type = ArgTupleT;
0064   };
0065 
0066   // Detector for InvokeMock.
0067   template <class T>
0068   using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
0069       std::declval<IdType>(), std::declval<void*>(), std::declval<void*>()));
0070 
0071   // Empty implementation of InvokeMock.
0072   template <typename KeyT, typename ReturnT, typename ArgTupleT, typename URBG,
0073             typename... Args>
0074   static absl::optional<ReturnT> InvokeMockImpl(char, URBG*, Args&&...) {
0075     return absl::nullopt;
0076   }
0077 
0078   // Non-empty implementation of InvokeMock.
0079   template <typename KeyT, typename ReturnT, typename ArgTupleT, typename URBG,
0080             typename = invoke_mock_t<URBG>, typename... Args>
0081   static absl::optional<ReturnT> InvokeMockImpl(int, URBG* urbg,
0082                                                 Args&&... args) {
0083     ArgTupleT arg_tuple(std::forward<Args>(args)...);
0084     ReturnT result;
0085     if (urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
0086                          &result)) {
0087       return result;
0088     }
0089     return absl::nullopt;
0090   }
0091 
0092  public:
0093   // InvokeMock is private; this provides access for some specialized use cases.
0094   template <typename URBG>
0095   static inline bool PrivateInvokeMock(URBG* urbg, IdType type,
0096                                        void* args_tuple, void* result) {
0097     return urbg->InvokeMock(type, args_tuple, result);
0098   }
0099 
0100   // Invoke a mock for the KeyT (may or may not be a signature).
0101   //
0102   // KeyT is used to generate a typeid-based lookup key for the mock.
0103   // KeyT is a signature of the form:
0104   //   result_type(discriminator_type, std::tuple<args...>)
0105   // The mocked function signature will be composed from KeyT as:
0106   //   result_type(args...)
0107   //
0108   // An instance of arg_tuple_type must be constructable from Args..., since
0109   // the underlying mechanism requires a pointer to an argument tuple.
0110   template <typename KeyT, typename URBG, typename... Args>
0111   static auto MaybeInvokeMock(URBG* urbg, Args&&... args)
0112       -> absl::optional<typename KeySignature<KeyT>::result_type> {
0113     // Use function overloading to dispatch to the implementation since
0114     // more modern patterns (e.g. require + constexpr) are not supported in all
0115     // compiler configurations.
0116     return InvokeMockImpl<KeyT, typename KeySignature<KeyT>::result_type,
0117                           typename KeySignature<KeyT>::arg_tuple_type, URBG>(
0118         0, urbg, std::forward<Args>(args)...);
0119   }
0120 
0121   // Acquire a mock for the KeyT (may or may not be a signature), set up to use
0122   // the ValidatorT to verify that the result is in the range of the RNG
0123   // function.
0124   //
0125   // KeyT is used to generate a typeid-based lookup for the mock.
0126   // KeyT is a signature of the form:
0127   //   result_type(discriminator_type, std::tuple<args...>)
0128   // The mocked function signature will be composed from KeyT as:
0129   //   result_type(args...)
0130   // ValidatorT::Validate will be called after the result of the RNG. The
0131   //   signature is expected to be of the form:
0132   //      ValidatorT::Validate(result, args...)
0133   template <typename KeyT, typename ValidatorT, typename MockURBG>
0134   static auto MockFor(MockURBG& m, ValidatorT)
0135       -> decltype(m.template RegisterMock<
0136                   typename KeySignature<KeyT>::result_type,
0137                   typename KeySignature<KeyT>::arg_tuple_type>(
0138           m, std::declval<IdType>(), ValidatorT())) {
0139     return m.template RegisterMock<typename KeySignature<KeyT>::result_type,
0140                                    typename KeySignature<KeyT>::arg_tuple_type>(
0141         m, ::absl::base_internal::FastTypeId<KeyT>(), ValidatorT());
0142   }
0143 
0144   // Acquire a mock for the KeyT (may or may not be a signature).
0145   //
0146   // KeyT is used to generate a typeid-based lookup for the mock.
0147   // KeyT is a signature of the form:
0148   //   result_type(discriminator_type, std::tuple<args...>)
0149   // The mocked function signature will be composed from KeyT as:
0150   //   result_type(args...)
0151   template <typename KeyT, typename MockURBG>
0152   static decltype(auto) MockFor(MockURBG& m) {
0153     return MockFor<KeyT>(m, NoOpValidator());
0154   }
0155 };
0156 
0157 }  // namespace random_internal
0158 ABSL_NAMESPACE_END
0159 }  // namespace absl
0160 
0161 #endif  // ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_