diff --git a/src/Type.cc b/src/Type.cc index f9bb36db01..75f414eeda 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -63,68 +63,9 @@ const char* type_name(TypeTag t) BroType::BroType(TypeTag t, bool arg_base_type) { tag = t; - is_network_order = 0; + is_network_order = ::is_network_order(t); base_type = arg_base_type; - - switch ( tag ) { - case TYPE_VOID: - internal_tag = TYPE_INTERNAL_VOID; - break; - - case TYPE_BOOL: - case TYPE_INT: - case TYPE_ENUM: - internal_tag = TYPE_INTERNAL_INT; - break; - - case TYPE_COUNT: - case TYPE_COUNTER: - internal_tag = TYPE_INTERNAL_UNSIGNED; - break; - - case TYPE_PORT: - internal_tag = TYPE_INTERNAL_UNSIGNED; - is_network_order = 1; - break; - - case TYPE_DOUBLE: - case TYPE_TIME: - case TYPE_INTERVAL: - internal_tag = TYPE_INTERNAL_DOUBLE; - break; - - case TYPE_STRING: - internal_tag = TYPE_INTERNAL_STRING; - break; - - case TYPE_ADDR: - internal_tag = TYPE_INTERNAL_ADDR; - break; - - case TYPE_SUBNET: - internal_tag = TYPE_INTERNAL_SUBNET; - break; - - case TYPE_PATTERN: - case TYPE_TIMER: - case TYPE_ANY: - case TYPE_TABLE: - case TYPE_UNION: - case TYPE_RECORD: - case TYPE_LIST: - case TYPE_FUNC: - case TYPE_FILE: - case TYPE_OPAQUE: - case TYPE_VECTOR: - case TYPE_TYPE: - internal_tag = TYPE_INTERNAL_OTHER; - break; - - case TYPE_ERROR: - internal_tag = TYPE_INTERNAL_ERROR; - break; - } - + internal_tag = to_internal_type_tag(tag); } BroType* BroType::ShallowClone() diff --git a/src/Type.h b/src/Type.h index 15f17d6ecf..e46c050395 100644 --- a/src/Type.h +++ b/src/Type.h @@ -44,6 +44,11 @@ typedef enum { #define NUM_TYPES (int(TYPE_ERROR) + 1) } TypeTag; +constexpr bool is_network_order(TypeTag tag) noexcept + { + return tag == TYPE_PORT; + } + typedef enum { FUNC_FLAVOR_FUNCTION, FUNC_FLAVOR_EVENT, @@ -57,6 +62,60 @@ typedef enum { TYPE_INTERNAL_OTHER, TYPE_INTERNAL_ERROR } InternalTypeTag; +constexpr InternalTypeTag to_internal_type_tag(TypeTag tag) noexcept + { + switch ( tag ) { + case TYPE_VOID: + return TYPE_INTERNAL_VOID; + + case TYPE_BOOL: + case TYPE_INT: + case TYPE_ENUM: + return TYPE_INTERNAL_INT; + + case TYPE_COUNT: + case TYPE_COUNTER: + return TYPE_INTERNAL_UNSIGNED; + + case TYPE_PORT: + return TYPE_INTERNAL_UNSIGNED; + + case TYPE_DOUBLE: + case TYPE_TIME: + case TYPE_INTERVAL: + return TYPE_INTERNAL_DOUBLE; + + case TYPE_STRING: + return TYPE_INTERNAL_STRING; + + case TYPE_ADDR: + return TYPE_INTERNAL_ADDR; + + case TYPE_SUBNET: + return TYPE_INTERNAL_SUBNET; + + case TYPE_PATTERN: + case TYPE_TIMER: + case TYPE_ANY: + case TYPE_TABLE: + case TYPE_UNION: + case TYPE_RECORD: + case TYPE_LIST: + case TYPE_FUNC: + case TYPE_FILE: + case TYPE_OPAQUE: + case TYPE_VECTOR: + case TYPE_TYPE: + return TYPE_INTERNAL_OTHER; + + case TYPE_ERROR: + return TYPE_INTERNAL_ERROR; + } + + /* this should be unreachable */ + return TYPE_INTERNAL_VOID; + } + // Returns the name of the type. extern const char* type_name(TypeTag t);