layout/style/nsCSSPropertySet.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/style/nsCSSPropertySet.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     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 +/* bit vectors for sets of CSS properties */
     1.9 +
    1.10 +#ifndef nsCSSPropertySet_h__
    1.11 +#define nsCSSPropertySet_h__
    1.12 +
    1.13 +#include "mozilla/ArrayUtils.h"
    1.14 +
    1.15 +#include "nsCSSProperty.h"
    1.16 +#include <limits.h> // for CHAR_BIT
    1.17 +
    1.18 +/**
    1.19 + * nsCSSPropertySet maintains a set of non-shorthand CSS properties.  In
    1.20 + * other words, for each longhand CSS property we support, it has a bit
    1.21 + * for whether that property is in the set.
    1.22 + */
    1.23 +class nsCSSPropertySet {
    1.24 +public:
    1.25 +    nsCSSPropertySet() { Empty(); }
    1.26 +    // auto-generated copy-constructor OK
    1.27 +
    1.28 +    void AssertInSetRange(nsCSSProperty aProperty) const {
    1.29 +        NS_ASSERTION(0 <= aProperty &&
    1.30 +                     aProperty < eCSSProperty_COUNT_no_shorthands,
    1.31 +                     "out of bounds");
    1.32 +    }
    1.33 +
    1.34 +    // Conversion of aProperty to |size_t| after AssertInSetRange
    1.35 +    // lets the compiler generate significantly tighter code.
    1.36 +
    1.37 +    void AddProperty(nsCSSProperty aProperty) {
    1.38 +        AssertInSetRange(aProperty);
    1.39 +        size_t p = aProperty;
    1.40 +        mProperties[p / kBitsInChunk] |=
    1.41 +          property_set_type(1) << (p % kBitsInChunk);
    1.42 +    }
    1.43 +
    1.44 +    void RemoveProperty(nsCSSProperty aProperty) {
    1.45 +        AssertInSetRange(aProperty);
    1.46 +        size_t p = aProperty;
    1.47 +        mProperties[p / kBitsInChunk] &=
    1.48 +            ~(property_set_type(1) << (p % kBitsInChunk));
    1.49 +    }
    1.50 +
    1.51 +    bool HasProperty(nsCSSProperty aProperty) const {
    1.52 +        AssertInSetRange(aProperty);
    1.53 +        size_t p = aProperty;
    1.54 +        return (mProperties[p / kBitsInChunk] &
    1.55 +                (property_set_type(1) << (p % kBitsInChunk))) != 0;
    1.56 +    }
    1.57 +
    1.58 +    void Empty() {
    1.59 +        memset(mProperties, 0, sizeof(mProperties));
    1.60 +    }
    1.61 +
    1.62 +    void AssertIsEmpty(const char* aText) const {
    1.63 +        for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
    1.64 +            NS_ASSERTION(mProperties[i] == 0, aText);
    1.65 +        }
    1.66 +    }
    1.67 +
    1.68 +private:
    1.69 +    typedef unsigned long property_set_type;
    1.70 +public:
    1.71 +    // number of bits in |property_set_type|.
    1.72 +    static const size_t kBitsInChunk = sizeof(property_set_type)*CHAR_BIT;
    1.73 +    // number of |property_set_type|s in the set
    1.74 +    static const size_t kChunkCount =
    1.75 +        (eCSSProperty_COUNT_no_shorthands + kBitsInChunk - 1) / kBitsInChunk;
    1.76 +
    1.77 +    /*
    1.78 +     * For fast enumeration of all the bits that are set, callers can
    1.79 +     * check each chunk against zero (since in normal cases few bits are
    1.80 +     * likely to be set).
    1.81 +     */
    1.82 +    bool HasPropertyInChunk(size_t aChunk) const {
    1.83 +        return mProperties[aChunk] != 0;
    1.84 +    }
    1.85 +    bool HasPropertyAt(size_t aChunk, size_t aBit) const {
    1.86 +        return (mProperties[aChunk] & (property_set_type(1) << aBit)) != 0;
    1.87 +    }
    1.88 +    static nsCSSProperty CSSPropertyAt(size_t aChunk, size_t aBit) {
    1.89 +        return nsCSSProperty(aChunk * kBitsInChunk + aBit);
    1.90 +    }
    1.91 +
    1.92 +private:
    1.93 +    property_set_type mProperties[kChunkCount];
    1.94 +};
    1.95 +
    1.96 +#endif /* !defined(nsCSSPropertySet_h__) */

mercurial