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.

distribution_caller.h 4.1 kB

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