content/base/src/nsAttrValueInlines.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef nsAttrValueInlines_h__
     6 #define nsAttrValueInlines_h__
     8 #include <stdint.h>
    10 #include "nsAttrValue.h"
    12 struct MiscContainer
    13 {
    14   typedef nsAttrValue::ValueType ValueType;
    16   ValueType mType;
    17   // mStringBits points to either nsIAtom* or nsStringBuffer* and is used when
    18   // mType isn't mCSSStyleRule.
    19   // Note eStringBase and eAtomBase is used also to handle the type of
    20   // mStringBits.
    21   uintptr_t mStringBits;
    22   union {
    23     struct {
    24       union {
    25         int32_t mInteger;
    26         nscolor mColor;
    27         uint32_t mEnumValue;
    28         int32_t mPercent;
    29         mozilla::css::StyleRule* mCSSStyleRule;
    30         mozilla::css::URLValue* mURL;
    31         mozilla::css::ImageValue* mImage;
    32         nsAttrValue::AtomArray* mAtomArray;
    33         nsIntMargin* mIntMargin;
    34         const nsSVGAngle* mSVGAngle;
    35         const nsSVGIntegerPair* mSVGIntegerPair;
    36         const nsSVGLength2* mSVGLength;
    37         const mozilla::SVGLengthList* mSVGLengthList;
    38         const mozilla::SVGNumberList* mSVGNumberList;
    39         const nsSVGNumberPair* mSVGNumberPair;
    40         const mozilla::SVGPathData* mSVGPathData;
    41         const mozilla::SVGPointList* mSVGPointList;
    42         const mozilla::SVGAnimatedPreserveAspectRatio* mSVGPreserveAspectRatio;
    43         const mozilla::SVGStringList* mSVGStringList;
    44         const mozilla::SVGTransformList* mSVGTransformList;
    45         const nsSVGViewBox* mSVGViewBox;
    46       };
    47       uint32_t mRefCount : 31;
    48       uint32_t mCached : 1;
    49     } mValue;
    50     double mDoubleValue;
    51   };
    53   MiscContainer()
    54     : mType(nsAttrValue::eColor),
    55       mStringBits(0)
    56   {
    57     MOZ_COUNT_CTOR(MiscContainer);
    58     mValue.mColor = 0;
    59     mValue.mRefCount = 0;
    60     mValue.mCached = 0;
    61   }
    63   ~MiscContainer()
    64   {
    65     if (IsRefCounted()) {
    66       MOZ_ASSERT(mValue.mRefCount == 0);
    67       MOZ_ASSERT(!mValue.mCached);
    68     }
    69     MOZ_COUNT_DTOR(MiscContainer);
    70   }
    72   bool GetString(nsAString& aString) const;
    74   inline bool IsRefCounted() const
    75   {
    76     // Nothing stops us from refcounting (and sharing) other types of
    77     // MiscContainer (except eDoubleValue types) but there's no compelling
    78     // reason to 
    79     return mType == nsAttrValue::eCSSStyleRule;
    80   }
    82   inline int32_t AddRef() {
    83     MOZ_ASSERT(IsRefCounted());
    84     return ++mValue.mRefCount;
    85   }
    87   inline int32_t Release() {
    88     MOZ_ASSERT(IsRefCounted());
    89     return --mValue.mRefCount;
    90   }
    92   void Cache();
    93   void Evict();
    94 };
    97 /**
    98  * Implementation of inline methods
    99  */
   101 inline int32_t
   102 nsAttrValue::GetIntegerValue() const
   103 {
   104   NS_PRECONDITION(Type() == eInteger, "wrong type");
   105   return (BaseType() == eIntegerBase)
   106          ? GetIntInternal()
   107          : GetMiscContainer()->mValue.mInteger;
   108 }
   110 inline int16_t
   111 nsAttrValue::GetEnumValue() const
   112 {
   113   NS_PRECONDITION(Type() == eEnum, "wrong type");
   114   // We don't need to worry about sign extension here since we're
   115   // returning an int16_t which will cut away the top bits.
   116   return static_cast<int16_t>((
   117     (BaseType() == eIntegerBase)
   118     ? static_cast<uint32_t>(GetIntInternal())
   119     : GetMiscContainer()->mValue.mEnumValue)
   120       >> NS_ATTRVALUE_ENUMTABLEINDEX_BITS);
   121 }
   123 inline float
   124 nsAttrValue::GetPercentValue() const
   125 {
   126   NS_PRECONDITION(Type() == ePercent, "wrong type");
   127   return ((BaseType() == eIntegerBase)
   128           ? GetIntInternal()
   129           : GetMiscContainer()->mValue.mPercent)
   130             / 100.0f;
   131 }
   133 inline nsAttrValue::AtomArray*
   134 nsAttrValue::GetAtomArrayValue() const
   135 {
   136   NS_PRECONDITION(Type() == eAtomArray, "wrong type");
   137   return GetMiscContainer()->mValue.mAtomArray;
   138 }
   140 inline mozilla::css::StyleRule*
   141 nsAttrValue::GetCSSStyleRuleValue() const
   142 {
   143   NS_PRECONDITION(Type() == eCSSStyleRule, "wrong type");
   144   return GetMiscContainer()->mValue.mCSSStyleRule;
   145 }
   147 inline mozilla::css::URLValue*
   148 nsAttrValue::GetURLValue() const
   149 {
   150   NS_PRECONDITION(Type() == eURL, "wrong type");
   151   return GetMiscContainer()->mValue.mURL;
   152 }
   154 inline mozilla::css::ImageValue*
   155 nsAttrValue::GetImageValue() const
   156 {
   157   NS_PRECONDITION(Type() == eImage, "wrong type");
   158   return GetMiscContainer()->mValue.mImage;
   159 }
   161 inline double
   162 nsAttrValue::GetDoubleValue() const
   163 {
   164   NS_PRECONDITION(Type() == eDoubleValue, "wrong type");
   165   return GetMiscContainer()->mDoubleValue;
   166 }
   168 inline bool
   169 nsAttrValue::GetIntMarginValue(nsIntMargin& aMargin) const
   170 {
   171   NS_PRECONDITION(Type() == eIntMarginValue, "wrong type");
   172   nsIntMargin* m = GetMiscContainer()->mValue.mIntMargin;
   173   if (!m)
   174     return false;
   175   aMargin = *m;
   176   return true;
   177 }
   179 inline bool
   180 nsAttrValue::IsSVGType(ValueType aType) const
   181 {
   182   return aType >= eSVGTypesBegin && aType <= eSVGTypesEnd;
   183 }
   185 inline void
   186 nsAttrValue::SetPtrValueAndType(void* aValue, ValueBaseType aType)
   187 {
   188   NS_ASSERTION(!(NS_PTR_TO_INT32(aValue) & ~NS_ATTRVALUE_POINTERVALUE_MASK),
   189                "pointer not properly aligned, this will crash");
   190   mBits = reinterpret_cast<intptr_t>(aValue) | aType;
   191 }
   193 inline void
   194 nsAttrValue::ResetIfSet()
   195 {
   196   if (mBits) {
   197     Reset();
   198   }
   199 }
   201 inline MiscContainer*
   202 nsAttrValue::GetMiscContainer() const
   203 {
   204   NS_ASSERTION(BaseType() == eOtherBase, "wrong type");
   205   return static_cast<MiscContainer*>(GetPtr());
   206 }
   208 inline int32_t
   209 nsAttrValue::GetIntInternal() const
   210 {
   211   NS_ASSERTION(BaseType() == eIntegerBase,
   212                "getting integer from non-integer");
   213   // Make sure we get a signed value.
   214   // Lets hope the optimizer optimizes this into a shift. Unfortunatly signed
   215   // bitshift right is implementaion dependant.
   216   return static_cast<int32_t>(mBits & ~NS_ATTRVALUE_INTEGERTYPE_MASK) /
   217          NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER;
   218 }
   220 #endif

mercurial