I'm trying to create control/window events similar to how they are done in C#'s Windows Forms/WPF. Each event class contains an argument type and passes that as well as a pointer to the sending object. This is all fine and dandy when using a lambda function, but when I try to add a member function to the event I get a compile error of C2276 '&': illegal operation on bound member function expression and C2660 'Event<MouseButtonEventArgs>::add': function does not take 1 arguments.
How would I create an event class that can take lambda functions and member functions from any class (whether or not that class is the "sender")?
Event.h
template<class Args> struct EventListener {
/* The function type to be called by the event. */
typedef function<void(ControlBaseSPtr, Args&)> ArgsFunction;
string name;
ArgsFunction function;
};
template<class Args> class Event {
/* The function type to be called by the event. */
typedef function<void(ControlBaseSPtr, Args&)> ArgsFunction;
deque<EventListener<Args>> listeners;
Event(EventDirections direction) {
this->direction = direction;
}
void add(const string& name, ArgsFunction function) {
EventListener<Args> listener = EventListener<Args>(name, function);
for (auto it = listeners.begin(); it != listeners.end(); it++) {
if (it->name == name) {
throw exception("Event::operator+= listener already exists in the event");
}
}
listeners.push_back(listener);
}
//...
};
Control.h
class ControlBase {
Event<MouseEventArgs> _eventMouseMovedGlobal;
Event<MouseButtonEventArgs> _eventMouseButtonGlobal;
ControlBase();
void onMouseMovedGlobal(ControlBaseSPtr sender, MouseEventArgs e);
};
Control.cpp
ControlBase::ControlBase() {
// Works
_eventMouseButtonGlobal.add("some name", [](ControlSPtr control, MouseButtonEventArgs e){
//...
};
// Doesn't work
_eventMouseMovedGlobal.add("some name", bind(&onMouseMovedGlobal, this, placeholders::_2))
// Doesn't work
_eventMouseMovedGlobal.add("some name", bind(&onMouseMovedGlobal, this))
}
void ControlBase::onMouseWheelGlobal(ControlBaseSPtr sender, MouseWheelEventArgs e) {
//...
}
Well, since onMouseMovedGlobal has 2 input arguments, you need placeholders or values for both of them when creating a binder. So, instead of
_eventMouseMovedGlobal.add("some name", bind(&onMouseMovedGlobal, this, placeholders::_2))
you need
_eventMouseMovedGlobal.add("some name", bind(&ControlBase::onMouseMovedGlobal, this, placeholders::_1, placeholders::_2))
Related
I am about to write a custom ESPHome component. I don't have much experience with C language and I am facing some troubles using external library.
For demonstration, I prepared a simple component class..
class Test: public Component {
public:
auto t = timer_create_default();
void setup() override {
ESP_LOGD("TEST", "Test setup called!");
t.every(1000, TestLog);
}
void loop() override {
ESP_LOGD("TEST", "Test loop called!");
t.tick();
}
bool TestLog(void *) {
ESP_LOGD("TEST", "TestLOG!");
return true;
}
}
With this, I receive:
In file included from src\main.cpp:32:0: src\Test.h:7:35: error:
non-static data member declared 'auto'
auto t = timer_create_default();
I took it from some example where they did not have the class, but I can't find out, how to use it.
The library is:
https://github.com/contrem/arduino-timer/
I can still rewrite it without this timer completely and handle it only in the loop function, but I would like to understand what I am doing wrong.
If I change the return type to Timer<> I got another error:
src\Test.h: In member function 'virtual void Test::setup()':
src\Test.h:11:24: error: no matching function for call to
'Timer<>::every(int, )'
t.every(1000, TestLog);
You can not use auto to declare non-static member variables so you need to replace auto with the type returned by timer_create_default().
If you are not sure what type it returns, you can simply use decltype in the declaration:
decltype(timer_create_default()) t = timer_create_default();
If I read the code in the repo correctly, the returned type is Timer<>, so this should also work:
Timer<> t = timer_create_default();
or simply:
Timer<> t;
Also: The function pointer passed to t.every() should be a bool (*)(void*) but TestLog is a non-static member function and the pointer type is bool (Test::*)(void*) - You can fix that by making TestLog static:
class Test: public Component {
public:
// ...
static bool TestLog(void *) {
ESP_LOGD("TEST", "TestLOG!");
return true;
}
};
If you want to get the Test instance in the TestLog callback, make the Timer
Timer<TIMER_MAX_TASKS, millis, Test*> t;
and change TestLog:
class Test: public Component {
public:
// ...
static bool TestLog(Test* t) {
ESP_LOGD("TEST", "TestLOG!");
return true;
}
};
and in setup():
t.every(1000, TestLog, this);
You'll now get a pointer to the Test instance in the TestLog callback and you can use this to call a non-static member function in Test.
Full example:
class Test : public Component {
public:
Timer<TIMER_MAX_TASKS, millis, Test*> t;
void setup() override {
ESP_LOGD("TEST", "Test setup called!");
// call the static member function every second:
t.every(1000, TestLogProxy, this);
}
void loop() override {
ESP_LOGD("TEST", "Test loop called!");
t.tick();
}
bool TestLog() {
ESP_LOGD("TEST", "TestLOG!");
return true;
}
static bool TestLogProxy(Test* t) {
// forward the callback call to the non-static member function:
return t->TestLog();
}
};
class close_queue
{};
class dispatcher
{
queue* q;
bool chained;
dispatcher(dispatcher const&)=delete;
dispatcher& operator=(dispatcher const&)=delete;
template<
typename Dispatcher,
typename Msg,
typename Func>
friend class TemplateDispatcher;
void wait_and_dispatch()
{
for(;;)
{
auto msg=q->wait_and_pop();
dispatch(msg);
}
}
bool dispatch(
std::shared_ptr<message_base> const& msg)
{
if(dynamic_cast<wrapped_message<close_queue>*>(msg.get()))
{
throw close_queue();
}
return false;
}
public:
dispatcher(dispatcher&& other):
q(other.q),chained(other.chained)
{
other.chained=true;
}
explicit dispatcher(queue* q_):
q(q_),chained(false)
{}
template<typename Message,typename Func>
TemplateDispatcher<dispatcher,Message,Func>
handle(Func&& f)
{
return TemplateDispatcher<dispatcher,Message,Func>(
q,this,std::forward<Func>(f));
}
~dispatcher() noexcept(false)
{
if(!chained)
{
wait_and_dispatch();
}
}
};
class receiver
{
queue q;
public:
sender operator()()
{
return sender(&q);
}
dispatcher wait()
{
return dispatcher(&q);
}
};
template<typename PreviousDispatcher,typename Msg,typename Func>
class TemplateDispatcher
{
queue* q;
PreviousDispatcher* prev;
Func f;
bool chained;
TemplateDispatcher(TemplateDispatcher const&)=delete;
TemplateDispatcher& operator=(TemplateDispatcher const&)=delete;
template<typename Dispatcher,typename OtherMsg,typename OtherFunc>
friend class TemplateDispatcher;
void wait_and_dispatch()
{
for(;;)
{
auto msg=q->wait_and_pop();
if(dispatch(msg))
break;
}
}
bool dispatch(std::shared_ptr<message_base> const& msg)
{
if(wrapped_message<Msg>* wrapper=
dynamic_cast<wrapped_message<Msg>*>(msg.get()))
{
f(wrapper->contents);
return true;
}
else
{
return prev->dispatch(msg);
}
}
public:
TemplateDispatcher(TemplateDispatcher&& other):
q(other.q),prev(other.prev),f(std::move(other.f)),
chained(other.chained)
{
other.chained=true;
}
TemplateDispatcher(queue* q_,PreviousDispatcher* prev_,Func&& f_):
q(q_),prev(prev_),f(std::forward<Func>(f_)),chained(false)
{
prev_->chained=true;
}
template<typename OtherMsg,typename OtherFunc>
TemplateDispatcher<TemplateDispatcher,OtherMsg,OtherFunc>
handle(OtherFunc&& of)
{
return TemplateDispatcher<
TemplateDispatcher,OtherMsg,OtherFunc>(
q,this,std::forward<OtherFunc>(of));
}
~TemplateDispatcher() noexcept(false)
{
if(!chained)
{
wait_and_dispatch();
}
}
};
class bank_machine
{
messaging::receiver incoming;
public:
bank_machine():
void run()
{
try
{
for(;;)
{
incoming.wait()
.handle<verify_pin>(
[&](verify_pin const& msg)
{
}
)
.handle<withdraw>(
[&](withdraw const& msg)
{
}
)
.handle<get_balance>(
[&](get_balance const& msg)
{
msg.atm_queue.send(::balance(balance));
}
)
.handle<withdrawal_processed>(
[&](withdrawal_processed const& msg)
{
}
)
.handle<cancel_withdrawal>(
[&](cancel_withdrawal const& msg)
{
}
);
}
}
}
};
The code above is a snippet from
C++ Concurrency in Action.
and I was wondering if someone can explain, what looks like, chained template instantiation inside bank_machine::run()? Why is it that we can we have a long chain of handle<some_type>( ...).handle<some_type>( ...).handle<some_type>( ...) . If you could point me to some resources and also correct any missuses of nomenclature I would appreciate it.
Cheers!
Why is it that we can we have a long chain of handle<some_type>( ...).handle<some_type>( ...).handle<some_type>( ...)?
For the same reason that you can chain any operator, e.g.
a + b + c + ...
works, so long as a + b returns an object that can be used as the left-hand side of operator+ with c as the right hand side.
In your example
handle<some_type>(...)
must return an object that has a member access operator . that can be invoked on it, where the member itself can be a handle<some_other_type> that can then be invoked, and so on.
I studied this example code snippet as well and used it in some of my own projects so I wanted to understand it in depth. This my best bet:
Everytime you call
handle
on the temporary dispatcher-object constructed by
incoming.wait()
a temporary object of type TemplateDispatcher is created. The magic happens in the destructor:
the last object to be created will be destroyed first and this will trigger the call to
wait_and_dispatch()
This makes the current thread of execution which is executing this line of code wait for a message to arrive at the message queue (it will sleep, besides spurious wakeups, as the code for dequeueing messages involves condition_variables and associated mutexes). If a message arrives, the thread of execution will check if it is able to deal with the message type and else, if this is not the case, delegate the call to the previously chained TemplateDispatcher or dispatcher-object. When this call is resolved, the destruction of the temporary objects will conclude and due to the
for(;;)
the thread will continue to wait for incoming messages in the same manner, again, until a close_queue-message arrives at the queue and will trigger an exception thrown in the temporary dispatcher-object's code, exactly in:
dispatcher::dispatch()
The TemplateDispatcher-objects created by calls to
handle
will not deal with close_queue-message objects and will therefore delegate any objects of this type to their predecessor in the call chain (dispatcher-class should be the only class that can deal with close_queue-objects) and it will finally be delegated to the dispatcher-object which will then trigger the exception.
The delegation of message objects that are not handled by a certain TemplateDispatcher-objects (identified by the template parameters on instantation in the handle-calls) are delegated to the previous TemplateDispatcher-object in the method
dispatch
of class TemplateDispatcher. A dynamic cast is used to determine if the current TemplateDispatcher-object has to deal with the arrived message.
I've written the most basic Event System I can think of. I come from a javascript background so I followed the On, Off, Emit syntax. The intention is to be able to create an EventSystem that can Emit any type of derived Event object and have the appropriate handlers called.
Please note, I am stuck in C++98 for reasons
So far my best idea is to have a simple Event object and a typedef for each Event type to handle it.
class Event {};
class AlarmEvent : Event {};
class ErrorEvent : Event {};
typedef void (*EventHandler)(Event event);
typedef void (*AlarmEventHandler)(AlarmEvent event);
typedef void (*ErrorEventHandler)(ErrorEvent event);
My issue is I want my modules to be able to attach as easy as possible.
int main()
{
Module module;
EventSystem es;
Event shutdown_event("shutdown");
AlarmEvent alarm_event("alarm", "Oh crap");
es.On("shutdown", module.OnEvent);
es.On("shutdown", module.OnEvent);
es.On("alarm", module.OnAlarmEvent);
es.Emit(shutdown_event);
es.Emit(alarm_event);
}
But looking at the EventSystem
class EventSystem {
public:
void On(std::string id, EventHandler handler);
void Emit(Event event);
void GetEventHandlers(std::string id, std::vector<EventHandler> *&handlers);
std::map<std::string, std::vector<EventHandler> > events;
};
I'd need an On, GetEventHandlers, and events property for every event type. This would quickly become terrible. Is there a better path where I can use a template to allow EventSystem to stay as simple as possible?
C++98 is old, older than variadic templates. The following emulates variadic templates with linked lists, which is very much suboptimal, but it should work.
// linked lists for "variadic" templates
struct Nil { };
template<typename X, typename XS>
struct Cons { };
// utility type
struct BlackHole {
template<typename T>
BlackHole(const T&) { }
};
// anything can be converted to a BlackHole implicitly, but it's a "worse"
// conversion than being converted to a base class
// I would template your event system over every event type
// this implementation only works properly if more derived events appear before their bases
template<typename Events> // e.g. Events = Cons<AlarmEvent, Cons<ErrorEvent, Cons<Event, Nil>>>
class EventSystem;
template<>
class EventSystem<Nil> {
protected:
// see below for Emit/EmitEmitted thing
// usage of BlackHole means that e.g. if calling with AlarmEvent
// and only overloads for Event and BlackHole are visible
// then the former will be chosen, since the latter conversion is worse
// can't just say template<typename T> EmitEmitted(T const&) { }
void EmitEmitted(BlackHole) { }
public:
// these overloads exist so the using declarations ahead don't fail
// for maximum type-safety, create a private type and
// make it an argument of each, so they can never be called
// using Emit/EmitEmitted creates type safety; again, see below
void Emit() { }
// On has easy type safety: you just can't call it for an unknown type
void On() { }
// GetEventHandlers doesn't really make sense anyway
// I don't think you need it, you can't have a vector of mixed handlers
// so why bother?
};
template<typename X, typename XS>
class EventSystem<Cons<X, XS> > : public EventSystem<XS> {
std::vector<void (*)(X)> handlers;
protected:
// "forward" all the EmitEmitted overloads made for XS
using EventSystem<XS>::EmitEmitted;
// overload for the specific case of an X
void EmitEmitted(X x) {
// fire all of the X-specific handlers
for(typename std::vector<void (*)(X)>::iterator i = handlers.begin(); i != handlers.end(); ++i) {
(*i)(x);
}
// call the rest of the handlers
EventSystem<XS>::EmitEmitted(x);
}
public:
// more "forwarding"
using EventSystem<XS>::Emit;
void Emit(X x) {
return EmitEmitted(x);
}
// suppose you have an EventSystem<Cons<std::string, Nil> >
// if you Emit an int, say, then you want this to fail
// thus the overload of Emit in EventSystem<Nil> should not be
// a catch-all or anything
// however, if you emit a std::string, then you need to recursively
// emit from EventSystem<Nil>, to handle any handlers for superclasses
// now you don't want it to explode
// solution? two functions
// Emit is the public entry point, and fails on unknown types
// EmitEmitted is named so because, once it's called, the type
// is known to be known, and will/has been emitted by at least one layer
// it no-ops once the base case is reached
// it is protected, and it is where the actual logic is
// easy now, right?
using EventSystem<XS>::On;
void On(void (*handler)(X)) {
handlers.push_back(handler);
}
};
Example usage:
struct Event {
std::string message;
Event(std::string message) : message(message) { }
};
void HandleEvent(Event e) {
std::cerr << e.message << "\n";
}
class AlarmEvent : public Event {
int hour;
int minute;
static std::string BuildMessage(int hour, int minute) {
std::stringstream builder;
builder << "Alarm: " << std::setfill('0');
builder << std::setw(2) << hour << ":";
builder << std::setw(2) << minute;
return builder.str();
}
friend void HandleAlarm(AlarmEvent);
public:
AlarmEvent(int hour, int minute) : Event(BuildMessage(hour, minute)), hour(hour), minute(minute) { }
};
void HandleAlarm(AlarmEvent a) {
// please ignore the fact that this is very stupid
if((a.hour + (a.minute / 60)) % 24 < 12) std::cerr << "AM Alarm\n";
else std::cerr << "PM Alarm\n";
}
struct ErrorEvent : Event {
ErrorEvent(std::string message) : Event(message) { }
};
void HandleError(ErrorEvent) {
static int count = 1;
std::cerr << "Error " << count++ << "\n";
}
int main() {
EventSystem<Cons<AlarmEvent, Cons<ErrorEvent, Cons<Event, Nil> > > > system;
// all handled by overload resolution
// no need to say what type you're dealing with
system.On(HandleEvent);
system.On(HandleAlarm);
system.On(HandleError);
// doesn't work
// system.On(std::exit)
system.Emit(ErrorEvent("Bad things"));
system.Emit(AlarmEvent(2, 30));
system.Emit(Event("Something happened"));
system.Emit(ErrorEvent("More bad things"));
system.Emit(AlarmEvent(11, 67));
// doesn't work
// system.Emit(5);
}
Not sure that all the example code is C++98, but that doesn't matter. It appears to work nicely. Also, there's a lot of copying going on here. It may be advisable to change handlers from void (*)(T) (which necessitates a copy) to void (*)(T&) or void (*)(T const&).
If your functions took references or pointers, then you would be able to pass polymorphic child types into them. So then you only need one type of function ptr.
typedef void (*EventHandler)(Event& event);
or
typedef void (*EventHandler)(Event* event);
I'm writing a simple event bus system to get familiar with this model. I have an addEvent function which takes an event name (string) and a function. I'm having trouble establishing my event class.
// Event class to define our event
class Event
{
public:
// function is some function that needs to be executed later
Event(const string eventName, void * function)
{
msgEvent.first = event;
msgEvent.second = function;
}
string getEvent(){
return msgEvent;
}
private:
pair<string, void*> msgEvent;
};
so when I make a call addEvent("open", openFunction), I would like to store this information as part of an Event.
I'm having a hard time understanding how I can store the function and if I'm correctly passing a function in the constructor as a parameter.
You can use function pointers or std::function. void* is for sure not correct. In any case, you need to know what signature your function has. Let's say, your functions do not take any input and do not return. Then, their signature is void()
Then, you can use the following code:
#include<functional>
#include<string>
class Event
{
public:
// function is some function that needs to be executed later
Event(const std::string eventName, std::function<void()> functionName)
{
msgEvent.first = eventName;
msgEvent.second = functionName;
}
std::string getEvent(){
return msgEvent.first;
}
void execute() {
msgEvent.second();
}
private:
std::pair< std::string, std::function<void()> > msgEvent; // why are you using
// std::pair here?
};
Now, you can write
Event myEvent( "open", [](){ /* do something */ } );
myEvent.execute();
I'm trying to make the equivalent of a Event Listener from Java, but in C++.
My goal is, that I can call a function from a class, which triggers my listener I added to this class.
I found the following Link which gave me a solution to do this.
The problem hereby is, that my program crashed as soon as I tried to call the listeners.
My code is structured like this:
class MessageHandler abstract
{
public:
typedef const std::function<void(int, std::string)> Handler;
void addHandler(Handler& handler) {
handlers.push_back(&handler);
}
private:
std::vector<Handler*> handlers;
protected:
void someFunction(int id, std::string message) {
for (auto& handler : handlers) {
(*handler)(id, message); //Here it will crash
}
}
};
As you maybe already mentioned, this is the base class from which I derive some childclasses. These childclasses call then my "someFunction" code.
And the class where I create one of these childclasses, is structured like this:
class Server
{
private:
SubHandler handler;
void setHandlers() {
handler.addHandler([&](int id, std::string message) { executingFunction(id, message); });
}
void executingFunction(int id, std::string message) {
std::cout << "Listener Worked!" << std::endl;
//Not actually the code inside, but it doesn't matter, case I don't even get to this code
}
};
The program crashes at the line, where I loop over my listeners and call them with error:
"Access violation when reading at position 0x000000000000000010."
(This is translated, so its not the message you will get if you have your Visual Studio set to English)
You should compile your code using /permissive-. The compiler should refuse your code.
void addHandler(Handler& handler) {
handlers.push_back(&handler);
}
You shouldn't be able to send a temporary to this function, but yet you are!
// v----- This lambda is a temporary object --------------------------v
handler.addHandler([&](int id, std::string message) { executingFunction(id, message); });
The lambda object created at that line dies just after the statement is finished.
// v---- pointer to the temporary.
handlers.push_back(&handler);
My recomendation would be to drop the pointer and use std::function object by value. They are made to be used like that:
// abstract is not a C++ keyword.
class MessageHandler /* abstract */
{
public:
// using instead of typedef and non const
using Handler = std::function<void(int, std::string)>;
void addHandler(Handler const& handler) { // const reference
// insert by value
handlers.push_back(handler);
}
private:
// no pointer here.
std::vector<Handler> handlers;
protected:
void someFunction(int id, std::string message) {
for (auto const& handler : handlers) {
handler(id, message); //Here it will not crash anymore
}
}
};
This is because your lambda defined in your Server class method isn't in the scope of your MessageHandler class. I suggest you read through this : https://blog.feabhas.com/2014/03/demystifying-c-lambdas/ to get a good idea of what the problem is and how to fix it.
Though, it might be a good solution to define a struct holding your lambda, which would then work with std::mem_fn.
Hope this helps
Your source is bad :/
You might use instead something like:
class MessageHandler
{
public:
using Handler = std::function<void(int, const std::string&)> Handler;
void addHandler(const Handler& handler) { handlers.push_back(handler); }
void execute(int id, const std::string& message) {
for (auto& handler : handlers) {
(*handler)(id, message);
}
}
private:
std::vector<Handler> handlers;
};
And then use it:
class Server
{
private:
MessageHandler handler;
void setHandlers()
{
handler.addHandler(&Server::executingFunction);
handler.addHandler(
[](int id, const std::string& message)
{
std::cout << message << id << std::endl;
});
}
static void executingFunction(int id, const std::string& message) {
std::cout << "Listener Worked!" << std::endl;
}
};