distribution_caller.h 3.4 KB

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