/ API / JSRetainPtr.h
JSRetainPtr.h
  1  /*
  2   * Copyright (C) 2005-2020 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  
 29  #pragma once
 30  
 31  #include <JavaScriptCore/JSContextRef.h>
 32  #include <JavaScriptCore/JSObjectRef.h>
 33  #include <JavaScriptCore/JSStringRef.h>
 34  #include <algorithm>
 35  
 36  inline void JSRetain(JSClassRef context) { JSClassRetain(context); }
 37  inline void JSRelease(JSClassRef context) { JSClassRelease(context); }
 38  inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
 39  inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
 40  inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
 41  inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
 42  
 43  enum AdoptTag { Adopt };
 44  
 45  template<typename T> class JSRetainPtr {
 46  public:
 47      JSRetainPtr() = default;
 48      JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
 49      JSRetainPtr(const JSRetainPtr&);
 50      JSRetainPtr(JSRetainPtr&&);
 51      ~JSRetainPtr();
 52      
 53      T get() const { return m_ptr; }
 54      
 55      void clear();
 56      T leakRef() WARN_UNUSED_RETURN;
 57  
 58      T operator->() const { return m_ptr; }
 59      
 60      bool operator!() const { return !m_ptr; }
 61      explicit operator bool() const { return m_ptr; }
 62  
 63      JSRetainPtr& operator=(const JSRetainPtr&);
 64      JSRetainPtr& operator=(JSRetainPtr&&);
 65      JSRetainPtr& operator=(T);
 66  
 67      void swap(JSRetainPtr&);
 68  
 69      friend JSRetainPtr<JSStringRef> adopt(JSStringRef);
 70      friend JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef);
 71  
 72      // FIXME: Make this private once Apple's internal code is updated to not rely on it.
 73      // https://bugs.webkit.org/show_bug.cgi?id=189644
 74      JSRetainPtr(AdoptTag, T);
 75  
 76  private:
 77      T m_ptr { nullptr };
 78  };
 79  
 80  JSRetainPtr<JSClassRef> adopt(JSClassRef);
 81  JSRetainPtr<JSStringRef> adopt(JSStringRef);
 82  JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef);
 83  
 84  template<typename T> inline JSRetainPtr<T>::JSRetainPtr(AdoptTag, T ptr)
 85      : m_ptr(ptr)
 86  {
 87  }
 88  
 89  inline JSRetainPtr<JSClassRef> adopt(JSClassRef o)
 90  {
 91      return JSRetainPtr<JSClassRef>(Adopt, o);
 92  }
 93  
 94  inline JSRetainPtr<JSStringRef> adopt(JSStringRef o)
 95  {
 96      return JSRetainPtr<JSStringRef>(Adopt, o);
 97  }
 98  
 99  inline JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef o)
100  {
101      return JSRetainPtr<JSGlobalContextRef>(Adopt, o);
102  }
103  
104  template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
105      : m_ptr(o.m_ptr)
106  {
107      if (m_ptr)
108          JSRetain(m_ptr);
109  }
110  
111  template<typename T> inline JSRetainPtr<T>::JSRetainPtr(JSRetainPtr&& o)
112      : m_ptr(o.leakRef())
113  {
114  }
115  
116  template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
117  {
118      if (m_ptr)
119          JSRelease(m_ptr);
120  }
121  
122  template<typename T> inline void JSRetainPtr<T>::clear()
123  {
124      if (T ptr = leakRef())
125          JSRelease(ptr);
126  }
127  
128  template<typename T> inline T JSRetainPtr<T>::leakRef()
129  {
130      return std::exchange(m_ptr, nullptr);
131  }
132  
133  template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
134  {
135      return operator=(o.get());
136  }
137  
138  template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(JSRetainPtr&& o)
139  {
140      if (T ptr = std::exchange(m_ptr, o.leakRef()))
141          JSRelease(ptr);
142      return *this;
143  }
144  
145  template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
146  {
147      if (optr)
148          JSRetain(optr);
149      if (T ptr = std::exchange(m_ptr, optr))
150          JSRelease(ptr);
151      return *this;
152  }
153  
154  template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
155  {
156      std::swap(m_ptr, o.m_ptr);
157  }
158  
159  template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
160  {
161      a.swap(b);
162  }
163  
164  template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
165  { 
166      return a.get() == b.get(); 
167  }
168  
169  template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
170  { 
171      return a.get() == b; 
172  }
173  
174  template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b) 
175  {
176      return a == b.get(); 
177  }
178  
179  template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
180  { 
181      return a.get() != b.get(); 
182  }
183  
184  template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
185  {
186      return a.get() != b; 
187  }
188  
189  template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
190  { 
191      return a != b.get(); 
192  }