RESOLVED FIXED 109004
Fix EnumClass so that it can be used with switch statements.
https://bugs.webkit.org/show_bug.cgi?id=109004
Summary Fix EnumClass so that it can be used with switch statements.
Mark Lam
Reported 2013-02-05 20:19:11 PST
When we use the ENUM_CLASS() macro on platforms that don't support C++11 strong enums, ENUM_CLASS will be implemented using the EnumClass template. Currently, if we declare an enum list like this (without C++11 support): ENUM_CLASS(MyEnum) { Enum1, Enum2 } ENUM_CLASS_END(MyEnum); ... and we use it like this: void foo(MyEnum value) { switch(value) { // <========== Compilation error. case Enum1: // do something. case Enum2: // do something else. } } In this example, the switch statement will result in a compilation error. To fix this, EnumClass needs to add a cast operator for Value (where Value is the type of the enumerated values): operator Value() { return m_value; } In addition, we also need to add the following comparison operators: bool operator==(const Value value) { return m_value == value; } bool operator!=(const Value value) { return m_value != value; } bool operator<(const Value value) { return m_value < value; } bool operator<=(const Value value) { return m_value <= value; } bool operator>(const Value value) { return m_value > value; } bool operator>=(const Value value) { return m_value >= value; } Note: the EnumClass template already has comparison operators for comparing against another EnumClass. It also has a conversion constructor that takes a Value argument. The reason for adding these comparison operators that compare against a Value is because when an instance of EnumClass is compared against a Value (or an int), there is an ambiguity in terms of how the C++ compiler can realize that comparison. Here are the 2 ways: 1. Convert the EnumClass to a Value (i.e. int) using the cast operator, and then do an int comparison. 2. Convert the other int value int an EnumClass instance, and then use one of the pre-existing comparison operators to do the comparison. By adding these comparison operators that take a Value argument, we provide an explicit approach to compare between an EnumClass and a Value, and therefore removed the ambiguity above.
Attachments
the fix. (2.00 KB, patch)
2013-02-05 20:28 PST, Mark Lam
no flags
Mark Lam
Comment 1 2013-02-05 20:28:53 PST
Created attachment 186751 [details] the fix.
Mark Lam
Comment 2 2013-02-05 20:33:02 PST
Reviewed by Sam. Landed in r141965: <http://trac.webkit.org/changeset/141965>.
Note You need to log in before you can comment on or make changes to this bug.