2 * Copyright (C) 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <wtf/Optional.h>
29 #include <wtf/TriState.h>
33 class DefinePropertyAttributes {
35 static_assert(FalseTriState == 0, "FalseTriState is 0.");
36 static_assert(TrueTriState == 1, "TrueTriState is 1.");
37 static_assert(MixedTriState == 2, "MixedTriState is 2.");
39 static const unsigned ConfigurableShift = 0;
40 static const unsigned EnumerableShift = 2;
41 static const unsigned WritableShift = 4;
42 static const unsigned ValueShift = 6;
43 static const unsigned GetShift = 7;
44 static const unsigned SetShift = 8;
46 DefinePropertyAttributes()
48 (MixedTriState << ConfigurableShift)
49 | (MixedTriState << EnumerableShift)
50 | (MixedTriState << WritableShift)
57 explicit DefinePropertyAttributes(unsigned attributes)
58 : m_attributes(attributes)
62 unsigned rawRepresentation() const
69 return m_attributes & (0b1 << ValueShift);
74 m_attributes = m_attributes | (0b1 << ValueShift);
79 return m_attributes & (0b1 << GetShift);
84 m_attributes = m_attributes | (0b1 << GetShift);
89 return m_attributes & (0b1 << SetShift);
94 m_attributes = m_attributes | (0b1 << SetShift);
97 bool hasWritable() const
99 return extractTriState(WritableShift) != MixedTriState;
102 std::optional<bool> writable() const
106 return extractTriState(WritableShift) == TrueTriState;
109 bool hasConfigurable() const
111 return extractTriState(ConfigurableShift) != MixedTriState;
114 std::optional<bool> configurable() const
116 if (!hasConfigurable())
118 return extractTriState(ConfigurableShift) == TrueTriState;
121 bool hasEnumerable() const
123 return extractTriState(EnumerableShift) != MixedTriState;
126 std::optional<bool> enumerable() const
128 if (!hasEnumerable())
130 return extractTriState(EnumerableShift) == TrueTriState;
133 void setWritable(bool value)
135 fillWithTriState(value ? TrueTriState : FalseTriState, WritableShift);
138 void setConfigurable(bool value)
140 fillWithTriState(value ? TrueTriState : FalseTriState, ConfigurableShift);
143 void setEnumerable(bool value)
145 fillWithTriState(value ? TrueTriState : FalseTriState, EnumerableShift);
149 void fillWithTriState(TriState state, unsigned shift)
151 unsigned mask = 0b11 << shift;
152 m_attributes = (m_attributes & ~mask) | (state << shift);
155 TriState extractTriState(unsigned shift) const
157 return static_cast<TriState>((m_attributes >> shift) & 0b11);
160 unsigned m_attributes;