boost-bind

Messaging system: Callbacks can be anything

ぃ、小莉子 提交于 2019-11-29 08:21:48
I'm trying to write an event system for my game. The callbacks that my event manager will store can be both plain functions as well as functors. I also need to be able to compare functions/functors so I know which one I need to disconnect from the event manager. • Initially I tried using boost::function; it handles functions and functors perfectly well, except it has no operator==, so I can't remove callbacks if I want to. class EventManager { typedef boost::function<void (boost::weak_ptr<Event>)> Callback; std::map<Event::Type, std::vector<Callback>> eventHandlerMap_; }; • I also tried using

Getting return value from a boost::threaded member function?

荒凉一梦 提交于 2019-11-29 04:29:18
I have a worker class like the one below: class Worker{ public: int Do(){ int ret = 100; // do stuff return ret; } } It's intended to be executed with boost::thread and boost::bind, like: Worker worker; boost::function<int()> th_func = boost::bind(&Worker::Do, &worker); boost::thread th(th_func); th.join(); My question is, how do I get the return value of Worker::Do? Thanks in advance. I don't think you can get the return value. Instead, you can store the value as a member of Worker: class Worker{ public: void Do(){ int ret = 100; // do stuff m_ReturnValue = ret; } int m_ReturnValue; } And use

About shared_ptr and pointer to member operator `->*` and `std::bind`

ⅰ亾dé卋堺 提交于 2019-11-29 03:58:24
Recently I discovered that shared_ptr does not have pointer to member operator ->* . I created simple example: template <typename Pointer, typename Function, typename... Args> auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...)) { return (p->*f)(args...); } struct A { void g() { std::cout << "A::g()\n"; } }; int main() { A a; invoke1(&a, &A::g); // works!! std::shared_ptr<A> sa = std::make_shared<A>(); invoke1(sa, &A::g); // compile error!! } Q1: Why is so? Why shared_ptr does not have this operator? I added such operator for shared_ptr and the example started to

How does boost bind work behind the scenes in general?

狂风中的少年 提交于 2019-11-28 20:12:14
Without spending a long time reviewing the boost source code, could someone give me a quick rundown of how boost bind is implemented? I like this piece of the bind source: template<class R, class F, class L> class bind_t { public: typedef bind_t this_type; bind_t(F f, L const & l): f_(f), l_(l) {} #define BOOST_BIND_RETURN return #include <boost/bind/bind_template.hpp> #undef BOOST_BIND_RETURN }; Tells you almost all you need to know, really. The bind_template header expands to a list of inline operator() definitions. For example, the simplest: result_type operator()() { list0 a; BOOST_BIND

Role of placeholder in Boost::bind in the following example

僤鯓⒐⒋嵵緔 提交于 2019-11-28 11:16:08
问题 There are numerous example on SO regarding the use of placeholders however I am still a little bit confused and would appreciate it if someone could explain the difference between the following two statements void SomeMethod(int a) { std::cout << "Parameter pass " << a << "\n"; } Statement 1 : boost::bind(&SomeMethod,_1)(12); Statement 2 : boost::bind(&SomeMethod,12)(); I believe I understand statement 1 which is chaining. The output of boost::bind(&SomeMethod,_1) gets to have a parameter of

How can I store a boost::bind object as a class member?

戏子无情 提交于 2019-11-28 08:38:18
问题 I'm writing an application that uses boost::asio . Asio's async_receive (or async_read ) is invariably shown using a boost::bind object given for callback: boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), chat_message::header_length), boost::bind(&chat_session::handle_read_header, shared_from_this(), boost::asio::placeholders::error)); That's perfectly nice, but I'd like not to have to recreate the bind object after each call to the callback. Instead, I'd like to create

Messaging system: Callbacks can be anything

萝らか妹 提交于 2019-11-28 01:55:29
问题 I'm trying to write an event system for my game. The callbacks that my event manager will store can be both plain functions as well as functors. I also need to be able to compare functions/functors so I know which one I need to disconnect from the event manager. • Initially I tried using boost::function; it handles functions and functors perfectly well, except it has no operator==, so I can't remove callbacks if I want to. class EventManager { typedef boost::function<void (boost::weak_ptr

Pass and call a member function (boost::bind / boost::function?)

萝らか妹 提交于 2019-11-28 00:58:13
问题 I have a probably embarassingly simple problem: pass and call a member function in a class. I know I want to use BOOST bind (and or function), but I haven't really grasped the concept to it yet. The following code compiles and executes with problem. But when I want to change the "f3" function to a non-static class function, then the fun begins: #include <iostream> #include <inttypes.h> #include <boost/bind.hpp> #include <boost/function.hpp> class Test { public: void f1(); private: void f2

Can I use (boost) bind with a function template?

孤街醉人 提交于 2019-11-28 00:51:49
Is it possible to bind arguments to a function template with (boost) bind ? // Define a template function (just a silly example) template<typename ARG1, typename ARG2> ARG1 FCall2Templ(ARG1 arg1, ARG2 arg2) { return arg1 + arg2; } // try to bind this template function (and call it) ... boost::bind(FCall2Templ<int, int>, 42, 56)(); // This works boost::bind(FCall2Templ, 42, 56)(); // This emits 5 pages of error messages on VS2005 // beginning with: error C2780: // 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> // boost::bind(M T::* ,A1)' :

Calling base class definition of virtual member function with function pointer

≡放荡痞女 提交于 2019-11-28 00:41:46
I want to call the base class implementation of a virtual function using a member function pointer. class Base { public: virtual void func() { cout << "base" << endl; } }; class Derived: public Base { public: void func() { cout << "derived" << endl; } void callFunc() { void (Base::*fp)() = &Base::func; (this->*fp)(); // Derived::func will be called. // In my application I store the pointer for later use, // so I can't simply do Base::func(). } }; In the code above the derived class implementation of func will be called from callFunc. Is there a way I can save a member function pointer that