Commit c24a7a4f authored by andersca@apple.com's avatar andersca@apple.com

Tweak WeakObjCPtr

https://bugs.webkit.org/show_bug.cgi?id=125311

Reviewed by Darin Adler.

Source/WebKit2:

Make it possible to use WeakObjCPtr with pointer types such as id. Also,
add a getAutoreleased() member that will load the weak pointer and retain + autorelease it.

* Shared/mac/WeakObjCPtr.h:
(WebKit::WeakObjCPtr::WeakObjCPtr):
(WebKit::WeakObjCPtr::operator=):
(WebKit::WeakObjCPtr::get):
(WebKit::WeakObjCPtr::getAutoreleased):

Tools:

Split up tests into more logical groups. Add new tests for new functionality.

* TestWebKitAPI/Tests/WebKit2/mac/WeakObjCPtr.mm:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@160187 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent afeead10
2013-12-05 Anders Carlsson <andersca@apple.com>
Tweak WeakObjCPtr
https://bugs.webkit.org/show_bug.cgi?id=125311
Reviewed by Darin Adler.
Make it possible to use WeakObjCPtr with pointer types such as id. Also,
add a getAutoreleased() member that will load the weak pointer and retain + autorelease it.
* Shared/mac/WeakObjCPtr.h:
(WebKit::WeakObjCPtr::WeakObjCPtr):
(WebKit::WeakObjCPtr::operator=):
(WebKit::WeakObjCPtr::get):
(WebKit::WeakObjCPtr::getAutoreleased):
2013-12-05 Zan Dobersek <zdobersek@igalia.com>
[GTK][WK2] Clean up WorkQueueGtk
......
......@@ -27,6 +27,7 @@
#define WeakObjCPtr_h
#include <objc/runtime.h>
#include <type_traits>
#include <wtf/RetainPtr.h>
#if __has_include(<objc/objc-internal.h>)
......@@ -43,12 +44,14 @@ namespace WebKit {
template<typename T> class WeakObjCPtr {
public:
typedef typename std::remove_pointer<T>::type ValueType;
WeakObjCPtr()
: m_weakReference(nullptr)
{
}
WeakObjCPtr(T *ptr)
WeakObjCPtr(ValueType *ptr)
{
objc_initWeak(&m_weakReference, ptr);
}
......@@ -58,18 +61,23 @@ public:
objc_destroyWeak(&m_weakReference);
}
WeakObjCPtr& operator=(T *ptr)
WeakObjCPtr& operator=(ValueType *ptr)
{
objc_storeWeak(&m_weakReference, ptr);
return *this;
}
RetainPtr<T> get() const
RetainPtr<ValueType> get() const
{
return adoptNS(objc_loadWeakRetained(const_cast<id*>(&m_weakReference)));
}
ValueType *getAutoreleased() const
{
return static_cast<ValueType *>(objc_loadWeak(const_cast<id*>(&m_weakReference)));
}
private:
id m_weakReference;
};
......
2013-12-05 Anders Carlsson <andersca@apple.com>
Tweak WeakObjCPtr
https://bugs.webkit.org/show_bug.cgi?id=125311
Reviewed by Darin Adler.
Split up tests into more logical groups. Add new tests for new functionality.
* TestWebKitAPI/Tests/WebKit2/mac/WeakObjCPtr.mm:
2013-12-05 Carlos Garcia Campos <cgarcia@igalia.com>
[GTK] Fix gtk-doc warnings when generating DOM bindings API docs
......
......@@ -29,52 +29,80 @@
using WebKit::WeakObjCPtr;
TEST(WebKit2, WeakObjCPtr)
TEST(WebKit2_WeakObjCPtr, Construction)
{
// Test construction.
{
NSObject *object = [[NSObject alloc] init];
NSObject *object = [[NSObject alloc] init];
WeakObjCPtr<NSObject> weak(object);
WeakObjCPtr<NSObject> weak(object);
EXPECT_EQ(weak.get(), object);
EXPECT_EQ(weak.get(), object);
[object release];
[object release];
EXPECT_EQ(weak.get(), (void*)nil);
}
EXPECT_EQ(weak.get(), (void*)nil);
}
// Test assignment.
{
NSObject *object1 = [[NSObject alloc] init];
TEST(WebKit2_WeakObjCPtr, Assignment)
{
NSObject *object1 = [[NSObject alloc] init];
WeakObjCPtr<NSObject> weak(object1);
WeakObjCPtr<NSObject> weak(object1);
EXPECT_EQ(weak.get(), object1);
EXPECT_EQ(weak.get(), object1);
NSObject *object2 = [[NSObject alloc] init];
NSObject *object2 = [[NSObject alloc] init];
weak = object2;
EXPECT_EQ(weak.get(), object2);
weak = object2;
EXPECT_EQ(weak.get(), object2);
[object1 release];
EXPECT_EQ(weak.get(), object2);
[object1 release];
EXPECT_EQ(weak.get(), object2);
[object2 release];
EXPECT_EQ(weak.get(), (void*)nil);
}
[object2 release];
EXPECT_EQ(weak.get(), (void*)nil);
}
TEST(WebKit2_WeakObjCPtr, ObjectOutlivesItsWeakPointer)
{
NSObject *object = [[NSObject alloc] init];
// Test having an object that outlives its weak pointer.
{
NSObject *object = [[NSObject alloc] init];
WeakObjCPtr<NSObject> weak(object);
{
WeakObjCPtr<NSObject> weak(object);
EXPECT_EQ(weak.get(), object);
}
[object release];
}
TEST(WebKit2_WeakObjCPtr, GetAutoreleased)
{
WeakObjCPtr<NSObject> weak;
@autoreleasepool {
NSObject *object = [[NSObject alloc] init];
EXPECT_EQ(weak.get(), object);
}
weak = object;
EXPECT_EQ(weak.getAutoreleased(), object);
[object release];
// The object is still in the autorelease pool.
EXPECT_EQ(weak.getAutoreleased(), object);
}
EXPECT_EQ(weak.getAutoreleased(), (id)nil);
}
TEST(WebKit2_WeakObjCPtr, Id)
{
id object = [[NSObject alloc] init];
WeakObjCPtr<id> weak(object);
EXPECT_EQ(weak.get(), object);
[object release];
EXPECT_EQ(weak.get(), (void*)nil);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment