Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: mojo/public/cpp/bindings/tests/sync_method_unittest.cc

Issue 2090013004: Adds base::WrapFunction to wrap lambdas as base::Callbacks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/function_util.h ('K') | « base/function_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <utility> 5 #include <utility>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/function_util.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "base/macros.h" 10 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
12 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
13 #include "mojo/public/cpp/bindings/associated_binding.h" 14 #include "mojo/public/cpp/bindings/associated_binding.h"
14 #include "mojo/public/cpp/bindings/binding.h" 15 #include "mojo/public/cpp/bindings/binding.h"
15 #include "mojo/public/interfaces/bindings/tests/test_sync_methods.mojom.h" 16 #include "mojo/public/interfaces/bindings/tests/test_sync_methods.mojom.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace mojo { 19 namespace mojo {
19 namespace test { 20 namespace test {
20 namespace { 21 namespace {
21 22
22 template <typename... Args>
23 struct LambdaBinder {
24 using CallbackType = base::Callback<void(Args...)>;
25
26 template <typename Func>
27 static void RunLambda(Func func, Args... args) {
28 func(std::move(args)...);
29 }
30
31 template <typename Func>
32 static CallbackType BindLambda(Func func) {
33 return base::Bind(&LambdaBinder::RunLambda<Func>, func);
34 }
35 };
36
37 class TestSyncCommonImpl { 23 class TestSyncCommonImpl {
38 public: 24 public:
39 TestSyncCommonImpl() {} 25 TestSyncCommonImpl() {}
40 26
41 using PingHandler = base::Callback<void(const base::Callback<void()>&)>; 27 using PingHandler = base::Callback<void(const base::Callback<void()>&)>;
42 using PingBinder = LambdaBinder<const base::Callback<void()>&>;
43 template <typename Func> 28 template <typename Func>
44 void set_ping_handler(Func handler) { 29 void set_ping_handler(Func handler) {
45 ping_handler_ = PingBinder::BindLambda(handler); 30 ping_handler_ = base::WrapFunction<PingHandler>(handler);
46 } 31 }
47 32
48 using EchoHandler = 33 using EchoHandler =
49 base::Callback<void(int32_t, const base::Callback<void(int32_t)>&)>; 34 base::Callback<void(int32_t, const base::Callback<void(int32_t)>&)>;
50 using EchoBinder =
51 LambdaBinder<int32_t, const base::Callback<void(int32_t)>&>;
52 template <typename Func> 35 template <typename Func>
53 void set_echo_handler(Func handler) { 36 void set_echo_handler(Func handler) {
54 echo_handler_ = EchoBinder::BindLambda(handler); 37 echo_handler_ = base::WrapFunction<EchoHandler>(handler);
55 } 38 }
56 39
57 using AsyncEchoHandler = 40 using AsyncEchoHandler =
58 base::Callback<void(int32_t, const base::Callback<void(int32_t)>&)>; 41 base::Callback<void(int32_t, const base::Callback<void(int32_t)>&)>;
59 using AsyncEchoBinder =
60 LambdaBinder<int32_t, const base::Callback<void(int32_t)>&>;
61 template <typename Func> 42 template <typename Func>
62 void set_async_echo_handler(Func handler) { 43 void set_async_echo_handler(Func handler) {
63 async_echo_handler_ = AsyncEchoBinder::BindLambda(handler); 44 async_echo_handler_ = base::WrapFunction<AsyncEchoHandler>(handler);
64 } 45 }
65 46
66 using SendInterfaceHandler = base::Callback<void(TestSyncAssociatedPtrInfo)>; 47 using SendInterfaceHandler = base::Callback<void(TestSyncAssociatedPtrInfo)>;
67 using SendInterfaceBinder = LambdaBinder<TestSyncAssociatedPtrInfo>;
68 template <typename Func> 48 template <typename Func>
69 void set_send_interface_handler(Func handler) { 49 void set_send_interface_handler(Func handler) {
70 send_interface_handler_ = SendInterfaceBinder::BindLambda(handler); 50 send_interface_handler_ = base::WrapFunction<SendInterfaceHandler>(handler);
71 } 51 }
72 52
73 using SendRequestHandler = base::Callback<void(TestSyncAssociatedRequest)>; 53 using SendRequestHandler = base::Callback<void(TestSyncAssociatedRequest)>;
74 using SendRequestBinder = LambdaBinder<TestSyncAssociatedRequest>;
75 template <typename Func> 54 template <typename Func>
76 void set_send_request_handler(Func handler) { 55 void set_send_request_handler(Func handler) {
77 send_request_handler_ = SendRequestBinder::BindLambda(handler); 56 send_request_handler_ = base::WrapFunction<SendRequestHandler>(handler);
78 } 57 }
79 58
80 void PingImpl(const base::Callback<void()>& callback) { 59 void PingImpl(const base::Callback<void()>& callback) {
81 if (ping_handler_.is_null()) { 60 if (ping_handler_.is_null()) {
82 callback.Run(); 61 callback.Run();
83 return; 62 return;
84 } 63 }
85 ping_handler_.Run(callback); 64 ping_handler_.Run(callback);
86 } 65 }
87 void EchoImpl(int32_t value, const base::Callback<void(int32_t)>& callback) { 66 void EchoImpl(int32_t value, const base::Callback<void(int32_t)>& callback) {
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 int32_t result_value = -1; 712 int32_t result_value = -1;
734 ASSERT_TRUE(master_ptr_->Echo(456, &result_value)); 713 ASSERT_TRUE(master_ptr_->Echo(456, &result_value));
735 EXPECT_EQ(456, result_value); 714 EXPECT_EQ(456, result_value);
736 } 715 }
737 716
738 // TODO(yzshen): Add more tests related to associated interfaces. 717 // TODO(yzshen): Add more tests related to associated interfaces.
739 718
740 } // namespace 719 } // namespace
741 } // namespace test 720 } // namespace test
742 } // namespace mojo 721 } // namespace mojo
OLDNEW
« base/function_util.h ('K') | « base/function_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698