File indexing completed on 2025-12-17 09:37:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0030
0031
0032
0033 struct NoOpValidator {
0034
0035 template <typename ResultT, typename... Args>
0036 static void Validate(ResultT, Args&&...) {}
0037 };
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 class MockHelpers {
0051 using IdType = ::absl::base_internal::FastTypeIdType;
0052
0053
0054
0055
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
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
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
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
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
0101
0102
0103
0104
0105
0106
0107
0108
0109
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
0114
0115
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
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
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
0145
0146
0147
0148
0149
0150
0151 template <typename KeyT, typename MockURBG>
0152 static decltype(auto) MockFor(MockURBG& m) {
0153 return MockFor<KeyT>(m, NoOpValidator());
0154 }
0155 };
0156
0157 }
0158 ABSL_NAMESPACE_END
0159 }
0160
0161 #endif