Skip to content
Permalink
Browse files
Add WKRetainPtr helper class as private header
https://bugs.webkit.org/show_bug.cgi?id=37603

Reviewed by Anders Carlsson.

WKRetainPtr is just like RetainPtr, but works for WK types instead of
CF/NS types.

* UIProcess/API/cpp: Added.
* UIProcess/API/cpp/WKRetainPtr.h: Added.
* WebKit2.xcodeproj/project.pbxproj: Add new file.
* win/WebKit2.vcproj: Ditto.



Canonical link: https://commits.webkit.org/48893@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@57605 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
weinig committed Apr 14, 2010
1 parent c231f2c commit 516e6249eaf3f79fdd8abde3891e0ba0fa6740e5
Showing 4 changed files with 249 additions and 0 deletions.
@@ -1,3 +1,18 @@
2010-04-14 Sam Weinig <sam@webkit.org>

Reviewed by Anders Carlsson.

Add WKRetainPtr helper class as private header
https://bugs.webkit.org/show_bug.cgi?id=37603

WKRetainPtr is just like RetainPtr, but works for WK types instead of
CF/NS types.

* UIProcess/API/cpp: Added.
* UIProcess/API/cpp/WKRetainPtr.h: Added.
* WebKit2.xcodeproj/project.pbxproj: Add new file.
* win/WebKit2.vcproj: Ditto.

2010-04-14 Sam Weinig <sam@webkit.org>

Reviewed by Anders Carlsson.
@@ -0,0 +1,214 @@
/*
* Copyright (C) 2010 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.
*/

#ifndef WKRetainPtr_h
#define WKRetainPtr_h

#include <WebKit2/WebKit2.h>

namespace WebKit {

// FIXME: Consider moving these to their respective headers instead of putting
// them all here.
#define DECLARE_RETAIN_RELEASE_OVERLOADS(WKType) \
inline void WKRetain(WKType##Ref p) { WKType##Retain(p); } \
inline void WKRelease(WKType##Ref p) { WKType##Release(p); } \
// end of macro

DECLARE_RETAIN_RELEASE_OVERLOADS(WKContext)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKFrame)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKFramePolicyListener)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKPage)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKPageNamespace)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKPreferences)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKString)
DECLARE_RETAIN_RELEASE_OVERLOADS(WKURL)

#undef DECLARE_RETAIN_RELEASE_OVERLOADS

enum WKAdoptTag { AdoptWK };

template <typename T>
class WKRetainPtr {
public:
typedef T PtrType;

WKRetainPtr()
: m_ptr(0)
{
}

WKRetainPtr(PtrType ptr)
: m_ptr(ptr)
{
if (ptr)
retainWKPtr(ptr);
}

WKRetainPtr(WKAdoptTag, PtrType ptr)
: m_ptr(ptr)
{
}

template <typename U>
WKRetainPtr(const WKRetainPtr<U>& o)
: m_ptr(o.get())
{
if (PtrType ptr = m_ptr)
WKRetain(ptr);
}

WKRetainPtr(const WKRetainPtr& o)
: m_ptr(o.m_ptr)
{
if (PtrType ptr = m_ptr)
WKRetain(ptr);
}

~RetainPtr()
{
if (PtrType ptr = m_ptr)
WKRelease(ptr);
}

PtrType get() const { return m_ptr; }
PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }

PtrType operator->() const { return m_ptr; }
bool operator!() const { return !m_ptr; }

// This conversion operator allows implicit conversion to bool but not to other integer types.
typedef PtrType WKRetainPtr::*UnspecifiedBoolType;
operator UnspecifiedBoolType() const { return m_ptr ? &WKRetainPtr::m_ptr : 0; }

WKRetainPtr& operator=(const WKRetainPtr&);
template <typename U> WKRetainPtr& operator=(const WKRetainPtr<U>&);
WKRetainPtr& operator=(PtrType);
template <typename U> WKRetainPtr& operator=(U*);

void adopt(PtrType);
void swap(WKRetainPtr&);

private:
PtrType m_ptr;
};

template <typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<T>& o)
{
PtrType optr = o.get();
if (optr)
WKRetain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
WKRelease(ptr);
return *this;
}

template <typename T> template <typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<U>& o)
{
PtrType optr = o.get();
if (optr)
WKRetain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
WKRelease(ptr);
return *this;
}

template <typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(PtrType optr)
{
if (optr)
WKRetain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
WKRelease(ptr);
return *this;
}

template <typename T> inline void WKRetainPtr<T>::adopt(PtrType optr)
{
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
WKRelease(ptr);
}

template <typename T> template <typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(U* optr)
{
if (optr)
WKRetain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
WKRelease(ptr);
return *this;
}

template <class T> inline void WKRetainPtr<T>::swap(WKRetainPtr<T>& o)
{
std::swap(m_ptr, o.m_ptr);
}

template <class T> inline void swap(WKRetainPtr<T>& a, WKRetainPtr<T>& b)
{
a.swap(b);
}

template <typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
{
return a.get() == b.get();
}

template <typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, U* b)
{
return a.get() == b;
}

template <typename T, typename U> inline bool operator==(T* a, const WKRetainPtr<U>& b)
{
return a == b.get();
}

template <typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
{
return a.get() != b.get();
}

template <typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, U* b)
{
return a.get() != b;
}

template <typename T, typename U> inline bool operator!=(T* a, const WKRetainPtr<U>& b)
{
return a != b.get();
}

} // namespace WebKit

#endif // WKRetainPtr_h
@@ -118,6 +118,7 @@
BC8699B7116AADAA002A925B /* WKViewInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = BC8699B4116AADAA002A925B /* WKViewInternal.h */; };
BC8780FC1161C2B800CC2768 /* PlatformProcessIdentifier.h in Headers */ = {isa = PBXBuildFile; fileRef = BC8780FB1161C2B800CC2768 /* PlatformProcessIdentifier.h */; };
BC87DFAA1018101400564216 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = BC87DFA91018101400564216 /* libicucore.dylib */; };
BC8A501511765F5600757573 /* WKRetainPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = BC8A501411765F5600757573 /* WKRetainPtr.h */; settings = {ATTRIBUTES = (Private, ); }; };
BC963D6B113DD19200574BE2 /* WebPage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BC963D6A113DD19200574BE2 /* WebPage.cpp */; };
BC963D6E113DD1A500574BE2 /* WebPageMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC963D6D113DD1A500574BE2 /* WebPageMac.mm */; };
BC9B38A110F538BE00443A15 /* WebFrameProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = BC9B389F10F538BE00443A15 /* WebFrameProxy.h */; };
@@ -307,6 +308,7 @@
BC8780FB1161C2B800CC2768 /* PlatformProcessIdentifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlatformProcessIdentifier.h; sourceTree = "<group>"; };
BC87DFA91018101400564216 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = /usr/lib/libicucore.dylib; sourceTree = "<absolute>"; };
BC8A501011765AF700757573 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = WebProcess/Info.plist; sourceTree = "<group>"; };
BC8A501411765F5600757573 /* WKRetainPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKRetainPtr.h; sourceTree = "<group>"; };
BC963D6A113DD19200574BE2 /* WebPage.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebPage.cpp; sourceTree = "<group>"; };
BC963D6D113DD1A500574BE2 /* WebPageMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebPageMac.mm; sourceTree = "<group>"; };
BC9B389F10F538BE00443A15 /* WebFrameProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebFrameProxy.h; sourceTree = "<group>"; };
@@ -609,6 +611,7 @@
children = (
BC111B47112F616900337BAB /* mac */,
BC0C376610F807660076D7CB /* C */,
BC8A501311765F4500757573 /* cpp */,
);
path = API;
sourceTree = "<group>";
@@ -760,6 +763,14 @@
name = WebProcess;
sourceTree = "<group>";
};
BC8A501311765F4500757573 /* cpp */ = {
isa = PBXGroup;
children = (
BC8A501411765F5600757573 /* WKRetainPtr.h */,
);
path = cpp;
sourceTree = "<group>";
};
BC963D6C113DD19500574BE2 /* mac */ = {
isa = PBXGroup;
children = (
@@ -869,6 +880,7 @@
BC20EC06116EF7FF00094A50 /* WKStringCF.h in Headers */,
BC20ED66116F971100094A50 /* WKURLCF.h in Headers */,
BC131BC911726C2800B69727 /* CoreIPCMessageKinds.h in Headers */,
BC8A501511765F5600757573 /* WKRetainPtr.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1440,6 +1440,14 @@
</FileConfiguration>
</File>
</Filter>
<Filter
Name="cpp"
>
<File
RelativePath="..\UIProcess\API\cpp\WKRetainPtr.h"
>
</File>
</Filter>
</Filter>
<Filter
Name="Launcher"

0 comments on commit 516e624

Please sign in to comment.