I have a class named Handler wich stores some lambdas. What I want to do is to have a std::vector of std::function that stores all my events, for exemple. I really can't figure out why lambdas doesn't work as I expected.
Here's the handler.h:
class Handler
{
public:
Handler();
~Handler();
void Register(const char* outcome, std::function<auto()> lambda);
void Trigger(const char* outcome);
private:
std::vector<int> identifier;
std::vector<char*> outcome;
std::vector<std::function<auto()>> func;
};
And handler.cpp:
Handler::Handler()
{
//ctor stuff here
}
Handler::~Handler()
{
this->func.clear();
this->outcome.clear();
this->identifier.clear();
//...
}
void Handler::Register(const char* outcome, std::function<auto()> lambda)
{
static int identifier = 0;
identifier++;
this->outcome.push_back((char*)outcome);
this->identifier.push_back(identifier);
this->func.push_back(lambda);
//Sort outcome
}
void Handler::Trigger(const char * outcome)
{
int i;
for (i = 0; i < this->identifier.size(); i++)
{
if (!strcmp(outcome, this->outcome.at(i)))
break;
}
this->func[i]();
}
However, if I specify lambdas in a Handler::Register it wont let me throwing no suitable user-defined conversion from "lambda []void ()->void" to "std::function<auto()> exists. In this example I use void return type but other types also error, I don't understant why can't the template from std::function deduce it out, if it is what's happening.
Handler* events = new Handler();
events->Register("Up", [=]() -> void { //Error here!
//do stuff
//return something?
});
Is there any other way to do this, like without overloading Handler::Register?
auto is not a type, so std::function<auto()> is not a type either. From how you are using it, std::function<void()> is probably what you want.
There are other problems with your code, as noted in the comments, so I would change Handler to this
class Handler
{
public:
Handler();
// default ~Handler is fine
void Register(std::string outcome, std::function<void()> lambda);
void Trigger(const std::string & outcome outcome) const;
void Trigger(std::size_t index) const;
private:
using Outcomes = std::map<std::string, std::function<void()>/*, custom string comparator ?*/>;
std::vector<Outcomes::iterator> identifier;
Outcomes outcomes;
};
void Handler::Register(std::string outcome, std::function<void()> func)
{
auto emplaced = outcomes.emplace(std::move(outcome), std::move(func));
identifier.push_back(emplaced.first);
}
void Handler::Trigger(const std::string & outcome) const
{
outcomes.at(outcome)();
}
void Handler::Trigger(std::size_t index) const
{
identifier[index]->second();
}
Related
Inspired by another article here on SO (C++ callback using class member) I tried to write a universal CallbackHandler.
CallbackHandler.hpp
#pragma once
#include <functional>
template <typename CallbackClass, typename CallbackArgType>
class CallbackHandler
{
public:
std::function<void(CallbackArgType ct)> m_callbackFunc;
CallbackHandler(CallbackClass * handler, std::function<void(CallbackArgType)> method)
{
//m_callbackFunc is supposed to stand for a member to pointer callback function with one
//parameter of any type
m_callbackFunc = std::bind(method, handler, std::placeholders::_1);
}
};
#include "wrapper_T.cpp"
I want to use it in several other templated namespaces/classes like here:
wrapper.hpp
//this wrappers main purpose is to combine the constructor of a non templated class (MyModule)
//and hold a (global) callback method for it (m_parentCallback)
namespace wrapper
{
extern std::function<void(wxImage *)> m_parentCallback;
template<typename ParentClass>
MyModule GetNewModule(ParentClass* parent, void (ParentClass::* method)(wxImage *));
}
wrapper.cpp
namespace wrapper
{
//This is only to avoid multiple definition error - actual definition is in wrapper_T.cpp
std::function<void(wxImage *)> m_parentCallback;
}
wrapper_T.cpp
namespace wrapper
{
template<typename ParentClass>
MyModule GetNewModule(ParentClass* parent, void (ParentClass::* method)(wxImage *))
{
//the callback type of this wrapper/class is wxImage*
std::shared_ptr<CallbackHandler<ParentClass, wxImage*>> handler =
std::make_shared< CallbackHandler<ParentClass, wxImage*>>(parent, method);
//EDIT - SOLVED: <- Error C2664: Cant convert argument 2 from "void (__thiscall MyModule::*)(void)" to "std::function<void(wxImage*)>"
m_parentCallback = std::bind(&CallbackHandler<ParentClass, wxImage*>::m_callbackFunc, handler, std::placeholders::_1);
//<- Error C2679: no suitable binary operator "=" found
return std::make_unique<MyModule>();
}
}
I wanted to use the callback like this:
MyModule.cpp
wrapper::m_parentCallback(&img);
I want to initialize the whole thing like this:
MainClass.cpp
MainClass::MainClass()
{
//declared in header: std::unique_ptr<MyModule> module
module = std::move(wrapper::GetNewModule(this, &MainClass::CallbackFunc));
}
void MainClass::CallbackFunc(wxImage * img)
{ /* do something with it */ }
I have the class with the "this" pointer and the pointer to method "CallbackFunc", which should be alright.
But I dont see how to use my CallbackHandler class for a std::function callback pointer.
Or did I overdo it with the wrapper holding a pointer to a method of the CallbackHandler, which holds a pointer to a method of the actual callback method?
All of this is no design choice, I just want the CallbackHandler to be portable and working, while having an interface which is easy to use.
EDIT:
I tried to apply the comments suggestions on the code, but I was to fast
with claiming the first problem was solved. The error was just hidden by the next error. If I try to compile with just this line:
std::shared_ptr<CallbackHandler<ParentClass, wxImage*>> handler =
std::make_shared< CallbackHandler<ParentClass, wxImage*>>(parent, method);
//<- Error C2664: "CallbackHandler<ParentClass,wxImage *>::
//CallbackHandler(CallbackHandler<ParentClass,wxImage *> &&)"
//: converting argument 2 from "void (__thiscall MainClass::* )(wxImage *)"
//to "std::function<void (wxImage *)>" not possible
// with
// [
// ParentClass=MainClass
]
//(freely translated into english by me)
So, the missing argument was not the only problem.
If std::bind on methods (member functions) does not work, I do have to change the CallbackClass as well, dont I?
Maybe something along those lines:
std::function<void(CallbackArgType cat)> m_callbackFunc;
CallbackHandler(CallbackClass * handler, std::function<void(CallbackArgType)> method)
{
//m_callbackFunc = std::bind(method, handler, std::placeholders::_1);
m_callbackFunc = [method](auto img) { method(img); };
}
Replace
m_parentCallback = std::bind(&CallbackHandler<ParentClass, wxImage*>::m_callbackFunc, handler, std::placeholders::_1);
with
m_parentCallback = [handler](auto img){ handler->m_parentCallback(img); };
I don't think bind was designed to work with member functor objects on top of everything. But lambdas handle that seamlessly.
I noticed that my problem is beeing solved by every event handler, calling an event function. So I used an event handler as basis vor my CallbackHandler. I found and used this event handler, as there are most certainly many other good written examples:
https://www.codeproject.com/Articles/1256352/CppEvent-How-to-Implement-Events-using-Standard-Cp#holdingHandlerFunction
I just wrapped it in a class and it is a little overkill for my purpose, but it works. All credits to Shmuel Zang (see link above)
EventHandler.hpp
#ifndef EventHandler_hpp
#define EventHandler_hpp
// https://www.codeproject.com/Articles/1256352/CppEvent-How-to-Implement-Events-using-Standard-Cp#holdingHandlerFunction
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <atomic>
#include <mutex>
#include <future>
namespace EventHandler
{
template <typename... Args> class EventHandler_Base
{
public:
typedef std::function<void(Args...)> handler_func_type;
typedef unsigned int handler_id_type;
explicit EventHandler_Base(const handler_func_type& handlerFunc)
: m_handlerFunc(handlerFunc)
{
m_handlerId = ++m_handlerIdCounter;
}
// copy constructor
EventHandler_Base(const EventHandler_Base& src)
: m_handlerFunc(src.m_handlerFunc), m_handlerId(src.m_handlerId)
{
}
// move constructor
EventHandler_Base(EventHandler_Base&& src)
: m_handlerFunc(std::move(src.m_handlerFunc)), m_handlerId(src.m_handlerId)
{
}
// copy assignment operator
EventHandler_Base& operator=(const EventHandler_Base& src)
{
m_handlerFunc = src.m_handlerFunc;
m_handlerId = src.m_handlerId;
return *this;
}
// move assignment operator
EventHandler_Base& operator=(EventHandler_Base&& src)
{
std::swap(m_handlerFunc, src.m_handlerFunc);
m_handlerId = src.m_handlerId;
return *this;
}
// function call operator
void operator()(Args... params) const
{
if (m_handlerFunc)
{
m_handlerFunc(params...);
}
}
bool operator==(const EventHandler_Base& other) const
{
return m_handlerId == other.m_handlerId;
}
operator bool() const
{
return m_handlerFunc;
}
handler_id_type id() const
{
return m_handlerId;
}
private:
handler_func_type m_handlerFunc;
handler_id_type m_handlerId;
static std::atomic_uint m_handlerIdCounter;
};
template <typename... Args> std::atomic_uint EventHandler_Base<Args...>::m_handlerIdCounter(0);
template <typename... Args> class Event_Base
{
public:
typedef EventHandler_Base<Args...> handler_type;
Event_Base()
{
}
// copy constructor
Event_Base(const Event_Base& src)
{
std::lock_guard<std::mutex> lock(src.m_handlersLocker);
m_handlers = src.m_handlers;
}
// move constructor
Event_Base(Event_Base&& src)
{
std::lock_guard<std::mutex> lock(src.m_handlersLocker);
m_handlers = std::move(src.m_handlers);
}
// copy assignment operator
Event_Base& operator=(const Event_Base& src)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
std::lock_guard<std::mutex> lock2(src.m_handlersLocker);
m_handlers = src.m_handlers;
return *this;
}
// move assignment operator
Event_Base& operator=(Event_Base&& src)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
std::lock_guard<std::mutex> lock2(src.m_handlersLocker);
std::swap(m_handlers, src.m_handlers);
return *this;
}
typename handler_type::handler_id_type add(const handler_type& handler)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
m_handlers.push_back(handler);
return handler.id();
}
inline typename handler_type::handler_id_type add(const typename handler_type::handler_func_type& handler)
{
return add(handler_type(handler));
}
bool remove(const handler_type& handler)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
auto it = std::find(m_handlers.begin(), m_handlers.end(), handler);
if (it != m_handlers.end())
{
m_handlers.erase(it);
return true;
}
return false;
}
bool remove_id(const typename handler_type::handler_id_type& handlerId)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
auto it = std::find_if(m_handlers.begin(), m_handlers.end(),
[handlerId](const handler_type& handler) { return handler.id() == handlerId; });
if (it != m_handlers.end())
{
m_handlers.erase(it);
return true;
}
return false;
}
void call(Args... params) const
{
handler_collection_type handlersCopy = get_handlers_copy();
call_impl(handlersCopy, params...);
}
std::future<void> call_async(Args... params) const
{
return std::async(std::launch::async, [this](Args... asyncParams) { call(asyncParams...); }, params...);
}
inline void operator()(Args... params) const
{
call(params...);
}
inline typename handler_type::handler_id_type operator+=(const handler_type& handler)
{
return add(handler);
}
inline typename handler_type::handler_id_type operator+=(const typename handler_type::handler_func_type& handler)
{
return add(handler);
}
inline bool operator-=(const handler_type& handler)
{
return remove(handler);
}
protected:
typedef std::list<handler_type> handler_collection_type;
void call_impl(const handler_collection_type& handlers, Args... params) const
{
for (const auto& handler : handlers)
{
handler(params...);
}
}
handler_collection_type get_handlers_copy() const
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
// Since the function return value is by copy,
// before the function returns (and destruct the lock_guard object),
// it creates a copy of the m_handlers container.
return m_handlers;
}
private:
handler_collection_type m_handlers;
mutable std::mutex m_handlersLocker;
};
}
#endif // EventHandler_hpp
I then use this generic class to save a callback member method:
CallbackHandler.hpp
#ifndef CallbackHandler_hpp
#define CallbackHandler_hpp
#pragma once
#include "EventHandler.hpp"
#include <functional>
#include <mutex>
using namespace EventHandler;
template <typename... Args>
class CallbackHandler
{
public:
typedef std::function<void(Args...)> callbackFunc_type;
explicit CallbackHandler() : mb_callbackIsSet(false), mi_handlerID(0){}
explicit CallbackHandler(const callbackFunc_type& handlerFunc) : mb_callbackIsSet(false), mi_handlerID(0)
{
SetCallbackMethod(handlerFunc);
}
~CallbackHandler()
{
m_callbackEvent.remove_id(mi_handlerID);
}
void SetCallbackMethod(const callbackFunc_type& handlerFunc)
{
m_callbackEvent.remove_id(mi_handlerID);
mi_handlerID = m_callbackEvent.add([=](Args... params) {
handlerFunc(params...);
});
mb_callbackIsSet = true;
}
bool DoCallback(Args... params)
{
if (mb_callbackIsSet)
{
std::lock_guard<std::mutex> lock(callbackLocker);
m_callbackEvent(params...);
return true;
}
return false;
}
private:
unsigned int mi_handlerID;
Event_Base<Args...> m_callbackEvent;
bool mb_callbackIsSet;
std::mutex callbackLocker;
};
#endif //CallbackHandler_hpp
I can use the CallbackHandler in every other class now:
//Imagine an example class named Screenshotmodul
//In this example I use wxImage as a callback object, it is the output of my Screenshotmodul class and should be given back to my main class
//It could be any other (or several others like this: <ObjectType, OtherCallbackType> )
class Screenshotmodul
{
public:
//[...]
CallbackHandler<wxImage> m_callbackHandler;
template<typename ParentClass>
void SetCallbackMethod(ParentClass* parent, void (ParentClass::* method)(wxImage))
{
//using the given object pointer and the given member method in a lambda function
//saving that lambda as callback method
m_callbackHandler.SetCallbackMethod([=](wxImage img) {
(parent->*method)(img); });
}
}
In my main class I can then set a callback method:
std::unique_ptr<Screenshotmodul> sm = std::make_unique<Screenshotmodul>();
sm->SetCallbackMethod(this, &MyMainClass::CallbackfuncForScreenshotmodul);
//Of course there should be a callback function as just described:
void MyMainClass::CallbackfuncForScreenshotmodul(wxImage img)
{
//img now contains the callback value, that the Screenshotmodul class created
}
This is my approach and it is not perfect, but it works for me.
Cheers
Natu
How can one pass a non moveable object to a std::function? One easy enough alternative is passing a std::reference_wrapper which would create the need for the lifecycle of function be dependant on the object. Example code follows to explain the issue better.
class Player {
std::atomic_int runs {0};
std::string name;
public:
Player(std::string&& name) : name(std::move(name)) {} //...
void inc() { ++runs;}
};
class PlayerStats {
std::array<std::unique_ptr<Player>,2> players;
public:
PlayerStats() {
for(int i = 0; i<2 ; i++)
players[i] = std::unique_ptr<Player>(new Player{"player"+std::to_string(i)});
}
Player* const operator() (int index) const {
return players[index].get();
}
};
using player_total_f = std::function<Player* const(int index)>;
class GameStats {
std::string game;
std::string date;
player_total_f f;
public:
GameStats(std::string&& game, std::string&& date, player_total_f&& _f) :
game(std::move(game)), date(std::move(date)), f(std::move(_f)) {}
};
int main(int argc, char *argv[])
{
PlayerStats st;
//GameStats("game1","10.11",std::ref(st)); //this seems like the only possibility, no way to make GameStats own the functor
return 0;
}
How can I set the function here to PlayerStats, given that it is non copyable, a std::ref seems to be like the only possibility?
template<class F>
auto shared_function( F&& f ){
auto spf=std::make_shared<std::decay_f<F>>(std::forward<F>(f));
return [spf=std::move(spf)](auto&&...args)->decltype(auto){
return (*pf)(decltype(args)(args)...);
};
}
own it in a shared ptr. Changes semantics a bit, but fixes it.
Or write your own non-copying std function.
This is my attempt:
#include <iostream>
#include <functional>
class Voice;
class EnvelopeMultiPoints
{
public:
std::function<double(Voice &, double)> mCallback;
void SetupModulation(std::function<double(Voice &, double)> callback, int paramID) {
mCallback = callback;
}
};
class Voice
{
public:
EnvelopeMultiPoints mEnvelopeMultiPoints;
};
class VoiceManager
{
public:
Voice mVoices[16];
inline void UpdateVoices(std::function<void(Voice &)> callback) {
for (int i = 0; i < 16; i++) {
callback(mVoices[i]);
}
}
static void SetupEnvelopeMultiPointsModulation(Voice &voice, std::function<double(Voice &, double)> callback, int paramID) {
voice.mEnvelopeMultiPoints.SetupModulation(callback, paramID);
}
};
class Oscillator
{
public:
double ModulatePitch(Voice &voice, double currentValue) {
// somethings with voice
return currentValue * 10.0;
}
};
int main()
{
VoiceManager voiceManager;
Oscillator *pOscillator = new Oscillator();
int param = 100;
auto callback = std::bind(&Oscillator::ModulatePitch, pOscillator, std::placeholders::_1, std::placeholders::_2);
voiceManager.UpdateVoices(std::bind(&VoiceManager::SetupEnvelopeMultiPointsModulation, std::placeholders::_1, callback, param));
Voice voice = voiceManager.mVoices[0];
std::cout << voice.mEnvelopeMultiPoints.mCallback(voice, 1.0) << std::endl;
delete pOscillator;
}
I create a sort of Voice Updater "basic" iterator, which I can pass any kind of functions later. It iterates all voices and pass the function I need for that iteration.
But it seems I'm wrong on bind the Oscillator::ModulatePitch function to pass to the Updater?
Where am I wrong here?
As pergy wrote in its comment, if you use std::function<double(Voice &, double)> callback = ... instead of auto, it works. The reason is that it forces a conversion from an bind object to a std::function.
If you do
auto callback1 = std::bind(&Oscillator::ModulatePitch, pOscillator, std::placeholders::_1, std::placeholders::_2);
std::function<double(Voice &, double)>callback2 = std::bind(&Oscillator::ModulatePitch, pOscillator, std::placeholders::_1, std::placeholders::_2);
std::cout << typeid(callback1).name();
std::cout << typeid(callback2).name();
you will see that the returning types are differents. And in this case, the second bind tries to build its object using the second type (which does not work). I think there is an issue with the conversion operator (from bind to std::function) not being called by the external bind.
I did quite a bit of searching, but the combination of * () and class scope has greatly hindered me in understanding of the syntax, with each edit throwing a new error, any help guys?
What I'm trying to do:
Declare a std::vector of pointers to member functions found in MyClass.h
Assign the actual member functions to the std::vector in MyClass.cpp's constructor
The member functions are not static
Thanks!
I'm curious where you're going to use them from. You see in order to call a C++ class member function you need to have an instance pointer with which to call it (each member function needs a this in order to access the class state). So normally you'd wrap the member function pointer together with the instance pointer with std::bind and then maybe store the result in std::function. To put them in vector they're all going to need the same signature.
Is this the kind of thing you were looking for:
class P
{
typedef std::function<void (void)> func_t;
std::vector<func_t> functions;
public:
P()
{
functions.push_back(std::bind(&P::foo1, this));
functions.push_back(std::bind(&P::foo2, this));
functions.push_back(std::bind(&P::foo3, this));
}
void foo1(void)
{
std::cout << "foo1\n";
}
void foo2(void)
{
std::cout << "foo2\n";
}
void foo3(void)
{
std::cout << "foo3\n";
}
void call()
{
for(auto it = functions.begin(); it != functions.end(); ++it)
{
(*it)();
}
}
};
int main()
{
P p;
p.call();
}
After further clarification from the OP I'll propose this:
class P
{
typedef std::function<void (void)> func_t;
std::map<const char*, func_t> functions;
public:
P()
{
functions["foo1"] = std::bind(&P::foo1, this);
functions["foo2"] = std::bind(&P::foo2, this);
functions["foo3"] = std::bind(&P::foo3, this);
}
void foo1(void)
{
std::cout << "foo1\n";
}
void foo2(void)
{
std::cout << "foo2\n";
}
void foo3(void)
{
std::cout << "foo3\n";
}
void call_by_name(const char* func_name)
{
functions[func_name]();
}
};
int main()
{
P p;
p.call_by_name("foo1");
p.call_by_name("foo2");
p.call_by_name("foo3");
}
You can use member function pointers like this (the C++11 is unrelated to that part):
struct S {
int foo(){std::cout<<"foo"; return 0;}
int bar(){std::cout<<"bar"; return 0;}
};
int main() {
std::vector<int(S::*)()> funcs{&S::foo, &S::bar};
S s;
for (auto func : funcs) {
(s.*func)();
}
}
However, if you use C++11, std::function can make it a bit cleaner:
std::vector<std::function<int(S &)>> funcs{&S::foo, &S::bar};
S s;
for (auto func : funcs) {
func(s);
}
If you use C++03, Boost has boost::function, which is similar.
Trying to create an asyncronous Observer pattern is causing a compiler error C3867, which I am clueless how to resolve it. The sample code snippet is as follows
class Subject;
class Observer
{
public:
virtual void notify(Subject* s) = 0;
virtual ~Observer() {};
};
class Subject
{
std::map<std::string, Observer *> observers;
protected:
void notify_observers()
{
std::map<std::string, Observer *>::iterator iter;
for (iter = observers.begin(); iter != observers.end(); ++iter) {
void (Observer::*notify)(Subject *) = iter->second->notify;
std::async(std::launch::async, notify, this);
}
}
public:
virtual ~Subject() {};
void observer(std::string id, Observer* o)
{
observers[id] = o;
}
};
template<typename Iter, typename type>
class Sort : public Observer {
public:
virtual void notify(Subject* s)
{
TestSort<Iter> *a;
a = dynamic_cast<TestSort<Iter> *>(s);
std::vector<type> temp(a->beg(), a->end());
sort(temp->beg(), temp->end());
}
};
template<typename Iter, typename type>
class InsertionSort : public Sort<Iter, type>
{
void sort(Iter beg, Iter end) {
for (Iter i = beg; i != end; ++i)
std::rotate(std::upper_bound(beg, i, *i), i, i+1);
}
};
int main ()
{
std::vector<double> data(100);
std::generate(data.begin(), data.end(), [](){return rand() % 500;} );
auto ts = TestSort<std::vector<double>::iterator >(data.begin(), data.end());
auto is = new InsertionSort<std::vector<double>::iterator, double >();
//.................
ts.observer("InsertionSort", is);
//.........................
ts.Triggerd();
return 0;
}
Though I understand the error
error C3867: 'Observer::notify': function call missing argument list; use '&Observer::notify' to create a pointer to member
Yet in this context I cannot figure out, how to resolve it.
In this context, if notify would had been a simply addreesable member function, instead of
void (Observer::*notify)(Subject *) = iter->second->notify;
I could have simply write
void (Observer::*notify)(Subject *) = &Observer::notify;
But notify is a polymorphic function and I cannot address the right function during compile time.
Please suggest how should I process
You don't need to figure out the right function during compile time, just as you don't have to figure it out for regular virtual function call. Just use &Observer::notify. The right function is selected at the time of call, not at the time of taking its address.
Change:
void (Observer::*notify)(Subject *) = iter->second->notify;
std::async(std::launch::async, notify, this);
To:
void (Observer::*notify)(Subject *) = &Observer::notify;
std::async(std::launch::async, std::mem_fun(notify), iter->second, this);
When you call a method, you need both the pointer-to-instance and arguments. The standard syntax is rettype retval = instance->method(arg);, but std::mem_fun will return a functor you can use like rettype retval = std::mem_fun(&InstanceType::method)(instance, arg); -- it makes the implicit this pointer passed to a member function explicit.
From a pointer to a virtual method, plus an object pointer, std::mem_fun can figure out which instance of the virtual method you should call.
A similar thing can be done with a bind or a lambda. Here is a roughly equivalent call using lambda syntax:
Observer* observer = iter->second;
std::async(std::launch::async, [observer,this]() { observer->notify(this); } );
See the comment below: you don't have to use std::mem_fun, async will do it for you. You do have to pass the instance pointer of the member function as the next argument still.