Changeset 215450 in webkit


Ignore:
Timestamp:
Apr 17, 2017, 10:37:45 PM (8 years ago)
Author:
achristensen@apple.com
Message:

Allow Variants of RetainPtrs
https://bugs.webkit.org/show_bug.cgi?id=170923

Reviewed by Tim Horton and Sam Weinig.

Source/WebCore:

No change in behavior. Just updated the one class that used RetainPtr::operator& to cleanly initialize
RetainPtrs instead of taking the address of a smart pointer and forcing a value inside of it.

  • platform/mac/SSLKeyGeneratorMac.mm:

(WebCore::signedPublicKeyAndChallengeString):

Source/WTF:

  • wtf/RetainPtr.h:

(WTF::RetainPtr::operator&): Deleted.
Operator& was causing a compile error when making Variant<RetainPtr<T>, ...>
and because it is strange and only used once, let's just remove it.

  • wtf/Variant.h:

(WTF::get):
(WTF::get_if):
Use std::addressof instead of operator& which could be overloaded to return any type with any meaning.

Tools:

  • TestWebKitAPI/Tests/WTF/Variant.cpp:

(TestWebKitAPI::TEST):
Add tests for RetainPtr and for another class with overloaded operator& to verify such classes can
work in Variants.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r215429 r215450  
     12017-04-17  Alex Christensen  <achristensen@webkit.org>
     2
     3        Allow Variants of RetainPtrs
     4        https://bugs.webkit.org/show_bug.cgi?id=170923
     5
     6        Reviewed by Tim Horton and Sam Weinig.
     7
     8        * wtf/RetainPtr.h:
     9        (WTF::RetainPtr::operator&): Deleted.
     10        Operator& was causing a compile error when making Variant<RetainPtr<T>, ...>
     11        and because it is strange and only used once, let's just remove it.
     12        * wtf/Variant.h:
     13        (WTF::get):
     14        (WTF::get_if):
     15        Use std::addressof instead of operator& which could be overloaded to return any type with any meaning.
     16
    1172017-04-17  Youenn Fablet  <youenn@apple.com>
    218
  • trunk/Source/WTF/wtf/RetainPtr.h

    r204567 r215450  
    8989    bool operator!() const { return !m_ptr; }
    9090
    91 #if !(defined (__OBJC__) && __has_feature(objc_arc))
    92     // This function is useful for passing RetainPtrs to functions that return
    93     // CF types as out parameters.
    94     PtrType* operator&()
    95     {
    96         // Require that the pointer is null, to prevent leaks.
    97         ASSERT(!m_ptr);
    98 
    99         return (PtrType*)&m_ptr;
    100     }
    101 #endif
    102 
    10391    // This conversion operator allows implicit conversion to bool but not to other integer types.
    10492    typedef StorageType RetainPtr::*UnspecifiedBoolType;
  • trunk/Source/WTF/wtf/Variant.h

    r207627 r215450  
    17891789    return *(
    17901790        (_Index!=__v.index())
    1791             ? &__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type const&>("Bad Variant index in get")
    1792             : &__variant_accessor<_Index,_Types...>::get(__v)
     1791            ? std::addressof(__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type const&>("Bad Variant index in get"))
     1792            : std::addressof(__variant_accessor<_Index,_Types...>::get(__v))
    17931793    );
    17941794}
     
    17981798    return *(
    17991799        (_Index!=__v.index())
    1800             ? &__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type&>("Bad Variant index in get")
    1801             : &__variant_accessor<_Index,_Types...>::get(__v)
     1800            ? std::addressof(__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type&>("Bad Variant index in get"))
     1801            : std::addressof(__variant_accessor<_Index,_Types...>::get(__v))
    18021802    );
    18031803}
     
    18191819template<typename _Type,typename ... _Types>
    18201820constexpr std::add_pointer_t<_Type> get_if(Variant<_Types...>& __v){
    1821     return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:&get<_Type>(__v);
     1821    return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:std::addressof(get<_Type>(__v));
    18221822}
    18231823
    18241824template<typename _Type,typename ... _Types>
    18251825constexpr std::add_pointer_t<_Type const> get_if(Variant<_Types...> const& __v){
    1826     return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:&get<_Type>(__v);
     1826    return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:std::addressof(get<_Type>(__v));
    18271827}
    18281828
     
    18301830constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type> get_if(Variant<_Types...>& __v){
    18311831    return ((_Index!=__v.index())?nullptr:
    1832         &__variant_accessor<_Index,_Types...>::get(__v));
     1832        std::addressof(__variant_accessor<_Index,_Types...>::get(__v)));
    18331833}
    18341834
     
    18371837    Variant<_Types...> const& __v){
    18381838    return ((_Index!=__v.index())?nullptr:
    1839         &__variant_accessor<_Index,_Types...>::get(__v));
     1839        std::addressof(__variant_accessor<_Index,_Types...>::get(__v)));
    18401840}
    18411841
  • trunk/Source/WebCore/ChangeLog

    r215447 r215450  
     12017-04-17  Alex Christensen  <achristensen@webkit.org>
     2
     3        Allow Variants of RetainPtrs
     4        https://bugs.webkit.org/show_bug.cgi?id=170923
     5
     6        Reviewed by Tim Horton and Sam Weinig.
     7
     8        No change in behavior.  Just updated the one class that used RetainPtr::operator& to cleanly initialize
     9        RetainPtrs instead of taking the address of a smart pointer and forcing a value inside of it.
     10
     11        * platform/mac/SSLKeyGeneratorMac.mm:
     12        (WebCore::signedPublicKeyAndChallengeString):
     13
    1142017-04-17  Fujii Hironori  <Hironori.Fujii@sony.com>
    215
  • trunk/Source/WebCore/platform/mac/SSLKeyGeneratorMac.mm

    r209822 r215450  
    129129    SignedPublicKeyAndChallenge signedPublicKeyAndChallenge { };
    130130
    131     RetainPtr<SecAccessRef> access;
    132     if (SecAccessCreate(keyDescription.createCFString().get(), nullptr, &access) != noErr)
    133         return String();
    134 
    135     RetainPtr<CFArrayRef> acls;
    136     if (SecAccessCopySelectedACLList(access.get(), CSSM_ACL_AUTHORIZATION_DECRYPT, &acls) != noErr)
    137         return String();
     131    SecAccessRef accessRef { nullptr };
     132    if (SecAccessCreate(keyDescription.createCFString().get(), nullptr, &accessRef) != noErr)
     133        return String();
     134    RetainPtr<SecAccessRef> access = adoptCF(accessRef);
     135
     136    CFArrayRef aclsRef { nullptr };
     137    if (SecAccessCopySelectedACLList(access.get(), CSSM_ACL_AUTHORIZATION_DECRYPT, &aclsRef) != noErr)
     138        return String();
     139    RetainPtr<CFArrayRef> acls = adoptCF(aclsRef);
    138140
    139141    SecACLRef acl = (SecACLRef)(CFArrayGetValueAtIndex(acls.get(), 0));
    140142
    141143    // Passing nullptr to SecTrustedApplicationCreateFromPath tells that function to assume the application bundle.
    142     RetainPtr<SecTrustedApplicationRef> trustedApp;
    143     if (SecTrustedApplicationCreateFromPath(nullptr, &trustedApp) != noErr)
    144         return String();
     144    SecTrustedApplicationRef trustedAppRef { nullptr };
     145    if (SecTrustedApplicationCreateFromPath(nullptr, &trustedAppRef) != noErr)
     146        return String();
     147    RetainPtr<SecTrustedApplicationRef> trustedApp = adoptCF(trustedAppRef);
    145148
    146149    const CSSM_ACL_KEYCHAIN_PROMPT_SELECTOR defaultSelector = {
     
    150153        return String();
    151154
    152     RetainPtr<SecKeyRef> publicKey;
    153     RetainPtr<SecKeyRef> privateKey;
    154     if (SecKeyCreatePair(nullptr, CSSM_ALGID_RSA, keySize, 0, CSSM_KEYUSE_ANY, CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_RETURN_REF, CSSM_KEYUSE_ANY, CSSM_KEYATTR_SENSITIVE | CSSM_KEYATTR_RETURN_REF | CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE, access.get(), &publicKey, &privateKey) != noErr)
    155         return String();
     155    SecKeyRef publicKeyRef { nullptr };
     156    SecKeyRef privateKeyRef { nullptr };
     157    if (SecKeyCreatePair(nullptr, CSSM_ALGID_RSA, keySize, 0, CSSM_KEYUSE_ANY, CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_RETURN_REF, CSSM_KEYUSE_ANY, CSSM_KEYATTR_SENSITIVE | CSSM_KEYATTR_RETURN_REF | CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE, access.get(), &publicKeyRef, &privateKeyRef) != noErr)
     158        return String();
     159    RetainPtr<SecKeyRef> publicKey = adoptCF(publicKeyRef);
     160    RetainPtr<SecKeyRef> privateKey = adoptCF(privateKeyRef);
    156161
    157162    CSSM_CSP_HANDLE cspHandle;
  • trunk/Tools/ChangeLog

    r215441 r215450  
     12017-04-17  Alex Christensen  <achristensen@webkit.org>
     2
     3        Allow Variants of RetainPtrs
     4        https://bugs.webkit.org/show_bug.cgi?id=170923
     5
     6        Reviewed by Tim Horton and Sam Weinig.
     7
     8        * TestWebKitAPI/Tests/WTF/Variant.cpp:
     9        (TestWebKitAPI::TEST):
     10        Add tests for RetainPtr and for another class with overloaded operator& to verify such classes can
     11        work in Variants.
     12
    1132017-04-17  Brady Eidson  <beidson@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Variant.cpp

    r207627 r215450  
    110110}
    111111
     112#if USE(CF)
     113TEST(WTF_Variant, RetainPtr)
     114{
     115    enum class Type {
     116        None,
     117        RefPtr,
     118        RetainPtr,
     119    };
     120   
     121    Type type = Type::None;
     122   
     123    auto visitor = WTF::makeVisitor(
     124        [&](const RefPtr<RefLogger>&) { type = Type::RefPtr; },
     125        [&](const RetainPtr<CFDataRef>&) { type = Type::RetainPtr; }
     126    );
     127   
     128    RefPtr<RefLogger> refPtr;
     129    RetainPtr<CFDataRef> retainPtr;
     130    Variant<RefPtr<RefLogger>, RetainPtr<CFDataRef>> variant(WTFMove(refPtr));
     131    WTF::visit(visitor, variant);
     132    EXPECT_TRUE(Type::RefPtr == type);
     133   
     134    variant = WTFMove(retainPtr);
     135    WTF::visit(visitor, variant);
     136    EXPECT_TRUE(Type::RetainPtr == type);
     137}
     138#endif
     139
    112140TEST(WTF_Variant, VisitorUsingMakeVisitor)
    113141{
     
    226254}
    227255
    228 }
     256template<class T>
     257class Holder {
     258public:
     259    T data;
     260
     261    Holder(T data) : data(data) { }
     262
     263    T* operator&() { return &data; }
     264};
     265
     266TEST(WTF_Variant, OperatorAmpersand)
     267{
     268    Variant<Holder<int>, int> v = Holder<int>(10);
     269    EXPECT_TRUE(WTF::get<Holder<int>>(v).data == 10);
     270
     271    v = 20;
     272    EXPECT_TRUE(WTF::get<int>(v) == 20);
     273}
     274
     275}
Note: See TracChangeset for help on using the changeset viewer.