| 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 |
|
| 17 |
#ifndef GSL_UTIL_H |
| 18 |
#define GSL_UTIL_H |
| 19 |
|
| 20 |
#include "./assert" // for Expects |
| 21 |
|
| 22 |
#include <array> |
| 23 |
#include <cstddef> // for ptrdiff_t, size_t |
| 24 |
#include <limits> // for numeric_limits |
| 25 |
#include <initializer_list> // for initializer_list |
| 26 |
#include <type_traits> // for is_signed, integral_constant |
| 27 |
#include <utility> // for exchange, forward |
| 28 |
|
| 29 |
#if defined(__has_include) && __has_include(<version>) |
| 30 |
#include <version> |
| 31 |
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L |
| 32 |
#include <span> |
| 33 |
#endif // __cpp_lib_span >= 202002L |
| 34 |
#endif //__has_include(<version>) |
| 35 |
|
| 36 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 37 |
|
| 38 |
#pragma warning(push) |
| 39 |
#pragma warning(disable : 4127) // conditional expression is constant |
| 40 |
|
| 41 |
#endif // _MSC_VER |
| 42 |
|
| 43 |
// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks |
| 44 |
#if defined(__clang__) |
| 45 |
#if __has_warning("-Wunsafe-buffer-usage") |
| 46 |
#pragma clang diagnostic push |
| 47 |
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" |
| 48 |
#endif // __has_warning("-Wunsafe-buffer-usage") |
| 49 |
#endif // defined(__clang__) |
| 50 |
|
| 51 |
#if defined(__cplusplus) && (__cplusplus >= 201703L) |
| 52 |
#define GSL_NODISCARD [[nodiscard]] |
| 53 |
#else |
| 54 |
#define GSL_NODISCARD |
| 55 |
#endif // defined(__cplusplus) && (__cplusplus >= 201703L) |
| 56 |
|
| 57 |
#if defined(__cpp_inline_variables) |
| 58 |
#define GSL_INLINE inline |
| 59 |
#else |
| 60 |
#define GSL_INLINE |
| 61 |
#endif |
| 62 |
|
| 63 |
#if defined(__has_cpp_attribute) |
| 64 |
#if __has_cpp_attribute(deprecated) |
| 65 |
#define GSL_DEPRECATED(msg) [[deprecated(msg)]] |
| 66 |
#endif // __has_cpp_attribute(deprecated) |
| 67 |
#endif // defined(__has_cpp_attribute) |
| 68 |
|
| 69 |
#if !defined(GSL_DEPRECATED) |
| 70 |
#if defined(__cplusplus) |
| 71 |
#if __cplusplus >= 201309L |
| 72 |
#define GSL_DEPRECATED(msg) [[deprecated(msg)]] |
| 73 |
#endif // __cplusplus >= 201309L |
| 74 |
#endif // defined(__cplusplus) |
| 75 |
#endif // !defined(GSL_DEPRECATED) |
| 76 |
|
| 77 |
#if !defined(GSL_DEPRECATED) |
| 78 |
#if defined(_MSC_VER) |
| 79 |
#define GSL_DEPRECATED(msg) __declspec(deprecated(msg)) |
| 80 |
#elif defined(__GNUC__) |
| 81 |
#define GSL_DEPRECATED(msg) __attribute__((deprecated(msg))) |
| 82 |
#endif // defined(_MSC_VER) |
| 83 |
#endif // !defined(GSL_DEPRECATED) |
| 84 |
|
| 85 |
#if !defined(GSL_DEPRECATED) |
| 86 |
#define GSL_DEPRECATED(msg) |
| 87 |
#endif // !defined(GSL_DEPRECATED) |
| 88 |
|
| 89 |
namespace gsl |
| 90 |
{ |
| 91 |
// |
| 92 |
// GSL.util: utilities |
| 93 |
// |
| 94 |
|
| 95 |
// index type for all container indexes/subscripts/sizes |
| 96 |
using index = std::ptrdiff_t; |
| 97 |
|
| 98 |
// final_action allows you to ensure something gets run at the end of a scope |
| 99 |
template <class F> |
| 100 |
class final_action |
| 101 |
{ |
| 102 |
public: |
| 103 |
explicit final_action(const F& ff) noexcept : f{ff} { } |
| 104 |
explicit final_action(F&& ff) noexcept : f{std::move(ff)} { } |
| 105 |
|
| 106 |
~final_action() noexcept { if (invoke) f(); } |
| 107 |
|
| 108 |
final_action(final_action&& other) noexcept |
| 109 |
: f(std::move(other.f)), invoke(std::exchange(other.invoke, false)) |
| 110 |
{ } |
| 111 |
|
| 112 |
final_action(const final_action&) = delete; |
| 113 |
void operator=(const final_action&) = delete; |
| 114 |
void operator=(final_action&&) = delete; |
| 115 |
|
| 116 |
private: |
| 117 |
F f; |
| 118 |
bool invoke = true; |
| 119 |
}; |
| 120 |
|
| 121 |
// finally() - convenience function to generate a final_action |
| 122 |
template <class F> |
| 123 |
GSL_NODISCARD auto finally(F&& f) noexcept |
| 124 |
{ |
| 125 |
return final_action<std::decay_t<F>>{std::forward<F>(f)}; |
| 126 |
} |
| 127 |
|
| 128 |
// narrow_cast(): a searchable way to do narrowing casts of values |
| 129 |
template <class T, class U> |
| 130 |
// clang-format off |
| 131 |
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute |
| 132 |
// clang-format on |
| 133 |
constexpr T narrow_cast(U&& u) noexcept |
| 134 |
{ |
| 135 |
return static_cast<T>(std::forward<U>(u)); |
| 136 |
} |
| 137 |
|
| 138 |
// |
| 139 |
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector |
| 140 |
// |
| 141 |
template <class T, std::size_t N> |
| 142 |
// clang-format off |
| 143 |
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute |
| 144 |
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute |
| 145 |
// clang-format on |
| 146 |
constexpr T& at(T (&arr)[N], const index i) |
| 147 |
{ |
| 148 |
static_assert(N <= static_cast<std::size_t>((std::numeric_limits<std::ptrdiff_t>::max)()), "We only support arrays up to PTRDIFF_MAX bytes."); |
| 149 |
Expects(i >= 0 && i < narrow_cast<index>(N)); |
| 150 |
return arr[narrow_cast<std::size_t>(i)]; |
| 151 |
} |
| 152 |
|
| 153 |
template <class Cont> |
| 154 |
// clang-format off |
| 155 |
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute |
| 156 |
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute |
| 157 |
// clang-format on |
| 158 |
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()]) |
| 159 |
{ |
| 160 |
Expects(i >= 0 && i < narrow_cast<index>(cont.size())); |
| 161 |
using size_type = decltype(cont.size()); |
| 162 |
return cont[narrow_cast<size_type>(i)]; |
| 163 |
} |
| 164 |
|
| 165 |
template <class T> |
| 166 |
// clang-format off |
| 167 |
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute |
| 168 |
// clang-format on |
| 169 |
constexpr T at(const std::initializer_list<T> cont, const index i) |
| 170 |
{ |
| 171 |
Expects(i >= 0 && i < narrow_cast<index>(cont.size())); |
| 172 |
return *(cont.begin() + i); |
| 173 |
} |
| 174 |
|
| 175 |
template <class T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value>> |
| 176 |
void swap(T& a, T& b) { std::swap(a, b); } |
| 177 |
|
| 178 |
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L |
| 179 |
template <class T, std::size_t extent = std::dynamic_extent> |
| 180 |
constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()]) |
| 181 |
{ |
| 182 |
Expects(i >= 0 && i < narrow_cast<index>(sp.size())); |
| 183 |
return sp[gsl::narrow_cast<std::size_t>(i)]; |
| 184 |
} |
| 185 |
#endif // __cpp_lib_span >= 202002L |
| 186 |
} // namespace gsl |
| 187 |
|
| 188 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 189 |
|
| 190 |
#pragma warning(pop) |
| 191 |
|
| 192 |
#endif // _MSC_VER |
| 193 |
|
| 194 |
#if defined(__clang__) |
| 195 |
#if __has_warning("-Wunsafe-buffer-usage") |
| 196 |
#pragma clang diagnostic pop |
| 197 |
#endif // __has_warning("-Wunsafe-buffer-usage") |
| 198 |
#endif // defined(__clang__) |
| 199 |
|
| 200 |
#endif // GSL_UTIL_H |