RefPtr.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_RefPtr_h
  6. #define mozilla_RefPtr_h
  7. #include "mozilla/AlreadyAddRefed.h"
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/Attributes.h"
  10. /*****************************************************************************/
  11. // template <class T> class RefPtrGetterAddRefs;
  12. class nsCOMPtr_helper;
  13. namespace mozilla {
  14. template<class T> class OwningNonNull;
  15. template<class T> class StaticRefPtr;
  16. // Traditionally, RefPtr supports automatic refcounting of any pointer type
  17. // with AddRef() and Release() methods that follow the traditional semantics.
  18. //
  19. // This traits class can be specialized to operate on other pointer types. For
  20. // example, we specialize this trait for opaque FFI types that represent
  21. // refcounted objects in Rust.
  22. //
  23. // Given the use of ConstRemovingRefPtrTraits below, U should not be a const-
  24. // qualified type.
  25. template<class U>
  26. struct RefPtrTraits
  27. {
  28. static void AddRef(U* aPtr) {
  29. aPtr->AddRef();
  30. }
  31. static void Release(U* aPtr) {
  32. aPtr->Release();
  33. }
  34. };
  35. } // namespace mozilla
  36. template <class T>
  37. class RefPtr
  38. {
  39. private:
  40. void
  41. assign_with_AddRef(T* aRawPtr)
  42. {
  43. if (aRawPtr) {
  44. ConstRemovingRefPtrTraits<T>::AddRef(aRawPtr);
  45. }
  46. assign_assuming_AddRef(aRawPtr);
  47. }
  48. void
  49. assign_assuming_AddRef(T* aNewPtr)
  50. {
  51. T* oldPtr = mRawPtr;
  52. mRawPtr = aNewPtr;
  53. if (oldPtr) {
  54. ConstRemovingRefPtrTraits<T>::Release(oldPtr);
  55. }
  56. }
  57. private:
  58. T* MOZ_OWNING_REF mRawPtr;
  59. public:
  60. typedef T element_type;
  61. ~RefPtr()
  62. {
  63. if (mRawPtr) {
  64. ConstRemovingRefPtrTraits<T>::Release(mRawPtr);
  65. }
  66. }
  67. // Constructors
  68. RefPtr()
  69. : mRawPtr(nullptr)
  70. // default constructor
  71. {
  72. }
  73. RefPtr(const RefPtr<T>& aSmartPtr)
  74. : mRawPtr(aSmartPtr.mRawPtr)
  75. // copy-constructor
  76. {
  77. if (mRawPtr) {
  78. ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
  79. }
  80. }
  81. RefPtr(RefPtr<T>&& aRefPtr)
  82. : mRawPtr(aRefPtr.mRawPtr)
  83. {
  84. aRefPtr.mRawPtr = nullptr;
  85. }
  86. // construct from a raw pointer (of the right type)
  87. MOZ_IMPLICIT RefPtr(T* aRawPtr)
  88. : mRawPtr(aRawPtr)
  89. {
  90. if (mRawPtr) {
  91. ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
  92. }
  93. }
  94. MOZ_IMPLICIT RefPtr(decltype(nullptr))
  95. : mRawPtr(nullptr)
  96. {
  97. }
  98. template <typename I>
  99. MOZ_IMPLICIT RefPtr(already_AddRefed<I>& aSmartPtr)
  100. : mRawPtr(aSmartPtr.take())
  101. // construct from |already_AddRefed|
  102. {
  103. }
  104. template <typename I>
  105. MOZ_IMPLICIT RefPtr(already_AddRefed<I>&& aSmartPtr)
  106. : mRawPtr(aSmartPtr.take())
  107. // construct from |otherRefPtr.forget()|
  108. {
  109. }
  110. template <typename I>
  111. MOZ_IMPLICIT RefPtr(const RefPtr<I>& aSmartPtr)
  112. : mRawPtr(aSmartPtr.get())
  113. // copy-construct from a smart pointer with a related pointer type
  114. {
  115. if (mRawPtr) {
  116. ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
  117. }
  118. }
  119. template <typename I>
  120. MOZ_IMPLICIT RefPtr(RefPtr<I>&& aSmartPtr)
  121. : mRawPtr(aSmartPtr.forget().take())
  122. // construct from |Move(RefPtr<SomeSubclassOfT>)|.
  123. {
  124. }
  125. MOZ_IMPLICIT RefPtr(const nsCOMPtr_helper& aHelper);
  126. // Defined in OwningNonNull.h
  127. template<class U>
  128. MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther);
  129. // Defined in StaticPtr.h
  130. template<class U>
  131. MOZ_IMPLICIT RefPtr(const mozilla::StaticRefPtr<U>& aOther);
  132. // Assignment operators
  133. RefPtr<T>&
  134. operator=(decltype(nullptr))
  135. {
  136. assign_assuming_AddRef(nullptr);
  137. return *this;
  138. }
  139. RefPtr<T>&
  140. operator=(const RefPtr<T>& aRhs)
  141. // copy assignment operator
  142. {
  143. assign_with_AddRef(aRhs.mRawPtr);
  144. return *this;
  145. }
  146. template <typename I>
  147. RefPtr<T>&
  148. operator=(const RefPtr<I>& aRhs)
  149. // assign from an RefPtr of a related pointer type
  150. {
  151. assign_with_AddRef(aRhs.get());
  152. return *this;
  153. }
  154. RefPtr<T>&
  155. operator=(T* aRhs)
  156. // assign from a raw pointer (of the right type)
  157. {
  158. assign_with_AddRef(aRhs);
  159. return *this;
  160. }
  161. template <typename I>
  162. RefPtr<T>&
  163. operator=(already_AddRefed<I>& aRhs)
  164. // assign from |already_AddRefed|
  165. {
  166. assign_assuming_AddRef(aRhs.take());
  167. return *this;
  168. }
  169. template <typename I>
  170. RefPtr<T>&
  171. operator=(already_AddRefed<I> && aRhs)
  172. // assign from |otherRefPtr.forget()|
  173. {
  174. assign_assuming_AddRef(aRhs.take());
  175. return *this;
  176. }
  177. RefPtr<T>& operator=(const nsCOMPtr_helper& aHelper);
  178. RefPtr<T>&
  179. operator=(RefPtr<T> && aRefPtr)
  180. {
  181. assign_assuming_AddRef(aRefPtr.mRawPtr);
  182. aRefPtr.mRawPtr = nullptr;
  183. return *this;
  184. }
  185. // Defined in OwningNonNull.h
  186. template<class U>
  187. RefPtr<T>&
  188. operator=(const mozilla::OwningNonNull<U>& aOther);
  189. // Defined in StaticPtr.h
  190. template<class U>
  191. RefPtr<T>&
  192. operator=(const mozilla::StaticRefPtr<U>& aOther);
  193. // Other pointer operators
  194. void
  195. swap(RefPtr<T>& aRhs)
  196. // ...exchange ownership with |aRhs|; can save a pair of refcount operations
  197. {
  198. T* temp = aRhs.mRawPtr;
  199. aRhs.mRawPtr = mRawPtr;
  200. mRawPtr = temp;
  201. }
  202. void
  203. swap(T*& aRhs)
  204. // ...exchange ownership with |aRhs|; can save a pair of refcount operations
  205. {
  206. T* temp = aRhs;
  207. aRhs = mRawPtr;
  208. mRawPtr = temp;
  209. }
  210. already_AddRefed<T>
  211. forget()
  212. // return the value of mRawPtr and null out mRawPtr. Useful for
  213. // already_AddRefed return values.
  214. {
  215. T* temp = nullptr;
  216. swap(temp);
  217. return already_AddRefed<T>(temp);
  218. }
  219. template <typename I>
  220. void
  221. forget(I** aRhs)
  222. // Set the target of aRhs to the value of mRawPtr and null out mRawPtr.
  223. // Useful to avoid unnecessary AddRef/Release pairs with "out"
  224. // parameters where aRhs bay be a T** or an I** where I is a base class
  225. // of T.
  226. {
  227. MOZ_ASSERT(aRhs, "Null pointer passed to forget!");
  228. *aRhs = mRawPtr;
  229. mRawPtr = nullptr;
  230. }
  231. T*
  232. get() const
  233. /*
  234. Prefer the implicit conversion provided automatically by |operator T*() const|.
  235. Use |get()| to resolve ambiguity or to get a castable pointer.
  236. */
  237. {
  238. return const_cast<T*>(mRawPtr);
  239. }
  240. operator T*() const
  241. #ifdef MOZ_HAVE_REF_QUALIFIERS
  242. &
  243. #endif
  244. /*
  245. ...makes an |RefPtr| act like its underlying raw pointer type whenever it
  246. is used in a context where a raw pointer is expected. It is this operator
  247. that makes an |RefPtr| substitutable for a raw pointer.
  248. Prefer the implicit use of this operator to calling |get()|, except where
  249. necessary to resolve ambiguity.
  250. */
  251. {
  252. return get();
  253. }
  254. #ifdef MOZ_HAVE_REF_QUALIFIERS
  255. // Don't allow implicit conversion of temporary RefPtr to raw pointer,
  256. // because the refcount might be one and the pointer will immediately become
  257. // invalid.
  258. operator T*() const && = delete;
  259. // These are needed to avoid the deleted operator above. XXX Why is operator!
  260. // needed separately? Shouldn't the compiler prefer using the non-deleted
  261. // operator bool instead of the deleted operator T*?
  262. explicit operator bool() const { return !!mRawPtr; }
  263. bool operator!() const { return !mRawPtr; }
  264. #endif
  265. T*
  266. operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
  267. {
  268. MOZ_ASSERT(mRawPtr != nullptr,
  269. "You can't dereference a NULL RefPtr with operator->().");
  270. return get();
  271. }
  272. template <typename R, typename... Args>
  273. class Proxy
  274. {
  275. typedef R (T::*member_function)(Args...);
  276. T* mRawPtr;
  277. member_function mFunction;
  278. public:
  279. Proxy(T* aRawPtr, member_function aFunction)
  280. : mRawPtr(aRawPtr),
  281. mFunction(aFunction)
  282. {
  283. }
  284. template<typename... ActualArgs>
  285. R operator()(ActualArgs&&... aArgs)
  286. {
  287. return ((*mRawPtr).*mFunction)(mozilla::Forward<ActualArgs>(aArgs)...);
  288. }
  289. };
  290. template <typename R, typename... Args>
  291. Proxy<R, Args...> operator->*(R (T::*aFptr)(Args...)) const
  292. {
  293. MOZ_ASSERT(mRawPtr != nullptr,
  294. "You can't dereference a NULL RefPtr with operator->*().");
  295. return Proxy<R, Args...>(get(), aFptr);
  296. }
  297. RefPtr<T>*
  298. get_address()
  299. // This is not intended to be used by clients. See |address_of|
  300. // below.
  301. {
  302. return this;
  303. }
  304. const RefPtr<T>*
  305. get_address() const
  306. // This is not intended to be used by clients. See |address_of|
  307. // below.
  308. {
  309. return this;
  310. }
  311. public:
  312. T&
  313. operator*() const
  314. {
  315. MOZ_ASSERT(mRawPtr != nullptr,
  316. "You can't dereference a NULL RefPtr with operator*().");
  317. return *get();
  318. }
  319. T**
  320. StartAssignment()
  321. {
  322. assign_assuming_AddRef(nullptr);
  323. return reinterpret_cast<T**>(&mRawPtr);
  324. }
  325. private:
  326. // This helper class makes |RefPtr<const T>| possible by casting away
  327. // the constness from the pointer when calling AddRef() and Release().
  328. //
  329. // This is necessary because AddRef() and Release() implementations can't
  330. // generally expected to be const themselves (without heavy use of |mutable|
  331. // and |const_cast| in their own implementations).
  332. //
  333. // This should be sound because while |RefPtr<const T>| provides a
  334. // const view of an object, the object itself should not be const (it
  335. // would have to be allocated as |new const T| or similar to be const).
  336. template<class U>
  337. struct ConstRemovingRefPtrTraits
  338. {
  339. static void AddRef(U* aPtr) {
  340. mozilla::RefPtrTraits<U>::AddRef(aPtr);
  341. }
  342. static void Release(U* aPtr) {
  343. mozilla::RefPtrTraits<U>::Release(aPtr);
  344. }
  345. };
  346. template<class U>
  347. struct ConstRemovingRefPtrTraits<const U>
  348. {
  349. static void AddRef(const U* aPtr) {
  350. mozilla::RefPtrTraits<U>::AddRef(const_cast<U*>(aPtr));
  351. }
  352. static void Release(const U* aPtr) {
  353. mozilla::RefPtrTraits<U>::Release(const_cast<U*>(aPtr));
  354. }
  355. };
  356. };
  357. class nsCycleCollectionTraversalCallback;
  358. template <typename T>
  359. void
  360. CycleCollectionNoteChild(nsCycleCollectionTraversalCallback& aCallback,
  361. T* aChild, const char* aName, uint32_t aFlags);
  362. template <typename T>
  363. inline void
  364. ImplCycleCollectionUnlink(RefPtr<T>& aField)
  365. {
  366. aField = nullptr;
  367. }
  368. template <typename T>
  369. inline void
  370. ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
  371. RefPtr<T>& aField,
  372. const char* aName,
  373. uint32_t aFlags = 0)
  374. {
  375. CycleCollectionNoteChild(aCallback, aField.get(), aName, aFlags);
  376. }
  377. template <class T>
  378. inline RefPtr<T>*
  379. address_of(RefPtr<T>& aPtr)
  380. {
  381. return aPtr.get_address();
  382. }
  383. template <class T>
  384. inline const RefPtr<T>*
  385. address_of(const RefPtr<T>& aPtr)
  386. {
  387. return aPtr.get_address();
  388. }
  389. template <class T>
  390. class RefPtrGetterAddRefs
  391. /*
  392. ...
  393. This class is designed to be used for anonymous temporary objects in the
  394. argument list of calls that return COM interface pointers, e.g.,
  395. RefPtr<IFoo> fooP;
  396. ...->GetAddRefedPointer(getter_AddRefs(fooP))
  397. DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| instead.
  398. When initialized with a |RefPtr|, as in the example above, it returns
  399. a |void**|, a |T**|, or an |nsISupports**| as needed, that the
  400. outer call (|GetAddRefedPointer| in this case) can fill in.
  401. This type should be a nested class inside |RefPtr<T>|.
  402. */
  403. {
  404. public:
  405. explicit
  406. RefPtrGetterAddRefs(RefPtr<T>& aSmartPtr)
  407. : mTargetSmartPtr(aSmartPtr)
  408. {
  409. // nothing else to do
  410. }
  411. operator void**()
  412. {
  413. return reinterpret_cast<void**>(mTargetSmartPtr.StartAssignment());
  414. }
  415. operator T**()
  416. {
  417. return mTargetSmartPtr.StartAssignment();
  418. }
  419. T*&
  420. operator*()
  421. {
  422. return *(mTargetSmartPtr.StartAssignment());
  423. }
  424. private:
  425. RefPtr<T>& mTargetSmartPtr;
  426. };
  427. template <class T>
  428. inline RefPtrGetterAddRefs<T>
  429. getter_AddRefs(RefPtr<T>& aSmartPtr)
  430. /*
  431. Used around a |RefPtr| when
  432. ...makes the class |RefPtrGetterAddRefs<T>| invisible.
  433. */
  434. {
  435. return RefPtrGetterAddRefs<T>(aSmartPtr);
  436. }
  437. // Comparing two |RefPtr|s
  438. template <class T, class U>
  439. inline bool
  440. operator==(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
  441. {
  442. return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs.get());
  443. }
  444. template <class T, class U>
  445. inline bool
  446. operator!=(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
  447. {
  448. return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs.get());
  449. }
  450. // Comparing an |RefPtr| to a raw pointer
  451. template <class T, class U>
  452. inline bool
  453. operator==(const RefPtr<T>& aLhs, const U* aRhs)
  454. {
  455. return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs);
  456. }
  457. template <class T, class U>
  458. inline bool
  459. operator==(const U* aLhs, const RefPtr<T>& aRhs)
  460. {
  461. return static_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
  462. }
  463. template <class T, class U>
  464. inline bool
  465. operator!=(const RefPtr<T>& aLhs, const U* aRhs)
  466. {
  467. return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs);
  468. }
  469. template <class T, class U>
  470. inline bool
  471. operator!=(const U* aLhs, const RefPtr<T>& aRhs)
  472. {
  473. return static_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
  474. }
  475. template <class T, class U>
  476. inline bool
  477. operator==(const RefPtr<T>& aLhs, U* aRhs)
  478. {
  479. return static_cast<const T*>(aLhs.get()) == const_cast<const U*>(aRhs);
  480. }
  481. template <class T, class U>
  482. inline bool
  483. operator==(U* aLhs, const RefPtr<T>& aRhs)
  484. {
  485. return const_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
  486. }
  487. template <class T, class U>
  488. inline bool
  489. operator!=(const RefPtr<T>& aLhs, U* aRhs)
  490. {
  491. return static_cast<const T*>(aLhs.get()) != const_cast<const U*>(aRhs);
  492. }
  493. template <class T, class U>
  494. inline bool
  495. operator!=(U* aLhs, const RefPtr<T>& aRhs)
  496. {
  497. return const_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
  498. }
  499. // Comparing an |RefPtr| to |nullptr|
  500. template <class T>
  501. inline bool
  502. operator==(const RefPtr<T>& aLhs, decltype(nullptr))
  503. {
  504. return aLhs.get() == nullptr;
  505. }
  506. template <class T>
  507. inline bool
  508. operator==(decltype(nullptr), const RefPtr<T>& aRhs)
  509. {
  510. return nullptr == aRhs.get();
  511. }
  512. template <class T>
  513. inline bool
  514. operator!=(const RefPtr<T>& aLhs, decltype(nullptr))
  515. {
  516. return aLhs.get() != nullptr;
  517. }
  518. template <class T>
  519. inline bool
  520. operator!=(decltype(nullptr), const RefPtr<T>& aRhs)
  521. {
  522. return nullptr != aRhs.get();
  523. }
  524. /*****************************************************************************/
  525. template <class T>
  526. inline already_AddRefed<T>
  527. do_AddRef(T* aObj)
  528. {
  529. RefPtr<T> ref(aObj);
  530. return ref.forget();
  531. }
  532. template <class T>
  533. inline already_AddRefed<T>
  534. do_AddRef(const RefPtr<T>& aObj)
  535. {
  536. RefPtr<T> ref(aObj);
  537. return ref.forget();
  538. }
  539. namespace mozilla {
  540. /**
  541. * Helper function to be able to conveniently write things like:
  542. *
  543. * already_AddRefed<T>
  544. * f(...)
  545. * {
  546. * return MakeAndAddRef<T>(...);
  547. * }
  548. */
  549. template<typename T, typename... Args>
  550. already_AddRefed<T>
  551. MakeAndAddRef(Args&&... aArgs)
  552. {
  553. RefPtr<T> p(new T(Forward<Args>(aArgs)...));
  554. return p.forget();
  555. }
  556. } // namespace mozilla
  557. #endif /* mozilla_RefPtr_h */