Bug 987305 - Add BitFlagsEnumSerializer - r=nfroyd
authorBenoit Jacob <bjacob@mozilla.com>
Fri, 25 Apr 2014 22:34:04 -0400
changeset 180720 8d424037ce207b3c6a8d379de7c05ed134fd10f5
parent 180719 4ad1f662f521eba6514463d13aee536dd17c45ca
child 180721 1dfd9a457f340016c9af45501a56578d6ac8d9ca
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersnfroyd
bugs987305
milestone31.0a1
Bug 987305 - Add BitFlagsEnumSerializer - r=nfroyd
ipc/glue/IPCMessageUtils.h
--- a/ipc/glue/IPCMessageUtils.h
+++ b/ipc/glue/IPCMessageUtils.h
@@ -150,16 +150,36 @@ public:
   static bool IsLegalValue(E e)
   {
     typedef MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(E) ActualEnumType;
     return IsLessThanOrEqual(MinLegal, ActualEnumType(e)) &&
            ActualEnumType(e) < HighBound;
   }
 };
 
+template <typename E,
+          E AllBits>
+struct BitFlagsEnumValidator
+{
+  static bool IsLegalValue(E e)
+  {
+    return (e & AllBits) == e;
+  }
+};
+
+template <typename E,
+          MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(E) AllBits>
+struct BitFlagsTypedEnumValidator
+{
+  static bool IsLegalValue(E e)
+  {
+    return (e & AllBits) == e;
+  }
+};
+
 /**
  * Specialization of EnumSerializer for enums with contiguous enum values.
  *
  * Provide two values: MinLegal, HighBound. An enum value x will be
  * considered legal if MinLegal <= x < HighBound.
  *
  * For example, following is definition of serializer for enum type FOO.
  * \code
@@ -187,16 +207,55 @@ struct ContiguousEnumSerializer
 template <typename E,
           MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(E) MinLegal,
           MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(E) HighBound>
 struct ContiguousTypedEnumSerializer
   : EnumSerializer<E,
                    ContiguousTypedEnumValidator<E, MinLegal, HighBound>>
 {};
 
+/**
+ * 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 {
+ *   FOO_FIRST =  1 << 0,
+ *   FOO_SECOND = 1 << 1,
+ *   FOO_LAST =   1 << 2,
+ *   ALL_BITS =   (1 << 3) - 1
+ * };
+ *
+ * template <>
+ * struct ParamTraits<FOO>:
+ *     public BitFlagsEnumSerializer<FOO, FOO::ALL_BITS> {};
+ * \endcode
+ */
+template <typename E,
+          E AllBits>
+struct BitFlagsEnumSerializer
+  : EnumSerializer<E,
+                   BitFlagsEnumValidator<E, AllBits>>
+{};
+
+/**
+ * Similar to BitFlagsEnumSerializer, but for MFBT typed enums
+ * as constructed by MOZ_BEGIN_ENUM_CLASS. This can go away when
+ * we drop MOZ_BEGIN_ENUM_CLASS and use C++11 enum classes directly.
+ */
+template <typename E,
+          MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(E) AllBits>
+struct BitFlagsTypedEnumSerializer
+  : EnumSerializer<E,
+                   BitFlagsTypedEnumValidator<E, AllBits>>
+{};
+
 template <>
 struct ParamTraits<base::ChildPrivileges>
   : public ContiguousEnumSerializer<base::ChildPrivileges,
                                     base::PRIVILEGES_DEFAULT,
                                     base::PRIVILEGES_LAST>
 { };
 
 template<>