在C 中实现HAL的设计模式
Design pattern for HAL implementation in C++
您是否有关于在C 中实现硬件抽象层的设计模式或技术的建议,以便我可以在构建时间轻松地在平台之间切换?我正在考虑使用我在GOF或C 模板中阅读的桥梁图案之类的东西,但是我不确定这是最好的选择。
我认为在建筑时使用桥梁模式不是一个好选择。
这是我的解决方案:
将标准设备类定义为接口:
class Device {
... // Common functions
};
x86平台:
#ifdef X86 // X86 just is an example, user should find the platform define.
class X86Device: public Device{
... // special code for X86 platform
};
#endif
对于手臂平台:
#ifdef ARM // ARM just is an example, user should find the platform define.
class ARMDevice: public Device {
... // Special code for ARM platform
};
#endif
使用这些设备:
#ifdef X86
Device* dev = new X86Device();
#elif ARM
Device* dev = new ARMDevice();
#endif
编译选项:
$ g++ -DARM ... // using ArmDevice
$ g++ -DX86 ... // using X86Device
有关更多想法,请参阅此问题的答案:跨平台C 代码和单个标头 - 多个实现
当我面临类似问题时,我最终使用了pimpl成语。
相关文章:
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 资源管理设计模式
- 用于在回调中调用解析器的设计模式
- 设计帮助 - 为不同类型的消息处理通用接口的设计模式
- 在这种情况下我应该使用哪种设计模式
- C++中物体改变识别的设计模式?
- 确保所有构造函数调用相同的函数 c++ 设计模式
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 工厂设计模式优化
- 使用C++模板的数据映射器设计模式
- 为什么以及如何使用原型设计模式
- 具有多个继承共享一个资源的对象 - 寻找良好的设计模式
- 在C++中创建观察器设计模式的好方法
- 现代C++在多大程度上消除了对设计模式的需求?
- 对于存储另一个类所需信息的类,例如其构造,是否有设计模式?
- 下面抽象工厂设计模式的实现是正确的吗
- sql记录集函数的状态设计模式
- 是否有可以处理方法调用依赖关系的设计模式?
- 在C 中实现HAL的设计模式