summaryrefslogtreecommitdiffstats
path: root/mfbt/EnumTypeTraits.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
commitfbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 (patch)
tree4c1ccaf5486d4f2009f9a338a98a83e886e29c97 /mfbt/EnumTypeTraits.h
parentReleasing progress-linux version 124.0.1-1~progress7.99u1. (diff)
downloadfirefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.tar.xz
firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.zip
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mfbt/EnumTypeTraits.h')
-rw-r--r--mfbt/EnumTypeTraits.h82
1 files changed, 59 insertions, 23 deletions
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<ExampleEnum>
- * {
- * static constexpr unsigned int value = static_cast<unsigned int>(HAMSTER);
- * };
- */
-template <typename T>
-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<typename std::underlying_type<T>::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<ExampleEnum>
+ * {
+ * static constexpr ExampleEnumvalue = HAMSTER;
+ * };
+ */
+
+template <typename T>
+struct MinContiguousEnumValue {
+ static constexpr T value = static_cast<T>(0);
+};
+
+template <typename T>
+struct MaxContiguousEnumValue;
+
+template <typename T>
+struct MaxEnumValue {
+ static constexpr auto value = MaxContiguousEnumValue<T>::value;
+};
+
+// Provides the min and max values for a contiguous enum (requires at least
+// MaxContiguousEnumValue to be defined).
+template <typename T>
+struct ContiguousEnumValues {
+ static constexpr auto min = MinContiguousEnumValue<T>::value;
+ static constexpr auto max = MaxContiguousEnumValue<T>::value;
+};
+
+// Provides the total number of values for a contiguous enum (requires at least
+// MaxContiguousEnumValue to be defined).
+template <typename T>
+struct ContiguousEnumSize {
+ static constexpr size_t value =
+ UnderlyingValue(ContiguousEnumValues<T>::max) + 1 -
+ UnderlyingValue(ContiguousEnumValues<T>::min);
+};
+
} // namespace mozilla
#endif /* mozilla_EnumTypeTraits_h */