Warning, /include/gsl/util is written in an unsupported language. File is not indexed.
0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0004 //
0005 // This code is licensed under the MIT License (MIT).
0006 //
0007 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0008 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0009 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0010 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0011 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0012 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0013 // THE SOFTWARE.
0014 //
0015 ///////////////////////////////////////////////////////////////////////////////
0016
0017 #ifndef GSL_UTIL_H
0018 #define GSL_UTIL_H
0019
0020 #include "./assert" // for Expects
0021
0022 #include <array>
0023 #include <cstddef> // for ptrdiff_t, size_t
0024 #include <limits> // for numeric_limits
0025 #include <initializer_list> // for initializer_list
0026 #include <type_traits> // for is_signed, integral_constant
0027 #include <utility> // for exchange, forward
0028
0029 #if defined(__has_include) && __has_include(<version>)
0030 #include <version>
0031 #if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
0032 #include <span>
0033 #endif // __cpp_lib_span >= 202002L
0034 #endif //__has_include(<version>)
0035
0036 #if defined(_MSC_VER) && !defined(__clang__)
0037
0038 #pragma warning(push)
0039 #pragma warning(disable : 4127) // conditional expression is constant
0040
0041 #endif // _MSC_VER
0042
0043 // Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
0044 #if defined(__clang__)
0045 #if __has_warning("-Wunsafe-buffer-usage")
0046 #pragma clang diagnostic push
0047 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
0048 #endif // __has_warning("-Wunsafe-buffer-usage")
0049 #endif // defined(__clang__)
0050
0051 #if defined(__cplusplus) && (__cplusplus >= 201703L)
0052 #define GSL_NODISCARD [[nodiscard]]
0053 #else
0054 #define GSL_NODISCARD
0055 #endif // defined(__cplusplus) && (__cplusplus >= 201703L)
0056
0057 #if defined(__cpp_inline_variables)
0058 #define GSL_INLINE inline
0059 #else
0060 #define GSL_INLINE
0061 #endif
0062
0063 #if defined(__has_cpp_attribute)
0064 #if __has_cpp_attribute(deprecated)
0065 #define GSL_DEPRECATED(msg) [[deprecated(msg)]]
0066 #endif // __has_cpp_attribute(deprecated)
0067 #endif // defined(__has_cpp_attribute)
0068
0069 #if !defined(GSL_DEPRECATED)
0070 #if defined(__cplusplus)
0071 #if __cplusplus >= 201309L
0072 #define GSL_DEPRECATED(msg) [[deprecated(msg)]]
0073 #endif // __cplusplus >= 201309L
0074 #endif // defined(__cplusplus)
0075 #endif // !defined(GSL_DEPRECATED)
0076
0077 #if !defined(GSL_DEPRECATED)
0078 #if defined(_MSC_VER)
0079 #define GSL_DEPRECATED(msg) __declspec(deprecated(msg))
0080 #elif defined(__GNUC__)
0081 #define GSL_DEPRECATED(msg) __attribute__((deprecated(msg)))
0082 #endif // defined(_MSC_VER)
0083 #endif // !defined(GSL_DEPRECATED)
0084
0085 #if !defined(GSL_DEPRECATED)
0086 #define GSL_DEPRECATED(msg)
0087 #endif // !defined(GSL_DEPRECATED)
0088
0089 namespace gsl
0090 {
0091 //
0092 // GSL.util: utilities
0093 //
0094
0095 // index type for all container indexes/subscripts/sizes
0096 using index = std::ptrdiff_t;
0097
0098 // final_action allows you to ensure something gets run at the end of a scope
0099 template <class F>
0100 class final_action
0101 {
0102 public:
0103 explicit final_action(const F& ff) noexcept : f{ff} { }
0104 explicit final_action(F&& ff) noexcept : f{std::move(ff)} { }
0105
0106 ~final_action() noexcept { if (invoke) f(); }
0107
0108 final_action(final_action&& other) noexcept
0109 : f(std::move(other.f)), invoke(std::exchange(other.invoke, false))
0110 { }
0111
0112 final_action(const final_action&) = delete;
0113 void operator=(const final_action&) = delete;
0114 void operator=(final_action&&) = delete;
0115
0116 private:
0117 F f;
0118 bool invoke = true;
0119 };
0120
0121 // finally() - convenience function to generate a final_action
0122 template <class F>
0123 GSL_NODISCARD auto finally(F&& f) noexcept
0124 {
0125 return final_action<std::decay_t<F>>{std::forward<F>(f)};
0126 }
0127
0128 // narrow_cast(): a searchable way to do narrowing casts of values
0129 template <class T, class U>
0130 // clang-format off
0131 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
0132 // clang-format on
0133 constexpr T narrow_cast(U&& u) noexcept
0134 {
0135 return static_cast<T>(std::forward<U>(u));
0136 }
0137
0138 //
0139 // at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
0140 //
0141 template <class T, std::size_t N>
0142 // clang-format off
0143 GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
0144 GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
0145 // clang-format on
0146 constexpr T& at(T (&arr)[N], const index i)
0147 {
0148 static_assert(N <= static_cast<std::size_t>((std::numeric_limits<std::ptrdiff_t>::max)()), "We only support arrays up to PTRDIFF_MAX bytes.");
0149 Expects(i >= 0 && i < narrow_cast<index>(N));
0150 return arr[narrow_cast<std::size_t>(i)];
0151 }
0152
0153 template <class Cont>
0154 // clang-format off
0155 GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
0156 GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
0157 // clang-format on
0158 constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
0159 {
0160 Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
0161 using size_type = decltype(cont.size());
0162 return cont[narrow_cast<size_type>(i)];
0163 }
0164
0165 template <class T>
0166 // clang-format off
0167 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
0168 // clang-format on
0169 constexpr T at(const std::initializer_list<T> cont, const index i)
0170 {
0171 Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
0172 return *(cont.begin() + i);
0173 }
0174
0175 template <class T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value>>
0176 void swap(T& a, T& b) { std::swap(a, b); }
0177
0178 #if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
0179 template <class T, std::size_t extent = std::dynamic_extent>
0180 constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()])
0181 {
0182 Expects(i >= 0 && i < narrow_cast<index>(sp.size()));
0183 return sp[gsl::narrow_cast<std::size_t>(i)];
0184 }
0185 #endif // __cpp_lib_span >= 202002L
0186 } // namespace gsl
0187
0188 #if defined(_MSC_VER) && !defined(__clang__)
0189
0190 #pragma warning(pop)
0191
0192 #endif // _MSC_VER
0193
0194 #if defined(__clang__)
0195 #if __has_warning("-Wunsafe-buffer-usage")
0196 #pragma clang diagnostic pop
0197 #endif // __has_warning("-Wunsafe-buffer-usage")
0198 #endif // defined(__clang__)
0199
0200 #endif // GSL_UTIL_H