C++内联函数指针和模板函数设计

标签 c++ embedded

好的,我有一个允许用户在 100x64 LCD 上设置像素的类。

// (U8 = unsigned char)
inline void pixelOn(const U8 X, const U8 Y) {
   *(disp + ((Y / LCD_DEPTH) * LCD_WIDTH + X)) |= (1 << (Y % LCD_DEPTH));
}


inline void pixelOff(const U8 X, const U8 Y) {
   *(disp + ((Y / LCD_DEPTH) * LCD_WIDTH + X)) &= !(1 << (Y % LCD_DEPTH));
}

现在我有一个在 lcd 上画线的类。 有 3 个函数使用不同的像素“设置函数”:show、erease、invert

void NLine::show(bool update) const {
    if(lcd == 0) return;

    if (!NLcd::pixelInLcd(x, y) && !NLcd::pixelInLcd(endX, endY))
        return;
    if ((x == endX) || (y == endY)) {
        straight(&NLcd::pixelOn);
    } else {
        bresenham(&NLcd::pixelOn);
    }
    visible = true;
    if (update) {
        display_update();
    }
}

此时调用私有(private)函数以使用函数指针设置像素。

void NLine::bresenham(void (NLcd::*fpPixelState)(const U8, const U8)) const {
    // predetermine function to avoid ifs during calculation!
    // low level pixel functions use U8!
    S8 ix = x;
    S8 iy = y;
    S8 sx = ix < endX ? 1 : -1;
    S8 sy = iy < endY ? 1 : -1;
    S16 err = width + (-height), e2;

    for (;;) {
        // how to get the compiler to inline this (template?)!
        (lcd->*fpPixelState)(static_cast<U8>(ix), static_cast<U8>(iy));
        if (ix == endX && iy == endY)
            break;
        e2 = 2 * err;
        if (e2 > (-height)) {
            err += (-height);
            ix += sx;
        }
        if (e2 < width) {
            err += width;
            iy += sy;
        }
    }
}

我认为我希望编译器在 for 循环中内联此函数是可以理解的。我试图用模板解决这个问题,但同样的问题我不知道编译器是否使用内联。 我应该使用完全不同的设计还是如何解决这个问题? 下一个问题是,如果我调用 show erase 和 invert,编译器会生成很多代码,因为内联不同,所以我认为我应该使用不同的代码设计?

编辑:

首先,感谢 Dietmar Kühl 的设计建议! 所以这里是结论:

这是测试代码:

NLine line(lcd, 0, 0, 99, 0);
t0 = timer.now();
for(S8 i=0; i<NLcd::LCD_HEIGHT; ++i) {
    // x0, y0, xend, yend
    line.setPosition(NLine::keep, i, NLine::keep, i);
    // call only straight not the bigger bresenham function
    line.show();
    line.erase();
    line.invert();
}
t1 = timer.now();

方法一:使用函数指针(第一种)
内存:26384
保留时间:51 毫秒

方法 2:使用函数对象(Dietmar Kühl)
内存:26592
保留时间:27 毫秒

方法三:使用switch in for循环确定像素操作函数
内存:26416
保留时间:36 毫秒

方法 2:最佳 RT 但事实证明,如果实现,程序会变得非常大 围绕绘图方法变得非常大,尤其是对于布雷森纳姆。发生这种情况是因为模板实现为所有 3 个像素函数生成了完整代码。

方法三:看起来最简单的方法是一个很好的权衡。

欢迎提出进一步的建议。

最佳答案

您不能将函数作为指向另一个函数的指针传递,并期望编译器将其内联,除非代码真的很简单(这样编译器就可以内联函数指针传递给的函数)。编译器(通常)不会知道您不会在其他上下文中(例如,在不同的编译单元中)传递其他函数指针,因此,它仍然必须使用适当的函数指针“工作”。

可能有几种不同的方法可以“解决”这个问题,如果它真的那么重要的话——但是调用一个函数的开销 [通过指针或不通过指针] 可能比你正在做的数学非常小(除以通过 LCD_DEPTH 是昂贵的)。您是否实际进行了测量以确定造成差异的是通话时间?

此外,您的 pixelOff 代码可能有误,我预计:

*(disp + ((Y / LCD_DEPTH) * LCD_WIDTH + X)) &= !(1 << (Y % LCD_DEPTH));

应该是:

*(disp + ((Y / LCD_DEPTH) * LCD_WIDTH + X)) &= ~(1 << (Y % LCD_DEPTH));

因为那会做相反的事情

关于C++内联函数指针和模板函数设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19874929/

相关文章:

c++ - boost::gregorian input_facet 意外结果

c++ - int8_t 和 char : converts between pointers to integer types with different sign - but it doesn't

debugging - 如何可视化 AVR 程序的内存 (SRAM) 使用情况?

c++ - 是否有开源线程安全的 C++ 对象池实现?

c++ - Vector 不会存储正确的数据类型(wchar_t 而不是 uint16_t)

C++ BLOB 平均值

c++ - 我的 for 循环怎么搞砸了?

c - CAN通信协议(protocol)栈的设计

C 编译器无法在 Linux 系统上创建可执行文件

embedded - Arduino 上的中断会中断其他中断吗?