boost-function

Post callbacks to a task queue using boost::bind

血红的双手。 提交于 2021-01-27 10:41:53
问题 Suppose I have a function called subscribe() that takes a callback handler, which will be called when the event is triggered. Now, I have another version, called subscribe2() . Everything is the same except that, when triggered, it needs to post it to an event queue. It is implemented using the original subscribe() , with a helper funciton called helper() . All it does is to bind the original handler and whatever additional arguments into a functor, and call postToEventQueue() . Now, I wonder

Post callbacks to a task queue using boost::bind

倾然丶 夕夏残阳落幕 提交于 2021-01-27 10:40:35
问题 Suppose I have a function called subscribe() that takes a callback handler, which will be called when the event is triggered. Now, I have another version, called subscribe2() . Everything is the same except that, when triggered, it needs to post it to an event queue. It is implemented using the original subscribe() , with a helper funciton called helper() . All it does is to bind the original handler and whatever additional arguments into a functor, and call postToEventQueue() . Now, I wonder

Crash related to boost::function usage in thread pool

给你一囗甜甜゛ 提交于 2020-01-25 11:00:35
问题 I am trying to implement thread pool in C++ using pthread. I want to encapsulate logic related to threads management in one object which is taking ownership of these threads. That means whenever this object is destroyed, threads must be stopped and cleaned up. I've been testing my code and it turns out that I get segmentation fault when I destroy WorkerThreadManager object while there is boost::function called. See the code and backtrace from GDB. I don't really understand why it happens, as

Sending Python function as Boost.Function argument

房东的猫 提交于 2020-01-04 14:03:32
问题 Things are getting complicated in my world of trying to mesh Python code with my C++. Essentially, I want to be able to assign a callback function to be used after an HTTP call receives a response, and I want to be able to do this from either C++ or Python. In other words, I want to be able to call this from C++: http.get_asyc("www.google.ca", [&](int a) { std::cout << "response recieved: " << a << std::endl; }); and this from Python: def f(r): print str.format('response recieved: {}', r)

boost::bind and insert of a boost::unordered_map

这一生的挚爱 提交于 2020-01-03 04:57:06
问题 I want to use boost::bind to create a boost::function inserting a new key-value pair into a boost::unoredered_map but I got few compilation errors. typedef boost::unordered_map< std::string, std::string > dict_type; inline void insert( const std::string& key, const std::string& value ){ typedef std::pair<dict_type::iterator, bool> out_type; dict_type::value_type to_insert(key,value); boost::function<void()> f = boost::bind<out_type>( &dict_type::insert ,obj_ ,boost::cref(to_insert) ); } The

How can I use boost::bind to bind a class member function?

独自空忆成欢 提交于 2020-01-02 03:46:04
问题 #include <QtCore/QCoreApplication> #include <boost/bind.hpp> #include <boost/function.hpp> class button { public: boost::function<void()> onClick; boost::function<void(int ,double )> onClick2; }; class player { public: void play(int i,double o){} void stop(){} }; button playButton, stopButton; player thePlayer; void connect() { //error C2298: 'return' : illegal operation on pointer to member function expression playButton.onClick2 = boost::bind(&player::play, &thePlayer); stopButton.onClick =

Storing boost::function objects in a container

孤街浪徒 提交于 2019-12-29 07:52:53
问题 I have a vector of KeyCallback s: typedef boost::function<void (const KeyEvent&)> KeyCallback which I use to store all listeners for when a keyboard button is pressed. I can add them and dispatch the events to all callbacks with for_each , but I do not know how to actually erase a specific KeyCallback signature from my vector. For example I want something like this: void InputManager::UnregisterCallback(KeyCallback callback) { mKeyCallbacks.erase(std::find(mKeyCallbacks.begin(), mKeyCallbacks

Can tr1::function swallow return values?

血红的双手。 提交于 2019-12-22 04:01:01
问题 The boost::function FAQ item 3 specifically addresses the scenario I am interested in: Why are there workarounds for void returns? C++ allows them! Void returns are permitted by the C++ standard, as in this code snippet: void f(); void g() { return f(); } This is a valid usage of boost::function because void returns are not used. With void returns, we would attempting to compile ill-formed code similar to: int f(); void g() { return f(); } In essence, not using void returns allows boost:

How to use boost::bind with non-copyable params, for example boost::promise?

Deadly 提交于 2019-12-21 07:57:23
问题 Some C++ objects have no copy constructor, but have move constructor. For example, boost::promise. How can I bind those objects using their move constructors ? #include <boost/thread.hpp> void fullfil_1(boost::promise<int>& prom, int x) { prom.set_value(x); } boost::function<void()> get_functor() { // boost::promise is not copyable, but movable boost::promise<int> pi; // compilation error boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1); // compilation error as well boost:

How to use boost::bind with non-copyable params, for example boost::promise?

本小妞迷上赌 提交于 2019-12-21 07:57:07
问题 Some C++ objects have no copy constructor, but have move constructor. For example, boost::promise. How can I bind those objects using their move constructors ? #include <boost/thread.hpp> void fullfil_1(boost::promise<int>& prom, int x) { prom.set_value(x); } boost::function<void()> get_functor() { // boost::promise is not copyable, but movable boost::promise<int> pi; // compilation error boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1); // compilation error as well boost: