Ok so I already have this as a working example but I'm just trying to clean it up.
currently I have a subscription styled event system with callback routines.
I have no problem with adding regular functions and class member functions into the storage variable for it.
But id like to clean it up by overloading my Event.subscribe method.
Currently it does something like so:
template <class T>
class EventObject
{
public:
void subscribe(const T& arg)
{
this->events.push_back(arg);
}
std::vector<T> events;
}
And then In my main its used like so:
void someCallback(const std::string& line)
{
cout <<line;
}
typedef std::function<void(const std::string& line)> onMessageCallbackFunction;
EventObject<onMessageCallbackFunction> messageCallback;
messageCallback.subscribe(someCallback);
Now the pickle comes forward when I use a Class member function. I hate having to intrinsically bind the function before passing it. It makes the code look dirty.
for instance:
class B
{
void callbk(const std::string& line) {}
};
B b;
using std::placeholders::_1;
onMessageCallbackFunction memfunc = std::bind(&B::callbk, b, _1);
messageCallback.subscribe(memfunc);
Now yea it works, but it looks crappy. what is the syntax so I can just preform:
messageCallback.subscribe(&B::callbk, b);
Tried a few but I cant seem to get it JUST right.
What it is not:
template <class J>
void subscribe(J::T cls,T& t); //Nope.
void subscribe(J&& cls,T& t); //Nope.
SUPER CLOSE: (thx tobi)
template< class J, class... Args >
void subscribe(void(J::*funct)(Args), J& inst) {
using std::placeholders::_1;
subscribe(std::bind(funct, inst, _1));
}
Now just need to generic the argument list.