From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- mfbt/EnumTypeTraits.h | 82 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 23 deletions(-) (limited to 'mfbt/EnumTypeTraits.h') diff --git a/mfbt/EnumTypeTraits.h b/mfbt/EnumTypeTraits.h index 528e1db8a7..09ead3d0e9 100644 --- a/mfbt/EnumTypeTraits.h +++ b/mfbt/EnumTypeTraits.h @@ -61,29 +61,6 @@ struct EnumTypeFitsWithin "must provide an integral type"); }; -/* - * Provides information about highest enum member value. - * Each specialization of struct MaxEnumValue should define - * "static constexpr unsigned int value". - * - * example: - * - * enum ExampleEnum - * { - * CAT = 0, - * DOG, - * HAMSTER - * }; - * - * template <> - * struct MaxEnumValue - * { - * static constexpr unsigned int value = static_cast(HAMSTER); - * }; - */ -template -struct MaxEnumValue; // no need to define the primary template - /** * Get the underlying value of an enum, but typesafe. * @@ -108,6 +85,65 @@ inline constexpr auto UnderlyingValue(const T v) { return static_cast::type>(v); } +/* + * Specialize either MaxContiguousEnumValue or MaxEnumValue to provide the + * highest enum member value for an enum class. Note that specializing + * MaxContiguousEnumValue will make MaxEnumValue just take its value from the + * MaxContiguousEnumValue specialization. + * + * Specialize MinContiguousEnumValue and MaxContiguousEnumValue to provide both + * lowest and highest enum member values for an enum class with contiguous + * values. + * + * Each specialization of these structs should define "static constexpr" member + * variable named "value". + * + * example: + * + * enum ExampleEnum + * { + * CAT = 0, + * DOG, + * HAMSTER + * }; + * + * template <> + * struct MaxEnumValue + * { + * static constexpr ExampleEnumvalue = HAMSTER; + * }; + */ + +template +struct MinContiguousEnumValue { + static constexpr T value = static_cast(0); +}; + +template +struct MaxContiguousEnumValue; + +template +struct MaxEnumValue { + static constexpr auto value = MaxContiguousEnumValue::value; +}; + +// Provides the min and max values for a contiguous enum (requires at least +// MaxContiguousEnumValue to be defined). +template +struct ContiguousEnumValues { + static constexpr auto min = MinContiguousEnumValue::value; + static constexpr auto max = MaxContiguousEnumValue::value; +}; + +// Provides the total number of values for a contiguous enum (requires at least +// MaxContiguousEnumValue to be defined). +template +struct ContiguousEnumSize { + static constexpr size_t value = + UnderlyingValue(ContiguousEnumValues::max) + 1 - + UnderlyingValue(ContiguousEnumValues::min); +}; + } // namespace mozilla #endif /* mozilla_EnumTypeTraits_h */ -- cgit v1.2.3