如何定义可扩展的C ++枚举系统 [英] how to define an extensible C++ enum system

查看:209
本文介绍了如何定义可扩展的C ++枚举系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中遇到了一个枚举问题。
在EventDef.h中,

 枚举EventDef {
EVT1 = 0,
EVT2,
EVT3,
EVT_NUM,
}

我可以通过

  #includeEventDef.h
enum UIEventDef将EventDef系统扩展到另一个标题UIEventDef.h {
UIEVT1 = EVT_NUM,
UIEVT2,
UIEVT3,
}

但是,有一个限制,我不能这样做在NetEvent.h以同样的方式。

 包括EventDef.h
枚举NetEventDef {
NETEVT1 = EVT_NUM,
NETEVT2,//错误:这将具有与UIEVT2
相同的值NETEVT3,
}

C ++中是否有更好的编译时解决方案,例如可以帮助的模板?

解决方案

可扩展枚举的想法并不是固有的坏设计。在其他语言中有一个历史,即使c ++不直接支持它们。有不同的可扩展性。



可扩展枚举的内容对于




  • 错误代码

  • 消息类型

  • 设备标识(OID是类似系统的分级枚举)



枚举可扩展性的示例




  • 目标Modula Two有一个类可扩展的枚举,继承。

  • Java中的可扩展枚举模式,可以在c ++中实现。

  • Java枚举是可扩展的,因为额外的数据和方法可以是枚举的一部分。 >
  • 在c ++中,typeid运算符本质上是一个带有附加值的编译器生成的枚举。



您在示例代码中显示的可扩展性没有在无人值守的c ++中的优雅实现。事实上,正如你所指出的,它很容易导致问题。



想想你想要使用可扩展的枚举。也许一个不可变的单例对象的集合/映射将满足您的需要。



在c ++中具有可扩展枚举的另一种方法是使用代码生成器。每个想要添加到可扩展枚举的编译单元,都将它们记录在自己的单独的.enum文件中。在编译时,在编译之前,脚本(即p​​erl,bash,...)查找所有的.enum文件,读取它们,为每个id分配数值,并写出一个头文件,其中包含像其他任何一个。


I have encounter a problem in my project on enums. In EventDef.h,

enum EventDef {
    EVT1 = 0,
    EVT2,
    EVT3,
    EVT_NUM,
}

In this way, I can extend the EventDef system in another header UIEventDef.h by

#include "EventDef.h"
enum UIEventDef {
    UIEVT1 = EVT_NUM,
    UIEVT2,
    UIEVT3,
}

But, there is a limitation that i can not do this in NetEvent.h the same way.

#include "EventDef.h"
enum NetEventDef {
    NETEVT1 = EVT_NUM,
    NETEVT2,   //wrong: this will have the same value as UIEVT2
    NETEVT3,  
}

Is there a better compile time solution in C++ such as templates that can help ?

解决方案

The idea of extensible enums is not inherently "bad design". In other languages there is a history of them, even if c++ does not support them directly. There are different kinds of extensibility.

Things that extensible enums would be useful for

  • error codes
  • message types
  • device identification (OIDs are a hierarchical enum like system)

Examples of enum extensibility

  • Objective Modula Two has enums that are extensible with a class like inheritance.
  • The Extensible Enum Pattern in Java, which can be implemented in c++.
  • Java enums are extensible in that extra data and methods can be a part of an enum.
  • In c++, the typeid operator is essentially a compiler generated enum with attached values.

The kind of extensibility you showed in your sample code does not have an elegant implementation in unaided c++. In fact, as you pointed out, it easily leads to problems.

Think about how you are wanting to use an extensible enum. Perhaps a set/map of immutable singleton objects will meet your needs.

Another way to have extensible enums in c++ is to use a code generator. Every compilation unit that wants to add to an extensible enum, records the ids in its own, separate, .enum file. At build time, before compilation, a script (ie perl, bash, ...) looks for all .enum files, reads them, assigns numeric values to each id, and writes out a header file, which is included like any other.

这篇关于如何定义可扩展的C ++枚举系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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