content/base/src/nsAttrValueInlines.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsAttrValueInlines.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,220 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef nsAttrValueInlines_h__
     1.9 +#define nsAttrValueInlines_h__
    1.10 +
    1.11 +#include <stdint.h>
    1.12 +
    1.13 +#include "nsAttrValue.h"
    1.14 +
    1.15 +struct MiscContainer
    1.16 +{
    1.17 +  typedef nsAttrValue::ValueType ValueType;
    1.18 +
    1.19 +  ValueType mType;
    1.20 +  // mStringBits points to either nsIAtom* or nsStringBuffer* and is used when
    1.21 +  // mType isn't mCSSStyleRule.
    1.22 +  // Note eStringBase and eAtomBase is used also to handle the type of
    1.23 +  // mStringBits.
    1.24 +  uintptr_t mStringBits;
    1.25 +  union {
    1.26 +    struct {
    1.27 +      union {
    1.28 +        int32_t mInteger;
    1.29 +        nscolor mColor;
    1.30 +        uint32_t mEnumValue;
    1.31 +        int32_t mPercent;
    1.32 +        mozilla::css::StyleRule* mCSSStyleRule;
    1.33 +        mozilla::css::URLValue* mURL;
    1.34 +        mozilla::css::ImageValue* mImage;
    1.35 +        nsAttrValue::AtomArray* mAtomArray;
    1.36 +        nsIntMargin* mIntMargin;
    1.37 +        const nsSVGAngle* mSVGAngle;
    1.38 +        const nsSVGIntegerPair* mSVGIntegerPair;
    1.39 +        const nsSVGLength2* mSVGLength;
    1.40 +        const mozilla::SVGLengthList* mSVGLengthList;
    1.41 +        const mozilla::SVGNumberList* mSVGNumberList;
    1.42 +        const nsSVGNumberPair* mSVGNumberPair;
    1.43 +        const mozilla::SVGPathData* mSVGPathData;
    1.44 +        const mozilla::SVGPointList* mSVGPointList;
    1.45 +        const mozilla::SVGAnimatedPreserveAspectRatio* mSVGPreserveAspectRatio;
    1.46 +        const mozilla::SVGStringList* mSVGStringList;
    1.47 +        const mozilla::SVGTransformList* mSVGTransformList;
    1.48 +        const nsSVGViewBox* mSVGViewBox;
    1.49 +      };
    1.50 +      uint32_t mRefCount : 31;
    1.51 +      uint32_t mCached : 1;
    1.52 +    } mValue;
    1.53 +    double mDoubleValue;
    1.54 +  };
    1.55 +
    1.56 +  MiscContainer()
    1.57 +    : mType(nsAttrValue::eColor),
    1.58 +      mStringBits(0)
    1.59 +  {
    1.60 +    MOZ_COUNT_CTOR(MiscContainer);
    1.61 +    mValue.mColor = 0;
    1.62 +    mValue.mRefCount = 0;
    1.63 +    mValue.mCached = 0;
    1.64 +  }
    1.65 +
    1.66 +  ~MiscContainer()
    1.67 +  {
    1.68 +    if (IsRefCounted()) {
    1.69 +      MOZ_ASSERT(mValue.mRefCount == 0);
    1.70 +      MOZ_ASSERT(!mValue.mCached);
    1.71 +    }
    1.72 +    MOZ_COUNT_DTOR(MiscContainer);
    1.73 +  }
    1.74 +
    1.75 +  bool GetString(nsAString& aString) const;
    1.76 +
    1.77 +  inline bool IsRefCounted() const
    1.78 +  {
    1.79 +    // Nothing stops us from refcounting (and sharing) other types of
    1.80 +    // MiscContainer (except eDoubleValue types) but there's no compelling
    1.81 +    // reason to 
    1.82 +    return mType == nsAttrValue::eCSSStyleRule;
    1.83 +  }
    1.84 +
    1.85 +  inline int32_t AddRef() {
    1.86 +    MOZ_ASSERT(IsRefCounted());
    1.87 +    return ++mValue.mRefCount;
    1.88 +  }
    1.89 +
    1.90 +  inline int32_t Release() {
    1.91 +    MOZ_ASSERT(IsRefCounted());
    1.92 +    return --mValue.mRefCount;
    1.93 +  }
    1.94 +
    1.95 +  void Cache();
    1.96 +  void Evict();
    1.97 +};
    1.98 +
    1.99 +
   1.100 +/**
   1.101 + * Implementation of inline methods
   1.102 + */
   1.103 +
   1.104 +inline int32_t
   1.105 +nsAttrValue::GetIntegerValue() const
   1.106 +{
   1.107 +  NS_PRECONDITION(Type() == eInteger, "wrong type");
   1.108 +  return (BaseType() == eIntegerBase)
   1.109 +         ? GetIntInternal()
   1.110 +         : GetMiscContainer()->mValue.mInteger;
   1.111 +}
   1.112 +
   1.113 +inline int16_t
   1.114 +nsAttrValue::GetEnumValue() const
   1.115 +{
   1.116 +  NS_PRECONDITION(Type() == eEnum, "wrong type");
   1.117 +  // We don't need to worry about sign extension here since we're
   1.118 +  // returning an int16_t which will cut away the top bits.
   1.119 +  return static_cast<int16_t>((
   1.120 +    (BaseType() == eIntegerBase)
   1.121 +    ? static_cast<uint32_t>(GetIntInternal())
   1.122 +    : GetMiscContainer()->mValue.mEnumValue)
   1.123 +      >> NS_ATTRVALUE_ENUMTABLEINDEX_BITS);
   1.124 +}
   1.125 +
   1.126 +inline float
   1.127 +nsAttrValue::GetPercentValue() const
   1.128 +{
   1.129 +  NS_PRECONDITION(Type() == ePercent, "wrong type");
   1.130 +  return ((BaseType() == eIntegerBase)
   1.131 +          ? GetIntInternal()
   1.132 +          : GetMiscContainer()->mValue.mPercent)
   1.133 +            / 100.0f;
   1.134 +}
   1.135 +
   1.136 +inline nsAttrValue::AtomArray*
   1.137 +nsAttrValue::GetAtomArrayValue() const
   1.138 +{
   1.139 +  NS_PRECONDITION(Type() == eAtomArray, "wrong type");
   1.140 +  return GetMiscContainer()->mValue.mAtomArray;
   1.141 +}
   1.142 +
   1.143 +inline mozilla::css::StyleRule*
   1.144 +nsAttrValue::GetCSSStyleRuleValue() const
   1.145 +{
   1.146 +  NS_PRECONDITION(Type() == eCSSStyleRule, "wrong type");
   1.147 +  return GetMiscContainer()->mValue.mCSSStyleRule;
   1.148 +}
   1.149 +
   1.150 +inline mozilla::css::URLValue*
   1.151 +nsAttrValue::GetURLValue() const
   1.152 +{
   1.153 +  NS_PRECONDITION(Type() == eURL, "wrong type");
   1.154 +  return GetMiscContainer()->mValue.mURL;
   1.155 +}
   1.156 +
   1.157 +inline mozilla::css::ImageValue*
   1.158 +nsAttrValue::GetImageValue() const
   1.159 +{
   1.160 +  NS_PRECONDITION(Type() == eImage, "wrong type");
   1.161 +  return GetMiscContainer()->mValue.mImage;
   1.162 +}
   1.163 +
   1.164 +inline double
   1.165 +nsAttrValue::GetDoubleValue() const
   1.166 +{
   1.167 +  NS_PRECONDITION(Type() == eDoubleValue, "wrong type");
   1.168 +  return GetMiscContainer()->mDoubleValue;
   1.169 +}
   1.170 +
   1.171 +inline bool
   1.172 +nsAttrValue::GetIntMarginValue(nsIntMargin& aMargin) const
   1.173 +{
   1.174 +  NS_PRECONDITION(Type() == eIntMarginValue, "wrong type");
   1.175 +  nsIntMargin* m = GetMiscContainer()->mValue.mIntMargin;
   1.176 +  if (!m)
   1.177 +    return false;
   1.178 +  aMargin = *m;
   1.179 +  return true;
   1.180 +}
   1.181 +
   1.182 +inline bool
   1.183 +nsAttrValue::IsSVGType(ValueType aType) const
   1.184 +{
   1.185 +  return aType >= eSVGTypesBegin && aType <= eSVGTypesEnd;
   1.186 +}
   1.187 +
   1.188 +inline void
   1.189 +nsAttrValue::SetPtrValueAndType(void* aValue, ValueBaseType aType)
   1.190 +{
   1.191 +  NS_ASSERTION(!(NS_PTR_TO_INT32(aValue) & ~NS_ATTRVALUE_POINTERVALUE_MASK),
   1.192 +               "pointer not properly aligned, this will crash");
   1.193 +  mBits = reinterpret_cast<intptr_t>(aValue) | aType;
   1.194 +}
   1.195 +
   1.196 +inline void
   1.197 +nsAttrValue::ResetIfSet()
   1.198 +{
   1.199 +  if (mBits) {
   1.200 +    Reset();
   1.201 +  }
   1.202 +}
   1.203 +
   1.204 +inline MiscContainer*
   1.205 +nsAttrValue::GetMiscContainer() const
   1.206 +{
   1.207 +  NS_ASSERTION(BaseType() == eOtherBase, "wrong type");
   1.208 +  return static_cast<MiscContainer*>(GetPtr());
   1.209 +}
   1.210 +
   1.211 +inline int32_t
   1.212 +nsAttrValue::GetIntInternal() const
   1.213 +{
   1.214 +  NS_ASSERTION(BaseType() == eIntegerBase,
   1.215 +               "getting integer from non-integer");
   1.216 +  // Make sure we get a signed value.
   1.217 +  // Lets hope the optimizer optimizes this into a shift. Unfortunatly signed
   1.218 +  // bitshift right is implementaion dependant.
   1.219 +  return static_cast<int32_t>(mBits & ~NS_ATTRVALUE_INTEGERTYPE_MASK) /
   1.220 +         NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER;
   1.221 +}
   1.222 +
   1.223 +#endif

mercurial