mirror of
https://github.com/microsoft/GSL.git
synced 2026-07-05 08:00:49 +02:00
b2f6bec48e
* clang-format improvements - Add a clang-format linter check to the PR pipeline - Apply clang-format to files where the linter initially failed - Remove `CommentPragmas` from `.clang-format` - Remove all `// clang-format off` and `// NO-FORMAT` as they are not needed - Remove a commented out `GSL_SUPPRESS` * clang-format 20 * pipeline fail * Update .github/workflows/clang-format.yml Co-authored-by: Carson Radtke <nosrac925@gmail.com> * output used clang-format version * installed version is 18 which replaces "GSL_SUPPRESS(bounds.1)" with "GSL_SUPPRESS(bounds .1)" * only include/gsl, not include to prevent formatting of include/CMakeLists.txt * apply clang-format[-20] In a VS2026 developer command prompt I ran `clang-format -i include\gsl\* --assume-filename x.cpp`. This was necessary because VS GUI does not format files without an extension :(. Please note that `--assume-filename` is necessary here, otherwise the files will not be formatted. Surprisingly the behaviour for the formatter differs from the behaviour of `lang-format(-20) --dry-run --Werror include/gsl/*` where clang-format recognizes that the files is C++. * change "#if 0" back to original version with comments only * formatting scripts for windows and linux for linux with linter (shfmt and shellcheck) * add WhitespaceSensitiveMacros: [GSL_SUPPRESS] * provide path for clang-format (Windows) Currently not clear to me: On my personal computer at home, when I start "Developer Command Prompt for VS18", I can run `clang-format` without providing the path. On my managed (domain) company computer, when I start "Developer Command Prompt for VS18", I can NOT run `clang-format` without providing the path. I need to call `"%VCINSTALLDIR%Tools\Llvm\bin\clang-format"`. I have no idea what the difference is. At least the version `"%VCINSTALLDIR%Tools\Llvm\bin\clang-format"` works on both computers, so I add the path. --------- Co-authored-by: Werner Henze <w.henze@avm.de> Co-authored-by: Carson Radtke <nosrac925@gmail.com> Co-authored-by: Werner Henze <werner.henze+gitcommits@posteo.de>
196 lines
5.8 KiB
C++
196 lines
5.8 KiB
C++
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
//
|
|
// This code is licensed under the MIT License (MIT).
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef GSL_UTIL_H
|
|
#define GSL_UTIL_H
|
|
|
|
#include "./assert" // for Expects
|
|
|
|
#include <array>
|
|
#include <cstddef> // for ptrdiff_t, size_t
|
|
#include <initializer_list> // for initializer_list
|
|
#include <limits> // for numeric_limits
|
|
#include <type_traits> // for is_signed, integral_constant
|
|
#include <utility> // for exchange, forward
|
|
|
|
#if defined(__has_include) && __has_include(<version>)
|
|
#include <version>
|
|
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
|
|
#include <span>
|
|
#endif // __cpp_lib_span >= 202002L
|
|
#endif //__has_include(<version>)
|
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
|
|
|
#pragma warning(push)
|
|
#pragma warning(disable : 4127) // conditional expression is constant
|
|
|
|
#endif // _MSC_VER
|
|
|
|
// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
|
|
#if defined(__clang__)
|
|
#if __has_warning("-Wunsafe-buffer-usage")
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
|
|
#endif // __has_warning("-Wunsafe-buffer-usage")
|
|
#endif // defined(__clang__)
|
|
|
|
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
|
#define GSL_NODISCARD [[nodiscard]]
|
|
#else
|
|
#define GSL_NODISCARD
|
|
#endif // defined(__cplusplus) && (__cplusplus >= 201703L)
|
|
|
|
#if defined(__cpp_inline_variables)
|
|
#define GSL_INLINE inline
|
|
#else
|
|
#define GSL_INLINE
|
|
#endif
|
|
|
|
#if defined(__has_cpp_attribute)
|
|
#if __has_cpp_attribute(deprecated)
|
|
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
|
|
#endif // __has_cpp_attribute(deprecated)
|
|
#endif // defined(__has_cpp_attribute)
|
|
|
|
#if !defined(GSL_DEPRECATED)
|
|
#if defined(__cplusplus)
|
|
#if __cplusplus >= 201309L
|
|
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
|
|
#endif // __cplusplus >= 201309L
|
|
#endif // defined(__cplusplus)
|
|
#endif // !defined(GSL_DEPRECATED)
|
|
|
|
#if !defined(GSL_DEPRECATED)
|
|
#if defined(_MSC_VER)
|
|
#define GSL_DEPRECATED(msg) __declspec(deprecated(msg))
|
|
#elif defined(__GNUC__)
|
|
#define GSL_DEPRECATED(msg) __attribute__((deprecated(msg)))
|
|
#endif // defined(_MSC_VER)
|
|
#endif // !defined(GSL_DEPRECATED)
|
|
|
|
#if !defined(GSL_DEPRECATED)
|
|
#define GSL_DEPRECATED(msg)
|
|
#endif // !defined(GSL_DEPRECATED)
|
|
|
|
namespace gsl
|
|
{
|
|
//
|
|
// GSL.util: utilities
|
|
//
|
|
|
|
// index type for all container indexes/subscripts/sizes
|
|
using index = std::ptrdiff_t;
|
|
|
|
// final_action allows you to ensure something gets run at the end of a scope
|
|
template <class F>
|
|
class final_action
|
|
{
|
|
public:
|
|
explicit final_action(const F& ff) noexcept : f{ff} {}
|
|
explicit final_action(F&& ff) noexcept : f{std::move(ff)} {}
|
|
|
|
~final_action() noexcept
|
|
{
|
|
if (invoke) f();
|
|
}
|
|
|
|
final_action(final_action&& other) noexcept
|
|
: f(std::move(other.f)), invoke(std::exchange(other.invoke, false))
|
|
{}
|
|
|
|
final_action(const final_action&) = delete;
|
|
void operator=(const final_action&) = delete;
|
|
void operator=(final_action&&) = delete;
|
|
|
|
private:
|
|
F f;
|
|
bool invoke = true;
|
|
};
|
|
|
|
// finally() - convenience function to generate a final_action
|
|
template <class F>
|
|
GSL_NODISCARD auto finally(F&& f) noexcept
|
|
{
|
|
return final_action<std::decay_t<F>>{std::forward<F>(f)};
|
|
}
|
|
|
|
// narrow_cast(): a searchable way to do narrowing casts of values
|
|
template <class T, class U>
|
|
GSL_SUPPRESS(type.1) constexpr T narrow_cast(U&& u) noexcept
|
|
{
|
|
return static_cast<T>(std::forward<U>(u));
|
|
}
|
|
|
|
//
|
|
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
|
|
//
|
|
template <class T, std::size_t N>
|
|
GSL_SUPPRESS(bounds.4) GSL_SUPPRESS(bounds.2) constexpr T& at(T (&arr)[N], const index i)
|
|
{
|
|
static_assert(N <= static_cast<std::size_t>((std::numeric_limits<std::ptrdiff_t>::max)()),
|
|
"We only support arrays up to PTRDIFF_MAX bytes.");
|
|
Expects(i >= 0 && i < narrow_cast<index>(N));
|
|
return arr[narrow_cast<std::size_t>(i)];
|
|
}
|
|
|
|
template <class Cont>
|
|
GSL_SUPPRESS(bounds.4) GSL_SUPPRESS(bounds.2) constexpr auto at(Cont& cont, const index i)
|
|
-> decltype(cont[cont.size()])
|
|
{
|
|
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
|
|
using size_type = decltype(cont.size());
|
|
return cont[narrow_cast<size_type>(i)];
|
|
}
|
|
|
|
template <class T>
|
|
GSL_SUPPRESS(bounds.1) constexpr T at(const std::initializer_list<T> cont, const index i)
|
|
{
|
|
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
|
|
return *(cont.begin() + i);
|
|
}
|
|
|
|
template <class T, std::enable_if_t<std::is_move_assignable<T>::value &&
|
|
std::is_move_constructible<T>::value>>
|
|
void swap(T& a, T& b)
|
|
{
|
|
std::swap(a, b);
|
|
}
|
|
|
|
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
|
|
template <class T, std::size_t extent = std::dynamic_extent>
|
|
constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()])
|
|
{
|
|
Expects(i >= 0 && i < narrow_cast<index>(sp.size()));
|
|
return sp[gsl::narrow_cast<std::size_t>(i)];
|
|
}
|
|
#endif // __cpp_lib_span >= 202002L
|
|
} // namespace gsl
|
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif // _MSC_VER
|
|
|
|
#if defined(__clang__)
|
|
#if __has_warning("-Wunsafe-buffer-usage")
|
|
#pragma clang diagnostic pop
|
|
#endif // __has_warning("-Wunsafe-buffer-usage")
|
|
#endif // defined(__clang__)
|
|
|
|
#endif // GSL_UTIL_H
|