GyverLibs / FastBot

Многофункциональная быстрая библиотека для Телеграм бота на esp8266/esp32
MIT License
186 stars 31 forks source link

attach() variant with passing payload to callback #16

Closed filimonic closed 1 year ago

filimonic commented 2 years ago

attach() variant with passing payload to callback

Problem: When creating bot as a part of some class, unable to pass instance of class to callback function

class ControlTGBot : IControl
{
private:
    FastBot _bot;
    void _messageCb(FB_msg& msg) {...}
public: 
    void start()
    {
        _bot.attach(_messageCb); // Broken
    }
}

Solution: Overload attach() method as void attach(void (*handler)(FB_msg& msg, void* payload), void* payload)

class ControlTGBot : IControl
{
private:
    FastBot _bot;
    static void _messageCb(FB_msg& msg, void * control) {...}
public: 
    void start()
    {
        _bot.attach(_messageCb, this); 
    }
}