Brian Silverman | a6f7ce0 | 2018-07-07 15:04:00 -0700 | [diff] [blame^] | 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 <gsl/gsl_assert> // for Expects |
| 21 | |
| 22 | #include <array> |
| 23 | #include <cstddef> // for ptrdiff_t, size_t |
| 24 | #include <exception> // for exception |
| 25 | #include <initializer_list> // for initializer_list |
| 26 | #include <type_traits> // for is_signed, integral_constant |
| 27 | #include <utility> // for forward |
| 28 | |
| 29 | #if defined(_MSC_VER) |
| 30 | |
| 31 | #pragma warning(push) |
| 32 | #pragma warning(disable : 4127) // conditional expression is constant |
| 33 | |
| 34 | #if _MSC_VER < 1910 |
| 35 | #pragma push_macro("constexpr") |
| 36 | #define constexpr /*constexpr*/ |
| 37 | #endif // _MSC_VER < 1910 |
| 38 | #endif // _MSC_VER |
| 39 | |
| 40 | namespace gsl |
| 41 | { |
| 42 | // |
| 43 | // GSL.util: utilities |
| 44 | // |
| 45 | |
| 46 | // index type for all container indexes/subscripts/sizes |
| 47 | using index = std::ptrdiff_t; |
| 48 | |
| 49 | // final_action allows you to ensure something gets run at the end of a scope |
| 50 | template <class F> |
| 51 | class final_action |
| 52 | { |
| 53 | public: |
| 54 | explicit final_action(F f) noexcept : f_(std::move(f)) {} |
| 55 | |
| 56 | final_action(final_action&& other) noexcept : f_(std::move(other.f_)), invoke_(other.invoke_) |
| 57 | { |
| 58 | other.invoke_ = false; |
| 59 | } |
| 60 | |
| 61 | final_action(const final_action&) = delete; |
| 62 | final_action& operator=(const final_action&) = delete; |
| 63 | final_action& operator=(final_action&&) = delete; |
| 64 | |
| 65 | ~final_action() noexcept |
| 66 | { |
| 67 | if (invoke_) f_(); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | F f_; |
| 72 | bool invoke_ {true}; |
| 73 | }; |
| 74 | |
| 75 | // finally() - convenience function to generate a final_action |
| 76 | template <class F> |
| 77 | |
| 78 | final_action<F> finally(const F& f) noexcept |
| 79 | { |
| 80 | return final_action<F>(f); |
| 81 | } |
| 82 | |
| 83 | template <class F> |
| 84 | final_action<F> finally(F&& f) noexcept |
| 85 | { |
| 86 | return final_action<F>(std::forward<F>(f)); |
| 87 | } |
| 88 | |
| 89 | // narrow_cast(): a searchable way to do narrowing casts of values |
| 90 | template <class T, class U> |
| 91 | constexpr T narrow_cast(U&& u) noexcept |
| 92 | { |
| 93 | return static_cast<T>(std::forward<U>(u)); |
| 94 | } |
| 95 | |
| 96 | struct narrowing_error : public std::exception |
| 97 | { |
| 98 | }; |
| 99 | |
| 100 | namespace details |
| 101 | { |
| 102 | template <class T, class U> |
| 103 | struct is_same_signedness |
| 104 | : public std::integral_constant<bool, std::is_signed<T>::value == std::is_signed<U>::value> |
| 105 | { |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | // narrow() : a checked version of narrow_cast() that throws if the cast changed the value |
| 110 | template <class T, class U> |
| 111 | T narrow(U u) |
| 112 | { |
| 113 | T t = narrow_cast<T>(u); |
| 114 | if (static_cast<U>(t) != u) gsl::details::throw_exception(narrowing_error()); |
| 115 | if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{}))) |
| 116 | gsl::details::throw_exception(narrowing_error()); |
| 117 | return t; |
| 118 | } |
| 119 | |
| 120 | // |
| 121 | // at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector |
| 122 | // |
| 123 | template <class T, std::size_t N> |
| 124 | constexpr T& at(T (&arr)[N], const index i) |
| 125 | { |
| 126 | Expects(i >= 0 && i < narrow_cast<index>(N)); |
| 127 | return arr[static_cast<std::size_t>(i)]; |
| 128 | } |
| 129 | |
| 130 | template <class Cont> |
| 131 | constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()]) |
| 132 | { |
| 133 | Expects(i >= 0 && i < narrow_cast<index>(cont.size())); |
| 134 | using size_type = decltype(cont.size()); |
| 135 | return cont[static_cast<size_type>(i)]; |
| 136 | } |
| 137 | |
| 138 | template <class T> |
| 139 | constexpr T at(const std::initializer_list<T> cont, const index i) |
| 140 | { |
| 141 | Expects(i >= 0 && i < narrow_cast<index>(cont.size())); |
| 142 | return *(cont.begin() + i); |
| 143 | } |
| 144 | |
| 145 | } // namespace gsl |
| 146 | |
| 147 | #if defined(_MSC_VER) |
| 148 | #if _MSC_VER < 1910 |
| 149 | #undef constexpr |
| 150 | #pragma pop_macro("constexpr") |
| 151 | |
| 152 | #endif // _MSC_VER < 1910 |
| 153 | |
| 154 | #pragma warning(pop) |
| 155 | |
| 156 | #endif // _MSC_VER |
| 157 | |
| 158 | #endif // GSL_UTIL_H |