使用强类型枚举对类型和子类型进行建模
Modeling types and subtypes with strongly typed enums?
我想将ICMP标头定义为pod类型:
struct ICMPHeader
{
uint8_t Type; // ICMP type
uint8_t Code; // Subtype, value is dependent on ICMP type.
uint16_t Checksum; // Error checking data. See RFC 1071
uint32_t RestOfHeader; // Varies based on ICMP type and code.
};
对于ICMPType
字段,我可以使用强类型枚举来使其更好一点:
enum class ICMPType : uint8_t
{
EchoReply = 0,
Reserved1 = 1,
Reserved2 = 2,
DestinationUnreachable = 3,
SourceQuench = 4
// etc...
};
struct ICMPHeader
{
ICMPType Type; // ICMP type
uint8_t Code; // Subtype, value is dependent on ICMP type.
uint16_t Checksum; // Error checking data. See RFC 1071
uint32_t RestOfHeader; // Varies based on ICMP type and code.
};
现在,我自然也想将Code
字段指定为枚举。如果我可以使用模板专用化语法,那就太好了,但快速测试显示它不起作用:
// Compiler error
template<ICMPType>
enum class ICMPCode;
template<>
enum class ICMPCode<ICMPType::DestinationUnreachable>
{
DestinationNetworkUnreachable = 0,
DestinationHostUnreachable = 1,
DestinationProtocolUnreachable = 2
};
一种选择是将它们包装在结构中:
// Meaning of ICMP code is dependent on ICMP type.
template<ICMPType>
struct ICMPCode;
// Subcodes for DestinationUnreachable
template<> struct ICMPCode<ICMPType::DestinationUnreachable>
{
enum class Code : uint8_t
{
DestinationNetworkUnreachable = 0,
DestinationHostUnreachable = 1,
DestinationProtocolUnreachable = 2
// etc...
};
};
// Access: ICMPCode<ICMPType::DestinationUnreachable>::Code::DestinationHostUnreachable
但是这样做让我觉得我只是在胡闹,把事情弄得太复杂了。
我想这是一个更普遍的问题的具体例子:如何设置类型和子类型的系统?有什么建议吗?
附言:
- 另请参阅:http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#Header
示例代码:
#include <iostream>
// Trying to model ICMP types and codes with strongly typed enums
// See also http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#Header
enum class ICMPType : uint8_t
{
EchoReply = 0,
Reserved1 = 1,
Reserved2 = 2,
DestinationUnreachable = 3,
SourceQuench = 4
// etc...
};
// Meaning of ICMP code is dependent on ICMP type.
template<ICMPType>
struct ICMPCode;
// Subcodes for DestinationUnreachable
template<> struct ICMPCode<ICMPType::DestinationUnreachable>
{
enum class Code : uint8_t
{
DestinationNetworkUnreachable = 0,
DestinationHostUnreachable = 1,
DestinationProtocolUnreachable = 2
// etc...
};
};
ICMPCode<ICMPType::DestinationUnreachable>::Code GetReasonWhyDestinationIsUnreachable()
{
return ICMPCode<ICMPType::DestinationUnreachable>::Code::DestinationHostUnreachable;
}
int main()
{
std::cout << static_cast<int>(GetReasonWhyDestinationIsUnreachable()) << std::endl;
}
我认为你不能在编译时静态地执行此操作,因为你在运行时更改ICMPType
。
我建议:
- 为要表示的每个
code
范围创建一个枚举。 - 为每个类型创建一个容器(即特定于每个类型的多个
ICMPHeader
类型,并在那里丢失类型变量)。 - 创建一个工厂,该工厂采用原始 ICMP 标头,并使用适当的
ICMPType
枚举生成专用类型之一。
这应该是一种非常灵活的方法,但简单地根据类型转换代码的值可能就足够了,而且更容易处理。
在你的例子中,你只是投射回一个int,虽然它剥离了你刚刚开始实现的一切。
编辑 - 如果你所有的容器都继承自一个公共基类,你可以给出一个通用的GetDescription()方法,然后子级可以填充该方法。像这样抽象出细节可以制作出一个漂亮干净的设计......
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 如何查询类型是否建模结构
- 使用强类型枚举对类型和子类型进行建模
- 在c++中建模变量成员类型