我想将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()方法,然后子级可以填充该方法。像这样抽象出细节可以制作出一个漂亮干净的设计......