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 17 #ifndef ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_ 18 #define ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_ 19 20 #include <utility> 21 22 #include "absl/base/config.h" 23 24 namespace absl { 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 // Call the provided distribution type. The parameters are expected 34 // to be explicitly specified. 35 // DistrT is the distribution type. 36 // FormatT is the formatter type: 37 // 38 // struct FormatT { 39 // using result_type = distribution_t::result_type; 40 // static std::string FormatCall( 41 // const distribution_t& distr, 42 // absl::Span<const result_type>); 43 // 44 // static std::string FormatExpectation( 45 // absl::string_view match_args, 46 // absl::Span<const result_t> results); 47 // } 48 // 49 template <typename DistrT, typename FormatT, typename... Args> CallDistributionCaller50 static typename DistrT::result_type Call(URBG* urbg, Args&&... args) { 51 DistrT dist(std::forward<Args>(args)...); 52 return dist(*urbg); 53 } 54 }; 55 56 } // namespace random_internal 57 ABSL_NAMESPACE_END 58 } // namespace absl 59 60 #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTION_CALLER_H_ 61