Changeset 231223 in webkit


Ignore:
Timestamp:
May 1, 2018 9:32:33 PM (6 years ago)
Author:
Yusuke Suzuki
Message:

Use default std::optional if it is provided
https://bugs.webkit.org/show_bug.cgi?id=185159

Reviewed by JF Bastien.

Source/WebCore:

  • Modules/mediastream/RTCPeerConnection.cpp:

(WebCore::iceServersFromConfiguration):
(WebCore::RTCPeerConnection::setConfiguration):

  • css/parser/CSSParser.cpp:

(WebCore::CSSParser::parseSystemColor):

  • css/parser/CSSParser.h:
  • dom/DatasetDOMStringMap.cpp:

(WebCore::DatasetDOMStringMap::item const):
(WebCore::DatasetDOMStringMap::namedItem const):
(WebCore:: const): Deleted.

  • dom/DatasetDOMStringMap.h:
  • dom/Element.cpp:

(WebCore::Element::insertAdjacentHTML):

  • dom/Element.h:
  • inspector/DOMEditor.cpp:
  • platform/network/curl/CurlFormDataStream.cpp:

(WebCore::CurlFormDataStream::getPostData):
(): Deleted.

  • platform/network/curl/CurlFormDataStream.h:
  • testing/MockCDMFactory.cpp:

(WebCore::MockCDMFactory::keysForSessionWithID const):
(WebCore::MockCDMInstance::updateLicense):
(WebCore:: const): Deleted.

  • testing/MockCDMFactory.h:

Source/WebKit:

  • Shared/SandboxExtension.h:

(WebKit::SandboxExtension::Handle::decode):

  • Shared/TouchBarMenuItemData.cpp:

(WebKit::TouchBarMenuItemData::decode):

Source/WTF:

Now C++17 flag is enabled. It means that any standard libraries can use <optional> internally.
If we define std::optional regardless of the existence of the standard library's <optional>,
it causes compile errors. For example, in GCC 7 (specifically GCC 7.3.0) environment,
<optional> is included in <unordered_map>.
We do not define std::optional in WebKit side if <optional> is offered.

And we also remove std::optional<T&> use since this is not accepted in C++17. Use
std::optional<std::reference_wrapper<T>> instead.

  • wtf/Expected.h:

constexpr does not mean const in C++17.

  • wtf/Optional.h:

Do not define std::optional if <optional> is provided.

(WTF::valueOrCompute):

Location:
trunk/Source
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r231197 r231223  
     12018-05-01  Yusuke Suzuki  <utatane.tea@gmail.com>
     2
     3        Use default std::optional if it is provided
     4        https://bugs.webkit.org/show_bug.cgi?id=185159
     5
     6        Reviewed by JF Bastien.
     7
     8        Now C++17 flag is enabled. It means that any standard libraries can use <optional> internally.
     9        If we define std::optional regardless of the existence of the standard library's <optional>,
     10        it causes compile errors. For example, in GCC 7 (specifically GCC 7.3.0) environment,
     11        <optional> is included in <unordered_map>.
     12        We do not define std::optional in WebKit side if <optional> is offered.
     13
     14        And we also remove std::optional<T&> use since this is not accepted in C++17. Use
     15        std::optional<std::reference_wrapper<T>> instead.
     16
     17        * wtf/Expected.h:
     18        constexpr does not mean const in C++17.
     19
     20        * wtf/Optional.h:
     21        Do not define std::optional if <optional> is provided.
     22
     23        (WTF::valueOrCompute):
     24
    1252018-05-01  Robin Morisset  <rmorisset@apple.com>
    226
  • trunk/Source/WTF/wtf/Expected.h

    r225728 r231223  
    574574}}} // namespace std::experimental::fundamentals_v3
    575575
    576 __EXPECTED_INLINE_VARIABLE constexpr std::experimental::unexpected_t& unexpect = std::experimental::unexpect;
     576__EXPECTED_INLINE_VARIABLE constexpr auto& unexpect = std::experimental::unexpect;
    577577template<class T, class E> using Expected = std::experimental::expected<T, E>;
  • trunk/Source/WTF/wtf/Optional.h

    r230113 r231223  
    4747# include <wtf/Compiler.h>
    4848# include <wtf/StdLibExtras.h>
     49
     50#if !COMPILER(MSVC) && __has_include(<optional>)
     51# include <optional>
     52#endif
     53
     54#if !COMPILER(MSVC) && defined(__cpp_lib_optional) && __cpp_lib_optional >= 201603
     55
     56// Use default std::optional.
     57
     58#else
    4959
    5060# define TR2_OPTIONAL_REQUIRES(...) typename std::enable_if<__VA_ARGS__::value, bool>::type = false
     
    10131023} // namespace std
    10141024
     1025namespace std
     1026{
     1027  template <typename T>
     1028  struct hash<std::optional<T>>
     1029  {
     1030    typedef typename hash<T>::result_type result_type;
     1031    typedef std::optional<T> argument_type;
     1032
     1033    constexpr result_type operator()(argument_type const& arg) const {
     1034      return arg ? std::hash<T>{}(*arg) : result_type{};
     1035    }
     1036  };
     1037
     1038  template <typename T>
     1039  struct hash<std::optional<T&>>
     1040  {
     1041    typedef typename hash<T>::result_type result_type;
     1042    typedef std::optional<T&> argument_type;
     1043
     1044    constexpr result_type operator()(argument_type const& arg) const {
     1045      return arg ? std::hash<T>{}(*arg) : result_type{};
     1046    }
     1047  };
     1048}
     1049
     1050# undef TR2_OPTIONAL_REQUIRES
     1051# undef TR2_OPTIONAL_ASSERTED_EXPRESSION
     1052
     1053#endif // defined(__cpp_lib_optional)
     1054
    10151055namespace WTF {
    10161056
     
    10271067} // namespace WTF
    10281068
    1029 namespace std
    1030 {
    1031   template <typename T>
    1032   struct hash<std::optional<T>>
    1033   {
    1034     typedef typename hash<T>::result_type result_type;
    1035     typedef std::optional<T> argument_type;
    1036 
    1037     constexpr result_type operator()(argument_type const& arg) const {
    1038       return arg ? std::hash<T>{}(*arg) : result_type{};
    1039     }
    1040   };
    1041 
    1042   template <typename T>
    1043   struct hash<std::optional<T&>>
    1044   {
    1045     typedef typename hash<T>::result_type result_type;
    1046     typedef std::optional<T&> argument_type;
    1047 
    1048     constexpr result_type operator()(argument_type const& arg) const {
    1049       return arg ? std::hash<T>{}(*arg) : result_type{};
    1050     }
    1051   };
    1052 }
    1053 
    1054 # undef TR2_OPTIONAL_REQUIRES
    1055 # undef TR2_OPTIONAL_ASSERTED_EXPRESSION
    1056 
    10571069using WTF::valueOrCompute;
  • trunk/Source/WebCore/ChangeLog

    r231221 r231223  
     12018-05-01  Yusuke Suzuki  <utatane.tea@gmail.com>
     2
     3        Use default std::optional if it is provided
     4        https://bugs.webkit.org/show_bug.cgi?id=185159
     5
     6        Reviewed by JF Bastien.
     7
     8        * Modules/mediastream/RTCPeerConnection.cpp:
     9        (WebCore::iceServersFromConfiguration):
     10        (WebCore::RTCPeerConnection::setConfiguration):
     11        * css/parser/CSSParser.cpp:
     12        (WebCore::CSSParser::parseSystemColor):
     13        * css/parser/CSSParser.h:
     14        * dom/DatasetDOMStringMap.cpp:
     15        (WebCore::DatasetDOMStringMap::item const):
     16        (WebCore::DatasetDOMStringMap::namedItem const):
     17        (WebCore:: const): Deleted.
     18        * dom/DatasetDOMStringMap.h:
     19        * dom/Element.cpp:
     20        (WebCore::Element::insertAdjacentHTML):
     21        * dom/Element.h:
     22        * inspector/DOMEditor.cpp:
     23        * platform/network/curl/CurlFormDataStream.cpp:
     24        (WebCore::CurlFormDataStream::getPostData):
     25        (): Deleted.
     26        * platform/network/curl/CurlFormDataStream.h:
     27        * testing/MockCDMFactory.cpp:
     28        (WebCore::MockCDMFactory::keysForSessionWithID const):
     29        (WebCore::MockCDMInstance::updateLicense):
     30        (WebCore:: const): Deleted.
     31        * testing/MockCDMFactory.h:
     32
    1332018-05-01  Chris Dumez  <cdumez@apple.com>
    234
  • trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp

    r229645 r231223  
    302302
    303303// Implementation of https://w3c.github.io/webrtc-pc/#set-pc-configuration
    304 static inline ExceptionOr<Vector<MediaEndpointConfiguration::IceServerInfo>> iceServersFromConfiguration(RTCConfiguration& newConfiguration, std::optional<const RTCConfiguration&> existingConfiguration, bool isLocalDescriptionSet)
    305 {
    306     if (existingConfiguration && newConfiguration.bundlePolicy != existingConfiguration->bundlePolicy)
     304static inline ExceptionOr<Vector<MediaEndpointConfiguration::IceServerInfo>> iceServersFromConfiguration(RTCConfiguration& newConfiguration, std::optional<std::reference_wrapper<const RTCConfiguration>> existingConfiguration, bool isLocalDescriptionSet)
     305{
     306    if (existingConfiguration && newConfiguration.bundlePolicy != existingConfiguration->get().bundlePolicy)
    307307        return Exception { InvalidModificationError, "IceTransportPolicy does not match existing policy" };
    308308
    309     if (existingConfiguration && newConfiguration.iceCandidatePoolSize != existingConfiguration->iceCandidatePoolSize && isLocalDescriptionSet)
     309    if (existingConfiguration && newConfiguration.iceCandidatePoolSize != existingConfiguration->get().iceCandidatePoolSize && isLocalDescriptionSet)
    310310        return Exception { InvalidModificationError, "IceTransportPolicy pool size does not match existing pool size" };
    311311
     
    361361    INFO_LOG(LOGIDENTIFIER);
    362362
    363     auto servers = iceServersFromConfiguration(configuration, m_configuration, m_backend->isLocalDescriptionSet());
     363    auto servers = iceServersFromConfiguration(configuration, std::cref(m_configuration), m_backend->isLocalDescriptionSet());
    364364    if (servers.hasException())
    365365        return servers.releaseException();
  • trunk/Source/WebCore/css/parser/CSSParser.cpp

    r230978 r231223  
    179179}
    180180
    181 Color CSSParser::parseSystemColor(const String& string, std::optional<const CSSParserContext&> context)
     181Color CSSParser::parseSystemColor(const String& string, std::optional<std::reference_wrapper<const CSSParserContext>> context)
    182182{
    183183    CSSValueID id = cssValueKeywordID(string);
     
    186186
    187187    OptionSet<StyleColor::Options> options;
    188     if (context && context.value().useSystemAppearance)
     188    if (context && context->get().useSystemAppearance)
    189189        options |= StyleColor::Options::UseSystemAppearance;
    190190    return RenderTheme::singleton().systemColor(id, options);
  • trunk/Source/WebCore/css/parser/CSSParser.h

    r229448 r231223  
    7979
    8080    static Color parseColor(const String&, bool strict = false);
    81     static Color parseSystemColor(const String&, std::optional<const CSSParserContext&>);
     81    static Color parseSystemColor(const String&, std::optional<std::reference_wrapper<const CSSParserContext>>);
    8282
    8383private:
  • trunk/Source/WebCore/dom/DatasetDOMStringMap.cpp

    r219856 r231223  
    189189}
    190190
    191 std::optional<const AtomicString&> DatasetDOMStringMap::item(const String& propertyName) const
     191std::optional<std::reference_wrapper<const AtomicString>> DatasetDOMStringMap::item(const String& propertyName) const
    192192{
    193193    if (m_element.hasAttributes()) {
     
    199199            const Attribute& attribute = *attributeIteratorAccessor.begin();
    200200            if (propertyNameMatchesAttributeName(propertyName, attribute.localName()))
    201                 return attribute.value();
     201                return std::cref(attribute.value());
    202202        } else {
    203203            AtomicString attributeName = convertPropertyNameToAttributeName(propertyName);
    204204            for (const Attribute& attribute : attributeIteratorAccessor) {
    205205                if (attribute.localName() == attributeName)
    206                     return attribute.value();
     206                    return std::cref(attribute.value());
    207207            }
    208208        }
     
    214214String DatasetDOMStringMap::namedItem(const AtomicString& name) const
    215215{
    216     return item(name).value_or(String { });
     216    if (auto value = item(name))
     217        return value->get();
     218    return String { };
    217219}
    218220
  • trunk/Source/WebCore/dom/DatasetDOMStringMap.h

    r217773 r231223  
    5454
    5555private:
    56     std::optional<const AtomicString&> item(const String& name) const;
     56    std::optional<std::reference_wrapper<const AtomicString>> item(const String& name) const;
    5757
    5858    Element& m_element;
  • trunk/Source/WebCore/dom/Element.cpp

    r231114 r231223  
    37343734
    37353735// https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml
    3736 ExceptionOr<void> Element::insertAdjacentHTML(const String& where, const String& markup, std::optional<NodeVector&> addedNodes)
     3736ExceptionOr<void> Element::insertAdjacentHTML(const String& where, const String& markup, std::optional<std::reference_wrapper<NodeVector>> addedNodes)
    37373737{
    37383738    // Steps 1 and 2.
     
    37483748        // Must be called before insertAdjacent, as otherwise the children of fragment will be moved
    37493749        // to their new parent and will be harder to keep track of.
    3750         *addedNodes = collectChildNodes(fragment.returnValue());
     3750        addedNodes->get() = collectChildNodes(fragment.returnValue());
    37513751    }
    37523752
  • trunk/Source/WebCore/dom/Element.h

    r230229 r231223  
    315315    virtual RefPtr<Element> focusDelegate();
    316316
    317     ExceptionOr<void> insertAdjacentHTML(const String& where, const String& html, std::optional<NodeVector&> addedNodes);
     317    ExceptionOr<void> insertAdjacentHTML(const String& where, const String& html, std::optional<std::reference_wrapper<NodeVector>> addedNodes);
    318318
    319319    WEBCORE_EXPORT ExceptionOr<Element*> insertAdjacentElement(const String& where, Element& newChild);
  • trunk/Source/WebCore/inspector/DOMEditor.cpp

    r224648 r231223  
    267267    ExceptionOr<void> redo() final
    268268    {
    269         auto result = m_element->insertAdjacentHTML(m_position, m_html, m_addedNodes);
     269        auto result = m_element->insertAdjacentHTML(m_position, m_html, std::ref(m_addedNodes));
    270270        if (result.hasException())
    271271            return result.releaseException();
  • trunk/Source/WebCore/platform/network/curl/CurlFormDataStream.cpp

    r226656 r231223  
    7070}
    7171
    72 std::optional<const Vector<char>&> CurlFormDataStream::getPostData()
     72std::optional<std::reference_wrapper<const Vector<char>>> CurlFormDataStream::getPostData()
    7373{
    7474    if (!m_formData)
     
    7878        m_postData = std::make_unique<Vector<char>>(m_formData->flatten());
    7979
    80     return *m_postData;
     80    return std::cref(*m_postData);
    8181}
    8282
  • trunk/Source/WebCore/platform/network/curl/CurlFormDataStream.h

    r230229 r231223  
    4242    size_t elementSize() { return m_formData ? m_formData->elements().size() : 0; }
    4343
    44     std::optional<const Vector<char>&> getPostData();
     44    std::optional<std::reference_wrapper<const Vector<char>>> getPostData();
    4545    bool shouldUseChunkTransfer();
    4646    unsigned long long totalSize();
  • trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp

    r230973 r231223  
    491491    if (elementSize == 1) {
    492492        auto postData = m_formDataStream.getPostData();
    493         if (postData && postData->size())
    494             m_curlHandle->setPostFields(postData->data(), postData->size());
     493        if (postData && postData->get().size())
     494            m_curlHandle->setPostFields(postData->get().data(), postData->get().size());
    495495    } else
    496496        setupSendData(false);
  • trunk/Source/WebCore/testing/MockCDMFactory.cpp

    r228218 r231223  
    8282}
    8383
    84 std::optional<const Vector<Ref<SharedBuffer>>&> MockCDMFactory::keysForSessionWithID(const String& id) const
     84std::optional<std::reference_wrapper<const Vector<Ref<SharedBuffer>>>> MockCDMFactory::keysForSessionWithID(const String& id) const
    8585{
    8686    auto it = m_sessions.find(id);
    8787    if (it == m_sessions.end())
    8888        return std::nullopt;
    89     return it->value;
     89    return std::cref(it->value);
    9090}
    9191
     
    315315    std::optional<KeyStatusVector> changedKeys;
    316316    if (responseVector.contains(String(ASCIILiteral("keys-changed")))) {
    317         std::optional<const Vector<Ref<SharedBuffer>>&> keys = factory->keysForSessionWithID(sessionID);
     317        std::optional<std::reference_wrapper<const Vector<Ref<SharedBuffer>>>> keys = factory->keysForSessionWithID(sessionID);
    318318        if (keys) {
    319319            KeyStatusVector keyStatusVector;
    320             keyStatusVector.reserveInitialCapacity(keys->size());
    321             for (auto& key : *keys)
     320            keyStatusVector.reserveInitialCapacity(keys->get().size());
     321            for (auto& key : (*keys).get())
    322322                keyStatusVector.uncheckedAppend({ key.copyRef(), KeyStatus::Usable });
    323323
  • trunk/Source/WebCore/testing/MockCDMFactory.h

    r224707 r231223  
    7474    void removeSessionWithID(const String& id) { m_sessions.remove(id); }
    7575    void addKeysToSessionWithID(const String& id, Vector<Ref<SharedBuffer>>&&);
    76     std::optional<const Vector<Ref<SharedBuffer>>&> keysForSessionWithID(const String& id) const;
     76    std::optional<std::reference_wrapper<const Vector<Ref<SharedBuffer>>>> keysForSessionWithID(const String& id) const;
    7777    Vector<Ref<SharedBuffer>> removeKeysFromSessionWithID(const String& id);
    7878
  • trunk/Source/WebKit/ChangeLog

    r231215 r231223  
     12018-05-01  Yusuke Suzuki  <utatane.tea@gmail.com>
     2
     3        Use default std::optional if it is provided
     4        https://bugs.webkit.org/show_bug.cgi?id=185159
     5
     6        Reviewed by JF Bastien.
     7
     8        * Shared/SandboxExtension.h:
     9        (WebKit::SandboxExtension::Handle::decode):
     10        * Shared/TouchBarMenuItemData.cpp:
     11        (WebKit::TouchBarMenuItemData::decode):
     12
    1132018-05-01  Jer Noble  <jer.noble@apple.com>
    214
  • trunk/Source/WebKit/Shared/SandboxExtension.h

    r229309 r231223  
    121121inline SandboxExtension::Handle::~Handle() { }
    122122inline void SandboxExtension::Handle::encode(IPC::Encoder&) const { }
    123 inline std::optional<SandboxExtension::Handle> SandboxExtension::Handle::decode(IPC::Decoder&) { return {{ }}; }
     123inline std::optional<SandboxExtension::Handle> SandboxExtension::Handle::decode(IPC::Decoder&) { return SandboxExtension::Handle { }; }
    124124inline SandboxExtension::HandleArray::HandleArray() { }
    125125inline SandboxExtension::HandleArray::~HandleArray() { }
  • trunk/Source/WebKit/Shared/TouchBarMenuItemData.cpp

    r225505 r231223  
    6666        return std::nullopt;
    6767   
    68     return WTFMove(result);
     68    return std::make_optional(WTFMove(result));
    6969}
    7070
Note: See TracChangeset for help on using the changeset viewer.