Changeset 221760 in webkit


Ignore:
Timestamp:
Sep 7, 2017 2:57:18 PM (7 years ago)
Author:
achristensen@apple.com
Message:

Allow modern decoding of enums and OptionSets
https://bugs.webkit.org/show_bug.cgi?id=176480

Reviewed by Andy Estes.

  • Platform/IPC/ArgumentCoders.h:

(IPC::ArgumentCoder<OptionSet<T>>::decode):

  • Platform/IPC/Decoder.h:

(IPC::Decoder::operator>>):

  • Platform/IPC/Encoder.h:
  • Shared/WebsitePolicies.h:

(WebKit::WebsitePolicies::encode const):
(WebKit::WebsitePolicies::decode):

Location:
trunk/Source/WebKit
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r221753 r221760  
     12017-09-07  Alex Christensen  <achristensen@webkit.org>
     2
     3        Allow modern decoding of enums and OptionSets
     4        https://bugs.webkit.org/show_bug.cgi?id=176480
     5
     6        Reviewed by Andy Estes.
     7
     8        * Platform/IPC/ArgumentCoders.h:
     9        (IPC::ArgumentCoder<OptionSet<T>>::decode):
     10        * Platform/IPC/Decoder.h:
     11        (IPC::Decoder::operator>>):
     12        * Platform/IPC/Encoder.h:
     13        * Shared/WebsitePolicies.h:
     14        (WebKit::WebsitePolicies::encode const):
     15        (WebKit::WebsitePolicies::decode):
     16
    1172017-09-07  Frederic Wang  <fwang@igalia.com>
    218
  • trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h

    r221620 r221760  
    7070        return true;
    7171    }
     72
     73    static std::optional<OptionSet<T>> decode(Decoder& decoder)
     74    {
     75        std::optional<uint64_t> value;
     76        decoder >> value;
     77        if (!value)
     78            return std::nullopt;
     79        return OptionSet<T>::fromRaw(*value);
     80    }
    7281};
    7382
  • trunk/Source/WebKit/Platform/IPC/Decoder.h

    r221698 r221760  
    110110    }
    111111
     112    template<typename E, std::enable_if_t<std::is_enum<E>::value>* = nullptr>
     113    Decoder& operator>>(std::optional<E>& optional)
     114    {
     115        std::optional<uint64_t> value;
     116        *this >> value;
     117        if (value && isValidEnum<E>(*value))
     118            optional = static_cast<E>(*value);
     119        return *this;
     120    }
     121
    112122    template<typename T> bool decodeEnum(T& result)
    113123    {
  • trunk/Source/WebKit/Platform/IPC/Encoder.h

    r204977 r221760  
    6666    }
    6767
    68     template<typename T>
    69     auto encode(T&& t) -> std::enable_if_t<!std::is_enum<typename std::remove_const_t<std::remove_reference_t<T>>>::value>
     68    template<typename T, std::enable_if_t<!std::is_enum<typename std::remove_const_t<std::remove_reference_t<T>>>::value>* = nullptr>
     69    void encode(T&& t)
    7070    {
    7171        ArgumentCoder<typename std::remove_const<typename std::remove_reference<T>::type>::type>::encode(*this, std::forward<T>(t));
    7272    }
    7373
    74     template<typename T> Encoder& operator<<(T&& t)
     74    template<typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
     75    Encoder& operator<<(T&& t)
     76    {
     77        encode(static_cast<uint64_t>(t));
     78        return *this;
     79    }
     80
     81    template<typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
     82    Encoder& operator<<(T&& t)
    7583    {
    7684        encode(std::forward<T>(t));
  • trunk/Source/WebKit/Shared/WebsitePolicies.h

    r221333 r221760  
    2626#pragma once
    2727
     28#include <wtf/EnumTraits.h>
    2829#include <wtf/OptionSet.h>
    2930#include <wtf/Optional.h>
     
    5354};
    5455
     56} // namespace WebKit
     57
     58namespace WTF {
     59
     60template<> struct EnumTraits<WebKit::WebsiteAutoplayPolicy> {
     61    using values = EnumValues<
     62        WebKit::WebsiteAutoplayPolicy,
     63        WebKit::WebsiteAutoplayPolicy::Default,
     64        WebKit::WebsiteAutoplayPolicy::Allow,
     65        WebKit::WebsiteAutoplayPolicy::AllowWithoutSound,
     66        WebKit::WebsiteAutoplayPolicy::Deny
     67    >;
     68};
     69
     70} // namespace WTF
     71
     72namespace WebKit {
     73
    5574template<class Encoder> void WebsitePolicies::encode(Encoder& encoder) const
    5675{
    5776    encoder << contentBlockersEnabled;
    58     encoder.encodeEnum(autoplayPolicy);
     77    encoder << autoplayPolicy;
    5978    encoder << allowedAutoplayQuirks;
    6079}
     
    6281template<class Decoder> std::optional<WebsitePolicies> WebsitePolicies::decode(Decoder& decoder)
    6382{
    64     bool contentBlockersEnabled;
    65     if (!decoder.decode(contentBlockersEnabled))
     83    std::optional<bool> contentBlockersEnabled;
     84    decoder >> contentBlockersEnabled;
     85    if (!contentBlockersEnabled)
    6686        return std::nullopt;
    6787   
    68     WebsiteAutoplayPolicy autoplayPolicy;
    69     if (!decoder.decodeEnum(autoplayPolicy))
     88    std::optional<WebsiteAutoplayPolicy> autoplayPolicy;
     89    decoder >> autoplayPolicy;
     90    if (!autoplayPolicy)
    7091        return std::nullopt;
    7192
    72     OptionSet<WebsiteAutoplayQuirk> allowedAutoplayQuirks;
    73     if (!decoder.decode(allowedAutoplayQuirks))
     93    std::optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
     94    decoder >> allowedAutoplayQuirks;
     95    if (!allowedAutoplayQuirks)
    7496        return std::nullopt;
    7597
    76     return { { contentBlockersEnabled, allowedAutoplayQuirks, autoplayPolicy } };
     98    return { {
     99        WTFMove(*contentBlockersEnabled),
     100        WTFMove(*allowedAutoplayQuirks),
     101        WTFMove(*autoplayPolicy),
     102    } };
    77103}
    78104
Note: See TracChangeset for help on using the changeset viewer.