[英]C++ signal callback (like javascript)
I am new to C++, and I'm trying to port some javascript scripts while learning the new language. 我是C ++的新手,并且在学习新语言的同时尝试移植一些JavaScript脚本。
I am trying to find solutions to use callbacks like javascript, especially like the js-signals library 我正在尝试找到使用像javascript这样的回调的解决方案,尤其是像js-signals库这样的回调
Below is a script in javascript. 以下是javascript中的脚本。 Can it be converted to C++?
可以将其转换为C ++吗? How?
怎么样? If no, what is the best solution?
如果没有,什么是最佳解决方案?
Javascript 使用Javascript
var ns = {
_callback: null,
setUpdate: function(callback) {
ns._callback = callback;
},
update: function() {
// do some default things
ns._callback();
}
};
ns.setUpdate(function() {
console.log("I'm Changed"); // will be: std::cout << "I'm Changed\n";
});
C++ C ++
namespace ns {
// ??
};
// ns::setUpdate(??);
You can use a function pointer, like so: 您可以使用函数指针,如下所示:
#include <cstdio>
namespace ns {
// defines a type named Callback that is a pointer
// to a function that takes void and returns void
typedef void (*Callback)();
Callback _callback;
void setUpdate( Callback cb) {
_callback = cb;
}
void update() {
_callback();
}
}
void MyUpdate() {
printf("hello from callback\n");
}
int main(int argc, char* argv[]) {
ns::setUpdate(MyUpdate);
ns::update();
}
output: 输出:
hello from callback
你好从回调
See also: Typedef function pointer? 另请参见: Typedef函数指针?
I wouldn't use a namespace
like that. 我不会使用这样的
namespace
。 A class
is probably a better fit for something like this: 一个
class
可能更适合这样的事情:
#include <iostream>
#include <functional>
class MyThing
{
public:
MyThing() {}
MyThing(const std::function<void()>& callback) : callback_(callback) {}
void setUpdate(const std::function<void()>& callback)
{
callback_ = callback;
}
void update()
{
std::cout << "Update called\n";
if (callback_) {
callback_();
}
}
private:
std::function<void()> callback_;
};
int main()
{
MyThing my_thing1; // callback_ is initially unset in this object
my_thing1.update(); // no callback is called
my_thing1.setUpdate([](){ std::cout << "I'm Changed 1\n"; });
my_thing1.update(); // calls the lambda set in the previous line
MyThing my_thing2([](){ std::cout << "I'm Changed 2\n"; });
my_thing2.update(); // calls the lambda in the prevous line
}
This will output 这将输出
Update called
更新称为
Update called更新称为
I'm changed 1我变了1
Update called更新称为
I'm changed 2我变了2
This is a nice way to port javascript js-signals library to c++: 这是将javascript js信号库移植到c ++的好方法:
#include <string>
#include <map>
#include <functional>
// You only need this Signal class
class Signal {
// store all callbacks to a string map
std::map<std::string, std::function<void()>> callbacks;
// check if callback already exists. optional
bool exists(std::string key) {
if (callbacks.find(key) == callbacks.end()) {return false;}
else {return true;}
}
public:
void add(std::string key, std::function<void()> cb) {
if (!exists(key)) {remove(key);}
callbacks.insert(std::pair<std::string, std::function<void()>>(key, cb));
}
void remove(std::string key) {
callbacks.erase(key);
}
void dispatch() {
for (std::map<std::string, std::function<void()>>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
callbacks[it->first]();
}
}
// this destroys the signal
void dispose() {
callbacks.clear();
}
};
int main() {
// create a signal
Signal* onFire = new Signal();
// add some callbacks
int a_variable = 10;
onFire->add("a_sound_is_heard", [&]() {
// some code when onFire.dispatch() happens
std::cout << "i can use this variable too :) " << a_variable << std::endl;
// you can also use the onFire variable
});
onFire->add("a_bullet_appears", [&]() {
// some code again
});
// remove some callbacks (add a silencer for your gun)
onFire->remove("a_sound_is_heard");
// whenever you want, execute the event
onFire->dispatch();
// don't forget to delete the signal from memory
onFire->dispose();
delete onFire;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.