如何在C++中使用非静态成员函数作为回调 [英] How to use non static member functions as callback in C++

查看:29
本文介绍了如何在C++中使用非静态成员函数作为回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C++ 为 ESP8266 编写一个小程序,但遇到了麻烦.我创建了一个处理 LED 的 Led 类表单.这个想法是该类应该处理一个闪烁功能.为此,我使用了一个名为 Ticker 的库.

I am writing a small program for ESP8266 in C++ and run into trouble. I've created a Led class form handling leds. The idea is that the class should handle a blink function. For this I use a library called Ticker.

Ticker 中的一个函数,attach_ms 需要一个回调,我无法让它与非静态成员函数一起工作.

A function in Ticker, attach_ms requires a callback and I cant get that to work with a non static member functions.

这是我的头文件:

#ifndef led_h
#define led_h

#include <Arduino.h> 
#include <Ticker.h>
#include "debugutils.h"

#define tickLength 100


enum class LedState {
        OFF,
        ON,
        SLOW_BLINK,
        FAST_BLINK
};


class Led {
public:

    Led(Ticker *tick, uint8_t ledPin, int slowBlinkTime, int fastBlinkTime);

    void on();
    void off();
    void slowBlink( );
    void fastBlink( );

private:
    uint8_t pin;
    int counter;
    int slowNoBlinkTicks;
    int fastNoBlinkTicks;
    LedState state;
    void ledOn();
    void ledOff();
    void ledInvert();
    void clean();
    void blink(int par);
    void tickerCallbackLed();
};
#endif

这是我的代码文件:

#include "led.h"


void Led::ledOn() {
    digitalWrite(pin, HIGH);
}

void Led::ledOff() {
     digitalWrite(pin, LOW);
}

void Led::ledInvert() {
    digitalWrite(pin, !digitalRead(pin));
}

void Led::clean() {
    counter = 0;
}

void Led::blink(int par) {
    if (counter > par) {
        ledInvert();
        counter = 0;
    }
    else {
        counter++;
    }
}

void Led::tickerCallbackLed() {

    switch (state) {
        case LedState::OFF : break;
        case LedState::ON : break;
        case LedState::SLOW_BLINK : blink(slowNoBlinkTicks); break;
        case LedState::FAST_BLINK : blink (fastNoBlinkTicks);  break;
        default : break;
    };


};

void Led::on() {
    ledOn();
    state = LedState::ON;
};


void Led::off(){
    ledOff();
    state = LedState::OFF;
};

void Led::slowBlink(){
    clean();
    ledInvert();
    state = LedState::SLOW_BLINK;
};

void Led::fastBlink(){
    clean();
    ledInvert();
    state = LedState::FAST_BLINK;
};


Led::Led(Ticker *tick, uint8_t ledPin, int slowBlinkTime, int fastBlinkTime) {

    tick->attach_ms(tickLength, std::bind(&Led::tickerCallbackLed,this));

    slowNoBlinkTicks = slowBlinkTime/tickLength;
    fastNoBlinkTicks = fastBlinkTime/tickLength;

    pinMode(ledPin,OUTPUT);

    digitalWrite(ledPin,LOW);

    pin = ledPin;
    state = LedState::OFF;
    counter = 0;

}

这一行给出了一个编译错误,我不知道如何修复它.已尝试遵循我在互联网上找到的所有建议".

This line gives a compile error and I dont know how to fix it. Have tried to follow all "advice" I have found on internet.

 tick->attach_ms(tickLength, std::bind(&Led::tickerCallbackLed,this));

推荐答案

根据 这个版本的 Ticker.hTicker::attach_ms() 被重载以接受 std::functionvoid (*)(TArg) 作为回调:

According to this version of Ticker.h, Ticker::attach_ms() is overloaded to accept either a std::function<void(void)> or a void (*)(TArg) as the callback:

typedef void (*callback_with_arg_t)(void*);
typedef std::function<void(void)> callback_function_t;

void attach_ms(uint32_t milliseconds, callback_function_t callback)
{
        _callback_function = callback;
        attach_ms(milliseconds, _static_callback, (void*)this);
}

template<typename TArg>
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
        static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
        uint32_t arg32 = (uint32_t)arg;
        _attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}

在第一种情况下,您可以使用带有 std::function 的 lambda,您根本不需要 std::bind():

In the first case, you can use a lambda with std::function, you don't need std::bind() at all:

tick->attach_ms(tickLength, [this](){ this->tickerCallbackLed(); });

在第二种情况下,callback 接受一个传递给 Ticker::attach_ms() 的用户定义参数,因此您可以传递 this 作为该参数(正如您在上面看到的,这正是 Ticker::attach_ms()std::function 版本在内部所做的):

In the second case, the callback takes a user-defined argument that is passed to Ticker::attach_ms(), so you can pass this as that argument (which, as you can see above, is exactly what the std::function version of Ticker::attach_ms() does internally):

class Led {
...
private:
    ...
    static void staticTickerCallbackLed(Led *pThis);
    void tickerCallbackLed();
    ...
};

void Led::staticTickerCallbackLed(Led *pThis)
{
    pThis->TickerCallbackLed();
}

...

tick->attach_ms(tickLength, &Led::staticTickerCallbackLed, this);

但是请注意,Ticker::attach_ms() 不允许具有 > 的回调参数.大小为 4 个字节,这意味着在为 64 位编译时,任何一种方法都不起作用,其中指针为 8 个字节!恕我直言,这似乎是内部 Ticker::_attach_ms() 方法实现中的一个错误,该方法将回调参数作为 uint32_t 而不是 uintptr_t:

Do note, though, that Ticker::attach_ms() does not allow a callback argument that is > 4 bytes in size, which means either approach will not work when compiling for 64-bit, where pointers are 8 bytes! IMHO, that seems like a bug in the implementation of the internal Ticker::_attach_ms() method, which takes in a callback argument as uint32_t instead of as uintptr_t:

void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);

这篇关于如何在C++中使用非静态成员函数作为回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆