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

Changeset 238467 in webkit


Ignore:
Timestamp:
Nov 23, 2018, 10:08:31 PM (8 years ago)
Author:
commit-queue@webkit.org
Message:

Add raw pointer overloads to ListHashSet via SmartPtr specialized functions
https://bugs.webkit.org/show_bug.cgi?id=191936

Patch by Sam Weinig <sam@webkit.org> on 2018-11-23
Reviewed by Zalan Bujtas.

Source/WTF:

Adds overloads for find, contains, insertBefore and remove that take raw pointers
when the value type V of a ListHashSet is true for the predicate IsSmartPtr<V>::value.
This brings the interface to ListHashSet closer inline with HashSet, HashMap and HashCountedSet
which already have this functionality. Like in the other collections, this is especially
useful when using std::unique_ptr<> as the value, since there would be no way to pass it
to these functions. One difference between this set of overloads is the inclusion of insertBefore,
which is unique to ListHashSet. As would be expected, this specialization only changes the first
parameter, the one that needs to be found, to support a raw pointer.

  • wtf/ListHashSet.h:

(WTF::U>::find):
(WTF::U>::find const):
(WTF::U>::contains const):
(WTF::U>::insertBefore):
(WTF::U>::remove):

Tools:

Adds tests for raw pointer overloads in ListHashSet.

  • TestWebKitAPI/Tests/WTF/HashSet.cpp:
  • TestWebKitAPI/Tests/WTF/ListHashSet.cpp:
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r238439 r238467  
     12018-11-23  Sam Weinig  <sam@webkit.org>
     2
     3        Add raw pointer overloads to ListHashSet via SmartPtr specialized functions
     4        https://bugs.webkit.org/show_bug.cgi?id=191936
     5
     6        Reviewed by Zalan Bujtas.
     7
     8        Adds overloads for find, contains, insertBefore and remove that take raw pointers
     9        when the value type V of a ListHashSet is true for the predicate IsSmartPtr<V>::value.
     10        This brings the interface to ListHashSet closer inline with HashSet, HashMap and HashCountedSet
     11        which already have this functionality. Like in the other collections, this is especially
     12        useful when using std::unique_ptr<> as the value, since there would be no way to pass it
     13        to these functions. One difference between this set of overloads is the inclusion of insertBefore,
     14        which is unique to ListHashSet. As would be expected, this specialization only changes the first
     15        parameter, the one that needs to be found, to support a raw pointer. 
     16
     17        * wtf/ListHashSet.h:
     18        (WTF::U>::find):
     19        (WTF::U>::find const):
     20        (WTF::U>::contains const):
     21        (WTF::U>::insertBefore):
     22        (WTF::U>::remove):
     23
    1242018-11-21  Yusuke Suzuki  <yusukesuzuki@slowstart.org>
    225
  • trunk/Source/WTF/wtf/ListHashSet.h

    r237419 r238467  
    143143    void clear();
    144144
     145    // Overloads for smart pointer values that take the raw pointer type as the parameter.
     146    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type find(typename GetPtrHelper<V>::PtrType);
     147    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type find(typename GetPtrHelper<V>::PtrType) const;
     148    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type contains(typename GetPtrHelper<V>::PtrType) const;
     149    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type insertBefore(typename GetPtrHelper<V>::PtrType, const ValueType&);
     150    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type insertBefore(typename GetPtrHelper<V>::PtrType, ValueType&&);
     151    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type remove(typename GetPtrHelper<V>::PtrType);
     152
    145153private:
    146154    void unlink(Node*);
     
    627635
    628636template<typename T, typename U>
     637template<typename V>
     638inline auto ListHashSet<T, U>::find(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type
     639{
     640    auto it = m_impl.template find<BaseTranslator>(value);
     641    if (it == m_impl.end())
     642        return end();
     643    return makeIterator(*it);
     644}
     645
     646template<typename T, typename U>
     647template<typename V>
     648inline auto ListHashSet<T, U>::find(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type
     649{
     650    auto it = m_impl.template find<BaseTranslator>(value);
     651    if (it == m_impl.end())
     652        return end();
     653    return makeConstIterator(*it);
     654}
     655
     656template<typename T, typename U>
     657template<typename V>
     658inline auto ListHashSet<T, U>::contains(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
     659{
     660    return m_impl.template contains<BaseTranslator>(value);
     661}
     662
     663template<typename T, typename U>
     664template<typename V>
     665inline auto ListHashSet<T, U>::insertBefore(typename GetPtrHelper<V>::PtrType beforeValue, const ValueType& newValue) -> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type
     666{
     667    return insertBefore(find(beforeValue), newValue);
     668}
     669
     670template<typename T, typename U>
     671template<typename V>
     672inline auto ListHashSet<T, U>::insertBefore(typename GetPtrHelper<V>::PtrType beforeValue, ValueType&& newValue) -> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type
     673{
     674    return insertBefore(find(beforeValue), WTFMove(newValue));
     675}
     676
     677template<typename T, typename U>
     678template<typename V>
     679inline auto ListHashSet<T, U>::remove(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
     680{
     681    return remove(find(value));
     682}
     683
     684template<typename T, typename U>
    629685void ListHashSet<T, U>::unlink(Node* node)
    630686{
  • trunk/Tools/ChangeLog

    r238461 r238467  
     12018-11-23  Sam Weinig  <sam@webkit.org>
     2
     3        Add raw pointer overloads to ListHashSet via SmartPtr specialized functions
     4        https://bugs.webkit.org/show_bug.cgi?id=191936
     5
     6        Reviewed by Zalan Bujtas.
     7
     8        Adds tests for raw pointer overloads in ListHashSet.
     9
     10        * TestWebKitAPI/Tests/WTF/HashSet.cpp:
     11        * TestWebKitAPI/Tests/WTF/ListHashSet.cpp:
     12
    1132018-11-23  Wenson Hsieh  <wenson_hsieh@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp

    r237461 r238467  
    3737
    3838template<int initialCapacity>
    39     struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTraits<int> {
     39struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTraits<int> {
    4040    static const int minimumTableSize = initialCapacity;
    4141};
  • trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp

    r223041 r238467  
    2626#include "config.h"
    2727
     28#include "Counters.h"
    2829#include "MoveOnly.h"
    2930#include <wtf/ListHashSet.h>
     
    374375}
    375376
     377TEST(WTF_ListHashSet, UniquePtrKey)
     378{
     379    ConstructorDestructorCounter::TestingScope scope;
     380
     381    ListHashSet<std::unique_ptr<ConstructorDestructorCounter>> list;
     382
     383    auto uniquePtr = std::make_unique<ConstructorDestructorCounter>();
     384    list.add(WTFMove(uniquePtr));
     385
     386    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
     387    EXPECT_EQ(0u, ConstructorDestructorCounter::destructionCount);
     388
     389    list.clear();
     390
     391    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
     392    EXPECT_EQ(1u, ConstructorDestructorCounter::destructionCount);
     393}
     394
     395TEST(WTF_ListHashSet, UniquePtrKey_FindUsingRawPointer)
     396{
     397    ListHashSet<std::unique_ptr<int>> list;
     398
     399    auto uniquePtr = std::make_unique<int>(5);
     400    auto ptr = uniquePtr.get();
     401    list.add(WTFMove(uniquePtr));
     402
     403    auto it = list.find(ptr);
     404    ASSERT_TRUE(it != list.end());
     405    EXPECT_EQ(ptr, it->get());
     406    EXPECT_EQ(5, *it->get());
     407}
     408
     409TEST(WTF_ListHashSet, UniquePtrKey_ContainsUsingRawPointer)
     410{
     411    ListHashSet<std::unique_ptr<int>> list;
     412
     413    auto uniquePtr = std::make_unique<int>(5);
     414    auto ptr = uniquePtr.get();
     415    list.add(WTFMove(uniquePtr));
     416
     417    EXPECT_EQ(true, list.contains(ptr));
     418}
     419
     420TEST(WTF_ListHashSet, UniquePtrKey_InsertBeforeUsingRawPointer)
     421{
     422    ListHashSet<std::unique_ptr<int>> list;
     423
     424    auto uniquePtrWith2 = std::make_unique<int>(2);
     425    auto ptrWith2 = uniquePtrWith2.get();
     426    auto uniquePtrWith4 = std::make_unique<int>(4);
     427    auto ptrWith4 = uniquePtrWith4.get();
     428
     429    list.add(WTFMove(uniquePtrWith2));
     430    list.add(WTFMove(uniquePtrWith4));
     431
     432    // { 2, 4 }
     433    ASSERT_EQ(ptrWith2, list.first().get());
     434    ASSERT_EQ(2, *list.first().get());
     435    ASSERT_EQ(ptrWith4, list.last().get());
     436    ASSERT_EQ(4, *list.last().get());
     437
     438    auto uniquePtrWith3 = std::make_unique<int>(3);
     439    auto ptrWith3 = uniquePtrWith3.get();
     440
     441    list.insertBefore(ptrWith4, WTFMove(uniquePtrWith3));
     442   
     443    // { 2, 3, 4 }
     444    auto firstWith2 = list.takeFirst();
     445    ASSERT_EQ(ptrWith2, firstWith2.get());
     446    ASSERT_EQ(2, *firstWith2);
     447
     448    auto firstWith3 = list.takeFirst();
     449    ASSERT_EQ(ptrWith3, firstWith3.get());
     450    ASSERT_EQ(3, *firstWith3);
     451
     452    auto firstWith4 = list.takeFirst();
     453    ASSERT_EQ(ptrWith2, firstWith4.get());
     454    ASSERT_EQ(4, *firstWith4);
     455
     456    ASSERT_TRUE(list.isEmpty());
     457}
     458
     459TEST(WTF_ListHashSet, UniquePtrKey_RemoveUsingRawPointer)
     460{
     461    ConstructorDestructorCounter::TestingScope scope;
     462
     463    ListHashSet<std::unique_ptr<ConstructorDestructorCounter>> list;
     464
     465    auto uniquePtr = std::make_unique<ConstructorDestructorCounter>();
     466    auto* ptr = uniquePtr.get();
     467    list.add(WTFMove(uniquePtr));
     468
     469    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
     470    EXPECT_EQ(0u, ConstructorDestructorCounter::destructionCount);
     471
     472    bool result = list.remove(ptr);
     473    EXPECT_EQ(true, result);
     474
     475    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
     476    EXPECT_EQ(1u, ConstructorDestructorCounter::destructionCount);
     477}
     478
    376479} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.