Bug 1290023 - Allow enum class values to be passed to other nsCSSProps methods and stored in nsCSSValue/StyleAnimationValue. r=xidorn
authorCameron McCormack <cam@mcc.id.au>
Fri, 29 Jul 2016 16:18:48 +0800
changeset 347291 64fa2d708a10b158a57599016bf9f4a8f92b3024
parent 347290 e7e76c9521024792f6d9b430913bdaff3cb11254
child 347292 19c9fa346278d7fabd7a4dfebd4cee0c263ec551
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersxidorn
bugs1290023
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1290023 - Allow enum class values to be passed to other nsCSSProps methods and stored in nsCSSValue/StyleAnimationValue. r=xidorn MozReview-Commit-ID: 62usVamI3GA
layout/style/StyleAnimationValue.cpp
layout/style/StyleAnimationValue.h
layout/style/nsCSSProps.h
layout/style/nsCSSValue.h
layout/style/nsComputedDOMStyle.cpp
--- a/layout/style/StyleAnimationValue.cpp
+++ b/layout/style/StyleAnimationValue.cpp
@@ -3570,17 +3570,17 @@ StyleClipBasicShapeToCSSArray(const nsSt
       functionArray->Item(functionArray->Count() - 1).
                        SetArrayValue(radiusArray, eCSSUnit_Array);
       break;
     }
     default:
       MOZ_ASSERT_UNREACHABLE("Unknown shape type");
       return false;
   }
-  aResult->Item(1).SetIntValue(uint8_t(aClipPath.GetSizingBox()),
+  aResult->Item(1).SetIntValue(aClipPath.GetSizingBox(),
                                eCSSUnit_Enumerated);
   return true;
 }
 
 bool
 StyleAnimationValue::ExtractComputedValue(nsCSSProperty aProperty,
                                           nsStyleContext* aStyleContext,
                                           StyleAnimationValue& aComputedValue)
@@ -3961,17 +3961,17 @@ StyleAnimationValue::ExtractComputedValu
               new mozilla::css::URLValue(clipPath.GetURL(),
                                          uriAsStringBuffer,
                                          doc->GetDocumentURI(),
                                          doc->NodePrincipal());
             auto result = MakeUnique<nsCSSValue>();
             result->SetURLValue(url);
             aComputedValue.SetAndAdoptCSSValueValue(result.release(), eUnit_URL);
           } else if (type == StyleClipPathType::Box) {
-            aComputedValue.SetIntValue(uint8_t(clipPath.GetSizingBox()),
+            aComputedValue.SetIntValue(clipPath.GetSizingBox(),
                                        eUnit_Enumerated);
           } else if (type == StyleClipPathType::Shape) {
             RefPtr<nsCSSValue::Array> result = nsCSSValue::Array::Create(2);
             if (!StyleClipBasicShapeToCSSArray(clipPath, result)) {
               return false;
             }
             aComputedValue.SetCSSValueArrayValue(result, eUnit_Shape);
 
--- a/layout/style/StyleAnimationValue.h
+++ b/layout/style/StyleAnimationValue.h
@@ -425,16 +425,24 @@ public:
   StyleAnimationValue(nscolor aColor, ColorConstructorType);
 
   ~StyleAnimationValue() { FreeValue(); }
 
   void SetNormalValue();
   void SetAutoValue();
   void SetNoneValue();
   void SetIntValue(int32_t aInt, Unit aUnit);
+  template<typename T,
+           typename = typename std::enable_if<std::is_enum<T>::value>::type>
+  void SetIntValue(T aInt, Unit aUnit)
+  {
+    static_assert(mozilla::IsEnumFittingWithin<T, int32_t>::value,
+                  "aValue must be an enum that fits within mValue.mInt");
+    SetIntValue(static_cast<int32_t>(aInt), aUnit);
+  }
   void SetCoordValue(nscoord aCoord);
   void SetPercentValue(float aPercent);
   void SetFloatValue(float aFloat);
   void SetColorValue(nscolor aColor);
   void SetCurrentColorValue();
   void SetUnparsedStringValue(const nsString& aString);
   void SetCSSValueArrayValue(nsCSSValue::Array* aValue, Unit aUnit);
 
--- a/layout/style/nsCSSProps.h
+++ b/layout/style/nsCSSProps.h
@@ -444,21 +444,39 @@ public:
                                     const KTableEntry aTable[]);
 
   // Find |aKeyword| in |aTable|, if found set |aValue| to its corresponding value.
   // If not found, return false and do not set |aValue|.
   static bool FindKeyword(nsCSSKeyword aKeyword, const KTableEntry aTable[],
                           int32_t& aValue);
   // Return the first keyword in |aTable| that has the corresponding value |aValue|.
   // Return |eCSSKeyword_UNKNOWN| if not found.
-  static nsCSSKeyword ValueToKeywordEnum(int32_t aValue, 
+  static nsCSSKeyword ValueToKeywordEnum(int32_t aValue,
                                          const KTableEntry aTable[]);
+  template<typename T,
+           typename = typename std::enable_if<std::is_enum<T>::value>::type>
+  static nsCSSKeyword ValueToKeywordEnum(T aValue,
+                                         const KTableEntry aTable[])
+  {
+    static_assert(mozilla::IsEnumFittingWithin<T, int16_t>::value,
+                  "aValue must be an enum that fits within KTableEntry::mValue");
+    return ValueToKeywordEnum(static_cast<int16_t>(aValue), aTable);
+  }
   // Ditto but as a string, return "" when not found.
   static const nsAFlatCString& ValueToKeyword(int32_t aValue,
                                               const KTableEntry aTable[]);
+  template<typename T,
+           typename = typename std::enable_if<std::is_enum<T>::value>::type>
+  static const nsAFlatCString& ValueToKeyword(T aValue,
+                                              const KTableEntry aTable[])
+  {
+    static_assert(mozilla::IsEnumFittingWithin<T, int16_t>::value,
+                  "aValue must be an enum that fits within KTableEntry::mValue");
+    return ValueToKeyword(static_cast<int16_t>(aValue), aTable);
+  }
 
   static const nsStyleStructID kSIDTable[eCSSProperty_COUNT_no_shorthands];
   static const KTableEntry* const kKeywordTableTable[eCSSProperty_COUNT_no_shorthands];
   static const nsStyleAnimType kAnimTypeTable[eCSSProperty_COUNT_no_shorthands];
   static const ptrdiff_t
     kStyleStructOffsetTable[eCSSProperty_COUNT_no_shorthands];
 
 private:
--- a/layout/style/nsCSSValue.h
+++ b/layout/style/nsCSSValue.h
@@ -3,26 +3,29 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* representation of simple property values within CSS declarations */
 
 #ifndef nsCSSValue_h___
 #define nsCSSValue_h___
 
+#include <type_traits>
+
 #include "mozilla/Attributes.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/SheetType.h"
 #include "mozilla/UniquePtr.h"
 
 #include "nsIPrincipal.h"
 #include "nsIURI.h"
 #include "nsCOMPtr.h"
 #include "nsCSSKeywords.h"
 #include "nsCSSProperty.h"
+#include "nsCSSProps.h"
 #include "nsColor.h"
 #include "nsCoord.h"
 #include "nsProxyRelease.h"
 #include "nsRefPtrHashtable.h"
 #include "nsString.h"
 #include "nsStringBuffer.h"
 #include "nsTArray.h"
 #include "nsStyleConsts.h"
@@ -720,16 +723,24 @@ public:
     if (mUnit != eCSSUnit_Null)
       DoReset();
   }
 private:
   void DoReset();
 
 public:
   void SetIntValue(int32_t aValue, nsCSSUnit aUnit);
+  template<typename T,
+           typename = typename std::enable_if<std::is_enum<T>::value>::type>
+  void SetIntValue(T aValue, nsCSSUnit aUnit)
+  {
+    static_assert(mozilla::IsEnumFittingWithin<T, int32_t>::value,
+                  "aValue must be an enum that fits within mValue.mInt");
+    SetIntValue(static_cast<int32_t>(aValue), aUnit);
+  }
   void SetPercentValue(float aValue);
   void SetFloatValue(float aValue, nsCSSUnit aUnit);
   void SetStringValue(const nsString& aValue, nsCSSUnit aUnit);
   void SetColorValue(nscolor aValue);
   void SetIntegerColorValue(nscolor aValue, nsCSSUnit aUnit);
   void SetFloatColorValue(float aComponent1,
                           float aComponent2,
                           float aComponent3,
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -4170,17 +4170,17 @@ nsComputedDOMStyle::DoGetBoxPack()
   return val.forget();
 }
 
 already_AddRefed<CSSValue>
 nsComputedDOMStyle::DoGetBoxSizing()
 {
   RefPtr<nsROCSSPrimitiveValue> val = new nsROCSSPrimitiveValue;
   val->SetIdent(
-    nsCSSProps::ValueToKeywordEnum(uint8_t(StylePosition()->mBoxSizing),
+    nsCSSProps::ValueToKeywordEnum(StylePosition()->mBoxSizing,
                                    nsCSSProps::kBoxSizingKTable));
   return val.forget();
 }
 
 /* Border image properties */
 
 already_AddRefed<CSSValue>
 nsComputedDOMStyle::DoGetBorderImageSource()
@@ -6005,17 +6005,17 @@ nsComputedDOMStyle::CreatePrimitiveValue
   }
 
   if (aSizingBox == StyleClipShapeSizing::NoBox) {
     return valueList.forget();
   }
 
   nsAutoString boxString;
   AppendASCIItoUTF16(
-    nsCSSProps::ValueToKeyword(uint8_t(aSizingBox),
+    nsCSSProps::ValueToKeyword(aSizingBox,
                                nsCSSProps::kClipShapeSizingKTable),
                                boxString);
   RefPtr<nsROCSSPrimitiveValue> val = new nsROCSSPrimitiveValue;
   val->SetString(boxString);
   valueList->AppendCSSValue(val.forget());
 
   return valueList.forget();
 }