我试图在C中包装一个C库,使其成为一个现代的,高水平的,惯用的C库.我想要做的是使C对象完全不透明和/或直接从C代码中不可用,并用更高级别的替代品包装/替换它们.
我面临的问题很简单:我想只将C头包含在C源代码中,这样包含的C头也不会包含C头的声明,也就是说,它不会污染C头.全局命名空间
但看起来头文件和源文件的正确分离似乎不允许我这样做.这是我的问题的一个非常模糊的版本,评论将告诉你其余的:
my_header.h:
- typedef enum
- {
- my_Consts_ALPHA = /* some special value */,my_Consts_BETA = /* other special value */,} my_Consts;
- typedef struct
- {
- // members...
- } my_Type;
- void
- my_Type_method(my_Type *const,my_Enum);
my_header.hpp:
- namespace my
- {
- enum class Consts; // <-- This header is missing the constant values of
- // this enum,because its values are defined by
- // the C header :(
- class Type : public my_Type // <-- The super struct is coming from the
- // C header,but I don't want to include
- // that header here :(
- {
- public:
- void
- method(Consts constant);
- };
- }
my_source.cpp:
- extern "C"
- {
- #include "my_header.h"
- }
- #include "my_header.hpp"
- namespace my
- {
- enum class Consts
- {
- ALPHA = my_Consts_ALPHA,BETA = my_Consts_BETA,};
- void
- Type::method(Consts constant)
- {
- my_Type_method(static_cast<my_Type *const>(this),static_cast<my_Consts>(constant));
- }
- }
所以我的问题是:我错过了一些非常明显的东西吗?这有可能实现吗?有没有我不知道的伎俩?
解决方法
在讽刺性地提出的问题
@AnalPhabet的评论中,应该在命名空间内使用#include的C头.
@n.m.证实,它实际上是一个有效的解决方案,现在我在自己的设置上进行了测试,幸运的是它工作得很好.
(虽然我不知道,如果这是特定于实现或不是,但我测试了g和clang并且它正在工作.)
它没有解决不透明性问题,但至少它直接访问原始C数据有点困难,因为它现在生活在一个单独的命名空间中,因此用户不能偶然访问,而是心甘情愿.
所以,my_header.hpp应如下所示:
- namespace my
- {
- extern "C"
- {
- #include "my_header.h"
- }
- enum class Consts
- {
- ALPHA = my_Consts_ALPHA,};
- class Type : public my_Type
- {
- public:
- void
- method(Consts constant);
- };
- }
因此,无论my_header.hpp在哪里#include,用户只能访问C值,如下所示:
- my::my_Consts_ALPHA // The wrapped value is => my::Consts::ALPHA
- my::my_Type // The wrapped value is => my::Type
- my::my_Type_method(t,..) // The wrapped value is => t.method(..)