从枚举定义中删除依赖常量

发布于 2024-08-23 06:16:20 字数 436 浏览 13 评论 0原文

我试图通过使用不透明的结构和前向声明来安全地从我的项目中删除依赖项,但像大多数人一样,我仍然坚持我的枚举。

我试图从头文件中删除的头文件依赖项已经定义了我想要将枚举值设置为的常量。像这样的事情,

// depends header
#define DEP_TYPE_ONE   1
#define DEP_TYPE_TWO   2
#define DEP_TYPE_THREE 3

// My header
enum TYPES
{
    T_ONE     = DEP_TYPE_ONE,
    T_TWO     = DEP_TYPE_TWO,
    T_THREE   = DEP_TYPE_THREE
}

我试图找到一种方法,不必在我的标头中包含依赖标头。

答案很可能只是“你不能这样做”,但我只是想问,因为解决方案会让我的生活变得更加轻松。

I am trying to safely remove a dependency from my project by using opaque structures and forward declarations but like most I am still stuck on my enums.

The header file dependency I am trying to remove from my header files has defined constants that I want to set my enumeration's values to. Something like this

// depends header
#define DEP_TYPE_ONE   1
#define DEP_TYPE_TWO   2
#define DEP_TYPE_THREE 3

// My header
enum TYPES
{
    T_ONE     = DEP_TYPE_ONE,
    T_TWO     = DEP_TYPE_TWO,
    T_THREE   = DEP_TYPE_THREE
}

I am trying to figure out a way to not have to include the depends header in my header.

Odds are the answer is probably simply 'you can't do that' but I just want to ask because a solution would make my life infinity easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独木成林 2024-08-30 06:16:20

如何删除依赖标头的包含,对值进行硬编码,并注释依赖关系:

// my_header.h

// NOTE: Enumerands must be in sync with symbols defined in depends.h
enum TYPES
{
    T_ONE     = 1, // DEP_TYPE_ONE
    T_TWO     = 2, // DEP_TYPE_TWO
    T_THREE   = 3  // DEP_TYPE_THREE
};

为了减轻对值不同步的担忧,您可以拥有另一个标头或源文件(您的类或 API 的用户不需要的标头或源文件) t get) 包含一个或多个编译时断言:

// Some non-distributed file

#include <depends.h>
#include "my_header.h"

// Compile-time assertion macro
#define compile_time_assert(cond, msg) \
    typedef char msg[(cond) ? 1 : -1]

// Check assumptions at compile time...
compile_time_assert(T_ONE==DEP_TYPE_ONE, ValueOutOfSync1);
compile_time_assert(T_TWO==DEP_TYPE_TWO, ValueOutOfSync2);
    .
    .
    .

如果值不同步,这会给您带来编译时错误。

有关compile_time_assert 宏的详细信息,请参阅:http://www.embedded。 com/columns/programmingpointers/164900888?_requestid=379501

How about removing the include of the depends header, hard code the values, and comment the dependency:

// my_header.h

// NOTE: Enumerands must be in sync with symbols defined in depends.h
enum TYPES
{
    T_ONE     = 1, // DEP_TYPE_ONE
    T_TWO     = 2, // DEP_TYPE_TWO
    T_THREE   = 3  // DEP_TYPE_THREE
};

To allay fears about the values getting out of sync, you can have another header or source file (one that users of your class or API don't get) that contains one or more compile-time asserts:

// Some non-distributed file

#include <depends.h>
#include "my_header.h"

// Compile-time assertion macro
#define compile_time_assert(cond, msg) \
    typedef char msg[(cond) ? 1 : -1]

// Check assumptions at compile time...
compile_time_assert(T_ONE==DEP_TYPE_ONE, ValueOutOfSync1);
compile_time_assert(T_TWO==DEP_TYPE_TWO, ValueOutOfSync2);
    .
    .
    .

This would give you a compile time error if the values ever get out of sync.

For more info on the compile_time_assert macro, see: http://www.embedded.com/columns/programmingpointers/164900888?_requestid=379501

简单气质女生网名 2024-08-30 06:16:20

这不是您想要的,但这是不在标头中包含 dependent 标头的唯一方法:

enum TYPES 
{ 
    T_ONE     = 1, 
    T_TWO     = 2, 
    T_THREE   = 3 
} 

It is not what you want, but it is the only way not to include the depends header in your header:

enum TYPES 
{ 
    T_ONE     = 1, 
    T_TWO     = 2, 
    T_THREE   = 3 
} 
偏闹i 2024-08-30 06:16:20

这不是一个完美的答案,但是您是否考虑过匿名枚举?我曾经在使用图表库时遇到过类似的问题,该图表库在标头中定义了很多常量,并且具有很多内部依赖性。这影响了我们的编译时间。所以,我只是将整个 #define's 模拟到标头中匿名命名空间中的匿名枚举中!类似于:-

namespace {
  enum {
    DEP_TYPE_ONE = 1,
    DEP_TYPE_TWO,
    // ....
    DEP_TYPE_LAST
  };
}

使用这种方法,您将不必重构大量直接使用这些命名常量的代码。但是,一旦标头定义了新常量,就更新枚举是一个维护噩梦

我想在你的情况下值得一试。

哈特哈,

Not a perfect answer, but have you considered anonymous enums? I had a similar problem once when using a charting library that defined a lot of constants in a header with a lot of internal dependency. This was affecting our compile-time. So, i just MOCked the entire #define's into an anonymous enumeration in an anonymous namespace in a header! Something like :-

namespace {
  enum {
    DEP_TYPE_ONE = 1,
    DEP_TYPE_TWO,
    // ....
    DEP_TYPE_LAST
  };
}

With this approach you will not have to refactor a lot of code that directly uses those named constants. But it is a maintainance nightmare to update the enum as soon as the header defines new constants.

Well worth a try in your case i guess.

HTH,

故事未完 2024-08-30 06:16:20

你是对的,你不能。您在这里处理的是预处理器而不是编译器(与前向声明一样)。

如果您的编译器支持“强制包含”,则可能会“伪造” 选项,但依赖关系仍然存在,并且您仍然需要构建该文件。

You are right, you can't. You are dealing with the pre-processor here not the compiler (as with the forward declarations).

It may be possible to 'fake it' if your compiler supports an "forced include" option, but the dependency still remains, and you still need the file to build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文