You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mock_helpers.h 6.1 kB

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