X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b37bf2e156556c589aea3e1f58a377f2b1189665..81345200c95645a1b0d2635520f96ad55dfde63f:/API/JSRetainPtr.h diff --git a/API/JSRetainPtr.h b/API/JSRetainPtr.h index 69c6de1..f23e32f 100644 --- a/API/JSRetainPtr.h +++ b/API/JSRetainPtr.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved. + * Copyright (C) 2005, 2006, 2007, 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 @@ -10,7 +10,7 @@ * 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. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -29,31 +29,31 @@ #ifndef JSRetainPtr_h #define JSRetainPtr_h +#include #include #include inline void JSRetain(JSStringRef string) { JSStringRetain(string); } inline void JSRelease(JSStringRef string) { JSStringRelease(string); } +inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); } +inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); } enum AdoptTag { Adopt }; -template class JSRetainPtr { +template class JSRetainPtr { public: - JSRetainPtr() : m_ptr(0) {} + JSRetainPtr() : m_ptr(0) { } JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); } - JSRetainPtr(AdoptTag, T ptr) : m_ptr(ptr) { } - - JSRetainPtr(const JSRetainPtr& o) : m_ptr(o.m_ptr) { if (T ptr = m_ptr) JSRetain(ptr); } - - ~JSRetainPtr() { if (T ptr = m_ptr) JSRelease(ptr); } - - template JSRetainPtr(const JSRetainPtr& o) : m_ptr(o.get()) { if (T ptr = m_ptr) JSRetain(ptr); } + JSRetainPtr(const JSRetainPtr&); + template JSRetainPtr(const JSRetainPtr&); + ~JSRetainPtr(); T get() const { return m_ptr; } - T releaseRef() { T tmp = m_ptr; m_ptr = 0; return tmp; } - + void clear(); + T leakRef(); + T operator->() const { return m_ptr; } bool operator!() const { return !m_ptr; } @@ -63,9 +63,9 @@ public: operator UnspecifiedBoolType() const { return m_ptr ? &JSRetainPtr::m_ptr : 0; } JSRetainPtr& operator=(const JSRetainPtr&); - template JSRetainPtr& operator=(const JSRetainPtr&); + template JSRetainPtr& operator=(const JSRetainPtr&); JSRetainPtr& operator=(T); - template JSRetainPtr& operator=(U*); + template JSRetainPtr& operator=(U*); void adopt(T); @@ -75,7 +75,42 @@ private: T m_ptr; }; -template inline JSRetainPtr& JSRetainPtr::operator=(const JSRetainPtr& o) +template inline JSRetainPtr::JSRetainPtr(const JSRetainPtr& o) + : m_ptr(o.m_ptr) +{ + if (m_ptr) + JSRetain(m_ptr); +} + +template template inline JSRetainPtr::JSRetainPtr(const JSRetainPtr& o) + : m_ptr(o.get()) +{ + if (m_ptr) + JSRetain(m_ptr); +} + +template inline JSRetainPtr::~JSRetainPtr() +{ + if (m_ptr) + JSRelease(m_ptr); +} + +template inline void JSRetainPtr::clear() +{ + if (T ptr = m_ptr) { + m_ptr = 0; + JSRelease(ptr); + } +} + +template inline T JSRetainPtr::leakRef() +{ + T ptr = m_ptr; + m_ptr = 0; + return ptr; +} + +template inline JSRetainPtr& JSRetainPtr::operator=(const JSRetainPtr& o) { T optr = o.get(); if (optr) @@ -87,7 +122,7 @@ template inline JSRetainPtr& JSRetainPtr::operator=(const JSR return *this; } -template template inline JSRetainPtr& JSRetainPtr::operator=(const JSRetainPtr& o) +template template inline JSRetainPtr& JSRetainPtr::operator=(const JSRetainPtr& o) { T optr = o.get(); if (optr) @@ -99,7 +134,7 @@ template template inline JSRetainPtr& JSRetainPtr inline JSRetainPtr& JSRetainPtr::operator=(T optr) +template inline JSRetainPtr& JSRetainPtr::operator=(T optr) { if (optr) JSRetain(optr); @@ -110,7 +145,7 @@ template inline JSRetainPtr& JSRetainPtr::operator=(T optr) return *this; } -template inline void JSRetainPtr::adopt(T optr) +template inline void JSRetainPtr::adopt(T optr) { T ptr = m_ptr; m_ptr = optr; @@ -118,7 +153,7 @@ template inline void JSRetainPtr::adopt(T optr) JSRelease(ptr); } -template template inline JSRetainPtr& JSRetainPtr::operator=(U* optr) +template template inline JSRetainPtr& JSRetainPtr::operator=(U* optr) { if (optr) JSRetain(optr); @@ -129,42 +164,42 @@ template template inline JSRetainPtr& JSRetainPtr inline void JSRetainPtr::swap(JSRetainPtr& o) +template inline void JSRetainPtr::swap(JSRetainPtr& o) { std::swap(m_ptr, o.m_ptr); } -template inline void swap(JSRetainPtr& a, JSRetainPtr& b) +template inline void swap(JSRetainPtr& a, JSRetainPtr& b) { a.swap(b); } -template inline bool operator==(const JSRetainPtr& a, const JSRetainPtr& b) +template inline bool operator==(const JSRetainPtr& a, const JSRetainPtr& b) { return a.get() == b.get(); } -template inline bool operator==(const JSRetainPtr& a, U* b) +template inline bool operator==(const JSRetainPtr& a, U* b) { return a.get() == b; } -template inline bool operator==(T* a, const JSRetainPtr& b) +template inline bool operator==(T* a, const JSRetainPtr& b) { return a == b.get(); } -template inline bool operator!=(const JSRetainPtr& a, const JSRetainPtr& b) +template inline bool operator!=(const JSRetainPtr& a, const JSRetainPtr& b) { return a.get() != b.get(); } -template inline bool operator!=(const JSRetainPtr& a, U* b) +template inline bool operator!=(const JSRetainPtr& a, U* b) { return a.get() != b; } -template inline bool operator!=(T* a, const JSRetainPtr& b) +template inline bool operator!=(T* a, const JSRetainPtr& b) { return a != b.get(); }