c++ - 传递指向结构的引用作为模板参数

标签 c++ templates c++11 embedded

我想有效地将​​带有寄存器的结构映射到内存。 实际上我有这样的工作代码:

具有外设寄存器的结构:

struct Periph {
    volatile uint32_t REG1;
    volatile uint32_t REG2;
};

在设备中这个外设两次位于内存中的两个不同地址,所以定义这些地址:

static constexpr size_t PERIPH1_BASE = 0x40000000;
static constexpr size_t PERIPH2_BASE = 0x40001000;

然后我有一个驱动程序可以使用这些寄存器中的任何一个:

template<size_t Base> struct Driver {
    inline Periph &r() {
        return *reinterpret_cast<Periph *>(base);
    }
    void setFoo(uint32_t x) {
        r().REG1 = x;
    }
    uint32_t getBar() {
        return r().REG2;
    }
};

使用这个驱动很简单,只需要将某个外设的地址设置到模板中即可:

Driver<PERIPH1_BASE> drv;
uint32_t x = drv.getBar();
drv.setFoo(x);
...

如果编译器在优化后合并所有内联函数,那么此方法可以非常有效地使用寄存器并且没有任何开销。

但这不是很安全,因为我可以设置Driver来自不同外设的任何地址。

我的改进想法是将对结构的引用作为模板参数,但没有成功。

首先我定义了对寄存器的引用:

static Periph &PERIPH1 = *reinterpret_cast<Periph *>(PERIPH1_BASE);
static Periph &PERIPH2 = *reinterpret_cast<Periph *>(PERIPH2_BASE);

这是有效的,我可以像这样直接访问这些寄存器:

PERIPH2.REG1 = 123;

但我不知道如何将这些引用传递给模板参数,我的尝试如下:

template<Periph &r> struct Driver {
    void setFoo(uint32_t x) {
        r.REG1 = x;
    }
    uint32_t getBar() {
        return r.REG2;
    }
};

Driver<PERIPH2> drv;
drv.setFoo(x);

由此我得到以下错误:

`error: the value of 'PERIPH2' is not usable in a constant expression`

如果我将 PERIPH2 定义为 constexpr,则会出现另一个错误:

`error: reinterpret_cast from integer to pointer`

... 那么如何将对对象的引用作为模板参数呢? 或使它变得更好的想法或建议。

这里还存在许多其他解决方案(例如将引用放入 Driver 构造函数...),但这会减慢对寄存器的访问速度。

感谢您的帮助。

最佳答案

But I have no idea how to pass these references to template argument

因为它们是不同的东西。据我了解,这可能对您有所帮助:将您的外围设备结构用作具有封装基地址的单例。

template<std::size_t Base>
struct periph {

    static constexpr periph volatile& instance() {
        return *reinterpret_cast<periph volatile*>(Base);
    }

    template<std::size_t N>
    static constexpr std::uint32_t volatile& reg() {
        return periph::instance().reg_[N];
    }

    // prohibit instance constructing
    periph() = delete; 
    periph(periph const&) = delete;
    periph(periph&&) = delete;

private:

    uint32_t reg_[2];
};

您可以通过 instance() 访问寄存器或 reg<>()方法。例如:

periph<0x00001000>::reg<0>() = 0;

现在,让你的 Driver的模板参数采用类型而不是值:

template<typename Periph>
struct driver {
    using periph_type = Periph;

    // for example
    void foo() {
        periph_type::reg<0>() = 1234;
    }
};

看,Periph::instance()是你想传递的引用。如您所见,driver::foo()使用 Periph -定义的静态方法 Periph::reg<>()访问外围实例而不是显式地址。看起来更安全。

您也可以放弃默认模板实现并实现特化:

using periph1 = periph<0x40000000>;
using periph2 = periph<0x40001000>;

template<typename Periph>
struct driver;

template<>
struct driver<periph1> {
    // specialization for periph1 only
};

template<>
struct driver<periph2> {
    // specialization for periph2 only
};

或者

template<std::size_t Base>
struct driver< periph<Base> > {
    // use periph<Base> here
};

对于另一个(与periph 不同)外围设备,您应该实现另一个 类型(例如i2c<>)。可能,与 periph<> 相同的方式实现(封装地址作为模板参数)。但是,如果您处理多个相同类型的外围设备(例如,多个 CAN 总线),您应该/可能使用具有不同类型的相同类型 Base

更新:

另外,您可能想看看这个实现:

template<std::size_t Base>
struct periph {
private:
    struct context {
        std::uint32_t reg[2];
    };

    static constexpr context volatile& ctx() {
        return *reinterpret_cast<context volatile*>(Base);
    }

public:

    static volatile std::uint32_t & REG1;
    static volatile std::uint32_t & REG2;
};

template<std::size_t Base>
volatile std::uint32_t & periph<Base>::REG1 = ctx().reg[0];

template<std::size_t Base>
volatile std::uint32_t & periph<Base>::REG2 = ctx().reg[1];

在这种情况下,地址 ( Base ) 仍然被封装到 struct periph 中但寄存器可能作为静态成员访问:

periph<0> p;
p.REG1 = 1;

periph<0>::REG1 = 0;

关于c++ - 传递指向结构的引用作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32790106/

相关文章:

c++ - 在 C++1y 中从模板化基派生的类型上对 unique_ptr 使用 std::move

c++ - 构建 vector 的优先级队列

c++ - 模板特化取决于类型

c++ - 如何在 C++ 中的 Map 中存储 cv::Scalar 对象

c++ - 为什么声明 `void(*pf)(int) = bar;` 会触发下面代码片段中的 `static_assert`?

c++ - C++ 17 —将成员变量的类型映射到std::optionals

c++ - 在可变参数模板中使用 std::placeholders

c++ - C++技术建议中多线程环境中暂停和恢复一个线程从另一个线程

c++ - 我应该对引用使用 __restrict 吗?

c++ - 如何使用 OpenCV 计算 2 帧之间的旋转平移矩阵