distribution_caller.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef Y_ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
  17. #define Y_ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_
  18. #include <utility>
  19. #include <type_traits>
  20. #include "y_absl/base/config.h"
  21. #include "y_absl/base/internal/fast_type_id.h"
  22. #include "y_absl/utility/utility.h"
  23. namespace y_absl {
  24. Y_ABSL_NAMESPACE_BEGIN
  25. namespace random_internal {
  26. // DistributionCaller provides an opportunity to overload the general
  27. // mechanism for calling a distribution, allowing for mock-RNG classes
  28. // to intercept such calls.
  29. template <typename URBG>
  30. struct DistributionCaller {
  31. static_assert(!std::is_pointer<URBG>::value,
  32. "You must pass a reference, not a pointer.");
  33. // SFINAE to detect whether the URBG type includes a member matching
  34. // bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
  35. //
  36. // These live inside BitGenRef so that they have friend access
  37. // to MockingBitGen. (see similar methods in DistributionCaller).
  38. template <template <class...> class Trait, class AlwaysVoid, class... Args>
  39. struct detector : std::false_type {};
  40. template <template <class...> class Trait, class... Args>
  41. struct detector<Trait, y_absl::void_t<Trait<Args...>>, Args...>
  42. : std::true_type {};
  43. template <class T>
  44. using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
  45. std::declval<::y_absl::base_internal::FastTypeIdType>(),
  46. std::declval<void*>(), std::declval<void*>()));
  47. using HasInvokeMock = typename detector<invoke_mock_t, void, URBG>::type;
  48. // Default implementation of distribution caller.
  49. template <typename DistrT, typename... Args>
  50. static typename DistrT::result_type Impl(std::false_type, URBG* urbg,
  51. Args&&... args) {
  52. DistrT dist(std::forward<Args>(args)...);
  53. return dist(*urbg);
  54. }
  55. // Mock implementation of distribution caller.
  56. // The underlying KeyT must match the KeyT constructed by MockOverloadSet.
  57. template <typename DistrT, typename... Args>
  58. static typename DistrT::result_type Impl(std::true_type, URBG* urbg,
  59. Args&&... args) {
  60. using ResultT = typename DistrT::result_type;
  61. using ArgTupleT = std::tuple<y_absl::decay_t<Args>...>;
  62. using KeyT = ResultT(DistrT, ArgTupleT);
  63. ArgTupleT arg_tuple(std::forward<Args>(args)...);
  64. ResultT result;
  65. if (!urbg->InvokeMock(::y_absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
  66. &result)) {
  67. auto dist = y_absl::make_from_tuple<DistrT>(arg_tuple);
  68. result = dist(*urbg);
  69. }
  70. return result;
  71. }
  72. // Default implementation of distribution caller.
  73. template <typename DistrT, typename... Args>
  74. static typename DistrT::result_type Call(URBG* urbg, Args&&... args) {
  75. return Impl<DistrT, Args...>(HasInvokeMock{}, urbg,
  76. std::forward<Args>(args)...);
  77. }
  78. };
  79. } // namespace random_internal
  80. Y_ABSL_NAMESPACE_END
  81. } // namespace y_absl
  82. #endif // Y_ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_