Bug 1377020 - Add a new enum serializer ContiguousEnumSerializerInclusive. r=kats
This allows specifying the highest legal value rather than a sentinel
value, to support enumerations that don't have a sentinel.
MozReview-Commit-ID: 5Vj7SnYlfkH
--- a/ipc/glue/IPCMessageUtils.h
+++ b/ipc/glue/IPCMessageUtils.h
@@ -158,16 +158,34 @@ class ContiguousEnumValidator
public:
static bool IsLegalValue(E e)
{
return IsLessThanOrEqual(MinLegal, e) && e < HighBound;
}
};
template <typename E,
+ E MinLegal,
+ E MaxLegal>
+class ContiguousEnumValidatorInclusive
+{
+ // Silence overzealous -Wtype-limits bug in GCC fixed in GCC 4.8:
+ // "comparison of unsigned expression >= 0 is always true"
+ // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11856
+ template <typename T>
+ static bool IsLessThanOrEqual(T a, T b) { return a <= b; }
+
+public:
+ static bool IsLegalValue(E e)
+ {
+ return IsLessThanOrEqual(MinLegal, e) && e <= MaxLegal;
+ }
+};
+
+template <typename E,
E AllBits>
struct BitFlagsEnumValidator
{
static bool IsLegalValue(E e)
{
return (e & AllBits) == e;
}
};
@@ -192,16 +210,30 @@ template <typename E,
E MinLegal,
E HighBound>
struct ContiguousEnumSerializer
: EnumSerializer<E,
ContiguousEnumValidator<E, MinLegal, HighBound>>
{};
/**
+ * This is similar to ContiguousEnumSerializer, but the last template
+ * parameter is expected to be the highest legal value, rather than a
+ * sentinel value. This is intended to support enumerations that don't
+ * have sentinel values.
+ */
+template <typename E,
+ E MinLegal,
+ E MaxLegal>
+struct ContiguousEnumSerializerInclusive
+ : EnumSerializer<E,
+ ContiguousEnumValidatorInclusive<E, MinLegal, MaxLegal>>
+{};
+
+/**
* Specialization of EnumSerializer for enums representing bit flags.
*
* Provide one value: AllBits. An enum value x will be
* considered legal if (x & AllBits) == x;
*
* Example:
* \code
* enum FOO {