Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function message bus,注册的函数无法支持引用和指针类型 #25

Open
xiaolanpingguo opened this issue Jul 30, 2018 · 4 comments

Comments

@xiaolanpingguo
Copy link

用法是这样的:
struct person {
std::string foo(const int& a) {
return std::to_string(a);
}

void foo1(const double& a) {
	std::cout << a << std::endl;
}

};

int main()
{
auto& bus = FunctionMsgBus::get();

person p;
bus.register_handler(&person::foo, &p, "1");
bus.call(FnKey{ "1" }, 10);

std::getchar();
return 0;

}
会引起崩溃
然后查了下问题,发现如果函数参数是引用的话,会编译不过,如果函数参数是指针的话,同样会引起崩溃

@qicosmos
Copy link
Owner

谢谢你发现的bug,我后面看看是什么原因。

@qicosmos
Copy link
Owner

qicosmos commented Aug 2, 2018

已经修复了bug,具体原因是因为对于返回类型的函数调用的时候没有加返回类型。现在内部做了判空处理,对于这种情况会忽略返回值。

@qicosmos
Copy link
Owner

qicosmos commented Aug 2, 2018

支持引用和指针,这是测试代码:

struct person1 {
std::string foo(const int& a) {
	return std::to_string(a);
}

void foo1(const double& a) {
	std::cout << a << std::endl;
}

void foo2(int* a) {
	std::cout << *a << std::endl;
}

void foo3(std::shared_ptr<int> a) {
	std::cout << *a << std::endl;
}
};

void test_bus1() {
auto& bus = function_msg_bus::get();

person1 p;
bus.register_handler(&person1::foo, &p, "1");
bus.call(FnKey{ "1" }, 10);

bus.register_handler(&person1::foo1, &p, "2");
bus.call(FnKey{ "2" }, 10.5);

bus.register_handler(&person1::foo2, &p, "3");
bus.call(FnKey{ "3" }, new int(2));

bus.register_handler(&person1::foo3, &p, "4");
bus.call(FnKey{ "4" }, std::make_shared<int>(4));
}

@qicosmos
Copy link
Owner

qicosmos commented Aug 9, 2018

@xiaolanpingguo 还有新的问题吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants