1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is nsCSSDataBlock.h.
15 : *
16 : * The Initial Developer of the Original Code is L. David Baron.
17 : * Portions created by the Initial Developer are Copyright (C) 2003
18 : * the Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : * L. David Baron <dbaron@dbaron.org> (original author)
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : /* bit vectors for sets of CSS properties */
38 :
39 : #ifndef nsCSSPropertySet_h__
40 : #define nsCSSPropertySet_h__
41 :
42 : #include "mozilla/Util.h"
43 :
44 : #include "nsCSSProperty.h"
45 : #include <limits.h> // for CHAR_BIT
46 :
47 : /**
48 : * nsCSSPropertySet maintains a set of non-shorthand CSS properties. In
49 : * other words, for each longhand CSS property we support, it has a bit
50 : * for whether that property is in the set.
51 : */
52 : class nsCSSPropertySet {
53 : public:
54 48 : nsCSSPropertySet() { Empty(); }
55 : // auto-generated copy-constructor OK
56 :
57 0 : void AssertInSetRange(nsCSSProperty aProperty) const {
58 0 : NS_ASSERTION(0 <= aProperty &&
59 : aProperty < eCSSProperty_COUNT_no_shorthands,
60 : "out of bounds");
61 0 : }
62 :
63 : // Conversion of aProperty to |size_t| after AssertInSetRange
64 : // lets the compiler generate significantly tighter code.
65 :
66 0 : void AddProperty(nsCSSProperty aProperty) {
67 0 : AssertInSetRange(aProperty);
68 0 : size_t p = aProperty;
69 : mProperties[p / kBitsInChunk] |=
70 0 : property_set_type(1) << (p % kBitsInChunk);
71 0 : }
72 :
73 0 : void RemoveProperty(nsCSSProperty aProperty) {
74 0 : AssertInSetRange(aProperty);
75 0 : size_t p = aProperty;
76 : mProperties[p / kBitsInChunk] &=
77 0 : ~(property_set_type(1) << (p % kBitsInChunk));
78 0 : }
79 :
80 0 : bool HasProperty(nsCSSProperty aProperty) const {
81 0 : AssertInSetRange(aProperty);
82 0 : size_t p = aProperty;
83 0 : return (mProperties[p / kBitsInChunk] &
84 0 : (property_set_type(1) << (p % kBitsInChunk))) != 0;
85 : }
86 :
87 48 : void Empty() {
88 48 : memset(mProperties, 0, sizeof(mProperties));
89 48 : }
90 :
91 144 : void AssertIsEmpty(const char* aText) const {
92 1296 : for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
93 1152 : NS_ASSERTION(mProperties[i] == 0, aText);
94 : }
95 144 : }
96 :
97 : private:
98 : typedef unsigned long property_set_type;
99 : public:
100 : // number of bits in |property_set_type|.
101 : static const size_t kBitsInChunk = sizeof(property_set_type)*CHAR_BIT;
102 : // number of |property_set_type|s in the set
103 : static const size_t kChunkCount =
104 : (eCSSProperty_COUNT_no_shorthands + kBitsInChunk - 1) / kBitsInChunk;
105 :
106 : /*
107 : * For fast enumeration of all the bits that are set, callers can
108 : * check each chunk against zero (since in normal cases few bits are
109 : * likely to be set).
110 : */
111 0 : bool HasPropertyInChunk(size_t aChunk) const {
112 0 : return mProperties[aChunk] != 0;
113 : }
114 0 : bool HasPropertyAt(size_t aChunk, size_t aBit) const {
115 0 : return (mProperties[aChunk] & (property_set_type(1) << aBit)) != 0;
116 : }
117 0 : static nsCSSProperty CSSPropertyAt(size_t aChunk, size_t aBit) {
118 0 : return nsCSSProperty(aChunk * kBitsInChunk + aBit);
119 : }
120 :
121 : private:
122 : property_set_type mProperties[kChunkCount];
123 : };
124 :
125 : #endif /* !defined(nsCSSPropertySet_h__) */
|