⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 271358 in webkit


Ignore:
Timestamp:
Jan 11, 2021, 5:04:27 AM (6 years ago)
Author:
commit-queue@webkit.org
Message:

WebKit::IPC::Encoder needs definitions of all custom enum values at the Encoder definition time
​https://bugs.webkit.org/show_bug.cgi?id=220410

Patch by Kimmo Kinnunen <​kkinnunen@apple.com> on 2021-01-11
Reviewed by Darin Adler.

Change WTF::EnumTraits to have either EnumTraits::values or EnumTraits::isValidEnum().
Source/WebCore:

Change here needed to conform to the new protocol. Fixes an issue with the template usage
in WebKit::IPC::Encoder, see WebKit and WTF ChangeLogs for discussion.

  • platform/ContextMenuItem.h:

(WTF::EnumTraits<WebCore::ContextMenuAction>::isValidEnum):

Source/WebKit:

Fixes the upcoming problem where Encoder::encode(E) function is moved from Encoder
to other class, such as EncoderBase, and where we do not want to add redundant includes.
See the WTF/ ChangeLog for discussiong wrt why they were needed before.

  • Scripts/webkit/messages.py:
  • Scripts/webkit/tests/MessageNames.h:

(WTF::EnumTraits<IPC::MessageName>::isValidEnum):

Source/WTF:

Previously, if clients wanted to check isValidEnum for custom enum, they had to:

1) Define HasCustomIsValidEnum<TheirEnum> : true_type {};
2) Define new function template isValidEnum() for TheirEnum
3) Ensure that their isValidEnum() was defined before the definition of the call to

the isValidEnum().

This has the problem that isValidEnum() cannot be called in generic code, because
at the definition time generic code typically does not have all the types in scope
that will be used at instantiation time.
Each isValidEnum() is their own function template, C++ does not have function template
specialization.

After the change, clients need to:

1) Define EnumTraits<TheirEnum> { bool isValidEnum(...) }

Fix by using the fact that WTF::EnumTraits is a class and that can have template
specializations.

Consistent with the EnumTraits::values case:

a) Automatic case? --> Define EnumTraits::values
b) Manual/custom case? --> Define EnumTraits::isValidEnum()

  • wtf/EnumTraits.h:

(WTF::isValidEnum):

Location:
trunk/Source
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r271332 r271358  
     12021-01-11  Kimmo Kinnunen  <kkinnunen@apple.com>
     2
     3        WebKit::IPC::Encoder needs definitions of all custom enum values at the Encoder definition time
     4        https://bugs.webkit.org/show_bug.cgi?id=220410
     5
     6        Reviewed by Darin Adler.
     7
     8        Change WTF::EnumTraits to have either EnumTraits::values or EnumTraits::isValidEnum().
     9        Previously, if clients wanted to check isValidEnum for custom enum, they had to:
     10         1) Define HasCustomIsValidEnum<TheirEnum> : true_type {};
     11         2) Define new function template isValidEnum() for TheirEnum
     12         3) Ensure that their isValidEnum() was defined before the definition of the call to
     13            the isValidEnum().
     14
     15        This has the problem that isValidEnum() cannot be called in generic code, because
     16        at the definition time generic code typically does not have all the types in scope
     17        that will be used at instantiation time.
     18        Each isValidEnum() is their own function template, C++ does not have function template
     19        specialization.
     20
     21        After the change, clients need to:
     22         1) Define EnumTraits<TheirEnum> { bool isValidEnum(...) }
     23
     24        Fix by using the fact that WTF::EnumTraits is a class and that can have template
     25        specializations.
     26
     27        Consistent with the EnumTraits::values case:
     28          a) Automatic case?     --> Define EnumTraits::values
     29          b) Manual/custom case? --> Define EnumTraits::isValidEnum()
     30
     31        * wtf/EnumTraits.h:
     32        (WTF::isValidEnum):
     33
    1342021-01-08  Yusuke Suzuki  <ysuzuki@apple.com>
    235
  • trunk/Source/WTF/wtf/EnumTraits.h

    r262092 r271358  
    5252};
    5353
    54 template<typename U> class HasCustomIsValidEnum : public std::false_type { };
    55 
    56 template<typename E, typename T, std::enable_if_t<std::is_enum<E>::value && !std::is_same<std::underlying_type_t<E>, bool>::value && !HasCustomIsValidEnum<E>::value>* = nullptr>
    57 constexpr bool isValidEnum(T t)
     54template<typename E, typename T, typename = std::enable_if_t<!std::is_same_v<std::underlying_type_t<E>, bool>>>
     55constexpr auto isValidEnum(T t) -> decltype(EnumValueChecker<T, typename EnumTraits<E>::values>::isValidEnum(t), bool())
    5856{
    5957    static_assert(sizeof(T) >= sizeof(std::underlying_type_t<E>), "Integral type must be at least the size of the underlying enum type");
    … …  
    6260}
    6361
    64 template<typename E, typename T, std::enable_if_t<std::is_same<std::underlying_type_t<E>, bool>::value>* = nullptr>
     62template<typename E, typename T, typename = std::enable_if_t<!std::is_same_v<std::underlying_type_t<E>, bool>>>
     63auto isValidEnum(T t) -> decltype(EnumTraits<E>::isValidEnum(t), bool())
     64{
     65    return EnumTraits<E>::isValidEnum(t);
     66}
     67
     68template<typename E, typename T, typename = std::enable_if_t<std::is_same_v<std::underlying_type_t<E>, bool>>>
    6569constexpr bool isValidEnum(T t)
    6670{
  • trunk/Source/WebCore/ChangeLog

    r271357 r271358  
     12021-01-11  Kimmo Kinnunen  <kkinnunen@apple.com>
     2
     3        WebKit::IPC::Encoder needs definitions of all custom enum values at the Encoder definition time
     4        https://bugs.webkit.org/show_bug.cgi?id=220410
     5
     6        Reviewed by Darin Adler.
     7
     8        Change WTF::EnumTraits to have either EnumTraits::values or EnumTraits::isValidEnum().
     9
     10        Change here needed to conform to the new protocol. Fixes an issue with the template usage
     11        in WebKit::IPC::Encoder, see WebKit and WTF ChangeLogs for discussion.
     12
     13        * platform/ContextMenuItem.h:
     14        (WTF::EnumTraits<WebCore::ContextMenuAction>::isValidEnum):
     15
    1162021-01-07  Sergio Villar Senin  <svillar@igalia.com>
    217
  • trunk/Source/WebCore/platform/ContextMenuItem.h

    r270974 r271358  
    209209
    210210template<>
    211 class HasCustomIsValidEnum<WebCore::ContextMenuAction> : public std::true_type { };
    212 
    213 template<typename E, typename T, std::enable_if_t<std::is_same_v<E, WebCore::ContextMenuAction>>* = nullptr>
    214 bool isValidEnum(T action)
    215 {
    216     static_assert(sizeof(T) == sizeof(E), "isValidEnum<WebCore::ContextMenuAction> should only be called with 32-bit types");
    217     return WebCore::isValidContextMenuAction(static_cast<E>(action));
     211struct EnumTraits<WebCore::ContextMenuAction> {
     212    template<typename T>
     213    static std::enable_if_t<sizeof(T) == sizeof(WebCore::ContextMenuAction), bool> isValidEnum(T action)
     214    {
     215        return WebCore::isValidContextMenuAction(static_cast<WebCore::ContextMenuAction>(action));
     216    };
    218217};
    219218
  • trunk/Source/WebKit/ChangeLog

    r271354 r271358  
     12021-01-11  Kimmo Kinnunen  <kkinnunen@apple.com>
     2
     3        WebKit::IPC::Encoder needs definitions of all custom enum values at the Encoder definition time
     4        https://bugs.webkit.org/show_bug.cgi?id=220410
     5
     6        Reviewed by Darin Adler.
     7
     8        Change WTF::EnumTraits to have either EnumTraits::values or EnumTraits::isValidEnum().
     9
     10        Fixes the upcoming problem where Encoder::encode(E) function is moved from Encoder
     11        to other class, such as EncoderBase, and where we do not want to add redundant includes.
     12        See the WTF/ ChangeLog for discussiong wrt why they were needed before.
     13
     14        * Scripts/webkit/messages.py:
     15        * Scripts/webkit/tests/MessageNames.h:
     16        (WTF::EnumTraits<IPC::MessageName>::isValidEnum):
     17
    1182021-01-11  Carlos Garcia Campos  <cgarcia@igalia.com>
    219
  • trunk/Source/WebKit/Scripts/webkit/messages.py

    r271243 r271358  
    989989    result.append('\n')
    990990    result.append('template<>\n')
    991     result.append('class HasCustomIsValidEnum<IPC::MessageName> : public std::true_type { };\n')
    992     result.append('template<typename E, typename T, std::enable_if_t<std::is_same_v<E, IPC::MessageName>>* = nullptr>\n')
    993     result.append('bool isValidEnum(T messageName)\n')
    994     result.append('{\n')
    995     result.append('    static_assert(sizeof(T) == sizeof(E), "isValidEnum<IPC::MessageName> should only be called with 16-bit types");\n')
    996     result.append('    static_assert(std::is_unsigned<T>::value, "isValidEnum<IPC::MessageName> should only be called with unsigned types");\n')
    997     result.append('    if (messageName > static_cast<std::underlying_type<IPC::MessageName>::type>(IPC::MessageName::Last))\n')
    998     result.append('        return false;\n')
    999     result.append('    return IPC::isValidMessageName(static_cast<E>(messageName));\n')
     991    result.append('struct EnumTraits<IPC::MessageName> {\n')
     992    result.append('    template<typename T>\n')
     993    result.append('    static std::enable_if_t<sizeof(T) == sizeof(IPC::MessageName) && std::is_unsigned_v<T>, bool> isValidEnum(T messageName)\n')
     994    result.append('    {\n')
     995    result.append('        if (messageName > WTF::enumToUnderlyingType(IPC::MessageName::Last))\n')
     996    result.append('            return false;\n')
     997    result.append('        return IPC::isValidMessageName(static_cast<IPC::MessageName>(messageName));\n')
     998    result.append('    }\n')
    1000999    result.append('};\n')
    10011000    result.append('\n')
  • trunk/Source/WebKit/Scripts/webkit/tests/MessageNames.h

    r271243 r271358  
    116116
    117117template<>
    118 class HasCustomIsValidEnum<IPC::MessageName> : public std::true_type { };
    119 template<typename E, typename T, std::enable_if_t<std::is_same_v<E, IPC::MessageName>>* = nullptr>
    120 bool isValidEnum(T messageName)
    121 {
    122     static_assert(sizeof(T) == sizeof(E), "isValidEnum<IPC::MessageName> should only be called with 16-bit types");
    123     static_assert(std::is_unsigned<T>::value, "isValidEnum<IPC::MessageName> should only be called with unsigned types");
    124     if (messageName > static_cast<std::underlying_type<IPC::MessageName>::type>(IPC::MessageName::Last))
    125         return false;
    126     return IPC::isValidMessageName(static_cast<E>(messageName));
     118struct EnumTraits<IPC::MessageName> {
     119    template<typename T>
     120    static std::enable_if_t<sizeof(T) == sizeof(IPC::MessageName) && std::is_unsigned_v<T>, bool> isValidEnum(T messageName)
     121    {
     122        if (messageName > WTF::enumToUnderlyingType(IPC::MessageName::Last))
     123            return false;
     124        return IPC::isValidMessageName(static_cast<IPC::MessageName>(messageName));
     125    }
    127126};
    128127
Note: See TracChangeset for help on using the changeset viewer.