AlreadyAddRefed.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* Typed temporary pointers for reference-counted smart pointers. */
  7. #ifndef AlreadyAddRefed_h
  8. #define AlreadyAddRefed_h
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/Move.h"
  12. namespace mozilla {
  13. struct unused_t;
  14. } // namespace mozilla
  15. /**
  16. * already_AddRefed cooperates with reference counting smart pointers to enable
  17. * you to assign in a pointer _without_ |AddRef|ing it. You might want to use
  18. * this as a return type from a function that returns an already |AddRef|ed
  19. * pointer.
  20. *
  21. * TODO Move already_AddRefed to namespace mozilla. This has not yet been done
  22. * because of the sheer number of usages of already_AddRefed.
  23. */
  24. template<class T>
  25. struct MOZ_MUST_USE_TYPE MOZ_NON_AUTOABLE already_AddRefed
  26. {
  27. /*
  28. * We want to allow returning nullptr from functions returning
  29. * already_AddRefed<T>, for simplicity. But we also don't want to allow
  30. * returning raw T*, instead preferring creation of already_AddRefed<T> from
  31. * a reference counting smart pointer.
  32. *
  33. * We address the latter requirement by making the (T*) constructor explicit.
  34. * But |return nullptr| won't consider an explicit constructor, so we need
  35. * another constructor to handle it. Plain old (decltype(nullptr)) doesn't
  36. * cut it, because if nullptr is emulated as __null (with type int or long),
  37. * passing nullptr to an int/long parameter triggers compiler warnings. We
  38. * need a type that no one can pass accidentally; a pointer-to-member-function
  39. * (where no such function exists) does the trick nicely.
  40. *
  41. * That handles the return-value case. What about for locals, argument types,
  42. * and so on? |already_AddRefed<T>(nullptr)| considers both overloads (and
  43. * the (already_AddRefed<T>&&) overload as well!), so there's an ambiguity.
  44. * We can target true nullptr using decltype(nullptr), but we can't target
  45. * emulated nullptr the same way, because passing __null to an int/long
  46. * parameter triggers compiler warnings. So just give up on this, and provide
  47. * this behavior through the default constructor.
  48. *
  49. * We can revert to simply explicit (T*) and implicit (decltype(nullptr)) when
  50. * nullptr no longer needs to be emulated to support the ancient b2g compiler.
  51. * (The () overload could also be removed, if desired, if we changed callers.)
  52. */
  53. already_AddRefed() : mRawPtr(nullptr) {}
  54. // The return and argument types here are arbitrarily selected so no
  55. // corresponding member function exists.
  56. typedef void (already_AddRefed::* MatchNullptr)(double, float);
  57. MOZ_IMPLICIT already_AddRefed(MatchNullptr aRawPtr) : mRawPtr(nullptr) {}
  58. explicit already_AddRefed(T* aRawPtr) : mRawPtr(aRawPtr) {}
  59. // Disallow copy constructor and copy assignment operator: move semantics used instead.
  60. already_AddRefed(const already_AddRefed<T>& aOther) = delete;
  61. already_AddRefed<T>& operator=(const already_AddRefed<T>& aOther) = delete;
  62. already_AddRefed(already_AddRefed<T>&& aOther) : mRawPtr(aOther.take()) {}
  63. already_AddRefed<T>& operator=(already_AddRefed<T>&& aOther)
  64. {
  65. mRawPtr = aOther.take();
  66. return *this;
  67. }
  68. /**
  69. * This helper is useful in cases like
  70. *
  71. * already_AddRefed<BaseClass>
  72. * Foo()
  73. * {
  74. * RefPtr<SubClass> x = ...;
  75. * return x.forget();
  76. * }
  77. *
  78. * The autoconversion allows one to omit the idiom
  79. *
  80. * RefPtr<BaseClass> y = x.forget();
  81. * return y.forget();
  82. *
  83. * Note that nsRefPtr is the XPCOM reference counting smart pointer class.
  84. */
  85. template <typename U>
  86. MOZ_IMPLICIT already_AddRefed(already_AddRefed<U>&& aOther) : mRawPtr(aOther.take()) {}
  87. ~already_AddRefed() { MOZ_ASSERT(!mRawPtr); }
  88. // Specialize the unused operator<< for already_AddRefed, to allow
  89. // nsCOMPtr<nsIFoo> foo;
  90. // Unused << foo.forget();
  91. // Note that nsCOMPtr is the XPCOM reference counting smart pointer class.
  92. friend void operator<<(const mozilla::unused_t& aUnused,
  93. const already_AddRefed<T>& aRhs)
  94. {
  95. auto mutableAlreadyAddRefed = const_cast<already_AddRefed<T>*>(&aRhs);
  96. aUnused << mutableAlreadyAddRefed->take();
  97. }
  98. MOZ_MUST_USE T* take()
  99. {
  100. T* rawPtr = mRawPtr;
  101. mRawPtr = nullptr;
  102. return rawPtr;
  103. }
  104. /**
  105. * This helper provides a static_cast replacement for already_AddRefed, so
  106. * if you have
  107. *
  108. * already_AddRefed<Parent> F();
  109. *
  110. * you can write
  111. *
  112. * already_AddRefed<Child>
  113. * G()
  114. * {
  115. * return F().downcast<Child>();
  116. * }
  117. */
  118. template<class U>
  119. already_AddRefed<U> downcast()
  120. {
  121. U* tmp = static_cast<U*>(mRawPtr);
  122. mRawPtr = nullptr;
  123. return already_AddRefed<U>(tmp);
  124. }
  125. private:
  126. T* MOZ_OWNING_REF mRawPtr;
  127. };
  128. #endif // AlreadyAddRefed_h