JSRetainPtr.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2005-2018 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #pragma once
  29. #include <JavaScriptCore/JSContextRef.h>
  30. #include <JavaScriptCore/JSStringRef.h>
  31. #include <algorithm>
  32. inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
  33. inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
  34. inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
  35. inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
  36. enum AdoptTag { Adopt };
  37. template<typename T> class JSRetainPtr {
  38. public:
  39. JSRetainPtr() = default;
  40. JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
  41. JSRetainPtr(const JSRetainPtr&);
  42. JSRetainPtr(JSRetainPtr&&);
  43. ~JSRetainPtr();
  44. T get() const { return m_ptr; }
  45. void clear();
  46. T leakRef() WARN_UNUSED_RETURN;
  47. T operator->() const { return m_ptr; }
  48. bool operator!() const { return !m_ptr; }
  49. explicit operator bool() const { return m_ptr; }
  50. JSRetainPtr& operator=(const JSRetainPtr&);
  51. JSRetainPtr& operator=(JSRetainPtr&&);
  52. JSRetainPtr& operator=(T);
  53. void swap(JSRetainPtr&);
  54. friend JSRetainPtr<JSStringRef> adopt(JSStringRef);
  55. friend JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef);
  56. // FIXME: Make this private once Apple's internal code is updated to not rely on it.
  57. // https://bugs.webkit.org/show_bug.cgi?id=189644
  58. JSRetainPtr(AdoptTag, T);
  59. private:
  60. T m_ptr { nullptr };
  61. };
  62. JSRetainPtr<JSStringRef> adopt(JSStringRef);
  63. JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef);
  64. template<typename T> inline JSRetainPtr<T>::JSRetainPtr(AdoptTag, T ptr)
  65. : m_ptr(ptr)
  66. {
  67. }
  68. inline JSRetainPtr<JSStringRef> adopt(JSStringRef o)
  69. {
  70. return JSRetainPtr<JSStringRef>(Adopt, o);
  71. }
  72. inline JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef o)
  73. {
  74. return JSRetainPtr<JSGlobalContextRef>(Adopt, o);
  75. }
  76. template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
  77. : m_ptr(o.m_ptr)
  78. {
  79. if (m_ptr)
  80. JSRetain(m_ptr);
  81. }
  82. template<typename T> inline JSRetainPtr<T>::JSRetainPtr(JSRetainPtr&& o)
  83. : m_ptr(o.leakRef())
  84. {
  85. }
  86. template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
  87. {
  88. if (m_ptr)
  89. JSRelease(m_ptr);
  90. }
  91. template<typename T> inline void JSRetainPtr<T>::clear()
  92. {
  93. if (T ptr = leakRef())
  94. JSRelease(ptr);
  95. }
  96. template<typename T> inline T JSRetainPtr<T>::leakRef()
  97. {
  98. return std::exchange(m_ptr, nullptr);
  99. }
  100. template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
  101. {
  102. return operator=(o.get());
  103. }
  104. template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(JSRetainPtr&& o)
  105. {
  106. if (T ptr = std::exchange(m_ptr, o.leakRef()))
  107. JSRelease(ptr);
  108. return *this;
  109. }
  110. template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
  111. {
  112. if (optr)
  113. JSRetain(optr);
  114. if (T ptr = std::exchange(m_ptr, optr))
  115. JSRelease(ptr);
  116. return *this;
  117. }
  118. template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
  119. {
  120. std::swap(m_ptr, o.m_ptr);
  121. }
  122. template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
  123. {
  124. a.swap(b);
  125. }
  126. template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
  127. {
  128. return a.get() == b.get();
  129. }
  130. template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
  131. {
  132. return a.get() == b;
  133. }
  134. template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b)
  135. {
  136. return a == b.get();
  137. }
  138. template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
  139. {
  140. return a.get() != b.get();
  141. }
  142. template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
  143. {
  144. return a.get() != b;
  145. }
  146. template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
  147. {
  148. return a != b.get();
  149. }