haikuwebkit/Source/WTF/wtf/EnumTraits.h

83 lines
2.9 KiB
C
Raw Permalink Normal View History

/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <type_traits>
namespace WTF {
template<typename> struct EnumTraits;
template<typename E, E...> struct EnumValues;
template<typename T, typename E> struct EnumValueChecker;
template<typename T, typename E, E e, E... es>
struct EnumValueChecker<T, EnumValues<E, e, es...>> {
static constexpr bool isValidEnum(T t)
{
return (static_cast<T>(e) == t) ? true : EnumValueChecker<T, EnumValues<E, es...>>::isValidEnum(t);
}
};
template<typename T, typename E>
struct EnumValueChecker<T, EnumValues<E>> {
Simplify the PaymentCoordinator interface https://bugs.webkit.org/show_bug.cgi?id=169382 Part of rdar://problem/28880714. Reviewed by Tim Horton. Source/WebCore: Add four new structs: PaymentAuthorizationResult, PaymentMethodUpdate, ShippingContactUpdate and ShippingMethodUpdate. Change the various PaymentCoordinator and PaymentCoordinatorClient functions to take these new objects instead of multiple parameters. * Modules/applepay/ApplePaySession.cpp: (WebCore::ApplePaySession::completeShippingMethodSelection): (WebCore::ApplePaySession::completeShippingContactSelection): (WebCore::ApplePaySession::completePaymentMethodSelection): (WebCore::ApplePaySession::completePayment): (WebCore::ApplePaySession::didSelectShippingMethod): (WebCore::ApplePaySession::didSelectShippingContact): * Modules/applepay/PaymentAuthorizationStatus.h: * Modules/applepay/PaymentCoordinator.cpp: (WebCore::PaymentCoordinator::completeShippingMethodSelection): (WebCore::PaymentCoordinator::completeShippingContactSelection): (WebCore::PaymentCoordinator::completePaymentMethodSelection): (WebCore::PaymentCoordinator::completePaymentSession): * Modules/applepay/PaymentCoordinator.h: * Modules/applepay/PaymentCoordinatorClient.h: * Modules/applepay/PaymentRequest.h: * loader/EmptyClients.cpp: Source/WebKit/mac: Update for PaymentCoordinatorClient changes. * WebCoreSupport/WebPaymentCoordinatorClient.h: * WebCoreSupport/WebPaymentCoordinatorClient.mm: (WebPaymentCoordinatorClient::completeShippingMethodSelection): (WebPaymentCoordinatorClient::completeShippingContactSelection): (WebPaymentCoordinatorClient::completePaymentMethodSelection): (WebPaymentCoordinatorClient::completePaymentSession): Source/WebKit2: Send the new structs over the wire to the UI process and update the various proxy object to take them instead of multiple parameters. * Scripts/webkit/messages.py: (headers_for_type): * Shared/Cocoa/WebCoreArgumentCodersCocoa.mm: (IPC::ArgumentCoder<WebCore::PaymentAuthorizationResult>::encode): (IPC::ArgumentCoder<WebCore::PaymentAuthorizationResult>::decode): (IPC::ArgumentCoder<WebCore::PaymentError>::encode): (IPC::ArgumentCoder<WebCore::PaymentError>::decode): (IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::encode): (IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::decode): (IPC::ArgumentCoder<WebCore::ShippingContactUpdate>::encode): (IPC::ArgumentCoder<WebCore::ShippingContactUpdate>::decode): (IPC::ArgumentCoder<WebCore::ShippingMethodUpdate>::encode): (IPC::ArgumentCoder<WebCore::ShippingMethodUpdate>::decode): * Shared/WebCoreArgumentCoders.h: * UIProcess/ApplePay/WebPaymentCoordinatorProxy.cpp: (WebKit::WebPaymentCoordinatorProxy::completeShippingMethodSelection): (WebKit::WebPaymentCoordinatorProxy::completeShippingContactSelection): (WebKit::WebPaymentCoordinatorProxy::completePaymentMethodSelection): (WebKit::WebPaymentCoordinatorProxy::completePaymentSession): (WebKit::isValidEnum): Deleted. * UIProcess/ApplePay/WebPaymentCoordinatorProxy.h: * UIProcess/ApplePay/WebPaymentCoordinatorProxy.messages.in: * UIProcess/ApplePay/cocoa/WebPaymentCoordinatorProxyCocoa.mm: (WebKit::WebPaymentCoordinatorProxy::platformCompletePaymentSession): (WebKit::WebPaymentCoordinatorProxy::platformCompleteShippingMethodSelection): (WebKit::WebPaymentCoordinatorProxy::platformCompleteShippingContactSelection): (WebKit::WebPaymentCoordinatorProxy::platformCompletePaymentMethodSelection): * WebProcess/ApplePay/WebPaymentCoordinator.cpp: (WebKit::WebPaymentCoordinator::completeShippingMethodSelection): (WebKit::WebPaymentCoordinator::completeShippingContactSelection): (WebKit::WebPaymentCoordinator::completePaymentMethodSelection): (WebKit::WebPaymentCoordinator::completePaymentSession): * WebProcess/ApplePay/WebPaymentCoordinator.h: Source/WTF: * wtf/EnumTraits.h: Fix a build warning. Canonical link: https://commits.webkit.org/186359@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@213601 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-03-08 22:56:22 +00:00
static constexpr bool isValidEnum(T)
{
return false;
}
};
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): Canonical link: https://commits.webkit.org/232923@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@271358 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-01-11 13:04:27 +00:00
template<typename E, typename T, typename = std::enable_if_t<!std::is_same_v<std::underlying_type_t<E>, bool>>>
constexpr auto isValidEnum(T t) -> decltype(EnumValueChecker<T, typename EnumTraits<E>::values>::isValidEnum(t), bool())
{
static_assert(sizeof(T) >= sizeof(std::underlying_type_t<E>), "Integral type must be at least the size of the underlying enum type");
return EnumValueChecker<T, typename EnumTraits<E>::values>::isValidEnum(t);
}
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): Canonical link: https://commits.webkit.org/232923@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@271358 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-01-11 13:04:27 +00:00
template<typename E, typename T, typename = std::enable_if_t<!std::is_same_v<std::underlying_type_t<E>, bool>>>
auto isValidEnum(T t) -> decltype(EnumTraits<E>::isValidEnum(t), bool())
{
return EnumTraits<E>::isValidEnum(t);
}
template<typename E, typename T, typename = std::enable_if_t<std::is_same_v<std::underlying_type_t<E>, bool>>>
Clean up FrameLoader two-state enums https://bugs.webkit.org/show_bug.cgi?id=190731 Reviewed by Chris Dumez. Source/WebCore: This patch does three things: 1. Add an overload to EnumTraits so we do not need to list out the valid values of boolean enum classes. The valid values are always 0 and 1. This is used when decoding from IPC. 2. Add a 2-state enum class for NewLoadInProgress instad of a bool so we can understand the code better. 3. Begin passing LockBackForwardList to the UIProcess. We will need it soon for PSON. * history/CachedFrame.h: * loader/EmptyFrameLoaderClient.h: * loader/FrameLoader.cpp: (WebCore::FrameLoader::provisionalLoadStarted): (WebCore::FrameLoader::loadWithDocumentLoader): (WebCore::FrameLoader::commitProvisionalLoad): (WebCore::FrameLoader::clientRedirectCancelledOrFinished): (WebCore::FrameLoader::clientRedirected): (WebCore::FrameLoader::receivedMainResourceError): (WebCore::FrameLoader::continueLoadAfterNavigationPolicy): * loader/FrameLoader.h: * loader/FrameLoaderClient.h: * loader/FrameLoaderTypes.h: * loader/NavigationScheduler.cpp: (WebCore::ScheduledNavigation::didStopTimer): (WebCore::NavigationScheduler::cancel): * loader/NavigationScheduler.h: * platform/network/StoredCredentialsPolicy.h: Source/WebKit: * NetworkProcess/NetworkProcess.h: * NetworkProcess/NetworkProcess.messages.in: * Shared/WebCoreArgumentCoders.h: * UIProcess/WebPageProxy.cpp: (WebKit::WebPageProxy::willPerformClientRedirectForFrame): * UIProcess/WebPageProxy.h: * UIProcess/WebPageProxy.messages.in: * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp: (WebKit::WebFrameLoaderClient::dispatchWillPerformClientRedirect): * WebProcess/WebCoreSupport/WebFrameLoaderClient.h: Source/WebKitLegacy/mac: * WebCoreSupport/WebFrameLoaderClient.h: * WebCoreSupport/WebFrameLoaderClient.mm: (WebFrameLoaderClient::dispatchWillPerformClientRedirect): Source/WebKitLegacy/win: * WebCoreSupport/WebFrameLoaderClient.cpp: (WebFrameLoaderClient::dispatchWillPerformClientRedirect): * WebCoreSupport/WebFrameLoaderClient.h: Source/WTF: * wtf/EnumTraits.h: (WTF::isValidEnum): Canonical link: https://commits.webkit.org/205629@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@237264 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-10-18 21:16:52 +00:00
constexpr bool isValidEnum(T t)
{
return !t || t == 1;
}
Use enum classes within FileSystem https://bugs.webkit.org/show_bug.cgi?id=175172 Patch by Christopher Reid <chris.reid@sony.com> on 2017-11-06 Reviewed by Myles C. Maxfield. Source/WebCore: No new tests, no change in behavior. Using enum classes in filesystem to enforce stronger type safety. * Modules/webdatabase/OriginLock.cpp: * loader/appcache/ApplicationCacheStorage.cpp: * platform/FileHandle.h: * platform/FileStream.cpp: * platform/FileSystem.cpp: * platform/FileSystem.h: * platform/cocoa/FileMonitorCocoa.mm: * platform/glib/FileSystemGlib.cpp: * platform/network/curl/CurlCacheEntry.cpp: * platform/network/curl/CurlCacheManager.cpp: * platform/posix/FileSystemPOSIX.cpp: * platform/win/FileSystemWin.cpp: * rendering/RenderThemeWin.cpp: Source/WebKit: * NetworkProcess/Downloads/BlobDownloadClient.cpp: * NetworkProcess/NetworkDataTaskBlob.cpp: * NetworkProcess/cache/NetworkCache.cpp: * NetworkProcess/capture/NetworkCaptureManager.cpp: * NetworkProcess/capture/NetworkCaptureRecorder.cpp: * Shared/WebMemorySampler.cpp: * UIProcess/API/APIContentRuleListStore.cpp: * UIProcess/ResourceLoadStatisticsPersistentStorage.cpp: Source/WebKitLegacy/win: * Plugins/PluginDatabase.cpp: Source/WTF: Adding a helper function for converting enum classes to their underlying type when necessary. * wtf/EnumTraits.h: Tools: * TestWebKitAPI/Tests/WebCore/FileMonitor.cpp: * TestWebKitAPI/Tests/WebCore/cocoa/DatabaseTrackerTest.mm: Canonical link: https://commits.webkit.org/195421@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@224505 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-11-06 20:20:28 +00:00
template<typename E>
constexpr auto enumToUnderlyingType(E e)
{
return static_cast<std::underlying_type_t<E>>(e);
}
}
using WTF::isValidEnum;