C++信号回调(类似javascript)
C++ signal callback (like javascript)
我是C++的新手,在学习新语言的同时,我正在尝试移植一些javascript脚本。
我正在努力寻找使用javascript等回调的解决方案,尤其是js信号库
下面是一个javascript脚本。它能转换成C++吗?怎样如果没有,最佳解决方案是什么?
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++
namespace ns {
// ??
};
// ns::setUpdate(??);
您可以使用函数指针,如下所示:
#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();
}
输出:
你好,来自回调
另请参阅:Typedef函数指针?
我不会使用这样的namespace
。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
}
这将输出
已调用更新
已调用更新
我变了1
已调用更新
我换了2个
这是将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;
}
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- Javascript日期格式类似于ISO,但本地
- 通过传递图像的URL通过javascript读取/处理图像-类似于PHP中的file_get_contents
- 类似Javascript JQuery的库
- 我想在Javascript中探索类似C语言的字符串的整个字符
- C++信号回调(类似javascript)
- Javascript库创建类似heroku仪表板的应用程序
- Java中的数组,具有类似Javascript数组的性质
- 如何处理javascript中的循环引用,类似于Excel提供迭代限制的方式
- Javascript是否有类似FindControlRecursive的函数
- 类似按钮增量计数器不工作.PHP,AJAX,MySQL,javascript
- Javascript解析器本身是用类似ESPRIMA的Javascript编写的,这是如何工作的?然后谁解析ESPRIM
- 如何在JavaScript中创建类似于something.function()的函数
- JavaScript-通过类似的属性查找对象,并将其推送到一个新的数组中
- Javascript Ruby's类似地图的方法
- javascript中是否有类似于Java中的Map类型
- 纯JavaScript:类似jQuery's是Numeric()
- JavaScript:类似IIF的语句
- 改变图标多次使用onclick使用javascript类似于gmail的星星图标
- JavaScript类似的函数不起作用