util 5.1 KB

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