Warning, /include/gsl/util 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_UTIL_H
0019 #define GSL_UTIL_H
0020
0021 #include "./assert" // for Expects
0022
0023 #include <cstddef> // for ptrdiff_t, size_t
0024 #include <initializer_list> // for initializer_list
0025 #include <iterator> // for iterator_traits
0026 #include <limits> // for numeric_limits
0027 #include <type_traits> // for is_signed, integral_constant
0028 #include <utility> // for exchange, forward
0029
0030 #if defined(__has_include) && __has_include(<version>)
0031 #include <version>
0032 #if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
0033 #include <span>
0034 #endif // __cpp_lib_span >= 202002L
0035 #endif //__has_include(<version>)
0036
0037 #if defined(_MSC_VER) && !defined(__clang__)
0038
0039 #pragma warning(push)
0040 #pragma warning(disable : 4127) // conditional expression is constant
0041
0042 #endif // _MSC_VER
0043
0044 // Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
0045 #if defined(__clang__)
0046 #if __has_warning("-Wunsafe-buffer-usage")
0047 #pragma clang diagnostic push
0048 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
0049 #endif // __has_warning("-Wunsafe-buffer-usage")
0050 #endif // defined(__clang__)
0051
0052 #if defined(__cplusplus) && (__cplusplus >= 201703L)
0053 #define GSL_NODISCARD [[nodiscard]]
0054 #else
0055 #define GSL_NODISCARD
0056 #endif // defined(__cplusplus) && (__cplusplus >= 201703L)
0057
0058 #if defined(__cpp_inline_variables)
0059 #define GSL_INLINE inline
0060 #else
0061 #define GSL_INLINE
0062 #endif
0063
0064 #if defined(__has_cpp_attribute)
0065 #if __has_cpp_attribute(deprecated)
0066 #define GSL_DEPRECATED(msg) [[deprecated(msg)]]
0067 #endif // __has_cpp_attribute(deprecated)
0068 #endif // defined(__has_cpp_attribute)
0069
0070 #if !defined(GSL_DEPRECATED)
0071 #if defined(__cplusplus)
0072 #if __cplusplus >= 201309L
0073 #define GSL_DEPRECATED(msg) [[deprecated(msg)]]
0074 #endif // __cplusplus >= 201309L
0075 #endif // defined(__cplusplus)
0076 #endif // !defined(GSL_DEPRECATED)
0077
0078 #if !defined(GSL_DEPRECATED)
0079 #if defined(_MSC_VER)
0080 #define GSL_DEPRECATED(msg) __declspec(deprecated(msg))
0081 #elif defined(__GNUC__)
0082 #define GSL_DEPRECATED(msg) __attribute__((deprecated(msg)))
0083 #endif // defined(_MSC_VER)
0084 #endif // !defined(GSL_DEPRECATED)
0085
0086 #if !defined(GSL_DEPRECATED)
0087 #define GSL_DEPRECATED(msg)
0088 #endif // !defined(GSL_DEPRECATED)
0089
0090 #if __cplusplus >= 202002L
0091 #define GSL_CONSTEXPR_SINCE_CPP20 constexpr
0092 #else // ^^^ since C++20 /// before C++20 vvv
0093 #define GSL_BEFORE_CPP20
0094 #define GSL_CONSTEXPR_SINCE_CPP20
0095 #endif // __cplusplus >= 202002L
0096
0097 namespace gsl
0098 {
0099 //
0100 // GSL.util: utilities
0101 //
0102
0103 // index type for all container indexes/subscripts/sizes
0104 using index = std::ptrdiff_t;
0105
0106 namespace details
0107 {
0108 template <typename...>
0109 using void_t = void;
0110
0111 template <typename T, typename = void>
0112 struct is_iterator : std::false_type
0113 {
0114 };
0115
0116 template <typename T>
0117 struct is_iterator<T, void_t<typename std::iterator_traits<T>::value_type>> : std::true_type
0118 {
0119 };
0120
0121 template <typename T, typename = void>
0122 struct is_fwd_iterator : std::false_type
0123 {
0124 };
0125
0126 template <typename T>
0127 struct is_fwd_iterator<T, void_t<typename std::iterator_traits<T>::iterator_category>>
0128 : std::integral_constant<
0129 bool, std::is_base_of<std::forward_iterator_tag,
0130 typename std::iterator_traits<T>::iterator_category>::value>
0131 {
0132 };
0133 } // namespace details
0134
0135 // final_action allows you to ensure something gets run at the end of a scope
0136 // The bool member causes trailing padding when F has alignment > 1; suppress
0137 // -Wpadded since the padding is unavoidable for a generic callable wrapper.
0138 #if defined(__clang__)
0139 #pragma clang diagnostic push
0140 #pragma clang diagnostic ignored "-Wpadded"
0141 #endif // defined(__clang__)
0142 template <class F>
0143 class final_action
0144 {
0145 public:
0146 explicit final_action(const F& ff) noexcept : f{ff} {}
0147 explicit final_action(F&& ff) noexcept : f{std::move(ff)} {}
0148
0149 ~final_action() noexcept
0150 {
0151 if (invoke) f();
0152 }
0153
0154 final_action(final_action&& other) noexcept
0155 : f(std::move(other.f)), invoke(std::exchange(other.invoke, false))
0156 {}
0157
0158 final_action(const final_action&) = delete;
0159 void operator=(const final_action&) = delete;
0160 void operator=(final_action&&) = delete;
0161
0162 private:
0163 F f;
0164 bool invoke = true;
0165 };
0166 #if defined(__clang__)
0167 #pragma clang diagnostic pop
0168 #endif // defined(__clang__)
0169
0170 // finally() - convenience function to generate a final_action
0171 template <class F>
0172 GSL_NODISCARD auto finally(F&& f) noexcept
0173 {
0174 return final_action<std::decay_t<F>>{std::forward<F>(f)};
0175 }
0176
0177 // narrow_cast(): a searchable way to do narrowing casts of values
0178 template <class T, class U>
0179 GSL_SUPPRESS(type.1) constexpr T narrow_cast(U&& u) noexcept
0180 {
0181 #if defined(__clang__) || defined(__GNUC__)
0182 #pragma GCC diagnostic push
0183 #pragma GCC diagnostic ignored "-Wfloat-equal"
0184 #endif
0185 return static_cast<T>(std::forward<U>(u));
0186 #if defined(__clang__) || defined(__GNUC__)
0187 #pragma GCC diagnostic pop
0188 #endif
0189 }
0190
0191 //
0192 // at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
0193 //
0194 template <class T, std::size_t N>
0195 GSL_SUPPRESS(bounds.4) GSL_SUPPRESS(bounds.2) constexpr T& at(T (&arr)[N], const index i)
0196 {
0197 static_assert(N <= static_cast<std::size_t>((std::numeric_limits<std::ptrdiff_t>::max)()),
0198 "We only support arrays up to PTRDIFF_MAX bytes.");
0199 Expects(i >= 0 && i < narrow_cast<index>(N));
0200 return arr[narrow_cast<std::size_t>(i)];
0201 }
0202
0203 template <class Cont>
0204 GSL_SUPPRESS(bounds.4) GSL_SUPPRESS(bounds.2) constexpr auto at(Cont& cont, const index i)
0205 -> decltype(cont[cont.size()])
0206 {
0207 Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
0208 using size_type = decltype(cont.size());
0209 return cont[narrow_cast<size_type>(i)];
0210 }
0211
0212 template <class T>
0213 GSL_SUPPRESS(bounds.1) constexpr T at(const std::initializer_list<T> cont, const index i)
0214 {
0215 Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
0216 return *(cont.begin() + i);
0217 }
0218
0219 template <class T, std::enable_if_t<std::is_move_assignable<T>::value &&
0220 std::is_move_constructible<T>::value>>
0221 void swap(T& a, T& b)
0222 {
0223 std::swap(a, b);
0224 }
0225
0226 #if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
0227 template <class T, std::size_t extent = std::dynamic_extent>
0228 constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()])
0229 {
0230 Expects(i >= 0 && i < narrow_cast<index>(sp.size()));
0231 return sp[gsl::narrow_cast<std::size_t>(i)];
0232 }
0233 #endif // __cpp_lib_span >= 202002L
0234 } // namespace gsl
0235
0236 #if defined(_MSC_VER) && !defined(__clang__)
0237
0238 #pragma warning(pop)
0239
0240 #endif // _MSC_VER
0241
0242 #if defined(__clang__)
0243 #if __has_warning("-Wunsafe-buffer-usage")
0244 #pragma clang diagnostic pop
0245 #endif // __has_warning("-Wunsafe-buffer-usage")
0246 #endif // defined(__clang__)
0247
0248 #endif // GSL_UTIL_H