将头文件包含到头文件中而不将其内容暴露给包含者 [英] Including a header file into a header file without exposing it's content to the includer

查看:116
本文介绍了将头文件包含到头文件中而不将其内容暴露给包含者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为C API编写一个c ++包装器。为此,将C-API头部包含在我自己的头文件中是最方便的,但是这也包括头文件到外部系统的文件中,不应该暴露给C-API。



capi.h

  enum MyFlags {
MY_FLAG_A,
MY_FLAG_B ,
};

void FOO_bar(int flags);

cppapi.hh

  #includecapi.h

enum class MyFlags {
A = MY_FLAG_A,
B = MY_FLAG_B
};

namespace foo {
void bar(MyFlags flags){
FOO_bar((int)flags);


这只是将c命名约定转换为c ++语言特征。因此,当使用C ++变体语言时,我希望c_api不能用于我的自动完成,因此不会意外使用。

解决方案

在您的项目命名空间中定义一个命名空间 detail ,并在其中包含C标头。

C头文件没有在从C ++包含时定义en externC - block,执行以下操作之一:


  • 用一个外部的 externC块封装,并验证C头文件(如果包含的话)是有效的和等价的C ++文件。 (特别注意枚举大小和内联函数)。
  • 使用 #ifdef __cplusplus 。使其成为有效且等效的C ++。



  • 因此,所有这些C函数都隐藏在内部实现细节名称空间中,并且不会打扰任何人。

      namespace foo {
    名称空间内部{
    externC{
    #includecapi.h
    }
    }
    //函数别名
    使用bar = internal :: FOO_bar;
    //使用config_t = internal :: FOO_config输入别名
    ;

    $ / code>

    选择性地将您需要的符号放入 FOO -namespace using using



    顺便说一下,您的转发器函数应该是 inline


    I would like to write a c++ wrapper for a C API. for this it is most convenient to just include the C-API header in my own header, but this also includes the header into the file of the outer system, that should not be exposed to the C-API.

    capi.h

    enum MyFlags {
        MY_FLAG_A,
        MY_FLAG_B,
    };
    
    void FOO_bar(int flags);
    

    cppapi.hh

    #include "capi.h"
    
    enum class MyFlags { 
        A = MY_FLAG_A, 
        B = MY_FLAG_B 
    };
    
    namespace foo {
        void bar(MyFlags flags) { 
            FOO_bar((int)flags); 
        }
    }
    

    it is just to translate c naming convention into c++ language features. So when using the C++ variant of the language, I would like that the c_api is not available to my auto completion and therefore not accidently used.

    解决方案

    Define a namespace detail in your projects namespace, and include the C header there.

    If the C header does not define en extern "C"- block when included from C++, do one of these:

    • Wrap with an external extern "C" block and verify the C header is, if included thus, valid and equivalent C++. (Special attention to enum sizes and inline functions).
    • Make it valid and equivalent C++ using #ifdef __cplusplus.

    Thus, all those C functions are hidden in the internal implementation-detail namespace, and won't bother anyone.

    namespace foo {
        namespace internal {
            extern "C" {
                #include "capi.h"
            }
        }
        // Function alias
        using bar = internal::FOO_bar;
        // Type alias
        using config_t = internal::FOO_config;
    }
    

    Selectively get those symbols you need into the FOO-namespace using using.

    BTW: Your forwarder-function should really be inline!

    这篇关于将头文件包含到头文件中而不将其内容暴露给包含者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆