mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-01-31 11:33:27 +00:00
Yellow squiggly lines begone! Done automatically on .cpp files through `run-clang-tidy`, with manual corrections to the mistakes. If an import is directly used, but is technically unnecessary since it's recursively imported by something else, it is *not* removed. The tool doesn't touch .h files, so I did some of them by hand while fixing errors due to old recursive imports. Not everything is removed, but the cleanup should be substantial enough. Because this done on Linux, code that isn't used on it is mostly untouched. (Hopefully no open PR is depending on these imports...)
42 lines
1.3 KiB
C++
42 lines
1.3 KiB
C++
// Copyright 2024 Dolphin Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <span>
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
namespace Common
|
|
{
|
|
|
|
// Like std::span::subspan, except undefined behavior is replaced with returning a 0-length span.
|
|
template <class T>
|
|
[[nodiscard]] constexpr std::span<T> SafeSubspan(std::span<T> span, size_t offset,
|
|
size_t count = std::dynamic_extent)
|
|
{
|
|
if (count == std::dynamic_extent || offset > span.size())
|
|
return span.subspan(std::min(offset, span.size()));
|
|
else
|
|
return span.subspan(offset, std::min(count, span.size() - offset));
|
|
}
|
|
|
|
// Default-constructs an object of type T, then copies data into it from the specified offset in
|
|
// the specified span. Out-of-bounds reads will be skipped, meaning that specifying a too large
|
|
// offset results in the object partially or entirely remaining default constructed.
|
|
template <class T>
|
|
[[nodiscard]] T SafeSpanRead(std::span<const u8> span, size_t offset)
|
|
{
|
|
static_assert(std::is_trivially_copyable<T>());
|
|
|
|
const std::span<const u8> subspan = SafeSubspan(span, offset);
|
|
T result{};
|
|
std::memcpy(&result, subspan.data(), std::min(subspan.size(), sizeof(result)));
|
|
return result;
|
|
}
|
|
|
|
} // namespace Common
|