1 | /* |
2 | * Copyright (C) 2013 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
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. |
12 | * |
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. |
24 | */ |
25 | |
26 | #ifndef WTF_EnumClass_h |
27 | #define WTF_EnumClass_h |
28 | |
29 | #include <wtf/Compiler.h> |
30 | |
31 | namespace WTF { |
32 | |
33 | // How to define a type-safe enum list using the ENUM_CLASS macros? |
34 | // =============================================================== |
35 | // To get an enum list like this: |
36 | // |
37 | // enum class MyEnums { |
38 | // Value1, |
39 | // Value2, |
40 | // ... |
41 | // ValueN |
42 | // }; |
43 | // |
44 | // ... write this: |
45 | // |
46 | // ENUM_CLASS(MyEnums) { |
47 | // Value1, |
48 | // Value2, |
49 | // ... |
50 | // ValueN |
51 | // } ENUM_CLASS_END(MyEnums); |
52 | // |
53 | // The ENUM_CLASS macros will use C++11's enum class if the compiler supports it. |
54 | // Otherwise, it will use the EnumClass template below. |
55 | |
56 | #if COMPILER_SUPPORTS(CXX_STRONG_ENUMS) |
57 | |
58 | #define ENUM_CLASS(__enumName) \ |
59 | enum class __enumName |
60 | |
61 | #define ENUM_CLASS_END(__enumName) |
62 | |
63 | #else // !COMPILER_SUPPORTS(CXX_STRONG_ENUMS) |
64 | |
65 | // How to define a type-safe enum list using the EnumClass template? |
66 | // ================================================================ |
67 | // Definition should be a struct that encapsulates an enum list. |
68 | // The enum list should be names Enums. |
69 | // |
70 | // Here's an example of how to define a type-safe enum named MyEnum using |
71 | // the EnumClass template: |
72 | // |
73 | // struct MyEnumDefinition { |
74 | // enum Enums { |
75 | // ValueDefault, |
76 | // Value1, |
77 | // ... |
78 | // ValueN |
79 | // }; |
80 | // }; |
81 | // typedef EnumClass<MyEnumDefinition, MyEnumDefinition::ValueDefault> MyEnum; |
82 | // |
83 | // With that, you can now use MyEnum enum values as follow: |
84 | // |
85 | // MyEnum value1; // value1 is assigned MyEnum::ValueDefault by default. |
86 | // MyEnum value2 = MyEnum::Value1; // value2 is assigned MyEnum::Value1; |
87 | |
88 | template <typename Definition> |
89 | class EnumClass : public Definition { |
90 | typedef enum Definition::Enums Value; |
91 | public: |
92 | ALWAYS_INLINE EnumClass() { } |
93 | ALWAYS_INLINE EnumClass(Value value) : m_value(value) { } |
94 | |
95 | ALWAYS_INLINE Value value() const { return m_value; } |
96 | |
97 | ALWAYS_INLINE bool operator==(const EnumClass other) { return m_value == other.m_value; } |
98 | ALWAYS_INLINE bool operator!=(const EnumClass other) { return m_value != other.m_value; } |
99 | ALWAYS_INLINE bool operator<(const EnumClass other) { return m_value < other.m_value; } |
100 | ALWAYS_INLINE bool operator<=(const EnumClass other) { return m_value <= other.m_value; } |
101 | ALWAYS_INLINE bool operator>(const EnumClass other) { return m_value > other.m_value; } |
102 | ALWAYS_INLINE bool operator>=(const EnumClass other) { return m_value >= other.m_value; } |
103 | |
104 | ALWAYS_INLINE bool operator==(const Value value) { return m_value == value; } |
105 | ALWAYS_INLINE bool operator!=(const Value value) { return m_value != value; } |
106 | ALWAYS_INLINE bool operator<(const Value value) { return m_value < value; } |
107 | ALWAYS_INLINE bool operator<=(const Value value) { return m_value <= value; } |
108 | ALWAYS_INLINE bool operator>(const Value value) { return m_value > value; } |
109 | ALWAYS_INLINE bool operator>=(const Value value) { return m_value >= value; } |
110 | |
111 | ALWAYS_INLINE operator Value() { return m_value; } |
112 | |
113 | private: |
114 | Value m_value; |
115 | }; |
116 | |
117 | #define ENUM_CLASS(__enumName) \ |
118 | struct __enumName ## Definition { \ |
119 | enum Enums |
120 | |
121 | #define ENUM_CLASS_END(__enumName) \ |
122 | ; \ |
123 | }; \ |
124 | typedef EnumClass< __enumName ## Definition > __enumName |
125 | |
126 | #endif // !COMPILER_SUPPORTS(CXX_STRONG_ENUMS) |
127 | |
128 | } // namespace WTF |
129 | |
130 | #if !COMPILER_SUPPORTS(CXX_STRONG_ENUMS) |
131 | using WTF::EnumClass; |
132 | #endif |
133 | |
134 | #endif // WTF_EnumClass_h |
135 | |