pointers 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef GSL_POINTERS_H
  17. #define GSL_POINTERS_H
  18. #include "assert" // for Ensures, Expects
  19. #include <cstddef> // for ptrdiff_t, nullptr_t, size_t
  20. #include <functional> // for less, greater
  21. #include <memory> // for shared_ptr, unique_ptr, hash
  22. #include <type_traits> // for enable_if_t, is_convertible, is_assignable
  23. #include <utility> // for declval, forward
  24. #if !defined(GSL_NO_IOSTREAMS)
  25. #include <iosfwd> // for ostream
  26. #endif // !defined(GSL_NO_IOSTREAMS)
  27. namespace gsl
  28. {
  29. namespace details
  30. {
  31. template <typename T, typename = void>
  32. struct is_comparable_to_nullptr : std::false_type
  33. {
  34. };
  35. template <typename T>
  36. struct is_comparable_to_nullptr<
  37. T,
  38. std::enable_if_t<std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value>>
  39. : std::true_type
  40. {
  41. };
  42. // Resolves to the more efficient of `const T` or `const T&`, in the context of returning a const-qualified value
  43. // of type T.
  44. //
  45. // Copied from cppfront's implementation of the CppCoreGuidelines F.16 (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-in)
  46. template<typename T>
  47. using value_or_reference_return_t = std::conditional_t<
  48. sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible<T>::value,
  49. const T,
  50. const T&>;
  51. } // namespace details
  52. //
  53. // GSL.owner: ownership pointers
  54. //
  55. using std::shared_ptr;
  56. using std::unique_ptr;
  57. //
  58. // owner
  59. //
  60. // `gsl::owner<T>` is designed as a safety mechanism for code that must deal directly with raw pointers that own memory.
  61. // Ideally such code should be restricted to the implementation of low-level abstractions. `gsl::owner` can also be used
  62. // as a stepping point in converting legacy code to use more modern RAII constructs, such as smart pointers.
  63. //
  64. // T must be a pointer type
  65. // - disallow construction from any type other than pointer type
  66. //
  67. template <class T, class = std::enable_if_t<std::is_pointer<T>::value>>
  68. using owner = T;
  69. //
  70. // not_null
  71. //
  72. // Restricts a pointer or smart pointer to only hold non-null values.
  73. //
  74. // Has zero size overhead over T.
  75. //
  76. // If T is a pointer (i.e. T == U*) then
  77. // - allow construction from U*
  78. // - disallow construction from nullptr_t
  79. // - disallow default construction
  80. // - ensure construction from null U* fails
  81. // - allow implicit conversion to U*
  82. //
  83. template <class T>
  84. class not_null
  85. {
  86. public:
  87. static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
  88. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  89. constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u))
  90. {
  91. Expects(ptr_ != nullptr);
  92. }
  93. template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
  94. constexpr not_null(T u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::move(u))
  95. {
  96. Expects(ptr_ != nullptr);
  97. }
  98. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  99. constexpr not_null(const not_null<U>& other) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null(other.get())
  100. {}
  101. not_null(const not_null& other) = default;
  102. not_null& operator=(const not_null& other) = default;
  103. constexpr details::value_or_reference_return_t<T> get() const
  104. noexcept(noexcept(details::value_or_reference_return_t<T>{std::declval<T&>()}))
  105. {
  106. return ptr_;
  107. }
  108. constexpr operator T() const { return get(); }
  109. constexpr decltype(auto) operator->() const { return get(); }
  110. constexpr decltype(auto) operator*() const { return *get(); }
  111. // prevents compilation when someone attempts to assign a null pointer constant
  112. not_null(std::nullptr_t) = delete;
  113. not_null& operator=(std::nullptr_t) = delete;
  114. // unwanted operators...pointers only point to single objects!
  115. not_null& operator++() = delete;
  116. not_null& operator--() = delete;
  117. not_null operator++(int) = delete;
  118. not_null operator--(int) = delete;
  119. not_null& operator+=(std::ptrdiff_t) = delete;
  120. not_null& operator-=(std::ptrdiff_t) = delete;
  121. void operator[](std::ptrdiff_t) const = delete;
  122. private:
  123. T ptr_;
  124. };
  125. template <class T>
  126. auto make_not_null(T&& t) noexcept
  127. {
  128. return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
  129. }
  130. #if !defined(GSL_NO_IOSTREAMS)
  131. template <class T>
  132. std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
  133. {
  134. os << val.get();
  135. return os;
  136. }
  137. #endif // !defined(GSL_NO_IOSTREAMS)
  138. template <class T, class U>
  139. auto operator==(const not_null<T>& lhs,
  140. const not_null<U>& rhs) noexcept(noexcept(lhs.get() == rhs.get()))
  141. -> decltype(lhs.get() == rhs.get())
  142. {
  143. return lhs.get() == rhs.get();
  144. }
  145. template <class T, class U>
  146. auto operator!=(const not_null<T>& lhs,
  147. const not_null<U>& rhs) noexcept(noexcept(lhs.get() != rhs.get()))
  148. -> decltype(lhs.get() != rhs.get())
  149. {
  150. return lhs.get() != rhs.get();
  151. }
  152. template <class T, class U>
  153. auto operator<(const not_null<T>& lhs,
  154. const not_null<U>& rhs) noexcept(noexcept(std::less<>{}(lhs.get(), rhs.get())))
  155. -> decltype(std::less<>{}(lhs.get(), rhs.get()))
  156. {
  157. return std::less<>{}(lhs.get(), rhs.get());
  158. }
  159. template <class T, class U>
  160. auto operator<=(const not_null<T>& lhs,
  161. const not_null<U>& rhs) noexcept(noexcept(std::less_equal<>{}(lhs.get(), rhs.get())))
  162. -> decltype(std::less_equal<>{}(lhs.get(), rhs.get()))
  163. {
  164. return std::less_equal<>{}(lhs.get(), rhs.get());
  165. }
  166. template <class T, class U>
  167. auto operator>(const not_null<T>& lhs,
  168. const not_null<U>& rhs) noexcept(noexcept(std::greater<>{}(lhs.get(), rhs.get())))
  169. -> decltype(std::greater<>{}(lhs.get(), rhs.get()))
  170. {
  171. return std::greater<>{}(lhs.get(), rhs.get());
  172. }
  173. template <class T, class U>
  174. auto operator>=(const not_null<T>& lhs,
  175. const not_null<U>& rhs) noexcept(noexcept(std::greater_equal<>{}(lhs.get(), rhs.get())))
  176. -> decltype(std::greater_equal<>{}(lhs.get(), rhs.get()))
  177. {
  178. return std::greater_equal<>{}(lhs.get(), rhs.get());
  179. }
  180. // more unwanted operators
  181. template <class T, class U>
  182. std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
  183. template <class T>
  184. not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
  185. template <class T>
  186. not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
  187. template <class T>
  188. not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
  189. template <class T, class U = decltype(std::declval<const T&>().get()), bool = std::is_default_constructible<std::hash<U>>::value>
  190. struct not_null_hash
  191. {
  192. std::size_t operator()(const T& value) const { return std::hash<U>{}(value.get()); }
  193. };
  194. template <class T, class U>
  195. struct not_null_hash<T, U, false>
  196. {
  197. not_null_hash() = delete;
  198. not_null_hash(const not_null_hash&) = delete;
  199. not_null_hash& operator=(const not_null_hash&) = delete;
  200. };
  201. } // namespace gsl
  202. namespace std
  203. {
  204. template <class T>
  205. struct hash<gsl::not_null<T>> : gsl::not_null_hash<gsl::not_null<T>>
  206. {
  207. };
  208. } // namespace std
  209. namespace gsl
  210. {
  211. //
  212. // strict_not_null
  213. //
  214. // Restricts a pointer or smart pointer to only hold non-null values,
  215. //
  216. // - provides a strict (i.e. explicit constructor from T) wrapper of not_null
  217. // - to be used for new code that wishes the design to be cleaner and make not_null
  218. // checks intentional, or in old code that would like to make the transition.
  219. //
  220. // To make the transition from not_null, incrementally replace not_null
  221. // by strict_not_null and fix compilation errors
  222. //
  223. // Expect to
  224. // - remove all unneeded conversions from raw pointer to not_null and back
  225. // - make API clear by specifying not_null in parameters where needed
  226. // - remove unnecessary asserts
  227. //
  228. template <class T>
  229. class strict_not_null : public not_null<T>
  230. {
  231. public:
  232. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  233. constexpr explicit strict_not_null(U&& u) : not_null<T>(std::forward<U>(u))
  234. {}
  235. template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
  236. constexpr explicit strict_not_null(T u) : not_null<T>(u)
  237. {}
  238. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  239. constexpr strict_not_null(const not_null<U>& other) : not_null<T>(other)
  240. {}
  241. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  242. constexpr strict_not_null(const strict_not_null<U>& other) : not_null<T>(other)
  243. {}
  244. // To avoid invalidating the "not null" invariant, the contained pointer is actually copied
  245. // instead of moved. If it is a custom pointer, its constructor could in theory throw exceptions.
  246. strict_not_null(strict_not_null&& other) noexcept(std::is_nothrow_copy_constructible<T>::value) = default;
  247. strict_not_null(const strict_not_null& other) = default;
  248. strict_not_null& operator=(const strict_not_null& other) = default;
  249. strict_not_null& operator=(const not_null<T>& other)
  250. {
  251. not_null<T>::operator=(other);
  252. return *this;
  253. }
  254. // prevents compilation when someone attempts to assign a null pointer constant
  255. strict_not_null(std::nullptr_t) = delete;
  256. strict_not_null& operator=(std::nullptr_t) = delete;
  257. // unwanted operators...pointers only point to single objects!
  258. strict_not_null& operator++() = delete;
  259. strict_not_null& operator--() = delete;
  260. strict_not_null operator++(int) = delete;
  261. strict_not_null operator--(int) = delete;
  262. strict_not_null& operator+=(std::ptrdiff_t) = delete;
  263. strict_not_null& operator-=(std::ptrdiff_t) = delete;
  264. void operator[](std::ptrdiff_t) const = delete;
  265. };
  266. // more unwanted operators
  267. template <class T, class U>
  268. std::ptrdiff_t operator-(const strict_not_null<T>&, const strict_not_null<U>&) = delete;
  269. template <class T>
  270. strict_not_null<T> operator-(const strict_not_null<T>&, std::ptrdiff_t) = delete;
  271. template <class T>
  272. strict_not_null<T> operator+(const strict_not_null<T>&, std::ptrdiff_t) = delete;
  273. template <class T>
  274. strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
  275. template <class T>
  276. auto make_strict_not_null(T&& t) noexcept
  277. {
  278. return strict_not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
  279. }
  280. #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
  281. // deduction guides to prevent the ctad-maybe-unsupported warning
  282. template <class T>
  283. not_null(T) -> not_null<T>;
  284. template <class T>
  285. strict_not_null(T) -> strict_not_null<T>;
  286. #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
  287. } // namespace gsl
  288. namespace std
  289. {
  290. template <class T>
  291. struct hash<gsl::strict_not_null<T>> : gsl::not_null_hash<gsl::strict_not_null<T>>
  292. {
  293. };
  294. } // namespace std
  295. #endif // GSL_POINTERS_H