muon/include/JavaScriptCore/JSRetainPtr.h

188 lines
5.4 KiB
C
Raw Normal View History

2019-09-26 15:33:10 -05:00
/*
2022-10-03 02:11:02 -05:00
* Copyright (C) 2005-2018 Apple Inc. All rights reserved.
2019-09-26 15:33:10 -05:00
*
* 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.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
*/
2022-10-03 02:11:02 -05:00
#pragma once
2019-09-26 15:33:10 -05:00
#include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSStringRef.h>
#include <algorithm>
2022-10-03 02:11:02 -05:00
#if !defined(WARN_UNUSED_RETURN)
#define WARN_UNUSED_RETURN
#endif
2019-09-26 15:33:10 -05:00
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<typename T> class JSRetainPtr {
public:
2022-10-03 02:11:02 -05:00
JSRetainPtr() = default;
2019-09-26 15:33:10 -05:00
JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
JSRetainPtr(const JSRetainPtr&);
2022-10-03 02:11:02 -05:00
JSRetainPtr(JSRetainPtr&&);
2019-09-26 15:33:10 -05:00
~JSRetainPtr();
T get() const { return m_ptr; }
void clear();
2022-10-03 02:11:02 -05:00
T leakRef() WARN_UNUSED_RETURN;
2019-09-26 15:33:10 -05:00
T operator->() const { return m_ptr; }
bool operator!() const { return !m_ptr; }
explicit operator bool() const { return m_ptr; }
JSRetainPtr& operator=(const JSRetainPtr&);
2022-10-03 02:11:02 -05:00
JSRetainPtr& operator=(JSRetainPtr&&);
2019-09-26 15:33:10 -05:00
JSRetainPtr& operator=(T);
void swap(JSRetainPtr&);
2022-10-03 02:11:02 -05:00
friend JSRetainPtr<JSStringRef> adopt(JSStringRef);
friend JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef);
// FIXME: Make this private once Apple's internal code is updated to not rely on it.
// https://bugs.webkit.org/show_bug.cgi?id=189644
JSRetainPtr(AdoptTag, T);
2019-09-26 15:33:10 -05:00
private:
2022-10-03 02:11:02 -05:00
T m_ptr { nullptr };
2019-09-26 15:33:10 -05:00
};
2022-10-03 02:11:02 -05:00
JSRetainPtr<JSStringRef> adopt(JSStringRef);
JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef);
template<typename T> inline JSRetainPtr<T>::JSRetainPtr(AdoptTag, T ptr)
: m_ptr(ptr)
{
}
2019-09-26 15:33:10 -05:00
inline JSRetainPtr<JSStringRef> adopt(JSStringRef o)
{
return JSRetainPtr<JSStringRef>(Adopt, o);
}
inline JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef o)
{
return JSRetainPtr<JSGlobalContextRef>(Adopt, o);
}
template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
: m_ptr(o.m_ptr)
{
if (m_ptr)
JSRetain(m_ptr);
}
2022-10-03 02:11:02 -05:00
template<typename T> inline JSRetainPtr<T>::JSRetainPtr(JSRetainPtr&& o)
: m_ptr(o.leakRef())
2019-09-26 15:33:10 -05:00
{
}
template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
{
if (m_ptr)
JSRelease(m_ptr);
}
template<typename T> inline void JSRetainPtr<T>::clear()
{
2022-10-03 02:11:02 -05:00
if (T ptr = leakRef())
2019-09-26 15:33:10 -05:00
JSRelease(ptr);
}
template<typename T> inline T JSRetainPtr<T>::leakRef()
{
2022-10-03 02:11:02 -05:00
return std::exchange(m_ptr, nullptr);
2019-09-26 15:33:10 -05:00
}
template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
{
2022-10-03 02:11:02 -05:00
return operator=(o.get());
2019-09-26 15:33:10 -05:00
}
2022-10-03 02:11:02 -05:00
template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(JSRetainPtr&& o)
2019-09-26 15:33:10 -05:00
{
2022-10-03 02:11:02 -05:00
if (T ptr = std::exchange(m_ptr, o.leakRef()))
2019-09-26 15:33:10 -05:00
JSRelease(ptr);
return *this;
}
template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
{
if (optr)
JSRetain(optr);
2022-10-03 02:11:02 -05:00
if (T ptr = std::exchange(m_ptr, optr))
2019-09-26 15:33:10 -05:00
JSRelease(ptr);
return *this;
}
template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
{
std::swap(m_ptr, o.m_ptr);
}
template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
{
a.swap(b);
}
template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
{
return a.get() == b.get();
}
template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
{
return a.get() == b;
}
template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b)
{
return a == b.get();
}
template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
{
return a.get() != b.get();
}
template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
{
return a.get() != b;
}
template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
{
return a != b.get();
}