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

Changeset 155407 in webkit


Ignore:
Timestamp:
Sep 9, 2013, 5:43:51 PM (13 years ago)
Author:
andersca@apple.com
Message:

Introduce WTF::createOwned
https://bugs.webkit.org/show_bug.cgi?id=121059

Reviewed by Andreas Kling.

WTF::createOwned is a function template that does adoptPtr + new in a single function call,
with all the arguments being perfectly forwarded thanks to C++11.

Being forward-looking, createOwned returns an OwnPtr rather than a PassOwnPtr since the plan is
to get rid of PassOwnPtr and just use std::move instead.

  • wtf/FilePrintStream.cpp:
  • wtf/FilePrintStream.h:
  • wtf/HashTable.h:
  • wtf/ListHashSet.h:
  • wtf/OwnPtr.h:

(WTF::OwnPtr::OwnPtr):
(WTF::createOwned):
(WTF::createThread):
(WTF::establishIdentifierForPthreadHandle):
(WTF::createThreadInternal):
(WTF::Collator::userDefault):
(WTF::Collator::userDefault):

Location:
trunk/Source/WTF
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r155402 r155407  
     12013-09-09  Anders Carlsson  <andersca@apple.com>
     2
     3        Introduce WTF::createOwned
     4        https://bugs.webkit.org/show_bug.cgi?id=121059
     5
     6        Reviewed by Andreas Kling.
     7
     8        WTF::createOwned is a function template that does adoptPtr + new in a single function call,
     9        with all the arguments being perfectly forwarded thanks to C++11.
     10       
     11        Being forward-looking, createOwned returns an OwnPtr rather than a PassOwnPtr since the plan is
     12        to get rid of PassOwnPtr and just use std::move instead.
     13
     14        * wtf/FilePrintStream.cpp:
     15        * wtf/FilePrintStream.h:
     16        * wtf/HashTable.h:
     17        * wtf/ListHashSet.h:
     18        * wtf/OwnPtr.h:
     19        (WTF::OwnPtr::OwnPtr):
     20        (WTF::createOwned):
     21        (WTF::createThread):
     22        (WTF::establishIdentifierForPthreadHandle):
     23        (WTF::createThreadInternal):
     24        (WTF::Collator::userDefault):
     25        (WTF::Collator::userDefault):
     26
    1272013-09-09  Anders Carlsson  <andersca@apple.com>
    228
  • trunk/Source/WTF/wtf/FilePrintStream.cpp

    r136601 r155407  
    4242}
    4343
    44 PassOwnPtr<FilePrintStream> FilePrintStream::open(const char* filename, const char* mode)
     44OwnPtr<FilePrintStream> FilePrintStream::open(const char* filename, const char* mode)
    4545{
    4646    FILE* file = fopen(filename, mode);
    4747    if (!file)
    48         return PassOwnPtr<FilePrintStream>();
    49    
    50     return adoptPtr(new FilePrintStream(file));
     48        return nullptr;
     49
     50    return createOwned<FilePrintStream>(file);
    5151}
    5252
  • trunk/Source/WTF/wtf/FilePrintStream.h

    r150995 r155407  
    2828
    2929#include <stdio.h>
    30 #include <wtf/PassOwnPtr.h>
    3130#include <wtf/PrintStream.h>
     31#include <wtf/OwnPtr.h>
    3232
    3333namespace WTF {
     
    4343    virtual ~FilePrintStream();
    4444   
    45     WTF_EXPORT_PRIVATE static PassOwnPtr<FilePrintStream> open(const char* filename, const char* mode);
     45    WTF_EXPORT_PRIVATE static OwnPtr<FilePrintStream> open(const char* filename, const char* mode);
    4646   
    4747    FILE* file() { return m_file; }
  • trunk/Source/WTF/wtf/HashTable.h

    r155251 r155407  
    556556#if CHECK_HASHTABLE_ITERATORS
    557557        , m_iterators(0)
    558         , m_mutex(adoptPtr(new Mutex))
    559 #endif
    560 #if DUMP_HASHTABLE_STATS_PER_TABLE
    561         , m_stats(adoptPtr(new Stats))
     558        , m_mutex(createOwned<Mutex>())
     559#endif
     560#if DUMP_HASHTABLE_STATS_PER_TABLE
     561        , m_stats(createOwned<Stats>())
    562562#endif
    563563    {
     
    11671167#if CHECK_HASHTABLE_ITERATORS
    11681168        , m_iterators(0)
    1169         , m_mutex(adoptPtr(new Mutex))
    1170 #endif
    1171 #if DUMP_HASHTABLE_STATS_PER_TABLE
    1172         , m_stats(adoptPtr(new Stats(*other.m_stats)))
     1169        , m_mutex(createOwned<Mutex>())
     1170#endif
     1171#if DUMP_HASHTABLE_STATS_PER_TABLE
     1172        , m_stats(createOwned<Stats>(*other.m_stats))
    11731173#endif
    11741174    {
  • trunk/Source/WTF/wtf/ListHashSet.h

    r154967 r155407  
    512512        : m_head(0)
    513513        , m_tail(0)
    514         , m_allocator(adoptPtr(new NodeAllocator))
     514        , m_allocator(createOwned<NodeAllocator>())
    515515    {
    516516    }
     
    520520        : m_head(0)
    521521        , m_tail(0)
    522         , m_allocator(adoptPtr(new NodeAllocator))
     522        , m_allocator(createOwned<NodeAllocator>())
    523523    {
    524524        const_iterator end = other.end();
  • trunk/Source/WTF/wtf/OwnPtr.h

    r155357 r155407  
    3737
    3838    template<typename T> class OwnPtr {
    39 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
    40         // If rvalue references are not supported, the copy constructor is
    41         // public so OwnPtr cannot be marked noncopyable. See note below.
    42         WTF_MAKE_NONCOPYABLE(OwnPtr);
    43 #endif
    4439    public:
    4540        typedef typename std::remove_pointer<T>::type ValueType;
     
    9287
    9388    private:
     89        explicit OwnPtr(PtrType ptr) : m_ptr(ptr) { }
     90
     91        template<typename U> friend OwnPtr<U> createOwned();
     92        template<typename U, typename A1> friend OwnPtr<U> createOwned(A1&&);
     93        template<typename U, typename A1, typename A2> friend OwnPtr<U> createOwned(A1&&, A2&&);
     94
    9495#if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
    9596        // If rvalue references are supported, noncopyable takes care of this.
     
    213214    }
    214215
     216template<typename T>
     217inline OwnPtr<T> createOwned()
     218{
     219    return OwnPtr<T>(new T);
     220}
     221
     222template<typename T, typename A1>
     223inline OwnPtr<T> createOwned(A1&& a1)
     224{
     225    return OwnPtr<T>(new T(std::forward<A1>(a1)));
     226}
     227
     228template<typename T, typename A1, typename A2>
     229inline OwnPtr<T> createOwned(A1&& a1, A2&& a2)
     230{
     231    return OwnPtr<T>(new T(std::forward<A1>(a1), std::forward<A2>(a2)));
     232}
     233
    215234} // namespace WTF
    216235
    217236using WTF::OwnPtr;
     237using WTF::createOwned;
    218238
    219239#endif // WTF_OwnPtr_h
  • trunk/Source/WTF/wtf/Threading.cpp

    r148166 r155407  
    117117ThreadIdentifier createThread(ThreadFunctionWithReturnValue entryPoint, void* data, const char* name)
    118118{
    119     OwnPtr<ThreadFunctionWithReturnValueInvocation> invocation = adoptPtr(new ThreadFunctionWithReturnValueInvocation(entryPoint, data));
     119    auto invocation = createOwned<ThreadFunctionWithReturnValueInvocation>(entryPoint, data);
    120120
    121121    // Balanced by adoptPtr() in compatEntryPoint.
     
    137137ThreadIdentifier createThread(ThreadFunctionWithReturnValue entryPoint, void* data)
    138138{
    139     OwnPtr<ThreadFunctionWithReturnValueInvocation> invocation = adoptPtr(new ThreadFunctionWithReturnValueInvocation(entryPoint, data));
     139    auto invocation = createOwned<ThreadFunctionWithReturnValueInvocation>(entryPoint, data);
    140140
    141141    // Balanced by adoptPtr() in compatEntryPoint.
  • trunk/Source/WTF/wtf/ThreadingPthreads.cpp

    r149980 r155407  
    180180    MutexLocker locker(threadMapMutex());
    181181    static ThreadIdentifier identifierCount = 1;
    182     threadMap().add(identifierCount, adoptPtr(new PthreadState(pthreadHandle)));
     182    threadMap().add(identifierCount, createOwned<PthreadState>(pthreadHandle).release());
    183183    return identifierCount++;
    184184}
     
    199199ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
    200200{
    201     OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(new ThreadFunctionInvocation(entryPoint, data));
     201    auto invocation = WTF::createOwned<ThreadFunctionInvocation>(entryPoint, data);
    202202    pthread_t threadHandle;
    203203    if (pthread_create(&threadHandle, 0, wtfThreadEntryPoint, invocation.get())) {
  • trunk/Source/WTF/wtf/unicode/Collator.h

    r155251 r155407  
    3131
    3232#include <wtf/Noncopyable.h>
    33 #include <wtf/PassOwnPtr.h>
     33#include <wtf/OwnPtr.h>
    3434#include <wtf/unicode/Unicode.h>
    3535
     
    4949        WTF_EXPORT_PRIVATE void setOrderLowerFirst(bool);
    5050
    51         WTF_EXPORT_PRIVATE static PassOwnPtr<Collator> userDefault();
     51        WTF_EXPORT_PRIVATE static OwnPtr<Collator> userDefault();
    5252
    5353        WTF_EXPORT_PRIVATE Result collate(const ::UChar*, size_t, const ::UChar*, size_t) const;
  • trunk/Source/WTF/wtf/unicode/CollatorDefault.cpp

    r111778 r155407  
    4646}
    4747
    48 PassOwnPtr<Collator> Collator::userDefault()
     48OwnPtr<Collator> Collator::userDefault()
    4949{
    50     return adoptPtr(new Collator(0));
     50    return createOwned<Collator>(0);
    5151}
    5252
  • trunk/Source/WTF/wtf/unicode/icu/CollatorICU.cpp

    r149255 r155407  
    5959}
    6060
    61 PassOwnPtr<Collator> Collator::userDefault()
     61OwnPtr<Collator> Collator::userDefault()
    6262{
    6363#if OS(DARWIN) && USE(CF)
     
    7272    char buf[256];
    7373    if (!collationOrder)
    74         return adoptPtr(new Collator(""));
     74        return createOwned<Collator>("");
    7575    CFStringGetCString(collationOrder, buf, sizeof(buf), kCFStringEncodingASCII);
    76     return adoptPtr(new Collator(buf));
     76    return createOwned<Collator>(buf);
    7777#else
    78     return adoptPtr(new Collator(0));
     78    return createOwned<Collator>(static_cast<const char*>(0));
    7979#endif
    8080}
Note: See TracChangeset for help on using the changeset viewer.