gpt4 book ai didi

c++ - 如何将静态逻辑重构为实现由 C++ 中的参数触发的回调函数的泛型类

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:14:16 25 4
gpt4 key购买 nike

我编写了一个小的 C++ 程序来根据按钮按下的持续时间调用不同的函数。现在我想将其重构为更通用并且可以从类中访问。理想情况下,我想以与此类似的方式使用该类:

myduration buttonDuration()
myduration.add(*func1(1), *func2(4), *func3(8))

其中参数是一个数值,表示在多少秒后调用特定函数。我不知道从哪里开始寻找如何实现一个将带有参数的回调函数作为输入的类。

我当前的代码如下所示:(“Ticker”生成一个基于时间的中断并在此处用于计算经过的秒数)

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

Serial pc(USBTX, USBRX);
Ticker ticker;

InterruptIn pb(p17);

void state1(void){
led2 = !led2;
}

void state2(void){
led3 = !led3;
}

void state3(void){
led4 = !led4;
}


// Global count variable
int volatile counter = 0;
int volatile STATE = 0;

void countCallback(void) {
counter = counter + 1;
}
void stateEval(void) {
if(counter >=1 && counter <=3){
STATE = 1;
state1();
}
if (counter >= 4 && counter <=7){
STATE = 2;
state2();
}
if (counter >=8 && counter <=10){
STATE = 3;
state3();
}
pc.printf("STATE %d \n", STATE);
}

// pb Interrupt routine - is interrupt activated by a falling edge of pb input
void pb_hit_interrupt (void) {
ticker.attach(countCallback, 1);
counter = 0;
}

void pb_release_interrupt (void) {
ticker.detach();
stateEval();
counter = 0;
STATE = 0;
// pc.printf("Counter: %d \n", counter);
}

int main() {
// Use internal pullup for pushbutton
pb.mode(PullUp);
// Delay for initial pullup to take effect
wait(.01);
// Attach the address of the interrupt handler routine for pushbutton
pb.rise(&pb_release_interrupt);
pb.fall(&pb_hit_interrupt);
// Blink led1 in main routine forever while responding to pb changes
// via interrupts that activate pb_hit_interrupt routine
while (1) {
led1 = !led1;
wait(.5);
}
}

最佳答案

std::map 和 upper/lower_bound 的组合应该可以解决问题。

#include <iostream>
#include <functional>
#include <map>

using state_caller = std::map<double , std::function<void()>>;


void after3()
{
std::cout << "up to 3 seconds" << std::endl;
}

void after1()
{
std::cout << "up to 1 seconds" << std::endl;
}

void after8()
{
std::cout << "up to 8 seconds" << std::endl;
}

void handle(const state_caller& cs, double secs)
{
auto i = cs.lower_bound(secs);
if (i == cs.end())
{
std::cout << "no entry for this" << std::endl;
}
else
{
i->second();
}
}

int main()
{
state_caller caller;
caller.emplace(1, after1);
caller.emplace(3, after3);
caller.emplace(8, after8);

handle(caller, 0.5);
handle(caller, 2.9);
handle(caller, 3.0);
handle(caller, 10.0);
return 0;
}

but I don't have std::map available on my implementation.

也许是这样的?同样,iostream只是为了提供一个演示框架。如果您没有标题 <algorithm>方便(为什么不呢?)然后很容易写。有一个 std::find_if 的实现这里:http://en.cppreference.com/w/cpp/algorithm/find

#include <iostream>
#include <algorithm>

struct action
{
int counter_limit;
int new_state;
void (*transition)(void);
};

void state1() {
std::cout << __func__;
}
void state4() {
std::cout << __func__;
}
void state8() {
std::cout << __func__;
}

// note that action_table is ordered by ascending counter_limit, so std::find will find the lower
// entry where counter_limit > counter
const action action_table[] =
{
{ 4, 1, state1 },
{ 8, 2, state4 },
{ 11, 3, state8 },
};

int STATE =0;

void stateEval(int counter) {
auto iaction = std::find_if(std::begin(action_table),
std::end(action_table),
[counter](const action& a) {
return counter < a.counter_limit;
});
if (iaction == std::end(action_table)) {
// handle error here
}
else {
STATE = iaction->new_state;
iaction->transition();
}
std::cout << " : " << "counter = " << counter << ", STATE = " << STATE << std::endl;
}

int main()
{
for (int counter = 0 ; counter < 11; ++counter)
stateEval(counter);

return 0;
}

关于c++ - 如何将静态逻辑重构为实现由 C++ 中的参数触发的回调函数的泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36034275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com