NotNull.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_NotNull_h
  6. #define mozilla_NotNull_h
  7. // It's often unclear if a particular pointer, be it raw (T*) or smart
  8. // (RefPtr<T>, nsCOMPtr<T>, etc.) can be null. This leads to missing null
  9. // checks (which can cause crashes) and unnecessary null checks (which clutter
  10. // the code).
  11. //
  12. // C++ has a built-in alternative that avoids these problems: references. This
  13. // module defines another alternative, NotNull, which can be used in cases
  14. // where references are not suitable.
  15. //
  16. // In the comments below we use the word "handle" to cover all varieties of
  17. // pointers and references.
  18. //
  19. // References
  20. // ----------
  21. // References are always non-null. (You can do |T& r = *p;| where |p| is null,
  22. // but that's undefined behaviour. C++ doesn't provide any built-in, ironclad
  23. // guarantee of non-nullness.)
  24. //
  25. // A reference works well when you need a temporary handle to an existing
  26. // single object, e.g. for passing a handle to a function, or as a local handle
  27. // within another object. (In Rust parlance, this is a "borrow".)
  28. //
  29. // A reference is less appropriate in the following cases.
  30. //
  31. // - As a primary handle to an object. E.g. code such as this is possible but
  32. // strange: |T& t = *new T(); ...; delete &t;|
  33. //
  34. // - As a handle to an array. It's common for |T*| to refer to either a single
  35. // |T| or an array of |T|, but |T&| cannot refer to an array of |T| because
  36. // you can't index off a reference (at least, not without first converting it
  37. // to a pointer).
  38. //
  39. // - When the handle identity is meaningful, e.g. if you have a hashtable of
  40. // handles, because you have to use |&| on the reference to convert it to a
  41. // pointer.
  42. //
  43. // - Some people don't like using non-const references as function parameters,
  44. // because it is not clear at the call site that the argument might be
  45. // modified.
  46. //
  47. // - When you need "smart" behaviour. E.g. we lack reference equivalents to
  48. // RefPtr and nsCOMPtr.
  49. //
  50. // - When interfacing with code that uses pointers a lot, sometimes using a
  51. // reference just feels like an odd fit.
  52. //
  53. // Furthermore, a reference is impossible in the following cases.
  54. //
  55. // - When the handle is rebound to another object. References don't allow this.
  56. //
  57. // - When the handle has type |void|. |void&| is not allowed.
  58. //
  59. // NotNull is an alternative that can be used in any of the above cases except
  60. // for the last one, where the handle type is |void|. See below.
  61. #include "mozilla/Assertions.h"
  62. namespace mozilla {
  63. // NotNull can be used to wrap a "base" pointer (raw or smart) to indicate it
  64. // is not null. Some examples:
  65. //
  66. // - NotNull<char*>
  67. // - NotNull<RefPtr<Event>>
  68. // - NotNull<nsCOMPtr<Event>>
  69. //
  70. // NotNull has the following notable properties.
  71. //
  72. // - It has zero space overhead.
  73. //
  74. // - It must be initialized explicitly. There is no default initialization.
  75. //
  76. // - It auto-converts to the base pointer type.
  77. //
  78. // - It does not auto-convert from a base pointer. Implicit conversion from a
  79. // less-constrained type (e.g. T*) to a more-constrained type (e.g.
  80. // NotNull<T*>) is dangerous. Creation and assignment from a base pointer can
  81. // only be done with WrapNotNull(), which makes them impossible to overlook,
  82. // both when writing and reading code.
  83. //
  84. // - When initialized (or assigned) it is checked, and if it is null we abort.
  85. // This guarantees that it cannot be null.
  86. //
  87. // - |operator bool()| is deleted. This means you cannot check a NotNull in a
  88. // boolean context, which eliminates the possibility of unnecessary null
  89. // checks.
  90. //
  91. // NotNull currently doesn't work with UniquePtr. See
  92. // https://github.com/Microsoft/GSL/issues/89 for some discussion.
  93. //
  94. template <typename T>
  95. class NotNull
  96. {
  97. template <typename U> friend NotNull<U> WrapNotNull(U aBasePtr);
  98. T mBasePtr;
  99. // This constructor is only used by WrapNotNull().
  100. template <typename U>
  101. explicit NotNull(U aBasePtr) : mBasePtr(aBasePtr) {}
  102. public:
  103. // Disallow default construction.
  104. NotNull() = delete;
  105. // Construct/assign from another NotNull with a compatible base pointer type.
  106. template <typename U>
  107. MOZ_IMPLICIT NotNull(const NotNull<U>& aOther) : mBasePtr(aOther.get()) {}
  108. // Default copy/move construction and assignment.
  109. NotNull(const NotNull<T>&) = default;
  110. NotNull<T>& operator=(const NotNull<T>&) = default;
  111. NotNull(NotNull<T>&&) = default;
  112. NotNull<T>& operator=(NotNull<T>&&) = default;
  113. // Disallow null checks, which are unnecessary for this type.
  114. explicit operator bool() const = delete;
  115. // Explicit conversion to a base pointer. Use only to resolve ambiguity or to
  116. // get a castable pointer.
  117. const T& get() const { return mBasePtr; }
  118. // Implicit conversion to a base pointer. Preferable to get().
  119. operator const T&() const { return get(); }
  120. // Dereference operators.
  121. const T& operator->() const { return get(); }
  122. decltype(*mBasePtr) operator*() const { return *mBasePtr; }
  123. };
  124. template <typename T>
  125. NotNull<T>
  126. WrapNotNull(const T aBasePtr)
  127. {
  128. NotNull<T> notNull(aBasePtr);
  129. MOZ_RELEASE_ASSERT(aBasePtr);
  130. return notNull;
  131. }
  132. // Compare two NotNulls.
  133. template <typename T, typename U>
  134. inline bool
  135. operator==(const NotNull<T>& aLhs, const NotNull<U>& aRhs)
  136. {
  137. return aLhs.get() == aRhs.get();
  138. }
  139. template <typename T, typename U>
  140. inline bool
  141. operator!=(const NotNull<T>& aLhs, const NotNull<U>& aRhs)
  142. {
  143. return aLhs.get() != aRhs.get();
  144. }
  145. // Compare a NotNull to a base pointer.
  146. template <typename T, typename U>
  147. inline bool
  148. operator==(const NotNull<T>& aLhs, const U& aRhs)
  149. {
  150. return aLhs.get() == aRhs;
  151. }
  152. template <typename T, typename U>
  153. inline bool
  154. operator!=(const NotNull<T>& aLhs, const U& aRhs)
  155. {
  156. return aLhs.get() != aRhs;
  157. }
  158. // Compare a base pointer to a NotNull.
  159. template <typename T, typename U>
  160. inline bool
  161. operator==(const T& aLhs, const NotNull<U>& aRhs)
  162. {
  163. return aLhs == aRhs.get();
  164. }
  165. template <typename T, typename U>
  166. inline bool
  167. operator!=(const T& aLhs, const NotNull<U>& aRhs)
  168. {
  169. return aLhs != aRhs.get();
  170. }
  171. // Disallow comparing a NotNull to a nullptr.
  172. template <typename T>
  173. bool
  174. operator==(const NotNull<T>&, decltype(nullptr)) = delete;
  175. template <typename T>
  176. bool
  177. operator!=(const NotNull<T>&, decltype(nullptr)) = delete;
  178. // Disallow comparing a nullptr to a NotNull.
  179. template <typename T>
  180. bool
  181. operator==(decltype(nullptr), const NotNull<T>&) = delete;
  182. template <typename T>
  183. bool
  184. operator!=(decltype(nullptr), const NotNull<T>&) = delete;
  185. } // namespace mozilla
  186. #endif /* mozilla_NotNull_h */