bind.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_BIND_H_
  5. #define BASE_TEST_BIND_H_
  6. #include <type_traits>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/strings/string_piece.h"
  10. namespace base {
  11. class Location;
  12. namespace internal {
  13. template <typename Callable,
  14. typename Signature = decltype(&Callable::operator())>
  15. struct HasConstCallOperatorImpl : std::false_type {};
  16. template <typename Callable, typename R, typename... Args>
  17. struct HasConstCallOperatorImpl<Callable, R (Callable::*)(Args...) const>
  18. : std::true_type {};
  19. template <typename Callable>
  20. constexpr bool HasConstCallOperator =
  21. HasConstCallOperatorImpl<std::decay_t<Callable>>::value;
  22. template <typename F, typename Signature>
  23. struct BindLambdaHelper;
  24. template <typename F, typename R, typename... Args>
  25. struct BindLambdaHelper<F, R(Args...)> {
  26. static R Run(const std::decay_t<F>& f, Args... args) {
  27. return f(std::forward<Args>(args)...);
  28. }
  29. static R RunOnce(std::decay_t<F>&& f, Args... args) {
  30. return f(std::forward<Args>(args)...);
  31. }
  32. };
  33. } // namespace internal
  34. // A variant of BindRepeating() that can bind capturing lambdas for testing.
  35. // This doesn't support extra arguments binding as the lambda itself can do.
  36. template <typename Lambda,
  37. std::enable_if_t<internal::HasConstCallOperator<Lambda>>* = nullptr>
  38. decltype(auto) BindLambdaForTesting(Lambda&& lambda) {
  39. using Signature = internal::ExtractCallableRunType<std::decay_t<Lambda>>;
  40. // If WTF::BindRepeating is available, and a callback argument is in WTF, then
  41. // this call is ambiguous without the full namespace path.
  42. return ::base::BindRepeating(
  43. &internal::BindLambdaHelper<Lambda, Signature>::Run,
  44. std::forward<Lambda>(lambda));
  45. }
  46. // A variant of BindOnce() that can bind mutable capturing lambdas for
  47. // testing. This doesn't support extra arguments binding as the lambda itself
  48. // can do. Since a mutable lambda potentially can invalidate its state after
  49. // being run once, this method returns a OnceCallback instead of a
  50. // RepeatingCallback.
  51. template <typename Lambda,
  52. std::enable_if_t<!internal::HasConstCallOperator<Lambda>>* = nullptr>
  53. decltype(auto) BindLambdaForTesting(Lambda&& lambda) {
  54. static_assert(
  55. std::is_rvalue_reference<Lambda&&>() &&
  56. !std::is_const<std::remove_reference_t<Lambda>>(),
  57. "BindLambdaForTesting requires non-const rvalue for mutable lambda "
  58. "binding. I.e.: base::BindLambdaForTesting(std::move(lambda)).");
  59. using Signature = internal::ExtractCallableRunType<std::decay_t<Lambda>>;
  60. return BindOnce(&internal::BindLambdaHelper<Lambda, Signature>::RunOnce,
  61. std::move(lambda));
  62. }
  63. // Returns a closure that fails on destruction if it hasn't been run.
  64. OnceClosure MakeExpectedRunClosure(const Location& location,
  65. StringPiece message = StringPiece());
  66. RepeatingClosure MakeExpectedRunAtLeastOnceClosure(
  67. const Location& location,
  68. StringPiece message = StringPiece());
  69. // Returns a closure that fails the test if run.
  70. RepeatingClosure MakeExpectedNotRunClosure(const Location& location,
  71. StringPiece message = StringPiece());
  72. } // namespace base
  73. #endif // BASE_TEST_BIND_H_