pointers 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <gsl/gsl_assert> // for Ensures, Expects
  19. #include <algorithm> // for forward
  20. #include <iosfwd> // for ptrdiff_t, nullptr_t, ostream, size_t
  21. #include <memory> // for shared_ptr, unique_ptr
  22. #include <system_error> // for hash
  23. #include <type_traits> // for enable_if_t, is_convertible, is_assignable
  24. #if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
  25. #pragma push_macro("constexpr")
  26. #define constexpr /*constexpr*/
  27. #endif // defined(_MSC_VER) && _MSC_VER < 1910
  28. namespace gsl
  29. {
  30. //
  31. // GSL.owner: ownership pointers
  32. //
  33. using std::unique_ptr;
  34. using std::shared_ptr;
  35. //
  36. // owner
  37. //
  38. // owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason
  39. //
  40. // T must be a pointer type
  41. // - disallow construction from any type other than pointer type
  42. //
  43. template <class T, class = std::enable_if_t<std::is_pointer<T>::value>>
  44. using owner = T;
  45. //
  46. // not_null
  47. //
  48. // Restricts a pointer or smart pointer to only hold non-null values.
  49. //
  50. // Has zero size overhead over T.
  51. //
  52. // If T is a pointer (i.e. T == U*) then
  53. // - allow construction from U*
  54. // - disallow construction from nullptr_t
  55. // - disallow default construction
  56. // - ensure construction from null U* fails
  57. // - allow implicit conversion to U*
  58. //
  59. template <class T>
  60. class not_null
  61. {
  62. public:
  63. static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
  64. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  65. constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
  66. {
  67. Expects(ptr_ != nullptr);
  68. }
  69. template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
  70. constexpr not_null(T u) : ptr_(u)
  71. {
  72. Expects(ptr_ != nullptr);
  73. }
  74. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  75. constexpr not_null(const not_null<U>& other) : not_null(other.get())
  76. {
  77. }
  78. not_null(const not_null& other) = default;
  79. not_null& operator=(const not_null& other) = default;
  80. constexpr T get() const
  81. {
  82. Ensures(ptr_ != nullptr);
  83. return ptr_;
  84. }
  85. constexpr operator T() const { return get(); }
  86. constexpr T operator->() const { return get(); }
  87. constexpr decltype(auto) operator*() const { return *get(); }
  88. // prevents compilation when someone attempts to assign a null pointer constant
  89. not_null(std::nullptr_t) = delete;
  90. not_null& operator=(std::nullptr_t) = delete;
  91. // unwanted operators...pointers only point to single objects!
  92. not_null& operator++() = delete;
  93. not_null& operator--() = delete;
  94. not_null operator++(int) = delete;
  95. not_null operator--(int) = delete;
  96. not_null& operator+=(std::ptrdiff_t) = delete;
  97. not_null& operator-=(std::ptrdiff_t) = delete;
  98. void operator[](std::ptrdiff_t) const = delete;
  99. private:
  100. T ptr_;
  101. };
  102. template <class T>
  103. auto make_not_null(T&& t) {
  104. return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
  105. }
  106. template <class T>
  107. std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
  108. {
  109. os << val.get();
  110. return os;
  111. }
  112. template <class T, class U>
  113. auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() == rhs.get())
  114. {
  115. return lhs.get() == rhs.get();
  116. }
  117. template <class T, class U>
  118. auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() != rhs.get())
  119. {
  120. return lhs.get() != rhs.get();
  121. }
  122. template <class T, class U>
  123. auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() < rhs.get())
  124. {
  125. return lhs.get() < rhs.get();
  126. }
  127. template <class T, class U>
  128. auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() <= rhs.get())
  129. {
  130. return lhs.get() <= rhs.get();
  131. }
  132. template <class T, class U>
  133. auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() > rhs.get())
  134. {
  135. return lhs.get() > rhs.get();
  136. }
  137. template <class T, class U>
  138. auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() >= rhs.get())
  139. {
  140. return lhs.get() >= rhs.get();
  141. }
  142. // more unwanted operators
  143. template <class T, class U>
  144. std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
  145. template <class T>
  146. not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
  147. template <class T>
  148. not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
  149. template <class T>
  150. not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
  151. } // namespace gsl
  152. namespace std
  153. {
  154. template <class T>
  155. struct hash<gsl::not_null<T>>
  156. {
  157. std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value); }
  158. };
  159. } // namespace std
  160. namespace gsl
  161. {
  162. //
  163. // strict_not_null
  164. //
  165. // Restricts a pointer or smart pointer to only hold non-null values,
  166. //
  167. // - provides a strict (i.e. explicit constructor from T) wrapper of not_null
  168. // - to be used for new code that wishes the design to be cleaner and make not_null
  169. // checks intentional, or in old code that would like to make the transition.
  170. //
  171. // To make the transition from not_null, incrementally replace not_null
  172. // by strict_not_null and fix compilation errors
  173. //
  174. // Expect to
  175. // - remove all unneeded conversions from raw pointer to not_null and back
  176. // - make API clear by specifying not_null in parameters where needed
  177. // - remove unnecessary asserts
  178. //
  179. template <class T>
  180. class strict_not_null: public not_null<T>
  181. {
  182. public:
  183. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  184. constexpr explicit strict_not_null(U&& u) :
  185. not_null<T>(std::forward<U>(u))
  186. {}
  187. template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
  188. constexpr explicit strict_not_null(T u) :
  189. not_null<T>(u)
  190. {}
  191. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  192. constexpr strict_not_null(const not_null<U>& other) :
  193. not_null<T>(other)
  194. {}
  195. template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
  196. constexpr strict_not_null(const strict_not_null<U>& other) :
  197. not_null<T>(other)
  198. {}
  199. strict_not_null(strict_not_null&& other) = default;
  200. strict_not_null(const strict_not_null& other) = default;
  201. strict_not_null& operator=(const strict_not_null& other) = default;
  202. strict_not_null& operator=(const not_null<T>& other)
  203. {
  204. not_null<T>::operator=(other);
  205. return *this;
  206. }
  207. // prevents compilation when someone attempts to assign a null pointer constant
  208. strict_not_null(std::nullptr_t) = delete;
  209. strict_not_null& operator=(std::nullptr_t) = delete;
  210. // unwanted operators...pointers only point to single objects!
  211. strict_not_null& operator++() = delete;
  212. strict_not_null& operator--() = delete;
  213. strict_not_null operator++(int) = delete;
  214. strict_not_null operator--(int) = delete;
  215. strict_not_null& operator+=(std::ptrdiff_t) = delete;
  216. strict_not_null& operator-=(std::ptrdiff_t) = delete;
  217. void operator[](std::ptrdiff_t) const = delete;
  218. };
  219. // more unwanted operators
  220. template <class T, class U>
  221. std::ptrdiff_t operator-(const strict_not_null<T>&, const strict_not_null<U>&) = delete;
  222. template <class T>
  223. strict_not_null<T> operator-(const strict_not_null<T>&, std::ptrdiff_t) = delete;
  224. template <class T>
  225. strict_not_null<T> operator+(const strict_not_null<T>&, std::ptrdiff_t) = delete;
  226. template <class T>
  227. strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
  228. template <class T>
  229. auto make_strict_not_null(T&& t) {
  230. return strict_not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
  231. }
  232. #if ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
  233. // deduction guides to prevent the ctad-maybe-unsupported warning
  234. template <class T> not_null(T) -> not_null<T>;
  235. template <class T> strict_not_null(T) -> strict_not_null<T>;
  236. #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
  237. } // namespace gsl
  238. namespace std
  239. {
  240. template <class T>
  241. struct hash<gsl::strict_not_null<T>>
  242. {
  243. std::size_t operator()(const gsl::strict_not_null<T>& value) const { return hash<T>{}(value); }
  244. };
  245. } // namespace std
  246. #if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
  247. #undef constexpr
  248. #pragma pop_macro("constexpr")
  249. #endif // defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
  250. #endif // GSL_POINTERS_H