替代C 中的内类
Alternative to Inner Classes in C++
本文关键字:替代 更新时间:2023-10-16
假设我正在为BCM2835 rpi芯片编写一个"设备树斑点",但在C 文件中,而不是.dts
文件。目的是练习C 和OS概念。
我希望能够不仅封装登记地址,而且可以访问访问这些地址的函数,并仅将顶级用作用作API函数。
在C++
中,这可能是内部类
//bcm2835.h
class BMC2835 : public ARMCpu
{
public:
void ACKLedOn(void);
void ACKLdOff(void);
void ACKLedBlink(void);
// I2C write to device (this would be called by the device driver)
// It would ensure that I2C is setup, etc, etc
void I2C_Device_Write(I2C_Device* device, uint8_t* buffer);
private:
// Physical addresses for various peripheral register sets
/// Base Physical Address of the BCM 2835 peripheral registers
const uint32_t BCM2835_PERI_BASE = 0x20000000;
class GPIO()
{
private:
/// Base Physical Address of the Pads registers
const uint32_t BCM2835_GPIO_PADS = (BCM2835_PERI_BASE + 0x100000)
/// Sets the Function Select register for the given pin, which configures
/// the pin as Input, Output or one of the 6 alternate functions.
void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode);
}
class I2C()
{
private:
const uint32_t BCM2835_CORE_CLK_HZ = 250000000 ;///< 250 MHz
// Register masks for BSC_C
const uint32_t BCM2835_BSC_C_I2CEN = 0x00008000;///< I2C Enable, 0 = disabled, 1 = enabled
const uint32_t BCM2835_BSC_C_INTR = 0x00000400;///< Interrupt on RX
const uint32_t BCM2835_BSC_C_INTT = 0x00000200;///< Interrupt on TX
void bcm2835_i2c_begin(void);
void bcm2835_i2c_write(uint8_t address, uint8* pbuffer);
}
}
,然后我也可以为BCM2837提供一个类,该类别为64位,例如对LED的处理非常不同。
//bcm2837.h
class BCM2837 : public ARMCpu
{
public:
// LED is now a very different Implementation with Mailbox
// but exposed to Kernel as API
void ACKLedOn(void);
void ACKLdOff(void);
void ACKLedBlink(void);
...
...
}
我敢肯定,这种方法有很多问题。似乎最困扰我的是单个类的长度,包括 SPI
, UART
等,等等。
即使ARMCpu
良好地态和100%虚拟(我宁愿在嵌入式中避免),每个CPU类仍将相当冗长且难以阅读和维护。
有没有办法在C 中实现此类私人级别访问?
将每个芯片放在自己的.cpp文件中,并在该文件中声明所有这些私人的内部内容(而不是标题中)。您可以将它们包裹在匿名名称空间中,以防止它们暴露于链接器。
相关文章:
- 我应该使用什么来代替void作为变体中的替代类型之一
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- C++11 中不同类型的对象的 std::array 的替代方案
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- C++:当所有条目都保证是唯一时,替代 std::map
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 在文件中写入而不是在 c++ 中使用 "<<" 的替代方法?
- Memcpy C++替代方案
- EOF有更安全的替代方案吗?它在我的情况下不起作用
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- Visual Studio 中的 sqrtf 函数替代
- 使用类型id运算符的最佳替代方法
- 是否可以将不可复制的成员用作使对象不可复制的替代方法?
- 列表视图项的替代颜色
- 关于如何在具有单个参数的变体构造中选择替代方案?
- Winapi-WM_CLOSE的替代方案
- malloc_trim(0) 的替代项
- 在 C++11 中字符串化变量名称的替代方法