Bug 108533 - Abstraction for hiding enum class
Summary: Abstraction for hiding enum class
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Mark Lam
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-01-31 14:49 PST by Mark Lam
Modified: 2013-01-31 15:52 PST (History)
6 users (show)

See Also:


Attachments
the patch. (14.42 KB, patch)
2013-01-31 15:18 PST, Mark Lam
andersca: review+
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-01-31 14:49:02 PST
In https://bugs.webkit.org/show_bug.cgi?id=108279, I introduced a TypeSafeEnum template to do the job of "enum class" because we cannot require C++11 support yet.  We should provide an abstraction so that we can use the C++ enum class when available.

The proposed solution is to introduce 2 macros: ENUM_CLASS_BEGIN() and ENUM_CLASS_END().  With this approach, the following:

     enum class MyEnums {
         Value1,
         Value2,
         ...
         ValueN
     };

 ... will be expressed like this:

     ENUM_CLASS_BEGIN(MyEnums) {
         Value1,
         Value2,
         ...
         ValueN
     } ENUM_CLASS_END(MyEnums);
Comment 1 Mark Lam 2013-01-31 15:18:49 PST
Created attachment 185879 [details]
the patch.
Comment 2 WebKit Review Bot 2013-01-31 15:20:42 PST
Attachment 185879 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'Source/WTF/ChangeLog', u'Source/WTF/wtf/Compiler.h', u'Source/WTF/wtf/EnumClass.h', u'Source/WTF/wtf/TypeSafeEnum.h', u'Source/WebCore/ChangeLog', u'Source/WebCore/Modules/webdatabase/DatabaseError.h']" exit_code: 1
Source/WTF/wtf/EnumClass.h:114:  enum members should use InterCaps with an initial capital letter.  [readability/enum_casing] [4]
Source/WTF/wtf/EnumClass.h:121:  enum members should use InterCaps with an initial capital letter.  [readability/enum_casing] [4]
Source/WebCore/Modules/webdatabase/DatabaseError.h:35:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 3 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Mark Lam 2013-01-31 15:22:41 PST
Comment on attachment 185879 [details]
the patch.

The style checker is mistaking my EnumClass name as an enum, and its complaint is invalid.  May I have a review please?  Thanks.
Comment 4 Anders Carlsson 2013-01-31 15:33:37 PST
Comment on attachment 185879 [details]
the patch.

View in context: https://bugs.webkit.org/attachment.cgi?id=185879&action=review

> Source/WTF/wtf/EnumClass.h:92
> +    ALWAYS_INLINE EnumClass() : m_value(static_cast<Value>(0)) { }
> +    ALWAYS_INLINE EnumClass(Value value) : m_value(value) { }

Thinking some more about this, I don't think it's a good idea to have a default value. 

In C++11, MyEnum value1 would just get a random value, and we don't want that to differ between C++11 and C++98.
Comment 5 Mark Lam 2013-01-31 15:52:06 PST
(In reply to comment #4)
> (From update of attachment 185879 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=185879&action=review
> 
> > Source/WTF/wtf/EnumClass.h:92
> > +    ALWAYS_INLINE EnumClass() : m_value(static_cast<Value>(0)) { }
> > +    ALWAYS_INLINE EnumClass(Value value) : m_value(value) { }
> 
> Thinking some more about this, I don't think it's a good idea to have a default value. 
> 
> In C++11, MyEnum value1 would just get a random value, and we don't want that to differ between C++11 and C++98.

Thanks for the review.  Changed the default constructor to not initialize the value.  Landed in r141499: <http://trac.webkit.org/changeset/141499>.