2 * Copyright (C) 2013 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.
26 #ifndef WTF_EnumClass_h
27 #define WTF_EnumClass_h
29 #include "wtf/Compiler.h"
33 // How to define a type safe enum list using the ENUM_CLASS macros?
34 // ===============================================================
35 // To get an enum list like this:
37 // enum class MyEnums {
46 // ENUM_CLASS(MyEnums) {
51 // } ENUM_CLASS_END(MyEnums);
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.
56 #if COMPILER_SUPPORTS(CXX_STRONG_ENUMS)
58 #define ENUM_CLASS(__enumName) \
61 #define ENUM_CLASS_END(__enumName)
63 #else // !COMPILER_SUPPORTS(CXX_STRONG_ENUMS)
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.
70 // Here's an example of how to define a type safe enum named MyEnum using
71 // the EnumClass template:
73 // struct MyEnumDefinition {
81 // typedef EnumClass<MyEnumDefinition, MyEnumDefinition::ValueDefault> MyEnum;
83 // With that, you can now use MyEnum enum values as follow:
85 // MyEnum value1; // value1 is assigned MyEnum::ValueDefault by default.
86 // MyEnum value2 = MyEnum::Value1; // value2 is assigned MyEnum::Value1;
88 template <typename Definition>
89 class EnumClass : public Definition {
90 typedef enum Definition::Enums Value;
92 ALWAYS_INLINE EnumClass() { }
93 ALWAYS_INLINE EnumClass(Value value) : m_value(value) { }
95 ALWAYS_INLINE Value value() const { return m_value; }
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; }
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; }
111 ALWAYS_INLINE operator Value() { return m_value; }
117 #define ENUM_CLASS(__enumName) \
118 struct __enumName ## Definition { \
121 #define ENUM_CLASS_END(__enumName) \
124 typedef EnumClass< __enumName ## Definition > __enumName
126 #endif // !COMPILER_SUPPORTS(CXX_STRONG_ENUMS)
130 #if !COMPILER_SUPPORTS(CXX_STRONG_ENUMS)
131 using WTF::EnumClass;
134 #endif // WTF_EnumClass_h