Bug 109004 - Fix EnumClass so that it can be used with switch statements.
Summary: Fix EnumClass so that it can be used with switch statements.
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Mark Lam
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-02-05 20:19 PST by Mark Lam
Modified: 2013-02-05 20:33 PST (History)
5 users (show)

See Also:


Attachments
the fix. (2.00 KB, patch)
2013-02-05 20:28 PST, Mark Lam
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Lam 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.
Comment 1 Mark Lam 2013-02-05 20:28:53 PST
Created attachment 186751 [details]
the fix.
Comment 2 Mark Lam 2013-02-05 20:33:02 PST
Reviewed by Sam.  Landed in r141965: <http://trac.webkit.org/changeset/141965>.