chromium - base::Bind take parameter
发布日期:2021-06-30 22:19:28 浏览次数:2 分类:技术文章

本文共 3494 字,大约阅读时间需要 11 分钟。

前言

看资料,有个同学,描述的和官方资料差不多,但是编译不过去。

后来找到官方资料,这才看出为啥编译不过。
整个测试工程,将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 
#include
#include
#include
#include
#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
{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
; virtual ~cls_Ref() = default; DISALLOW_COPY_AND_ASSIGN(cls_Ref);};void fn_test_bind_class_member_function(){ // 用base::Bind()绑定类(不绑定参数) scoped_refptr
p_cls_ref1 = new (cls_Ref); base::Callback
cb_cls_ref1 = base::Bind(&cls_Ref::add, p_cls_ref1); DLOG(INFO) << cb_cls_ref1.Run(11, 22); // 这是不绑定参数的用法 // 用base::Bind()绑定类(绑定参数) scoped_refptr
p_cls_ref2 = new (cls_Ref); base::Callback
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
cb_fn_add_1 = base::Bind(&fn_add); LOG(INFO) << cb_fn_add_1.Run(1, 2); // 这是不绑定参数的用法 // 用base::Bind()绑定全局函数(绑定参数) base::Callback
cb_fn_add_2 = base::Bind(&fn_add, 3, 4); LOG(INFO) << cb_fn_add_2.Run(); // 这是绑定参数的用法}

Z:\chromium\src\base\test\

# 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",  ]}

转载地址:https://lostspeed.blog.csdn.net/article/details/85051447 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:chromium - get value from base:Value
下一篇:chromium - base::Bind类成员函数

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月14日 06时18分45秒