chromium - base::Bind take parameter
阿新 • • 發佈:2018-12-20
前言
看資料,有個同學,描述的和官方資料差不多,但是編譯不過去。
後來找到官方資料,這才看出為啥編譯不過。
整個測試工程,將base::Bind帶引數的2種情況(bind 全域性函式,bind 類成員函式)都驗證一下.
實驗
// @file Z:\chromium\src\base\test\test_by_me\main.cpp // @brief write some test code to study chromium project /** # 編譯命令執行的目錄 # cd /d Z:\chromium\src\ # # 產生工程 # gn --ide=vs args out\my_x86_d # # 檢視新測試工程在解決方案中的位置 # gn ls out\my_x86_d > d:\my_tmp\gn_list_target.log # 新加的測試工程位置//base/test:test_by_me # # 編譯工程 # autoninja -C out\my_x86_d test_by_me # # 執行測試程式 # out\my_x86_d\test_by_me */ #include <stdlib.h> #include <stdio.h> #include <memory> #include <set> #include <utility> #include "base/bind.h" // for base:Bind() #include "base/logging.h" // for LOG() #include "base/callback.h" // for base ::Callback() #include "base\memory\scoped_refptr.h" // for scoped_refptr<> #include "base\memory\ref_counted.h"// for RefCountedThreadSafe<> #define PROG_NAME "test_my_me" #define LINE60 "------------------------------------------------------------" void fn_test(); void fn_test_bind_global_function(); void fn_test_bind_class_member_function(); int main(int argc, char** argv) { DLOG(INFO) << LINE60; DLOG(INFO) << "- " << PROG_NAME; DLOG(INFO) << LINE60; fn_test(); system("pause"); return EXIT_SUCCESS; } /** run result on Z:\chromium\src\out\my_x86_d\debug.log [1217/155343.697:INFO:main.cpp(34)] ------------------------------------------------------------ [1217/155343.698:INFO:main.cpp(35)] - test_my_me [1217/155343.698:INFO:main.cpp(36)] ------------------------------------------------------------ [1217/155343.698:INFO:main.cpp(87)] >> fn_add(1, 2) [1217/155343.698:INFO:main.cpp(95)] 3 [1217/155343.698:INFO:main.cpp(87)] >> fn_add(3, 4) [1217/155343.698:INFO:main.cpp(99)] 7 [1217/155343.698:INFO:main.cpp(60)] >> cls_Ref::add(11, 22) [1217/155343.698:INFO:main.cpp(77)] 33 [1217/155343.698:INFO:main.cpp(60)] >> cls_Ref::add(33, 44) [1217/155343.698:INFO:main.cpp(82)] 77 */ void fn_test() { fn_test_bind_global_function(); fn_test_bind_class_member_function(); } class cls_Ref : public base::RefCountedThreadSafe<cls_Ref> { public: cls_Ref() = default; int add(int a, int b) { DLOG(INFO) << ">> cls_Ref::add(" << a << ", " << b << ")"; return (a + b); } void PrintBye() { LOG(INFO) << "bye."; } private: friend class base::RefCountedThreadSafe<cls_Ref>; virtual ~cls_Ref() = default; DISALLOW_COPY_AND_ASSIGN(cls_Ref); }; void fn_test_bind_class_member_function() { // 用base::Bind()繫結類(不繫結引數) scoped_refptr<cls_Ref> p_cls_ref1 = new (cls_Ref); base::Callback<int(int, int)> cb_cls_ref1 = base::Bind(&cls_Ref::add, p_cls_ref1); DLOG(INFO) << cb_cls_ref1.Run(11, 22); // 這是不繫結引數的用法 // 用base::Bind()繫結類(繫結引數) scoped_refptr<cls_Ref> p_cls_ref2 = new (cls_Ref); base::Callback<int()> cb_cls_ref2 = base::Bind(&cls_Ref::add, p_cls_ref2, 33, 44); DLOG(INFO) << cb_cls_ref2.Run(); // 這是繫結引數的用法 } int fn_add(int a, int b) { DLOG(INFO) << ">> fn_add(" << a << ", " << b << ")"; return (a + b); } void fn_test_bind_global_function() { // 用base::Bind()繫結全域性函式(不繫結引數) base::Callback<int(int,int)> cb_fn_add_1 = base::Bind(&fn_add); LOG(INFO) << cb_fn_add_1.Run(1, 2); // 這是不繫結引數的用法 // 用base::Bind()繫結全域性函式(繫結引數) base::Callback<int()> cb_fn_add_2 = base::Bind(&fn_add, 3, 4); LOG(INFO) << cb_fn_add_2.Run(); // 這是繫結引數的用法 }
Z:\chromium\src\base\test\BUILD.gn
# Trivial executable which outputs space-delimited argv to stdout, # used for testing. executable("test_child_process") { testonly = true sources = [ "test_child_process.cc", ] deps = [ "//build/config:exe_and_shlib_deps", ] } executable("test_by_me") { sources = [ "test_by_me/main.cpp", ] deps = [ "//base", ] }