Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/gsl/pointers is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 ///////////////////////////////////////////////////////////////////////////////
0003 //
0004 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0005 //
0006 // This code is licensed under the MIT License (MIT).
0007 //
0008 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0009 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0010 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0011 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0012 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0013 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0014 // THE SOFTWARE.
0015 //
0016 ///////////////////////////////////////////////////////////////////////////////
0017 
0018 #ifndef GSL_POINTERS_H
0019 #define GSL_POINTERS_H
0020 
0021 #include "./assert" // for Ensures, Expects
0022 #include "./util"   // for GSL_DEPRECATED
0023 
0024 #include <cstddef>     // for ptrdiff_t, nullptr_t, size_t
0025 #include <functional>  // for less, greater
0026 #include <memory>      // for shared_ptr, unique_ptr, hash
0027 #include <type_traits> // for enable_if_t, is_convertible, is_assignable
0028 #include <utility>     // for declval, forward
0029 
0030 #if !defined(GSL_NO_IOSTREAMS)
0031 #include <iosfwd> // for ostream
0032 #endif            // !defined(GSL_NO_IOSTREAMS)
0033 
0034 namespace gsl
0035 {
0036 
0037 namespace details
0038 {
0039     template <typename T, typename = void>
0040     struct is_comparable_to_nullptr : std::false_type
0041     {
0042     };
0043 
0044     template <typename T>
0045     struct is_comparable_to_nullptr<
0046         T,
0047         std::enable_if_t<std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value>>
0048         : std::true_type
0049     {
0050     };
0051 
0052     // Resolves to the more efficient of `const T` or `const T&`, in the context of returning a
0053     // const-qualified value of type T.
0054     //
0055     // Copied from cppfront's implementation of the CppCoreGuidelines F.16
0056     // (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rf-in)
0057     template <typename T>
0058     using value_or_reference_return_t =
0059         std::conditional_t<sizeof(T) <= 2 * sizeof(void*) &&
0060                                std::is_trivially_copy_constructible<T>::value,
0061                            const T, const T&>;
0062 
0063 } // namespace details
0064 
0065 //
0066 // GSL.owner: ownership pointers
0067 //
0068 template <typename... Ts>
0069 using shared_ptr GSL_DEPRECATED("Use std::shared_ptr instead") = std::shared_ptr<Ts...>;
0070 
0071 template <typename... Ts>
0072 using unique_ptr GSL_DEPRECATED("Use std::unique_ptr instead") = std::unique_ptr<Ts...>;
0073 
0074 //
0075 // owner
0076 //
0077 // `gsl::owner<T>` is designed as a safety mechanism for code that must deal directly with raw
0078 // pointers that own memory. Ideally such code should be restricted to the implementation of
0079 // low-level abstractions. `gsl::owner` can also be used as a stepping point in converting legacy
0080 // code to use more modern RAII constructs, such as smart pointers.
0081 //
0082 // T must be a pointer type
0083 // - disallow construction from any type other than pointer type
0084 //
0085 template <class T, std::enable_if_t<std::is_pointer<T>::value, bool> = true>
0086 using owner = T;
0087 
0088 //
0089 // not_null
0090 //
0091 // Restricts a pointer or smart pointer to only hold non-null values.
0092 //
0093 // Has zero size overhead over T.
0094 //
0095 // If T is a pointer (i.e. T == U*) then
0096 // - allow construction from U*
0097 // - disallow construction from nullptr_t
0098 // - disallow default construction
0099 // - ensure construction from null U* fails
0100 // - allow implicit conversion to U*
0101 //
0102 template <class T>
0103 class not_null
0104 {
0105 public:
0106     static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
0107 
0108     using element_type = T;
0109 
0110     template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
0111     constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value)
0112         : ptr_(std::forward<U>(u))
0113     {
0114         Expects(ptr_ != nullptr);
0115     }
0116 
0117     template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
0118     constexpr not_null(T u) noexcept(std::is_nothrow_move_constructible<T>::value)
0119         : ptr_(std::move(u))
0120     {
0121         Expects(ptr_ != nullptr);
0122     }
0123 
0124     template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
0125     constexpr not_null(const not_null<U>& other) noexcept(
0126         std::is_nothrow_move_constructible<T>::value)
0127         : not_null(other.get())
0128     {}
0129 
0130     not_null(const not_null& other) = default;
0131     not_null& operator=(const not_null& other) = default;
0132     constexpr details::value_or_reference_return_t<T> get() const
0133         noexcept(noexcept(details::value_or_reference_return_t<T>(std::declval<T&>())))
0134     {
0135         return ptr_;
0136     }
0137 
0138     constexpr operator T() const { return get(); }
0139     constexpr decltype(auto) operator->() const { return get(); }
0140     constexpr decltype(auto) operator*() const { return *get(); }
0141 
0142     // prevents compilation when someone attempts to assign a null pointer constant
0143     not_null(std::nullptr_t) = delete;
0144     not_null& operator=(std::nullptr_t) = delete;
0145 
0146     // unwanted operators...pointers only point to single objects!
0147     not_null& operator++() = delete;
0148     not_null& operator--() = delete;
0149     not_null operator++(int) = delete;
0150     not_null operator--(int) = delete;
0151     not_null& operator+=(std::ptrdiff_t) = delete;
0152     not_null& operator-=(std::ptrdiff_t) = delete;
0153     void operator[](std::ptrdiff_t) const = delete;
0154 
0155     void swap(not_null<T>& other) noexcept { std::swap(ptr_, other.ptr_); }
0156 
0157 private:
0158     T ptr_;
0159 };
0160 
0161 template <typename T, std::enable_if_t<std::is_move_assignable<T>::value &&
0162                                            std::is_move_constructible<T>::value,
0163                                        bool> = true>
0164 void swap(not_null<T>& a, not_null<T>& b) noexcept
0165 {
0166     a.swap(b);
0167 }
0168 
0169 template <class T>
0170 auto make_not_null(T&& t) noexcept
0171 {
0172     return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
0173 }
0174 
0175 #if !defined(GSL_NO_IOSTREAMS)
0176 template <class T>
0177 std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
0178 {
0179     os << val.get();
0180     return os;
0181 }
0182 #endif // !defined(GSL_NO_IOSTREAMS)
0183 
0184 template <class T, class U>
0185 constexpr auto operator==(const not_null<T>& lhs,
0186                           const not_null<U>& rhs) noexcept(noexcept(lhs.get() == rhs.get()))
0187     -> decltype(lhs.get() == rhs.get())
0188 {
0189     return lhs.get() == rhs.get();
0190 }
0191 
0192 template <class T, class U>
0193 constexpr auto operator!=(const not_null<T>& lhs,
0194                           const not_null<U>& rhs) noexcept(noexcept(lhs.get() != rhs.get()))
0195     -> decltype(lhs.get() != rhs.get())
0196 {
0197     return lhs.get() != rhs.get();
0198 }
0199 
0200 template <class T, class U>
0201 constexpr auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
0202     noexcept(std::less<>{}(lhs.get(), rhs.get()))) -> decltype(std::less<>{}(lhs.get(), rhs.get()))
0203 {
0204     return std::less<>{}(lhs.get(), rhs.get());
0205 }
0206 
0207 template <class T, class U>
0208 constexpr auto
0209 operator<=(const not_null<T>& lhs,
0210            const not_null<U>& rhs) noexcept(noexcept(std::less_equal<>{}(lhs.get(), rhs.get())))
0211     -> decltype(std::less_equal<>{}(lhs.get(), rhs.get()))
0212 {
0213     return std::less_equal<>{}(lhs.get(), rhs.get());
0214 }
0215 
0216 template <class T, class U>
0217 constexpr auto
0218 operator>(const not_null<T>& lhs,
0219           const not_null<U>& rhs) noexcept(noexcept(std::greater<>{}(lhs.get(), rhs.get())))
0220     -> decltype(std::greater<>{}(lhs.get(), rhs.get()))
0221 {
0222     return std::greater<>{}(lhs.get(), rhs.get());
0223 }
0224 
0225 template <class T, class U>
0226 constexpr auto
0227 operator>=(const not_null<T>& lhs,
0228            const not_null<U>& rhs) noexcept(noexcept(std::greater_equal<>{}(lhs.get(), rhs.get())))
0229     -> decltype(std::greater_equal<>{}(lhs.get(), rhs.get()))
0230 {
0231     return std::greater_equal<>{}(lhs.get(), rhs.get());
0232 }
0233 
0234 // more unwanted operators
0235 template <class T, class U>
0236 std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
0237 template <class T>
0238 not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
0239 template <class T>
0240 not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
0241 template <class T>
0242 not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
0243 
0244 // T is conceptually a pointer so we don't have to worry about it being a reference and violating
0245 // std::hash requirements
0246 template <class T, class U = typename T::element_type,
0247           bool = std::is_default_constructible<std::hash<U>>::value>
0248 struct not_null_hash
0249 {
0250     std::size_t operator()(const T& value) const noexcept { return std::hash<U>{}(value.get()); }
0251 };
0252 
0253 template <class T, class U>
0254 struct not_null_hash<T, U, false>
0255 {
0256     not_null_hash() = delete;
0257     not_null_hash(const not_null_hash&) = delete;
0258     not_null_hash& operator=(const not_null_hash&) = delete;
0259 };
0260 
0261 } // namespace gsl
0262 
0263 namespace std
0264 {
0265 template <class T>
0266 struct hash<gsl::not_null<T>> : gsl::not_null_hash<gsl::not_null<T>>
0267 {
0268 };
0269 
0270 } // namespace std
0271 
0272 namespace gsl
0273 {
0274 
0275 //
0276 // strict_not_null
0277 //
0278 // Restricts a pointer or smart pointer to only hold non-null values,
0279 //
0280 // - provides a strict (i.e. explicit constructor from T) wrapper of not_null
0281 // - to be used for new code that wishes the design to be cleaner and make not_null
0282 //   checks intentional, or in old code that would like to make the transition.
0283 //
0284 //   To make the transition from not_null, incrementally replace not_null
0285 //   by strict_not_null and fix compilation errors
0286 //
0287 //   Expect to
0288 //   - remove all unneeded conversions from raw pointer to not_null and back
0289 //   - make API clear by specifying not_null in parameters where needed
0290 //   - remove unnecessary asserts
0291 //
0292 template <class T>
0293 class strict_not_null : public not_null<T>
0294 {
0295 public:
0296     template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
0297     constexpr explicit strict_not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value)
0298         : not_null<T>(std::forward<U>(u))
0299     {}
0300 
0301     template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
0302     constexpr explicit strict_not_null(T u) noexcept(std::is_nothrow_move_constructible<T>::value)
0303         : not_null<T>(std::move(u))
0304     {}
0305 
0306     template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
0307     constexpr strict_not_null(const not_null<U>& other) noexcept(
0308         std::is_nothrow_move_constructible<T>::value)
0309         : not_null<T>(other)
0310     {}
0311 
0312     template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
0313     constexpr strict_not_null(const strict_not_null<U>& other) noexcept(
0314         std::is_nothrow_move_constructible<T>::value)
0315         : not_null<T>(other)
0316     {}
0317 
0318     // To avoid invalidating the "not null" invariant, the contained pointer is actually copied
0319     // instead of moved. If it is a custom pointer, its constructor could in theory throw
0320     // exceptions.
0321     strict_not_null(strict_not_null&& other) noexcept(
0322         std::is_nothrow_copy_constructible<T>::value) = default;
0323     strict_not_null(const strict_not_null& other) = default;
0324     strict_not_null& operator=(const strict_not_null& other) = default;
0325     strict_not_null& operator=(const not_null<T>& other)
0326     {
0327         not_null<T>::operator=(other);
0328         return *this;
0329     }
0330 
0331     // prevents compilation when someone attempts to assign a null pointer constant
0332     strict_not_null(std::nullptr_t) = delete;
0333     strict_not_null& operator=(std::nullptr_t) = delete;
0334 
0335     // unwanted operators...pointers only point to single objects!
0336     strict_not_null& operator++() = delete;
0337     strict_not_null& operator--() = delete;
0338     strict_not_null operator++(int) = delete;
0339     strict_not_null operator--(int) = delete;
0340     strict_not_null& operator+=(std::ptrdiff_t) = delete;
0341     strict_not_null& operator-=(std::ptrdiff_t) = delete;
0342     void operator[](std::ptrdiff_t) const = delete;
0343 };
0344 
0345 // more unwanted operators
0346 template <class T, class U>
0347 std::ptrdiff_t operator-(const strict_not_null<T>&, const strict_not_null<U>&) = delete;
0348 template <class T>
0349 strict_not_null<T> operator-(const strict_not_null<T>&, std::ptrdiff_t) = delete;
0350 template <class T>
0351 strict_not_null<T> operator+(const strict_not_null<T>&, std::ptrdiff_t) = delete;
0352 template <class T>
0353 strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
0354 
0355 template <class T>
0356 auto make_strict_not_null(T&& t) noexcept
0357 {
0358     return strict_not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
0359 }
0360 
0361 #if defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201703L)
0362 
0363 // deduction guides to prevent the ctad-maybe-unsupported warning
0364 template <class T>
0365 not_null(T) -> not_null<T>;
0366 template <class T>
0367 strict_not_null(T) -> strict_not_null<T>;
0368 
0369 #endif // defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201703L)
0370 
0371 } // namespace gsl
0372 
0373 namespace std
0374 {
0375 template <class T>
0376 struct hash<gsl::strict_not_null<T>> : gsl::not_null_hash<gsl::strict_not_null<T>>
0377 {
0378 };
0379 
0380 } // namespace std
0381 
0382 #endif // GSL_POINTERS_H