| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // ENUM_CLASS(MyEnums) { | 46 // ENUM_CLASS(MyEnums) { |
| 47 // Value1, | 47 // Value1, |
| 48 // Value2, | 48 // Value2, |
| 49 // ... | 49 // ... |
| 50 // ValueN | 50 // ValueN |
| 51 // } ENUM_CLASS_END(MyEnums); | 51 // } ENUM_CLASS_END(MyEnums); |
| 52 // | 52 // |
| 53 // The ENUM_CLASS macros will use C++11's enum class if the compiler supports it
. | 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. | 54 // Otherwise, it will use the EnumClass template below. |
| 55 | 55 |
| 56 #if COMPILER_SUPPORTS(CXX_STRONG_ENUMS) | |
| 57 | 56 |
| 58 #define ENUM_CLASS(__enumName) \ | 57 #define ENUM_CLASS(__enumName) \ |
| 59 enum class __enumName | 58 enum class __enumName |
| 60 | 59 |
| 61 #define ENUM_CLASS_END(__enumName) | 60 #define ENUM_CLASS_END(__enumName) |
| 62 | 61 |
| 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> MyEnu
m; | |
| 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 == oth
er.m_value; } | |
| 98 ALWAYS_INLINE bool operator!=(const EnumClass other) { return m_value != oth
er.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 <= oth
er.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 >= oth
er.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 | 62 } // namespace WTF |
| 129 | 63 |
| 130 #if !COMPILER_SUPPORTS(CXX_STRONG_ENUMS) | |
| 131 using WTF::EnumClass; | |
| 132 #endif | |
| 133 | |
| 134 #endif // WTF_EnumClass_h | 64 #endif // WTF_EnumClass_h |
| OLD | NEW |