传递指向结构的引用作为模板参数
passing reference pointing to struct as template parameter
我想有效地将带有寄存器的结构映射到内存。
实际上我有这样的工作代码:
具有外设寄存器的结构:
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
};
对于another(与periph
不同)外设你应该实现another类型(例如,i2c<>
).可能以与 periph<>
相同的方式实现(使用封装地址作为模板参数)。但是如果你处理多个相同类型的外围设备(例如,多个 CAN 总线)你 should/might 使用相同类型但 Base
s.
更新:
另外,您可能想看看这个实现:
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;
我想有效地将带有寄存器的结构映射到内存。 实际上我有这样的工作代码:
具有外设寄存器的结构:
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
};
对于another(与periph
不同)外设你应该实现another类型(例如,i2c<>
).可能以与 periph<>
相同的方式实现(使用封装地址作为模板参数)。但是如果你处理多个相同类型的外围设备(例如,多个 CAN 总线)你 should/might 使用相同类型但 Base
s.
更新:
另外,您可能想看看这个实现:
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;