diff options
Diffstat (limited to 'src/common')
53 files changed, 598 insertions, 582 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d120c8d3d..5d54516eb 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -110,6 +110,7 @@ add_library(common STATIC common_funcs.h common_paths.h common_types.h + concepts.h dynamic_library.cpp dynamic_library.h fiber.cpp @@ -171,7 +172,6 @@ add_library(common STATIC virtual_buffer.h wall_clock.cpp wall_clock.h - web_result.h zstd_compression.cpp zstd_compression.h ) diff --git a/src/common/algorithm.h b/src/common/algorithm.h index e21b1373c..4804a3421 100644 --- a/src/common/algorithm.h +++ b/src/common/algorithm.h @@ -15,7 +15,8 @@ namespace Common { template <class ForwardIt, class T, class Compare = std::less<>> -ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) { +[[nodiscard]] ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value, + Compare comp = {}) { // Note: BOTH type T and the type after ForwardIt is dereferenced // must be implicitly convertible to BOTH Type1 and Type2, used in Compare. // This is stricter than lower_bound requirement (see above) diff --git a/src/common/alignment.h b/src/common/alignment.h index ef4d6f896..5040043de 100644 --- a/src/common/alignment.h +++ b/src/common/alignment.h @@ -9,7 +9,7 @@ namespace Common { template <typename T> -constexpr T AlignUp(T value, std::size_t size) { +[[nodiscard]] constexpr T AlignUp(T value, std::size_t size) { static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); auto mod{static_cast<T>(value % size)}; value -= mod; @@ -17,31 +17,31 @@ constexpr T AlignUp(T value, std::size_t size) { } template <typename T> -constexpr T AlignDown(T value, std::size_t size) { +[[nodiscard]] constexpr T AlignDown(T value, std::size_t size) { static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); return static_cast<T>(value - value % size); } template <typename T> -constexpr T AlignBits(T value, std::size_t align) { +[[nodiscard]] constexpr T AlignBits(T value, std::size_t align) { static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); return static_cast<T>((value + ((1ULL << align) - 1)) >> align << align); } template <typename T> -constexpr bool Is4KBAligned(T value) { +[[nodiscard]] constexpr bool Is4KBAligned(T value) { static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); return (value & 0xFFF) == 0; } template <typename T> -constexpr bool IsWordAligned(T value) { +[[nodiscard]] constexpr bool IsWordAligned(T value) { static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); return (value & 0b11) == 0; } template <typename T> -constexpr bool IsAligned(T value, std::size_t alignment) { +[[nodiscard]] constexpr bool IsAligned(T value, std::size_t alignment) { using U = typename std::make_unsigned<T>::type; const U mask = static_cast<U>(alignment - 1); return (value & mask) == 0; @@ -64,7 +64,7 @@ public: template <typename T2> constexpr AlignmentAllocator(const AlignmentAllocator<T2, Align>&) noexcept {} - T* allocate(size_type n) { + [[nodiscard]] T* allocate(size_type n) { return static_cast<T*>(::operator new (n * sizeof(T), std::align_val_t{Align})); } diff --git a/src/common/assert.h b/src/common/assert.h index 5b67c5c52..06d7b5612 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -17,11 +17,12 @@ // enough for our purposes. template <typename Fn> #if defined(_MSC_VER) -__declspec(noinline, noreturn) +[[msvc::noinline, noreturn]] #elif defined(__GNUC__) - __attribute__((noinline, noreturn, cold)) +[[gnu::cold, gnu::noinline, noreturn]] #endif - static void assert_noinline_call(const Fn& fn) { +static void +assert_noinline_call(const Fn& fn) { fn(); Crash(); exit(1); // Keeps GCC's mouth shut about this actually returning diff --git a/src/common/atomic_ops.cpp b/src/common/atomic_ops.cpp index 1098e21ff..1612d0e67 100644 --- a/src/common/atomic_ops.cpp +++ b/src/common/atomic_ops.cpp @@ -14,50 +14,55 @@ namespace Common { #if _MSC_VER -bool AtomicCompareAndSwap(u8 volatile* pointer, u8 value, u8 expected) { - u8 result = _InterlockedCompareExchange8((char*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { + const u8 result = + _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); return result == expected; } -bool AtomicCompareAndSwap(u16 volatile* pointer, u16 value, u16 expected) { - u16 result = _InterlockedCompareExchange16((short*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { + const u16 result = + _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); return result == expected; } -bool AtomicCompareAndSwap(u32 volatile* pointer, u32 value, u32 expected) { - u32 result = _InterlockedCompareExchange((long*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { + const u32 result = + _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); return result == expected; } -bool AtomicCompareAndSwap(u64 volatile* pointer, u64 value, u64 expected) { - u64 result = _InterlockedCompareExchange64((__int64*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { + const u64 result = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), + value, expected); return result == expected; } -bool AtomicCompareAndSwap(u64 volatile* pointer, u128 value, u128 expected) { - return _InterlockedCompareExchange128((__int64*)pointer, value[1], value[0], - (__int64*)expected.data()) != 0; +bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected) { + return _InterlockedCompareExchange128(reinterpret_cast<volatile __int64*>(pointer), value[1], + value[0], + reinterpret_cast<__int64*>(expected.data())) != 0; } #else -bool AtomicCompareAndSwap(u8 volatile* pointer, u8 value, u8 expected) { +bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u16 volatile* pointer, u16 value, u16 expected) { +bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u32 volatile* pointer, u32 value, u32 expected) { +bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u64 volatile* pointer, u64 value, u64 expected) { +bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u64 volatile* pointer, u128 value, u128 expected) { +bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected) { unsigned __int128 value_a; unsigned __int128 expected_a; std::memcpy(&value_a, value.data(), sizeof(u128)); diff --git a/src/common/atomic_ops.h b/src/common/atomic_ops.h index e6181d521..b46888589 100644 --- a/src/common/atomic_ops.h +++ b/src/common/atomic_ops.h @@ -8,10 +8,10 @@ namespace Common { -bool AtomicCompareAndSwap(u8 volatile* pointer, u8 value, u8 expected); -bool AtomicCompareAndSwap(u16 volatile* pointer, u16 value, u16 expected); -bool AtomicCompareAndSwap(u32 volatile* pointer, u32 value, u32 expected); -bool AtomicCompareAndSwap(u64 volatile* pointer, u64 value, u64 expected); -bool AtomicCompareAndSwap(u64 volatile* pointer, u128 value, u128 expected); +[[nodiscard]] bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected); +[[nodiscard]] bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected); +[[nodiscard]] bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected); +[[nodiscard]] bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected); +[[nodiscard]] bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected); } // namespace Common diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 26ae6c7fc..0f0661172 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -36,13 +36,6 @@ #include "common/common_funcs.h" #include "common/swap.h" -// Inlining -#ifdef _WIN32 -#define FORCE_INLINE __forceinline -#else -#define FORCE_INLINE inline __attribute__((always_inline)) -#endif - /* * Abstract bitfield class * @@ -142,8 +135,8 @@ public: * containing several bitfields can be assembled by formatting each of their values and ORing * the results together. */ - static constexpr FORCE_INLINE StorageType FormatValue(const T& value) { - return ((StorageType)value << position) & mask; + [[nodiscard]] static constexpr StorageType FormatValue(const T& value) { + return (static_cast<StorageType>(value) << position) & mask; } /** @@ -151,7 +144,7 @@ public: * (such as Value() or operator T), but this can be used to extract a value from a bitfield * union in a constexpr context. */ - static constexpr FORCE_INLINE T ExtractValue(const StorageType& storage) { + [[nodiscard]] static constexpr T ExtractValue(const StorageType& storage) { if constexpr (std::numeric_limits<UnderlyingType>::is_signed) { std::size_t shift = 8 * sizeof(T) - bits; return static_cast<T>(static_cast<UnderlyingType>(storage << (shift - position)) >> @@ -175,7 +168,7 @@ public: constexpr BitField(BitField&&) noexcept = default; constexpr BitField& operator=(BitField&&) noexcept = default; - constexpr operator T() const { + [[nodiscard]] constexpr operator T() const { return Value(); } @@ -183,11 +176,11 @@ public: storage = static_cast<StorageType>((storage & ~mask) | FormatValue(value)); } - constexpr T Value() const { + [[nodiscard]] constexpr T Value() const { return ExtractValue(storage); } - constexpr explicit operator bool() const { + [[nodiscard]] constexpr explicit operator bool() const { return Value() != 0; } diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 6f7d5a947..29f59a9a3 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -17,12 +17,12 @@ namespace Common { /// Gets the size of a specified type T in bits. template <typename T> -constexpr std::size_t BitSize() { +[[nodiscard]] constexpr std::size_t BitSize() { return sizeof(T) * CHAR_BIT; } #ifdef _MSC_VER -inline u32 CountLeadingZeroes32(u32 value) { +[[nodiscard]] inline u32 CountLeadingZeroes32(u32 value) { unsigned long leading_zero = 0; if (_BitScanReverse(&leading_zero, value) != 0) { @@ -32,7 +32,7 @@ inline u32 CountLeadingZeroes32(u32 value) { return 32; } -inline u32 CountLeadingZeroes64(u64 value) { +[[nodiscard]] inline u32 CountLeadingZeroes64(u64 value) { unsigned long leading_zero = 0; if (_BitScanReverse64(&leading_zero, value) != 0) { @@ -42,7 +42,7 @@ inline u32 CountLeadingZeroes64(u64 value) { return 64; } #else -inline u32 CountLeadingZeroes32(u32 value) { +[[nodiscard]] inline u32 CountLeadingZeroes32(u32 value) { if (value == 0) { return 32; } @@ -50,7 +50,7 @@ inline u32 CountLeadingZeroes32(u32 value) { return static_cast<u32>(__builtin_clz(value)); } -inline u32 CountLeadingZeroes64(u64 value) { +[[nodiscard]] inline u32 CountLeadingZeroes64(u64 value) { if (value == 0) { return 64; } @@ -60,7 +60,7 @@ inline u32 CountLeadingZeroes64(u64 value) { #endif #ifdef _MSC_VER -inline u32 CountTrailingZeroes32(u32 value) { +[[nodiscard]] inline u32 CountTrailingZeroes32(u32 value) { unsigned long trailing_zero = 0; if (_BitScanForward(&trailing_zero, value) != 0) { @@ -70,7 +70,7 @@ inline u32 CountTrailingZeroes32(u32 value) { return 32; } -inline u32 CountTrailingZeroes64(u64 value) { +[[nodiscard]] inline u32 CountTrailingZeroes64(u64 value) { unsigned long trailing_zero = 0; if (_BitScanForward64(&trailing_zero, value) != 0) { @@ -80,7 +80,7 @@ inline u32 CountTrailingZeroes64(u64 value) { return 64; } #else -inline u32 CountTrailingZeroes32(u32 value) { +[[nodiscard]] inline u32 CountTrailingZeroes32(u32 value) { if (value == 0) { return 32; } @@ -88,7 +88,7 @@ inline u32 CountTrailingZeroes32(u32 value) { return static_cast<u32>(__builtin_ctz(value)); } -inline u32 CountTrailingZeroes64(u64 value) { +[[nodiscard]] inline u32 CountTrailingZeroes64(u64 value) { if (value == 0) { return 64; } @@ -99,13 +99,13 @@ inline u32 CountTrailingZeroes64(u64 value) { #ifdef _MSC_VER -inline u32 MostSignificantBit32(const u32 value) { +[[nodiscard]] inline u32 MostSignificantBit32(const u32 value) { unsigned long result; _BitScanReverse(&result, value); return static_cast<u32>(result); } -inline u32 MostSignificantBit64(const u64 value) { +[[nodiscard]] inline u32 MostSignificantBit64(const u64 value) { unsigned long result; _BitScanReverse64(&result, value); return static_cast<u32>(result); @@ -113,30 +113,30 @@ inline u32 MostSignificantBit64(const u64 value) { #else -inline u32 MostSignificantBit32(const u32 value) { +[[nodiscard]] inline u32 MostSignificantBit32(const u32 value) { return 31U - static_cast<u32>(__builtin_clz(value)); } -inline u32 MostSignificantBit64(const u64 value) { +[[nodiscard]] inline u32 MostSignificantBit64(const u64 value) { return 63U - static_cast<u32>(__builtin_clzll(value)); } #endif -inline u32 Log2Floor32(const u32 value) { +[[nodiscard]] inline u32 Log2Floor32(const u32 value) { return MostSignificantBit32(value); } -inline u32 Log2Ceil32(const u32 value) { +[[nodiscard]] inline u32 Log2Ceil32(const u32 value) { const u32 log2_f = Log2Floor32(value); return log2_f + ((value ^ (1U << log2_f)) != 0U); } -inline u32 Log2Floor64(const u64 value) { +[[nodiscard]] inline u32 Log2Floor64(const u64 value) { return MostSignificantBit64(value); } -inline u32 Log2Ceil64(const u64 value) { +[[nodiscard]] inline u32 Log2Ceil64(const u64 value) { const u64 log2_f = static_cast<u64>(Log2Floor64(value)); return static_cast<u32>(log2_f + ((value ^ (1ULL << log2_f)) != 0ULL)); } diff --git a/src/common/cityhash.h b/src/common/cityhash.h index 4b94f8e18..a00804e01 100644 --- a/src/common/cityhash.h +++ b/src/common/cityhash.h @@ -61,42 +61,43 @@ #pragma once +#include <cstddef> +#include <cstdint> #include <utility> -#include <stdint.h> -#include <stdlib.h> // for std::size_t. namespace Common { -typedef std::pair<uint64_t, uint64_t> uint128; +using uint128 = std::pair<uint64_t, uint64_t>; -inline uint64_t Uint128Low64(const uint128& x) { +[[nodiscard]] inline uint64_t Uint128Low64(const uint128& x) { return x.first; } -inline uint64_t Uint128High64(const uint128& x) { +[[nodiscard]] inline uint64_t Uint128High64(const uint128& x) { return x.second; } // Hash function for a byte array. -uint64_t CityHash64(const char* buf, std::size_t len); +[[nodiscard]] uint64_t CityHash64(const char* buf, std::size_t len); // Hash function for a byte array. For convenience, a 64-bit seed is also // hashed into the result. -uint64_t CityHash64WithSeed(const char* buf, std::size_t len, uint64_t seed); +[[nodiscard]] uint64_t CityHash64WithSeed(const char* buf, std::size_t len, uint64_t seed); // Hash function for a byte array. For convenience, two seeds are also // hashed into the result. -uint64_t CityHash64WithSeeds(const char* buf, std::size_t len, uint64_t seed0, uint64_t seed1); +[[nodiscard]] uint64_t CityHash64WithSeeds(const char* buf, std::size_t len, uint64_t seed0, + uint64_t seed1); // Hash function for a byte array. -uint128 CityHash128(const char* s, std::size_t len); +[[nodiscard]] uint128 CityHash128(const char* s, std::size_t len); // Hash function for a byte array. For convenience, a 128-bit seed is also // hashed into the result. -uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed); +[[nodiscard]] uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed); // Hash 128 input bits down to 64 bits of output. // This is intended to be a reasonably good hash function. -inline uint64_t Hash128to64(const uint128& x) { +[[nodiscard]] inline uint64_t Hash128to64(const uint128& x) { // Murmur-inspired hashing. const uint64_t kMul = 0x9ddfea08eb382d69ULL; uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; diff --git a/src/common/color.h b/src/common/color.h index 3a2222077..bbcac858e 100644 --- a/src/common/color.h +++ b/src/common/color.h @@ -10,45 +10,45 @@ #include "common/swap.h" #include "common/vector_math.h" -namespace Color { +namespace Common::Color { /// Convert a 1-bit color component to 8 bit -constexpr u8 Convert1To8(u8 value) { +[[nodiscard]] constexpr u8 Convert1To8(u8 value) { return value * 255; } /// Convert a 4-bit color component to 8 bit -constexpr u8 Convert4To8(u8 value) { +[[nodiscard]] constexpr u8 Convert4To8(u8 value) { return (value << 4) | value; } /// Convert a 5-bit color component to 8 bit -constexpr u8 Convert5To8(u8 value) { +[[nodiscard]] constexpr u8 Convert5To8(u8 value) { return (value << 3) | (value >> 2); } /// Convert a 6-bit color component to 8 bit -constexpr u8 Convert6To8(u8 value) { +[[nodiscard]] constexpr u8 Convert6To8(u8 value) { return (value << 2) | (value >> 4); } /// Convert a 8-bit color component to 1 bit -constexpr u8 Convert8To1(u8 value) { +[[nodiscard]] constexpr u8 Convert8To1(u8 value) { return value >> 7; } /// Convert a 8-bit color component to 4 bit -constexpr u8 Convert8To4(u8 value) { +[[nodiscard]] constexpr u8 Convert8To4(u8 value) { return value >> 4; } /// Convert a 8-bit color component to 5 bit -constexpr u8 Convert8To5(u8 value) { +[[nodiscard]] constexpr u8 Convert8To5(u8 value) { return value >> 3; } /// Convert a 8-bit color component to 6 bit -constexpr u8 Convert8To6(u8 value) { +[[nodiscard]] constexpr u8 Convert8To6(u8 value) { return value >> 2; } @@ -57,7 +57,7 @@ constexpr u8 Convert8To6(u8 value) { * @param bytes Pointer to encoded source color * @return Result color decoded as Common::Vec4<u8> */ -inline Common::Vec4<u8> DecodeRGBA8(const u8* bytes) { +[[nodiscard]] inline Common::Vec4<u8> DecodeRGBA8(const u8* bytes) { return {bytes[3], bytes[2], bytes[1], bytes[0]}; } @@ -66,7 +66,7 @@ inline Common::Vec4<u8> DecodeRGBA8(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Common::Vec4<u8> */ -inline Common::Vec4<u8> DecodeRGB8(const u8* bytes) { +[[nodiscard]] inline Common::Vec4<u8> DecodeRGB8(const u8* bytes) { return {bytes[2], bytes[1], bytes[0], 255}; } @@ -75,7 +75,7 @@ inline Common::Vec4<u8> DecodeRGB8(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Common::Vec4<u8> */ -inline Common::Vec4<u8> DecodeRG8(const u8* bytes) { +[[nodiscard]] inline Common::Vec4<u8> DecodeRG8(const u8* bytes) { return {bytes[1], bytes[0], 0, 255}; } @@ -84,7 +84,7 @@ inline Common::Vec4<u8> DecodeRG8(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Common::Vec4<u8> */ -inline Common::Vec4<u8> DecodeRGB565(const u8* bytes) { +[[nodiscard]] inline Common::Vec4<u8> DecodeRGB565(const u8* bytes) { u16_le pixel; std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), @@ -96,7 +96,7 @@ inline Common::Vec4<u8> DecodeRGB565(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Common::Vec4<u8> */ -inline Common::Vec4<u8> DecodeRGB5A1(const u8* bytes) { +[[nodiscard]] inline Common::Vec4<u8> DecodeRGB5A1(const u8* bytes) { u16_le pixel; std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), @@ -108,7 +108,7 @@ inline Common::Vec4<u8> DecodeRGB5A1(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Common::Vec4<u8> */ -inline Common::Vec4<u8> DecodeRGBA4(const u8* bytes) { +[[nodiscard]] inline Common::Vec4<u8> DecodeRGBA4(const u8* bytes) { u16_le pixel; std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), @@ -120,7 +120,7 @@ inline Common::Vec4<u8> DecodeRGBA4(const u8* bytes) { * @param bytes Pointer to encoded source value * @return Depth value as an u32 */ -inline u32 DecodeD16(const u8* bytes) { +[[nodiscard]] inline u32 DecodeD16(const u8* bytes) { u16_le data; std::memcpy(&data, bytes, sizeof(data)); return data; @@ -131,7 +131,7 @@ inline u32 DecodeD16(const u8* bytes) { * @param bytes Pointer to encoded source value * @return Depth value as an u32 */ -inline u32 DecodeD24(const u8* bytes) { +[[nodiscard]] inline u32 DecodeD24(const u8* bytes) { return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; } @@ -140,7 +140,7 @@ inline u32 DecodeD24(const u8* bytes) { * @param bytes Pointer to encoded source values * @return Resulting values stored as a Common::Vec2 */ -inline Common::Vec2<u32> DecodeD24S8(const u8* bytes) { +[[nodiscard]] inline Common::Vec2<u32> DecodeD24S8(const u8* bytes) { return {static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3]}; } @@ -268,4 +268,4 @@ inline void EncodeX24S8(u8 stencil, u8* bytes) { bytes[3] = stencil; } -} // namespace Color +} // namespace Common::Color diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 88cf5250a..367b6bf6e 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -53,43 +53,49 @@ __declspec(dllimport) void __stdcall DebugBreak(void); // Call directly after the command or use the error num. // This function might change the error code. // Defined in Misc.cpp. -std::string GetLastErrorMsg(); +[[nodiscard]] std::string GetLastErrorMsg(); #define DECLARE_ENUM_FLAG_OPERATORS(type) \ - constexpr type operator|(type a, type b) noexcept { \ + [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \ using T = std::underlying_type_t<type>; \ return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \ } \ - constexpr type operator&(type a, type b) noexcept { \ + [[nodiscard]] constexpr type operator&(type a, type b) noexcept { \ using T = std::underlying_type_t<type>; \ return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \ } \ - constexpr type& operator|=(type& a, type b) noexcept { \ + [[nodiscard]] constexpr type operator^(type a, type b) noexcept { \ using T = std::underlying_type_t<type>; \ - a = static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \ + return static_cast<type>(static_cast<T>(a) ^ static_cast<T>(b)); \ + } \ + constexpr type& operator|=(type& a, type b) noexcept { \ + a = a | b; \ return a; \ } \ constexpr type& operator&=(type& a, type b) noexcept { \ - using T = std::underlying_type_t<type>; \ - a = static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \ + a = a & b; \ + return a; \ + } \ + constexpr type& operator^=(type& a, type b) noexcept { \ + a = a ^ b; \ return a; \ } \ - constexpr type operator~(type key) noexcept { \ + [[nodiscard]] constexpr type operator~(type key) noexcept { \ using T = std::underlying_type_t<type>; \ return static_cast<type>(~static_cast<T>(key)); \ } \ - constexpr bool True(type key) noexcept { \ + [[nodiscard]] constexpr bool True(type key) noexcept { \ using T = std::underlying_type_t<type>; \ return static_cast<T>(key) != 0; \ } \ - constexpr bool False(type key) noexcept { \ + [[nodiscard]] constexpr bool False(type key) noexcept { \ using T = std::underlying_type_t<type>; \ return static_cast<T>(key) == 0; \ } namespace Common { -constexpr u32 MakeMagic(char a, char b, char c, char d) { +[[nodiscard]] constexpr u32 MakeMagic(char a, char b, char c, char d) { return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24; } diff --git a/src/common/common_paths.h b/src/common/common_paths.h index 076752d3b..3c593d5f6 100644 --- a/src/common/common_paths.h +++ b/src/common/common_paths.h @@ -35,6 +35,7 @@ #define KEYS_DIR "keys" #define LOAD_DIR "load" #define DUMP_DIR "dump" +#define SCREENSHOTS_DIR "screenshots" #define SHADER_DIR "shader" #define LOG_DIR "log" diff --git a/src/common/concepts.h b/src/common/concepts.h new file mode 100644 index 000000000..5bef3ad67 --- /dev/null +++ b/src/common/concepts.h @@ -0,0 +1,34 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <type_traits> + +namespace Common { + +// Check if type is like an STL container +template <typename T> +concept IsSTLContainer = requires(T t) { + typename T::value_type; + typename T::iterator; + typename T::const_iterator; + // TODO(ogniK): Replace below is std::same_as<void> when MSVC supports it. + t.begin(); + t.end(); + t.cbegin(); + t.cend(); + t.data(); + t.size(); +}; + +// TODO: Replace with std::derived_from when the <concepts> header +// is available on all supported platforms. +template <typename Derived, typename Base> +concept DerivedFrom = requires { + std::is_base_of_v<Base, Derived>; + std::is_convertible_v<const volatile Derived*, const volatile Base*>; +}; + +} // namespace Common diff --git a/src/common/detached_tasks.cpp b/src/common/detached_tasks.cpp index f268d6021..f2b4939df 100644 --- a/src/common/detached_tasks.cpp +++ b/src/common/detached_tasks.cpp @@ -34,8 +34,7 @@ void DetachedTasks::AddTask(std::function<void()> task) { std::unique_lock lock{instance->mutex}; --instance->count; std::notify_all_at_thread_exit(instance->cv, std::move(lock)); - }) - .detach(); + }).detach(); } } // namespace Common diff --git a/src/common/dynamic_library.cpp b/src/common/dynamic_library.cpp index 7ab54e9e4..7f0a10521 100644 --- a/src/common/dynamic_library.cpp +++ b/src/common/dynamic_library.cpp @@ -21,7 +21,7 @@ namespace Common { DynamicLibrary::DynamicLibrary() = default; DynamicLibrary::DynamicLibrary(const char* filename) { - Open(filename); + void(Open(filename)); } DynamicLibrary::DynamicLibrary(DynamicLibrary&& rhs) noexcept diff --git a/src/common/dynamic_library.h b/src/common/dynamic_library.h index 2a06372fd..3512da940 100644 --- a/src/common/dynamic_library.h +++ b/src/common/dynamic_library.h @@ -33,7 +33,7 @@ public: ~DynamicLibrary(); /// Returns the specified library name with the platform-specific suffix added. - static std::string GetUnprefixedFilename(const char* filename); + [[nodiscard]] static std::string GetUnprefixedFilename(const char* filename); /// Returns the specified library name in platform-specific format. /// Major/minor versions will not be included if set to -1. @@ -41,28 +41,29 @@ public: /// Windows: LIBNAME-MAJOR-MINOR.dll /// Linux: libLIBNAME.so.MAJOR.MINOR /// Mac: libLIBNAME.MAJOR.MINOR.dylib - static std::string GetVersionedFilename(const char* libname, int major = -1, int minor = -1); + [[nodiscard]] static std::string GetVersionedFilename(const char* libname, int major = -1, + int minor = -1); /// Returns true if a module is loaded, otherwise false. - bool IsOpen() const { + [[nodiscard]] bool IsOpen() const { return handle != nullptr; } /// Loads (or replaces) the handle with the specified library file name. /// Returns true if the library was loaded and can be used. - bool Open(const char* filename); + [[nodiscard]] bool Open(const char* filename); /// Unloads the library, any function pointers from this library are no longer valid. void Close(); /// Returns the address of the specified symbol (function or variable) as an untyped pointer. /// If the specified symbol does not exist in this library, nullptr is returned. - void* GetSymbolAddress(const char* name) const; + [[nodiscard]] void* GetSymbolAddress(const char* name) const; /// Obtains the address of the specified symbol, automatically casting to the correct type. /// Returns true if the symbol was found and assigned, otherwise false. template <typename T> - bool GetSymbol(const char* name, T* ptr) const { + [[nodiscard]] bool GetSymbol(const char* name, T* ptr) const { *ptr = reinterpret_cast<T>(GetSymbolAddress(name)); return *ptr != nullptr; } diff --git a/src/common/fiber.h b/src/common/fiber.h index dafc1100e..89dde5e36 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -47,7 +47,7 @@ public: /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. static void YieldTo(std::shared_ptr<Fiber>& from, std::shared_ptr<Fiber>& to); - static std::shared_ptr<Fiber> ThreadToFiber(); + [[nodiscard]] static std::shared_ptr<Fiber> ThreadToFiber(); void SetRewindPoint(std::function<void(void*)>&& rewind_func, void* start_parameter); diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 45b750e1e..16c3713e0 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -74,7 +74,7 @@ // This namespace has various generic functions related to files and paths. // The code still needs a ton of cleanup. // REMEMBER: strdup considered harmful! -namespace FileUtil { +namespace Common::FS { // Remove any ending forward slashes from directory paths // Modifies argument. @@ -196,7 +196,7 @@ bool CreateFullPath(const std::string& fullPath) { int panicCounter = 100; LOG_TRACE(Common_Filesystem, "path {}", fullPath); - if (FileUtil::Exists(fullPath)) { + if (Exists(fullPath)) { LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); return true; } @@ -212,7 +212,7 @@ bool CreateFullPath(const std::string& fullPath) { // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") std::string const subPath(fullPath.substr(0, position + 1)); - if (!FileUtil::IsDirectory(subPath) && !FileUtil::CreateDir(subPath)) { + if (!IsDirectory(subPath) && !CreateDir(subPath)) { LOG_ERROR(Common, "CreateFullPath: directory creation failed"); return false; } @@ -231,7 +231,7 @@ bool DeleteDir(const std::string& filename) { LOG_TRACE(Common_Filesystem, "directory {}", filename); // check if a directory - if (!FileUtil::IsDirectory(filename)) { + if (!IsDirectory(filename)) { LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); return false; } @@ -371,7 +371,7 @@ u64 GetSize(FILE* f) { bool CreateEmptyFile(const std::string& filename) { LOG_TRACE(Common_Filesystem, "{}", filename); - if (!FileUtil::IOFile(filename, "wb").IsOpen()) { + if (!IOFile(filename, "wb").IsOpen()) { LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); return false; } @@ -488,29 +488,34 @@ bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) return false; // Delete the outermost directory - FileUtil::DeleteDir(directory); + DeleteDir(directory); return true; } void CopyDir(const std::string& source_path, const std::string& dest_path) { #ifndef _WIN32 - if (source_path == dest_path) + if (source_path == dest_path) { return; - if (!FileUtil::Exists(source_path)) + } + if (!Exists(source_path)) { return; - if (!FileUtil::Exists(dest_path)) - FileUtil::CreateFullPath(dest_path); + } + if (!Exists(dest_path)) { + CreateFullPath(dest_path); + } DIR* dirp = opendir(source_path.c_str()); - if (!dirp) + if (!dirp) { return; + } while (struct dirent* result = readdir(dirp)) { const std::string virtualName(result->d_name); // check for "." and ".." if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || - ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) + ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) { continue; + } std::string source, dest; source = source_path + virtualName; @@ -518,11 +523,13 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) { if (IsDirectory(source)) { source += '/'; dest += '/'; - if (!FileUtil::Exists(dest)) - FileUtil::CreateFullPath(dest); + if (!Exists(dest)) { + CreateFullPath(dest); + } CopyDir(source, dest); - } else if (!FileUtil::Exists(dest)) - FileUtil::Copy(source, dest); + } else if (!Exists(dest)) { + Copy(source, dest); + } } closedir(dirp); #endif @@ -538,7 +545,7 @@ std::optional<std::string> GetCurrentDir() { if (!dir) { #endif LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); - return {}; + return std::nullopt; } #ifdef _WIN32 std::string strDir = Common::UTF16ToUTF8(dir); @@ -546,7 +553,7 @@ std::optional<std::string> GetCurrentDir() { std::string strDir = dir; #endif free(dir); - return strDir; + return std::move(strDir); } bool SetCurrentDir(const std::string& directory) { @@ -668,7 +675,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { if (user_path.empty()) { #ifdef _WIN32 user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; - if (!FileUtil::IsDirectory(user_path)) { + if (!IsDirectory(user_path)) { user_path = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP; } else { LOG_INFO(Common_Filesystem, "Using the local user directory"); @@ -677,7 +684,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); #else - if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { + if (Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); @@ -695,6 +702,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); paths.emplace(UserPath::LoadDir, user_path + LOAD_DIR DIR_SEP); paths.emplace(UserPath::DumpDir, user_path + DUMP_DIR DIR_SEP); + paths.emplace(UserPath::ScreenshotsDir, user_path + SCREENSHOTS_DIR DIR_SEP); paths.emplace(UserPath::ShaderDir, user_path + SHADER_DIR DIR_SEP); paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); paths.emplace(UserPath::KeysDir, user_path + KEYS_DIR DIR_SEP); @@ -703,7 +711,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { } if (!new_path.empty()) { - if (!FileUtil::IsDirectory(new_path)) { + if (!IsDirectory(new_path)) { LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path); return paths[path]; } else { @@ -901,10 +909,10 @@ std::string SanitizePath(std::string_view path_, DirectorySeparator directory_se return std::string(RemoveTrailingSlash(path)); } -IOFile::IOFile() {} +IOFile::IOFile() = default; IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { - Open(filename, openmode, flags); + void(Open(filename, openmode, flags)); } IOFile::~IOFile() { @@ -945,17 +953,18 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags) } bool IOFile::Close() { - if (!IsOpen() || 0 != std::fclose(m_file)) + if (!IsOpen() || 0 != std::fclose(m_file)) { return false; + } m_file = nullptr; return true; } u64 IOFile::GetSize() const { - if (IsOpen()) - return FileUtil::GetSize(m_file); - + if (IsOpen()) { + return FS::GetSize(m_file); + } return 0; } @@ -964,9 +973,9 @@ bool IOFile::Seek(s64 off, int origin) const { } u64 IOFile::Tell() const { - if (IsOpen()) + if (IsOpen()) { return ftello(m_file); - + } return std::numeric_limits<u64>::max(); } @@ -1015,4 +1024,4 @@ bool IOFile::Resize(u64 size) { ; } -} // namespace FileUtil +} // namespace Common::FS diff --git a/src/common/file_util.h b/src/common/file_util.h index f7a0c33fa..8b587320f 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -19,7 +19,7 @@ #include "common/string_util.h" #endif -namespace FileUtil { +namespace Common::FS { // User paths for GetUserPath enum class UserPath { @@ -32,6 +32,7 @@ enum class UserPath { SDMCDir, LoadDir, DumpDir, + ScreenshotsDir, ShaderDir, SysDataDir, UserDir, @@ -47,19 +48,19 @@ struct FSTEntry { }; // Returns true if file filename exists -bool Exists(const std::string& filename); +[[nodiscard]] bool Exists(const std::string& filename); // Returns true if filename is a directory -bool IsDirectory(const std::string& filename); +[[nodiscard]] bool IsDirectory(const std::string& filename); // Returns the size of filename (64bit) -u64 GetSize(const std::string& filename); +[[nodiscard]] u64 GetSize(const std::string& filename); // Overloaded GetSize, accepts file descriptor -u64 GetSize(const int fd); +[[nodiscard]] u64 GetSize(int fd); // Overloaded GetSize, accepts FILE* -u64 GetSize(FILE* f); +[[nodiscard]] u64 GetSize(FILE* f); // Returns true if successful, or path already exists. bool CreateDir(const std::string& filename); @@ -119,7 +120,7 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); // Returns the current directory -std::optional<std::string> GetCurrentDir(); +[[nodiscard]] std::optional<std::string> GetCurrentDir(); // Create directory and copy contents (does not overwrite existing files) void CopyDir(const std::string& source_path, const std::string& dest_path); @@ -131,20 +132,20 @@ bool SetCurrentDir(const std::string& directory); // directory. To be used in "multi-user" mode (that is, installed). const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); -std::string GetHactoolConfigurationPath(); +[[nodiscard]] std::string GetHactoolConfigurationPath(); -std::string GetNANDRegistrationDir(bool system = false); +[[nodiscard]] std::string GetNANDRegistrationDir(bool system = false); // Returns the path to where the sys file are -std::string GetSysDirectory(); +[[nodiscard]] std::string GetSysDirectory(); #ifdef __APPLE__ -std::string GetBundleDirectory(); +[[nodiscard]] std::string GetBundleDirectory(); #endif #ifdef _WIN32 -const std::string& GetExeDirectory(); -std::string AppDataRoamingDirectory(); +[[nodiscard]] const std::string& GetExeDirectory(); +[[nodiscard]] std::string AppDataRoamingDirectory(); #endif std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str); @@ -163,38 +164,55 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam // Splits the path on '/' or '\' and put the components into a vector // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } -std::vector<std::string> SplitPathComponents(std::string_view filename); +[[nodiscard]] std::vector<std::string> SplitPathComponents(std::string_view filename); // Gets all of the text up to the last '/' or '\' in the path. -std::string_view GetParentPath(std::string_view path); +[[nodiscard]] std::string_view GetParentPath(std::string_view path); // Gets all of the text after the first '/' or '\' in the path. -std::string_view GetPathWithoutTop(std::string_view path); +[[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path); // Gets the filename of the path -std::string_view GetFilename(std::string_view path); +[[nodiscard]] std::string_view GetFilename(std::string_view path); // Gets the extension of the filename -std::string_view GetExtensionFromFilename(std::string_view name); +[[nodiscard]] std::string_view GetExtensionFromFilename(std::string_view name); // Removes the final '/' or '\' if one exists -std::string_view RemoveTrailingSlash(std::string_view path); +[[nodiscard]] std::string_view RemoveTrailingSlash(std::string_view path); // Creates a new vector containing indices [first, last) from the original. template <typename T> -std::vector<T> SliceVector(const std::vector<T>& vector, std::size_t first, std::size_t last) { - if (first >= last) +[[nodiscard]] std::vector<T> SliceVector(const std::vector<T>& vector, std::size_t first, + std::size_t last) { + if (first >= last) { return {}; + } last = std::min<std::size_t>(last, vector.size()); return std::vector<T>(vector.begin() + first, vector.begin() + first + last); } -enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault }; +enum class DirectorySeparator { + ForwardSlash, + BackwardSlash, + PlatformDefault, +}; // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' // depending if directory_separator is BackwardSlash or PlatformDefault and running on windows -std::string SanitizePath(std::string_view path, - DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); +[[nodiscard]] std::string SanitizePath( + std::string_view path, + DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); + +// To deal with Windows being dumb at Unicode +template <typename T> +void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) { +#ifdef _MSC_VER + fstream.open(Common::UTF8ToUTF16W(filename), openmode); +#else + fstream.open(filename, openmode); +#endif +} // simple wrapper for cstdlib file functions to // hopefully will make error checking easier @@ -214,7 +232,7 @@ public: void Swap(IOFile& other) noexcept; - bool Open(const std::string& filename, const char openmode[], int flags = 0); + [[nodiscard]] bool Open(const std::string& filename, const char openmode[], int flags = 0); bool Close(); template <typename T> @@ -255,13 +273,13 @@ public: return WriteArray(str.data(), str.length()); } - bool IsOpen() const { + [[nodiscard]] bool IsOpen() const { return nullptr != m_file; } bool Seek(s64 off, int origin) const; - u64 Tell() const; - u64 GetSize() const; + [[nodiscard]] u64 Tell() const; + [[nodiscard]] u64 GetSize() const; bool Resize(u64 size); bool Flush(); @@ -277,14 +295,4 @@ private: std::FILE* m_file = nullptr; }; -} // namespace FileUtil - -// To deal with Windows being dumb at unicode: -template <typename T> -void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) { -#ifdef _MSC_VER - fstream.open(Common::UTF8ToUTF16W(filename), openmode); -#else - fstream.open(filename, openmode); -#endif -} +} // namespace Common::FS diff --git a/src/common/hash.h b/src/common/hash.h index b2538f3ea..298930702 100644 --- a/src/common/hash.h +++ b/src/common/hash.h @@ -5,36 +5,11 @@ #pragma once #include <cstddef> -#include <cstring> #include <utility> #include <boost/functional/hash.hpp> -#include "common/cityhash.h" -#include "common/common_types.h" namespace Common { -/** - * Computes a 64-bit hash over the specified block of data - * @param data Block of data to compute hash over - * @param len Length of data (in bytes) to compute hash over - * @returns 64-bit hash value that was computed over the data block - */ -static inline u64 ComputeHash64(const void* data, std::size_t len) { - return CityHash64(static_cast<const char*>(data), len); -} - -/** - * Computes a 64-bit hash of a struct. In addition to being trivially copyable, it is also critical - * that either the struct includes no padding, or that any padding is initialized to a known value - * by memsetting the struct to 0 before filling it in. - */ -template <typename T> -static inline u64 ComputeStructHash64(const T& data) { - static_assert(std::is_trivially_copyable_v<T>, - "Type passed to ComputeStructHash64 must be trivially copyable"); - return ComputeHash64(&data, sizeof(data)); -} - struct PairHash { template <class T1, class T2> std::size_t operator()(const std::pair<T1, T2>& pair) const noexcept { diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index c2f6cf0f6..74f52dd11 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp @@ -3,21 +3,9 @@ // Refer to the license.txt file included. #include "common/hex_util.h" -#include "common/logging/log.h" namespace Common { -u8 ToHexNibble(char c1) { - if (c1 >= 65 && c1 <= 70) - return c1 - 55; - if (c1 >= 97 && c1 <= 102) - return c1 - 87; - if (c1 >= 48 && c1 <= 57) - return c1 - 48; - LOG_ERROR(Common, "Invalid hex digit: 0x{:02X}", c1); - return 0; -} - std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) { std::vector<u8> out(str.size() / 2); if (little_endian) { @@ -30,26 +18,4 @@ std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) { return out; } -std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { - if (len != 32) { - LOG_ERROR(Common, - "Attempting to parse string to array that is not of correct size (expected=32, " - "actual={}).", - len); - return {}; - } - return HexStringToArray<16>(str); -} - -std::array<u8, 32> operator""_array32(const char* str, std::size_t len) { - if (len != 64) { - LOG_ERROR(Common, - "Attempting to parse string to array that is not of correct size (expected=64, " - "actual={}).", - len); - return {}; - } - return HexStringToArray<32>(str); -} - } // namespace Common diff --git a/src/common/hex_util.h b/src/common/hex_util.h index bb4736f96..120f1a5e6 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -14,25 +14,37 @@ namespace Common { -u8 ToHexNibble(char c1); +[[nodiscard]] constexpr u8 ToHexNibble(char c) { + if (c >= 65 && c <= 70) { + return c - 55; + } + + if (c >= 97 && c <= 102) { + return c - 87; + } + + return c - 48; +} -std::vector<u8> HexStringToVector(std::string_view str, bool little_endian); +[[nodiscard]] std::vector<u8> HexStringToVector(std::string_view str, bool little_endian); template <std::size_t Size, bool le = false> -std::array<u8, Size> HexStringToArray(std::string_view str) { +[[nodiscard]] constexpr std::array<u8, Size> HexStringToArray(std::string_view str) { std::array<u8, Size> out{}; if constexpr (le) { - for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) + for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) { out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + } } else { - for (std::size_t i = 0; i < 2 * Size; i += 2) + for (std::size_t i = 0; i < 2 * Size; i += 2) { out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + } } return out; } template <typename ContiguousContainer> -std::string HexToString(const ContiguousContainer& data, bool upper = true) { +[[nodiscard]] std::string HexToString(const ContiguousContainer& data, bool upper = true) { static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>, "Underlying type within the contiguous container must be u8."); @@ -48,7 +60,12 @@ std::string HexToString(const ContiguousContainer& data, bool upper = true) { return out; } -std::array<u8, 0x10> operator"" _array16(const char* str, std::size_t len); -std::array<u8, 0x20> operator"" _array32(const char* str, std::size_t len); +[[nodiscard]] constexpr std::array<u8, 16> AsArray(const char (&data)[17]) { + return HexStringToArray<16>(data); +} + +[[nodiscard]] constexpr std::array<u8, 32> AsArray(const char (&data)[65]) { + return HexStringToArray<32>(data); +} } // namespace Common diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 04bc3128f..62cfde397 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -113,19 +113,19 @@ private: Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, const char* function, std::string message) const { using std::chrono::duration_cast; + using std::chrono::microseconds; using std::chrono::steady_clock; - Entry entry; - entry.timestamp = - duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin); - entry.log_class = log_class; - entry.log_level = log_level; - entry.filename = filename; - entry.line_num = line_nr; - entry.function = function; - entry.message = std::move(message); - - return entry; + return { + .timestamp = duration_cast<microseconds>(steady_clock::now() - time_origin), + .log_class = log_class, + .log_level = log_level, + .filename = filename, + .line_num = line_nr, + .function = function, + .message = std::move(message), + .final_entry = false, + }; } std::mutex writing_mutex; diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index fc338c70d..da1c2f185 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -21,19 +21,13 @@ class Filter; */ struct Entry { std::chrono::microseconds timestamp; - Class log_class; - Level log_level; - const char* filename; - unsigned int line_num; + Class log_class{}; + Level log_level{}; + const char* filename = nullptr; + unsigned int line_num = 0; std::string function; std::string message; bool final_entry = false; - - Entry() = default; - Entry(Entry&& o) = default; - - Entry& operator=(Entry&& o) = default; - Entry& operator=(const Entry& o) = default; }; /** @@ -100,7 +94,7 @@ public: void Write(const Entry& entry) override; private: - FileUtil::IOFile file; + Common::FS::IOFile file; std::size_t bytes_written; }; diff --git a/src/common/lz4_compression.cpp b/src/common/lz4_compression.cpp index ade6759bb..25700015a 100644 --- a/src/common/lz4_compression.cpp +++ b/src/common/lz4_compression.cpp @@ -14,19 +14,19 @@ std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size) { ASSERT_MSG(source_size <= LZ4_MAX_INPUT_SIZE, "Source size exceeds LZ4 maximum input size"); const auto source_size_int = static_cast<int>(source_size); - const int max_compressed_size = LZ4_compressBound(source_size_int); + const auto max_compressed_size = static_cast<std::size_t>(LZ4_compressBound(source_size_int)); std::vector<u8> compressed(max_compressed_size); - const int compressed_size = LZ4_compress_default(reinterpret_cast<const char*>(source), - reinterpret_cast<char*>(compressed.data()), - source_size_int, max_compressed_size); + const int compressed_size = LZ4_compress_default( + reinterpret_cast<const char*>(source), reinterpret_cast<char*>(compressed.data()), + source_size_int, static_cast<int>(max_compressed_size)); if (compressed_size <= 0) { // Compression failed return {}; } - compressed.resize(compressed_size); + compressed.resize(static_cast<std::size_t>(compressed_size)); return compressed; } @@ -38,19 +38,19 @@ std::vector<u8> CompressDataLZ4HC(const u8* source, std::size_t source_size, compression_level = std::clamp(compression_level, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); const auto source_size_int = static_cast<int>(source_size); - const int max_compressed_size = LZ4_compressBound(source_size_int); + const auto max_compressed_size = static_cast<std::size_t>(LZ4_compressBound(source_size_int)); std::vector<u8> compressed(max_compressed_size); const int compressed_size = LZ4_compress_HC( reinterpret_cast<const char*>(source), reinterpret_cast<char*>(compressed.data()), - source_size_int, max_compressed_size, compression_level); + source_size_int, static_cast<int>(max_compressed_size), compression_level); if (compressed_size <= 0) { // Compression failed return {}; } - compressed.resize(compressed_size); + compressed.resize(static_cast<std::size_t>(compressed_size)); return compressed; } diff --git a/src/common/lz4_compression.h b/src/common/lz4_compression.h index 4c16f6e03..87a4be1b0 100644 --- a/src/common/lz4_compression.h +++ b/src/common/lz4_compression.h @@ -13,12 +13,12 @@ namespace Common::Compression { /** * Compresses a source memory region with LZ4 and returns the compressed data in a vector. * - * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. + * @param source The uncompressed source memory region. + * @param source_size The size of the uncompressed source memory region. * * @return the compressed data. */ -std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size); +[[nodiscard]] std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size); /** * Utilizes the LZ4 subalgorithm LZ4HC with the specified compression level. Higher compression @@ -26,23 +26,24 @@ std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size); * compression level has almost no impact on decompression speed. Data compressed with LZ4HC can * also be decompressed with the default LZ4 decompression. * - * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. - * @param compression_level the used compression level. Should be between 3 and 12. + * @param source The uncompressed source memory region. + * @param source_size The size of the uncompressed source memory region. + * @param compression_level The used compression level. Should be between 3 and 12. * * @return the compressed data. */ -std::vector<u8> CompressDataLZ4HC(const u8* source, std::size_t source_size, s32 compression_level); +[[nodiscard]] std::vector<u8> CompressDataLZ4HC(const u8* source, std::size_t source_size, + s32 compression_level); /** * Utilizes the LZ4 subalgorithm LZ4HC with the highest possible compression level. * - * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. + * @param source The uncompressed source memory region. + * @param source_size The size of the uncompressed source memory region * * @return the compressed data. */ -std::vector<u8> CompressDataLZ4HCMax(const u8* source, std::size_t source_size); +[[nodiscard]] std::vector<u8> CompressDataLZ4HCMax(const u8* source, std::size_t source_size); /** * Decompresses a source memory region with LZ4 and returns the uncompressed data in a vector. @@ -52,6 +53,7 @@ std::vector<u8> CompressDataLZ4HCMax(const u8* source, std::size_t source_size); * * @return the decompressed data. */ -std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, std::size_t uncompressed_size); +[[nodiscard]] std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, + std::size_t uncompressed_size); } // namespace Common::Compression
\ No newline at end of file diff --git a/src/common/math_util.h b/src/common/math_util.h index 83ef0201f..b35ad8507 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -9,7 +9,7 @@ namespace Common { -constexpr float PI = 3.14159265f; +constexpr float PI = 3.1415926535f; template <class T> struct Rectangle { @@ -23,7 +23,7 @@ struct Rectangle { constexpr Rectangle(T left, T top, T right, T bottom) : left(left), top(top), right(right), bottom(bottom) {} - T GetWidth() const { + [[nodiscard]] T GetWidth() const { if constexpr (std::is_floating_point_v<T>) { return std::abs(right - left); } else { @@ -31,7 +31,7 @@ struct Rectangle { } } - T GetHeight() const { + [[nodiscard]] T GetHeight() const { if constexpr (std::is_floating_point_v<T>) { return std::abs(bottom - top); } else { @@ -39,21 +39,21 @@ struct Rectangle { } } - Rectangle<T> TranslateX(const T x) const { + [[nodiscard]] Rectangle<T> TranslateX(const T x) const { return Rectangle{left + x, top, right + x, bottom}; } - Rectangle<T> TranslateY(const T y) const { + [[nodiscard]] Rectangle<T> TranslateY(const T y) const { return Rectangle{left, top + y, right, bottom + y}; } - Rectangle<T> Scale(const float s) const { + [[nodiscard]] Rectangle<T> Scale(const float s) const { return Rectangle{left, top, static_cast<T>(left + GetWidth() * s), static_cast<T>(top + GetHeight() * s)}; } }; template <typename T> -Rectangle(T, T, T, T)->Rectangle<T>; +Rectangle(T, T, T, T) -> Rectangle<T>; } // namespace Common diff --git a/src/common/memory_detect.h b/src/common/memory_detect.h index a73c0f3f4..0f73751c8 100644 --- a/src/common/memory_detect.h +++ b/src/common/memory_detect.h @@ -17,6 +17,6 @@ struct MemoryInfo { * Gets the memory info of the host system * @return Reference to a MemoryInfo struct with the physical and swap memory sizes in bytes */ -const MemoryInfo& GetMemInfo(); +[[nodiscard]] const MemoryInfo& GetMemInfo(); } // namespace Common
\ No newline at end of file diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h index 50acfdbf2..4b305bf40 100644 --- a/src/common/multi_level_queue.h +++ b/src/common/multi_level_queue.h @@ -223,15 +223,15 @@ public: ListShiftForward(levels[priority], n); } - std::size_t depth() const { + [[nodiscard]] std::size_t depth() const { return Depth; } - std::size_t size(u32 priority) const { + [[nodiscard]] std::size_t size(u32 priority) const { return levels[priority].size(); } - std::size_t size() const { + [[nodiscard]] std::size_t size() const { u64 priorities = used_priorities; std::size_t size = 0; while (priorities != 0) { @@ -242,64 +242,64 @@ public: return size; } - bool empty() const { + [[nodiscard]] bool empty() const { return used_priorities == 0; } - bool empty(u32 priority) const { + [[nodiscard]] bool empty(u32 priority) const { return (used_priorities & (1ULL << priority)) == 0; } - u32 highest_priority_set(u32 max_priority = 0) const { + [[nodiscard]] u32 highest_priority_set(u32 max_priority = 0) const { const u64 priorities = max_priority == 0 ? used_priorities : (used_priorities & ~((1ULL << max_priority) - 1)); return priorities == 0 ? Depth : static_cast<u32>(CountTrailingZeroes64(priorities)); } - u32 lowest_priority_set(u32 min_priority = Depth - 1) const { + [[nodiscard]] u32 lowest_priority_set(u32 min_priority = Depth - 1) const { const u64 priorities = min_priority >= Depth - 1 ? used_priorities : (used_priorities & ((1ULL << (min_priority + 1)) - 1)); return priorities == 0 ? Depth : 63 - CountLeadingZeroes64(priorities); } - const_iterator cbegin(u32 max_prio = 0) const { + [[nodiscard]] const_iterator cbegin(u32 max_prio = 0) const { const u32 priority = highest_priority_set(max_prio); return priority == Depth ? cend() : const_iterator{*this, levels[priority].cbegin(), priority}; } - const_iterator begin(u32 max_prio = 0) const { + [[nodiscard]] const_iterator begin(u32 max_prio = 0) const { return cbegin(max_prio); } - iterator begin(u32 max_prio = 0) { + [[nodiscard]] iterator begin(u32 max_prio = 0) { const u32 priority = highest_priority_set(max_prio); return priority == Depth ? end() : iterator{*this, levels[priority].begin(), priority}; } - const_iterator cend(u32 min_prio = Depth - 1) const { + [[nodiscard]] const_iterator cend(u32 min_prio = Depth - 1) const { return min_prio == Depth - 1 ? const_iterator{*this, Depth} : cbegin(min_prio + 1); } - const_iterator end(u32 min_prio = Depth - 1) const { + [[nodiscard]] const_iterator end(u32 min_prio = Depth - 1) const { return cend(min_prio); } - iterator end(u32 min_prio = Depth - 1) { + [[nodiscard]] iterator end(u32 min_prio = Depth - 1) { return min_prio == Depth - 1 ? iterator{*this, Depth} : begin(min_prio + 1); } - T& front(u32 max_priority = 0) { + [[nodiscard]] T& front(u32 max_priority = 0) { const u32 priority = highest_priority_set(max_priority); return levels[priority == Depth ? 0 : priority].front(); } - const T& front(u32 max_priority = 0) const { + [[nodiscard]] const T& front(u32 max_priority = 0) const { const u32 priority = highest_priority_set(max_priority); return levels[priority == Depth ? 0 : priority].front(); } - T back(u32 min_priority = Depth - 1) { + [[nodiscard]] T& back(u32 min_priority = Depth - 1) { const u32 priority = lowest_priority_set(min_priority); // intended return levels[priority == Depth ? 63 : priority].back(); } - const T& back(u32 min_priority = Depth - 1) const { + [[nodiscard]] const T& back(u32 min_priority = Depth - 1) const { const u32 priority = lowest_priority_set(min_priority); // intended return levels[priority == Depth ? 63 : priority].back(); } @@ -329,7 +329,8 @@ private: in_list.splice(position, out_list, element); } - static const_list_iterator ListIterateTo(const std::list<T>& list, const T& element) { + [[nodiscard]] static const_list_iterator ListIterateTo(const std::list<T>& list, + const T& element) { auto it = list.cbegin(); while (it != list.cend() && *it != element) { ++it; diff --git a/src/common/page_table.h b/src/common/page_table.h index 1e8bd3187..cf5eed780 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -36,11 +36,11 @@ struct SpecialRegion { MemoryHookPointer handler; - bool operator<(const SpecialRegion& other) const { + [[nodiscard]] bool operator<(const SpecialRegion& other) const { return std::tie(type, handler) < std::tie(other.type, other.handler); } - bool operator==(const SpecialRegion& other) const { + [[nodiscard]] bool operator==(const SpecialRegion& other) const { return std::tie(type, handler) == std::tie(other.type, other.handler); } }; diff --git a/src/common/param_package.h b/src/common/param_package.h index 6a0a9b656..c13e45479 100644 --- a/src/common/param_package.h +++ b/src/common/param_package.h @@ -19,19 +19,19 @@ public: explicit ParamPackage(const std::string& serialized); ParamPackage(std::initializer_list<DataType::value_type> list); ParamPackage(const ParamPackage& other) = default; - ParamPackage(ParamPackage&& other) = default; + ParamPackage(ParamPackage&& other) noexcept = default; ParamPackage& operator=(const ParamPackage& other) = default; ParamPackage& operator=(ParamPackage&& other) = default; - std::string Serialize() const; - std::string Get(const std::string& key, const std::string& default_value) const; - int Get(const std::string& key, int default_value) const; - float Get(const std::string& key, float default_value) const; + [[nodiscard]] std::string Serialize() const; + [[nodiscard]] std::string Get(const std::string& key, const std::string& default_value) const; + [[nodiscard]] int Get(const std::string& key, int default_value) const; + [[nodiscard]] float Get(const std::string& key, float default_value) const; void Set(const std::string& key, std::string value); void Set(const std::string& key, int value); void Set(const std::string& key, float value); - bool Has(const std::string& key) const; + [[nodiscard]] bool Has(const std::string& key) const; void Erase(const std::string& key); void Clear(); diff --git a/src/common/quaternion.h b/src/common/quaternion.h index 370198ae0..4d0871eb4 100644 --- a/src/common/quaternion.h +++ b/src/common/quaternion.h @@ -14,35 +14,66 @@ public: Vec3<T> xyz; T w{}; - Quaternion<decltype(-T{})> Inverse() const { + [[nodiscard]] Quaternion<decltype(-T{})> Inverse() const { return {-xyz, w}; } - Quaternion<decltype(T{} + T{})> operator+(const Quaternion& other) const { + [[nodiscard]] Quaternion<decltype(T{} + T{})> operator+(const Quaternion& other) const { return {xyz + other.xyz, w + other.w}; } - Quaternion<decltype(T{} - T{})> operator-(const Quaternion& other) const { + [[nodiscard]] Quaternion<decltype(T{} - T{})> operator-(const Quaternion& other) const { return {xyz - other.xyz, w - other.w}; } - Quaternion<decltype(T{} * T{} - T{} * T{})> operator*(const Quaternion& other) const { + [[nodiscard]] Quaternion<decltype(T{} * T{} - T{} * T{})> operator*( + const Quaternion& other) const { return {xyz * other.w + other.xyz * w + Cross(xyz, other.xyz), w * other.w - Dot(xyz, other.xyz)}; } - Quaternion<T> Normalized() const { + [[nodiscard]] Quaternion<T> Normalized() const { T length = std::sqrt(xyz.Length2() + w * w); return {xyz / length, w / length}; } + + [[nodiscard]] std::array<decltype(-T{}), 16> ToMatrix() const { + const T x2 = xyz[0] * xyz[0]; + const T y2 = xyz[1] * xyz[1]; + const T z2 = xyz[2] * xyz[2]; + + const T xy = xyz[0] * xyz[1]; + const T wz = w * xyz[2]; + const T xz = xyz[0] * xyz[2]; + const T wy = w * xyz[1]; + const T yz = xyz[1] * xyz[2]; + const T wx = w * xyz[0]; + + return {1.0f - 2.0f * (y2 + z2), + 2.0f * (xy + wz), + 2.0f * (xz - wy), + 0.0f, + 2.0f * (xy - wz), + 1.0f - 2.0f * (x2 + z2), + 2.0f * (yz + wx), + 0.0f, + 2.0f * (xz + wy), + 2.0f * (yz - wx), + 1.0f - 2.0f * (x2 + y2), + 0.0f, + 0.0f, + 0.0f, + 0.0f, + 1.0f}; + } }; template <typename T> -auto QuaternionRotate(const Quaternion<T>& q, const Vec3<T>& v) { +[[nodiscard]] auto QuaternionRotate(const Quaternion<T>& q, const Vec3<T>& v) { return v + 2 * Cross(q.xyz, Cross(q.xyz, v) + v * q.w); } -inline Quaternion<float> MakeQuaternion(const Vec3<float>& axis, float angle) { +[[nodiscard]] inline Quaternion<float> MakeQuaternion(const Vec3<float>& axis, float angle) { return {axis * std::sin(angle / 2), std::cos(angle / 2)}; } diff --git a/src/common/ring_buffer.h b/src/common/ring_buffer.h index abe3b4dc2..138fa0131 100644 --- a/src/common/ring_buffer.h +++ b/src/common/ring_buffer.h @@ -91,12 +91,12 @@ public: } /// @returns Number of slots used - std::size_t Size() const { + [[nodiscard]] std::size_t Size() const { return m_write_index.load() - m_read_index.load(); } /// @returns Maximum size of ring buffer - constexpr std::size_t Capacity() const { + [[nodiscard]] constexpr std::size_t Capacity() const { return capacity; } diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 1df5528c4..4f946a258 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -17,7 +17,7 @@ class SpinLock { public: void lock(); void unlock(); - bool try_lock(); + [[nodiscard]] bool try_lock(); private: std::atomic_flag lck = ATOMIC_FLAG_INIT; diff --git a/src/common/string_util.h b/src/common/string_util.h index 583fd05e6..a32c07c06 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -12,19 +12,19 @@ namespace Common { /// Make a string lowercase -std::string ToLower(std::string str); +[[nodiscard]] std::string ToLower(std::string str); /// Make a string uppercase -std::string ToUpper(std::string str); +[[nodiscard]] std::string ToUpper(std::string str); -std::string StringFromBuffer(const std::vector<u8>& data); +[[nodiscard]] std::string StringFromBuffer(const std::vector<u8>& data); -std::string StripSpaces(const std::string& s); -std::string StripQuotes(const std::string& s); +[[nodiscard]] std::string StripSpaces(const std::string& s); +[[nodiscard]] std::string StripQuotes(const std::string& s); -std::string StringFromBool(bool value); +[[nodiscard]] std::string StringFromBool(bool value); -std::string TabsToSpaces(int tab_size, std::string in); +[[nodiscard]] std::string TabsToSpaces(int tab_size, std::string in); void SplitString(const std::string& str, char delim, std::vector<std::string>& output); @@ -34,14 +34,15 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _ void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename); -std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest); +[[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src, + const std::string& dest); -std::string UTF16ToUTF8(const std::u16string& input); -std::u16string UTF8ToUTF16(const std::string& input); +[[nodiscard]] std::string UTF16ToUTF8(const std::u16string& input); +[[nodiscard]] std::u16string UTF8ToUTF16(const std::string& input); #ifdef _WIN32 -std::string UTF16ToUTF8(const std::wstring& input); -std::wstring UTF8ToUTF16W(const std::string& str); +[[nodiscard]] std::string UTF16ToUTF8(const std::wstring& input); +[[nodiscard]] std::wstring UTF8ToUTF16W(const std::string& str); #endif @@ -50,7 +51,7 @@ std::wstring UTF8ToUTF16W(const std::string& str); * `other` for equality. */ template <typename InIt> -bool ComparePartialString(InIt begin, InIt end, const char* other) { +[[nodiscard]] bool ComparePartialString(InIt begin, InIt end, const char* other) { for (; begin != end && *other != '\0'; ++begin, ++other) { if (*begin != *other) { return false; @@ -64,26 +65,15 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) { * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't * NUL-terminated then the string ends at max_len characters. */ -std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len); +[[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, + std::size_t max_len); /** * Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't * null-terminated, then the string ends at the greatest multiple of two less then or equal to * max_len_bytes. */ -std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer, - std::size_t max_len); - -/** - * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's - * intended to be used to strip a system-specific build directory from the `__FILE__` macro, - * leaving only the path relative to the sources root. - * - * @param path The input file path as a null-terminated string - * @param root The name of the root source directory as a null-terminated string. Path up to and - * including the last occurrence of this name will be stripped - * @return A pointer to the same string passed as `path`, but starting at the trimmed portion - */ -const char* TrimSourcePath(const char* path, const char* root = "src"); +[[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer, + std::size_t max_len); } // namespace Common diff --git a/src/common/telemetry.cpp b/src/common/telemetry.cpp index 16d42facd..6241d08b3 100644 --- a/src/common/telemetry.cpp +++ b/src/common/telemetry.cpp @@ -12,7 +12,7 @@ #include "common/x64/cpu_detect.h" #endif -namespace Telemetry { +namespace Common::Telemetry { void FieldCollection::Accept(VisitorInterface& visitor) const { for (const auto& field : fields) { @@ -88,4 +88,4 @@ void AppendOSInfo(FieldCollection& fc) { #endif } -} // namespace Telemetry +} // namespace Common::Telemetry diff --git a/src/common/telemetry.h b/src/common/telemetry.h index 854a73fae..a50c5d1de 100644 --- a/src/common/telemetry.h +++ b/src/common/telemetry.h @@ -10,7 +10,7 @@ #include <string> #include "common/common_types.h" -namespace Telemetry { +namespace Common::Telemetry { /// Field type, used for grouping fields together in the final submitted telemetry log enum class FieldType : u8 { @@ -63,30 +63,30 @@ public: void Accept(VisitorInterface& visitor) const override; - const std::string& GetName() const override { + [[nodiscard]] const std::string& GetName() const override { return name; } /** * Returns the type of the field. */ - FieldType GetType() const { + [[nodiscard]] FieldType GetType() const { return type; } /** * Returns the value of the field. */ - const T& GetValue() const { + [[nodiscard]] const T& GetValue() const { return value; } - bool operator==(const Field& other) const { + [[nodiscard]] bool operator==(const Field& other) const { return (type == other.type) && (name == other.name) && (value == other.value); } - bool operator!=(const Field& other) const { - return !(*this == other); + [[nodiscard]] bool operator!=(const Field& other) const { + return !operator==(other); } private: @@ -196,4 +196,4 @@ void AppendCPUInfo(FieldCollection& fc); /// such as platform name, etc. void AppendOSInfo(FieldCollection& fc); -} // namespace Telemetry +} // namespace Common::Telemetry diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 8e5935e6a..d2c1ac60d 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "common/common_funcs.h" +#include "common/logging/log.h" #include "common/thread.h" #ifdef __APPLE__ #include <mach/mach.h> @@ -19,6 +21,8 @@ #include <unistd.h> #endif +#include <string> + #ifdef __FreeBSD__ #define cpu_set_t cpuset_t #endif @@ -110,6 +114,14 @@ void SetCurrentThreadName(const char* name) { pthread_set_name_np(pthread_self(), name); #elif defined(__NetBSD__) pthread_setname_np(pthread_self(), "%s", (void*)name); +#elif defined(__linux__) + // Linux limits thread names to 15 characters and will outright reject any + // attempt to set a longer name with ERANGE. + std::string truncated(name, std::min(strlen(name), static_cast<size_t>(15))); + if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) { + errno = e; + LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg()); + } #else pthread_setname_np(pthread_self(), name); #endif diff --git a/src/common/thread.h b/src/common/thread.h index 52b359413..a8c17c71a 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -4,6 +4,7 @@ #pragma once +#include <atomic> #include <chrono> #include <condition_variable> #include <cstddef> @@ -25,13 +26,13 @@ public: void Wait() { std::unique_lock lk{mutex}; - condvar.wait(lk, [&] { return is_set; }); + condvar.wait(lk, [&] { return is_set.load(); }); is_set = false; } bool WaitFor(const std::chrono::nanoseconds& time) { std::unique_lock lk{mutex}; - if (!condvar.wait_for(lk, time, [this] { return is_set; })) + if (!condvar.wait_for(lk, time, [this] { return is_set.load(); })) return false; is_set = false; return true; @@ -40,7 +41,7 @@ public: template <class Clock, class Duration> bool WaitUntil(const std::chrono::time_point<Clock, Duration>& time) { std::unique_lock lk{mutex}; - if (!condvar.wait_until(lk, time, [this] { return is_set; })) + if (!condvar.wait_until(lk, time, [this] { return is_set.load(); })) return false; is_set = false; return true; @@ -54,9 +55,9 @@ public: } private: - bool is_set = false; std::condition_variable condvar; std::mutex mutex; + std::atomic_bool is_set{false}; }; class Barrier { diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h index 791f99a8c..def9e5d8d 100644 --- a/src/common/thread_queue_list.h +++ b/src/common/thread_queue_list.h @@ -18,14 +18,14 @@ struct ThreadQueueList { using Priority = unsigned int; // Number of priority levels. (Valid levels are [0..NUM_QUEUES).) - static const Priority NUM_QUEUES = N; + static constexpr Priority NUM_QUEUES = N; ThreadQueueList() { first = nullptr; } // Only for debugging, returns priority level. - Priority contains(const T& uid) const { + [[nodiscard]] Priority contains(const T& uid) const { for (Priority i = 0; i < NUM_QUEUES; ++i) { const Queue& cur = queues[i]; if (std::find(cur.data.cbegin(), cur.data.cend(), uid) != cur.data.cend()) { @@ -36,7 +36,7 @@ struct ThreadQueueList { return -1; } - T get_first() const { + [[nodiscard]] T get_first() const { const Queue* cur = first; while (cur != nullptr) { if (!cur->data.empty()) { @@ -49,7 +49,7 @@ struct ThreadQueueList { } template <typename UnaryPredicate> - T get_first_filter(UnaryPredicate filter) const { + [[nodiscard]] T get_first_filter(UnaryPredicate filter) const { const Queue* cur = first; while (cur != nullptr) { if (!cur->data.empty()) { @@ -129,7 +129,7 @@ struct ThreadQueueList { first = nullptr; } - bool empty(Priority priority) const { + [[nodiscard]] bool empty(Priority priority) const { const Queue* cur = &queues[priority]; return cur->data.empty(); } diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index 8268bbd5c..a4647314a 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h @@ -25,15 +25,15 @@ public: delete read_ptr; } - std::size_t Size() const { + [[nodiscard]] std::size_t Size() const { return size.load(); } - bool Empty() const { + [[nodiscard]] bool Empty() const { return Size() == 0; } - T& Front() const { + [[nodiscard]] T& Front() const { return read_ptr->current; } @@ -130,15 +130,15 @@ private: template <typename T> class MPSCQueue { public: - std::size_t Size() const { + [[nodiscard]] std::size_t Size() const { return spsc_queue.Size(); } - bool Empty() const { + [[nodiscard]] bool Empty() const { return spsc_queue.Empty(); } - T& Front() const { + [[nodiscard]] T& Front() const { return spsc_queue.Front(); } diff --git a/src/common/time_zone.h b/src/common/time_zone.h index 945daa09c..9f5939ca5 100644 --- a/src/common/time_zone.h +++ b/src/common/time_zone.h @@ -10,9 +10,9 @@ namespace Common::TimeZone { /// Gets the default timezone, i.e. "GMT" -std::string GetDefaultTimeZone(); +[[nodiscard]] std::string GetDefaultTimeZone(); /// Gets the offset of the current timezone (from the default), in seconds -std::chrono::seconds GetCurrentOffsetSeconds(); +[[nodiscard]] std::chrono::seconds GetCurrentOffsetSeconds(); } // namespace Common::TimeZone diff --git a/src/common/timer.h b/src/common/timer.h index 27b521baa..8894a143d 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -19,18 +19,18 @@ public: // The time difference is always returned in milliseconds, regardless of alternative internal // representation - std::chrono::milliseconds GetTimeDifference(); + [[nodiscard]] std::chrono::milliseconds GetTimeDifference(); void AddTimeDifference(); - static std::chrono::seconds GetTimeSinceJan1970(); - static std::chrono::seconds GetLocalTimeSinceJan1970(); - static double GetDoubleTime(); + [[nodiscard]] static std::chrono::seconds GetTimeSinceJan1970(); + [[nodiscard]] static std::chrono::seconds GetLocalTimeSinceJan1970(); + [[nodiscard]] static double GetDoubleTime(); - static std::string GetTimeFormatted(); - std::string GetTimeElapsedFormatted() const; - std::chrono::milliseconds GetTimeElapsed(); + [[nodiscard]] static std::string GetTimeFormatted(); + [[nodiscard]] std::string GetTimeElapsedFormatted() const; + [[nodiscard]] std::chrono::milliseconds GetTimeElapsed(); - static std::chrono::milliseconds GetTimeMs(); + [[nodiscard]] static std::chrono::milliseconds GetTimeMs(); private: std::chrono::milliseconds m_LastTime; diff --git a/src/common/uint128.h b/src/common/uint128.h index 503cd2d0c..969259ab6 100644 --- a/src/common/uint128.h +++ b/src/common/uint128.h @@ -10,13 +10,13 @@ namespace Common { // This function multiplies 2 u64 values and divides it by a u64 value. -u64 MultiplyAndDivide64(u64 a, u64 b, u64 d); +[[nodiscard]] u64 MultiplyAndDivide64(u64 a, u64 b, u64 d); // This function multiplies 2 u64 values and produces a u128 value; -u128 Multiply64Into128(u64 a, u64 b); +[[nodiscard]] u128 Multiply64Into128(u64 a, u64 b); // This function divides a u128 by a u32 value and produces two u64 values: // the result of division and the remainder -std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor); +[[nodiscard]] std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor); } // namespace Common diff --git a/src/common/uuid.h b/src/common/uuid.h index 4d3af8cec..4ab9a25f0 100644 --- a/src/common/uuid.h +++ b/src/common/uuid.h @@ -19,21 +19,21 @@ struct UUID { constexpr explicit UUID(const u128& id) : uuid{id} {} constexpr explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {} - constexpr explicit operator bool() const { + [[nodiscard]] constexpr explicit operator bool() const { return uuid[0] != INVALID_UUID[0] && uuid[1] != INVALID_UUID[1]; } - constexpr bool operator==(const UUID& rhs) const { + [[nodiscard]] constexpr bool operator==(const UUID& rhs) const { // TODO(DarkLordZach): Replace with uuid == rhs.uuid with C++20 return uuid[0] == rhs.uuid[0] && uuid[1] == rhs.uuid[1]; } - constexpr bool operator!=(const UUID& rhs) const { + [[nodiscard]] constexpr bool operator!=(const UUID& rhs) const { return !operator==(rhs); } // TODO(ogniK): Properly generate uuids based on RFC-4122 - static UUID Generate(); + [[nodiscard]] static UUID Generate(); // Set the UUID to {0,0} to be considered an invalid user constexpr void Invalidate() { @@ -41,12 +41,12 @@ struct UUID { } // TODO(ogniK): Properly generate a Nintendo ID - constexpr u64 GetNintendoID() const { + [[nodiscard]] constexpr u64 GetNintendoID() const { return uuid[0]; } - std::string Format() const; - std::string FormatSwitch() const; + [[nodiscard]] std::string Format() const; + [[nodiscard]] std::string FormatSwitch() const; }; static_assert(sizeof(UUID) == 16, "UUID is an invalid size!"); diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 429485329..2a0fcf541 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -52,15 +52,15 @@ public: constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {} template <typename T2> - constexpr Vec2<T2> Cast() const { + [[nodiscard]] constexpr Vec2<T2> Cast() const { return Vec2<T2>(static_cast<T2>(x), static_cast<T2>(y)); } - static constexpr Vec2 AssignToAll(const T& f) { + [[nodiscard]] static constexpr Vec2 AssignToAll(const T& f) { return Vec2{f, f}; } - constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const { + [[nodiscard]] constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const { return {x + other.x, y + other.y}; } constexpr Vec2& operator+=(const Vec2& other) { @@ -68,7 +68,7 @@ public: y += other.y; return *this; } - constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const { + [[nodiscard]] constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const { return {x - other.x, y - other.y}; } constexpr Vec2& operator-=(const Vec2& other) { @@ -78,15 +78,15 @@ public: } template <typename U = T> - constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { + [[nodiscard]] constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { return {-x, -y}; } - constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { + [[nodiscard]] constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { return {x * other.x, y * other.y}; } template <typename V> - constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { + [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { return {x * f, y * f}; } @@ -97,7 +97,7 @@ public: } template <typename V> - constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { + [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { return {x / f, y / f}; } @@ -107,18 +107,18 @@ public: return *this; } - constexpr T Length2() const { + [[nodiscard]] constexpr T Length2() const { return x * x + y * y; } // Only implemented for T=float - float Length() const; - float Normalize(); // returns the previous length, which is often useful + [[nodiscard]] float Length() const; + [[nodiscard]] float Normalize(); // returns the previous length, which is often useful - constexpr T& operator[](std::size_t i) { + [[nodiscard]] constexpr T& operator[](std::size_t i) { return *((&x) + i); } - constexpr const T& operator[](std::size_t i) const { + [[nodiscard]] constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } @@ -128,46 +128,46 @@ public: } // Common aliases: UV (texel coordinates), ST (texture coordinates) - constexpr T& u() { + [[nodiscard]] constexpr T& u() { return x; } - constexpr T& v() { + [[nodiscard]] constexpr T& v() { return y; } - constexpr T& s() { + [[nodiscard]] constexpr T& s() { return x; } - constexpr T& t() { + [[nodiscard]] constexpr T& t() { return y; } - constexpr const T& u() const { + [[nodiscard]] constexpr const T& u() const { return x; } - constexpr const T& v() const { + [[nodiscard]] constexpr const T& v() const { return y; } - constexpr const T& s() const { + [[nodiscard]] constexpr const T& s() const { return x; } - constexpr const T& t() const { + [[nodiscard]] constexpr const T& t() const { return y; } // swizzlers - create a subvector of specific components - constexpr Vec2 yx() const { + [[nodiscard]] constexpr Vec2 yx() const { return Vec2(y, x); } - constexpr Vec2 vu() const { + [[nodiscard]] constexpr Vec2 vu() const { return Vec2(y, x); } - constexpr Vec2 ts() const { + [[nodiscard]] constexpr Vec2 ts() const { return Vec2(y, x); } }; template <typename T, typename V> -constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { +[[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { return Vec2<T>(f * vec.x, f * vec.y); } @@ -196,15 +196,15 @@ public: constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {} template <typename T2> - constexpr Vec3<T2> Cast() const { + [[nodiscard]] constexpr Vec3<T2> Cast() const { return Vec3<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z)); } - static constexpr Vec3 AssignToAll(const T& f) { + [[nodiscard]] static constexpr Vec3 AssignToAll(const T& f) { return Vec3(f, f, f); } - constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const { + [[nodiscard]] constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const { return {x + other.x, y + other.y, z + other.z}; } @@ -215,7 +215,7 @@ public: return *this; } - constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const { + [[nodiscard]] constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const { return {x - other.x, y - other.y, z - other.z}; } @@ -227,16 +227,16 @@ public: } template <typename U = T> - constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { + [[nodiscard]] constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { return {-x, -y, -z}; } - constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const { + [[nodiscard]] constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const { return {x * other.x, y * other.y, z * other.z}; } template <typename V> - constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { + [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { return {x * f, y * f, z * f}; } @@ -246,7 +246,7 @@ public: return *this; } template <typename V> - constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { + [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { return {x / f, y / f, z / f}; } @@ -256,20 +256,20 @@ public: return *this; } - constexpr T Length2() const { + [[nodiscard]] constexpr T Length2() const { return x * x + y * y + z * z; } // Only implemented for T=float - float Length() const; - Vec3 Normalized() const; - float Normalize(); // returns the previous length, which is often useful + [[nodiscard]] float Length() const; + [[nodiscard]] Vec3 Normalized() const; + [[nodiscard]] float Normalize(); // returns the previous length, which is often useful - constexpr T& operator[](std::size_t i) { + [[nodiscard]] constexpr T& operator[](std::size_t i) { return *((&x) + i); } - constexpr const T& operator[](std::size_t i) const { + [[nodiscard]] constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } @@ -280,63 +280,63 @@ public: } // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) - constexpr T& u() { + [[nodiscard]] constexpr T& u() { return x; } - constexpr T& v() { + [[nodiscard]] constexpr T& v() { return y; } - constexpr T& w() { + [[nodiscard]] constexpr T& w() { return z; } - constexpr T& r() { + [[nodiscard]] constexpr T& r() { return x; } - constexpr T& g() { + [[nodiscard]] constexpr T& g() { return y; } - constexpr T& b() { + [[nodiscard]] constexpr T& b() { return z; } - constexpr T& s() { + [[nodiscard]] constexpr T& s() { return x; } - constexpr T& t() { + [[nodiscard]] constexpr T& t() { return y; } - constexpr T& q() { + [[nodiscard]] constexpr T& q() { return z; } - constexpr const T& u() const { + [[nodiscard]] constexpr const T& u() const { return x; } - constexpr const T& v() const { + [[nodiscard]] constexpr const T& v() const { return y; } - constexpr const T& w() const { + [[nodiscard]] constexpr const T& w() const { return z; } - constexpr const T& r() const { + [[nodiscard]] constexpr const T& r() const { return x; } - constexpr const T& g() const { + [[nodiscard]] constexpr const T& g() const { return y; } - constexpr const T& b() const { + [[nodiscard]] constexpr const T& b() const { return z; } - constexpr const T& s() const { + [[nodiscard]] constexpr const T& s() const { return x; } - constexpr const T& t() const { + [[nodiscard]] constexpr const T& t() const { return y; } - constexpr const T& q() const { + [[nodiscard]] constexpr const T& q() const { return z; } @@ -345,7 +345,7 @@ public: // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all // component names (x<->r) and permutations (xy<->yx) #define _DEFINE_SWIZZLER2(a, b, name) \ - constexpr Vec2<T> name() const { \ + [[nodiscard]] constexpr Vec2<T> name() const { \ return Vec2<T>(a, b); \ } #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ @@ -366,7 +366,7 @@ public: }; template <typename T, typename V> -constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { +[[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { return Vec3<T>(f * vec.x, f * vec.y, f * vec.z); } @@ -402,16 +402,16 @@ public: : x(x_), y(y_), z(z_), w(w_) {} template <typename T2> - constexpr Vec4<T2> Cast() const { + [[nodiscard]] constexpr Vec4<T2> Cast() const { return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z), static_cast<T2>(w)); } - static constexpr Vec4 AssignToAll(const T& f) { + [[nodiscard]] static constexpr Vec4 AssignToAll(const T& f) { return Vec4(f, f, f, f); } - constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const { + [[nodiscard]] constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const { return {x + other.x, y + other.y, z + other.z, w + other.w}; } @@ -423,7 +423,7 @@ public: return *this; } - constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const { + [[nodiscard]] constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const { return {x - other.x, y - other.y, z - other.z, w - other.w}; } @@ -436,16 +436,16 @@ public: } template <typename U = T> - constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { + [[nodiscard]] constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { return {-x, -y, -z, -w}; } - constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const { + [[nodiscard]] constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const { return {x * other.x, y * other.y, z * other.z, w * other.w}; } template <typename V> - constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { + [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { return {x * f, y * f, z * f, w * f}; } @@ -456,7 +456,7 @@ public: } template <typename V> - constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { + [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { return {x / f, y / f, z / f, w / f}; } @@ -466,15 +466,15 @@ public: return *this; } - constexpr T Length2() const { + [[nodiscard]] constexpr T Length2() const { return x * x + y * y + z * z + w * w; } - constexpr T& operator[](std::size_t i) { + [[nodiscard]] constexpr T& operator[](std::size_t i) { return *((&x) + i); } - constexpr const T& operator[](std::size_t i) const { + [[nodiscard]] constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } @@ -486,29 +486,29 @@ public: } // Common alias: RGBA (colors) - constexpr T& r() { + [[nodiscard]] constexpr T& r() { return x; } - constexpr T& g() { + [[nodiscard]] constexpr T& g() { return y; } - constexpr T& b() { + [[nodiscard]] constexpr T& b() { return z; } - constexpr T& a() { + [[nodiscard]] constexpr T& a() { return w; } - constexpr const T& r() const { + [[nodiscard]] constexpr const T& r() const { return x; } - constexpr const T& g() const { + [[nodiscard]] constexpr const T& g() const { return y; } - constexpr const T& b() const { + [[nodiscard]] constexpr const T& b() const { return z; } - constexpr const T& a() const { + [[nodiscard]] constexpr const T& a() const { return w; } @@ -520,7 +520,7 @@ public: // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and // permutations (xy<->yx) #define _DEFINE_SWIZZLER2(a, b, name) \ - constexpr Vec2<T> name() const { \ + [[nodiscard]] constexpr Vec2<T> name() const { \ return Vec2<T>(a, b); \ } #define DEFINE_SWIZZLER2_COMP1(a, a2) \ @@ -547,7 +547,7 @@ public: #undef _DEFINE_SWIZZLER2 #define _DEFINE_SWIZZLER3(a, b, c, name) \ - constexpr Vec3<T> name() const { \ + [[nodiscard]] constexpr Vec3<T> name() const { \ return Vec3<T>(a, b, c); \ } #define DEFINE_SWIZZLER3_COMP1(a, a2) \ @@ -581,7 +581,7 @@ public: }; template <typename T, typename V> -constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { +[[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; } @@ -593,39 +593,41 @@ constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b } template <typename T> -constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) { +[[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } template <typename T> -constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) { +[[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } template <typename T> -constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) { +[[nodiscard]] constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, + const Vec3<T>& b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } // linear interpolation via float: 0.0=begin, 1.0=end template <typename X> -constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, - const float t) { +[[nodiscard]] constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, + const float t) { return begin * (1.f - t) + end * t; } // linear interpolation via int: 0=begin, base=end template <typename X, int base> -constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, - const int t) { +[[nodiscard]] constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, + const X& end, + const int t) { return (begin * (base - t) + end * t) / base; } // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second // interpolation. template <typename X> -constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, - const float t) { +[[nodiscard]] constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, + const float s, const float t) { auto y0 = Lerp(x00, x01, s); auto y1 = Lerp(x10, x11, s); return Lerp(y0, y1, t); @@ -633,42 +635,42 @@ constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& // Utility vector factories template <typename T> -constexpr Vec2<T> MakeVec(const T& x, const T& y) { +[[nodiscard]] constexpr Vec2<T> MakeVec(const T& x, const T& y) { return Vec2<T>{x, y}; } template <typename T> -constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) { +[[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) { return Vec3<T>{x, y, z}; } template <typename T> -constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) { return MakeVec(x, y, zw[0], zw[1]); } template <typename T> -constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) { +[[nodiscard]] constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) { return MakeVec(xy[0], xy[1], z); } template <typename T> -constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) { +[[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) { return MakeVec(x, yz[0], yz[1]); } template <typename T> -constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) { return Vec4<T>{x, y, z, w}; } template <typename T> -constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) { return MakeVec(xy[0], xy[1], z, w); } template <typename T> -constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { return MakeVec(x, yz[0], yz[1], w); } @@ -676,17 +678,17 @@ constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error // out soon enough due to misuse of the returned structure. template <typename T> -constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) { return MakeVec(xy[0], xy[1], zw[0], zw[1]); } template <typename T> -constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) { return MakeVec(xyz[0], xyz[1], xyz[2], w); } template <typename T> -constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) { +[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) { return MakeVec(x, yzw[0], yzw[1], yzw[2]); } diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp index b426f4747..b009cb500 100644 --- a/src/common/virtual_buffer.cpp +++ b/src/common/virtual_buffer.cpp @@ -5,16 +5,7 @@ #ifdef _WIN32 #include <windows.h> #else -#include <stdio.h> #include <sys/mman.h> -#include <sys/types.h> -#if defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ -#include <sys/sysctl.h> -#elif defined __HAIKU__ -#include <OS.h> -#else -#include <sys/sysinfo.h> -#endif #endif #include "common/assert.h" @@ -38,7 +29,7 @@ void* AllocateMemoryPages(std::size_t size) { return base; } -void FreeMemoryPages(void* base, std::size_t size) { +void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) { if (!base) { return; } diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index da064e59e..125cb42f0 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -30,23 +30,23 @@ public: base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size)); } - constexpr const T& operator[](std::size_t index) const { + [[nodiscard]] constexpr const T& operator[](std::size_t index) const { return base_ptr[index]; } - constexpr T& operator[](std::size_t index) { + [[nodiscard]] constexpr T& operator[](std::size_t index) { return base_ptr[index]; } - constexpr T* data() { + [[nodiscard]] constexpr T* data() { return base_ptr; } - constexpr const T* data() const { + [[nodiscard]] constexpr const T* data() const { return base_ptr; } - constexpr std::size_t size() const { + [[nodiscard]] constexpr std::size_t size() const { return alloc_size / sizeof(T); } diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h index 367d72134..5db30083d 100644 --- a/src/common/wall_clock.h +++ b/src/common/wall_clock.h @@ -14,24 +14,24 @@ namespace Common { class WallClock { public: /// Returns current wall time in nanoseconds - virtual std::chrono::nanoseconds GetTimeNS() = 0; + [[nodiscard]] virtual std::chrono::nanoseconds GetTimeNS() = 0; /// Returns current wall time in microseconds - virtual std::chrono::microseconds GetTimeUS() = 0; + [[nodiscard]] virtual std::chrono::microseconds GetTimeUS() = 0; /// Returns current wall time in milliseconds - virtual std::chrono::milliseconds GetTimeMS() = 0; + [[nodiscard]] virtual std::chrono::milliseconds GetTimeMS() = 0; /// Returns current wall time in emulated clock cycles - virtual u64 GetClockCycles() = 0; + [[nodiscard]] virtual u64 GetClockCycles() = 0; /// Returns current wall time in emulated cpu cycles - virtual u64 GetCPUCycles() = 0; + [[nodiscard]] virtual u64 GetCPUCycles() = 0; virtual void Pause(bool is_paused) = 0; /// Tells if the wall clock, uses the host CPU's hardware clock - bool IsNative() const { + [[nodiscard]] bool IsNative() const { return is_native; } @@ -47,7 +47,7 @@ private: bool is_native; }; -std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, - u32 emulated_clock_frequency); +[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, + u32 emulated_clock_frequency); } // namespace Common diff --git a/src/common/web_result.h b/src/common/web_result.h deleted file mode 100644 index 8bfa2141d..000000000 --- a/src/common/web_result.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include <string> -#include "common/common_types.h" - -namespace Common { -struct WebResult { - enum class Code : u32 { - Success, - InvalidURL, - CredentialsMissing, - LibError, - HttpError, - WrongContent, - NoWebservice, - }; - Code result_code; - std::string result_string; - std::string returned_data; -}; -} // namespace Common diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h index a5f5d4fc1..26e4bfda5 100644 --- a/src/common/x64/xbyak_abi.h +++ b/src/common/x64/xbyak_abi.h @@ -11,7 +11,7 @@ namespace Common::X64 { -inline std::size_t RegToIndex(const Xbyak::Reg& reg) { +constexpr std::size_t RegToIndex(const Xbyak::Reg& reg) { using Kind = Xbyak::Reg::Kind; ASSERT_MSG((reg.getKind() & (Kind::REG | Kind::XMM)) != 0, "RegSet only support GPRs and XMM registers."); @@ -19,17 +19,17 @@ inline std::size_t RegToIndex(const Xbyak::Reg& reg) { return reg.getIdx() + (reg.getKind() == Kind::REG ? 0 : 16); } -inline Xbyak::Reg64 IndexToReg64(std::size_t reg_index) { +constexpr Xbyak::Reg64 IndexToReg64(std::size_t reg_index) { ASSERT(reg_index < 16); return Xbyak::Reg64(static_cast<int>(reg_index)); } -inline Xbyak::Xmm IndexToXmm(std::size_t reg_index) { +constexpr Xbyak::Xmm IndexToXmm(std::size_t reg_index) { ASSERT(reg_index >= 16 && reg_index < 32); return Xbyak::Xmm(static_cast<int>(reg_index - 16)); } -inline Xbyak::Reg IndexToReg(std::size_t reg_index) { +constexpr Xbyak::Reg IndexToReg(std::size_t reg_index) { if (reg_index < 16) { return IndexToReg64(reg_index); } else { @@ -45,17 +45,17 @@ inline std::bitset<32> BuildRegSet(std::initializer_list<Xbyak::Reg> regs) { return bits; } -const std::bitset<32> ABI_ALL_GPRS(0x0000FFFF); -const std::bitset<32> ABI_ALL_XMMS(0xFFFF0000); +constexpr inline std::bitset<32> ABI_ALL_GPRS(0x0000FFFF); +constexpr inline std::bitset<32> ABI_ALL_XMMS(0xFFFF0000); #ifdef _WIN32 // Microsoft x64 ABI -const Xbyak::Reg ABI_RETURN = Xbyak::util::rax; -const Xbyak::Reg ABI_PARAM1 = Xbyak::util::rcx; -const Xbyak::Reg ABI_PARAM2 = Xbyak::util::rdx; -const Xbyak::Reg ABI_PARAM3 = Xbyak::util::r8; -const Xbyak::Reg ABI_PARAM4 = Xbyak::util::r9; +constexpr inline Xbyak::Reg ABI_RETURN = Xbyak::util::rax; +constexpr inline Xbyak::Reg ABI_PARAM1 = Xbyak::util::rcx; +constexpr inline Xbyak::Reg ABI_PARAM2 = Xbyak::util::rdx; +constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::r8; +constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::r9; const std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({ // GPRs @@ -102,11 +102,11 @@ constexpr size_t ABI_SHADOW_SPACE = 0x20; #else // System V x86-64 ABI -const Xbyak::Reg ABI_RETURN = Xbyak::util::rax; -const Xbyak::Reg ABI_PARAM1 = Xbyak::util::rdi; -const Xbyak::Reg ABI_PARAM2 = Xbyak::util::rsi; -const Xbyak::Reg ABI_PARAM3 = Xbyak::util::rdx; -const Xbyak::Reg ABI_PARAM4 = Xbyak::util::rcx; +constexpr inline Xbyak::Reg ABI_RETURN = Xbyak::util::rax; +constexpr inline Xbyak::Reg ABI_PARAM1 = Xbyak::util::rdi; +constexpr inline Xbyak::Reg ABI_PARAM2 = Xbyak::util::rsi; +constexpr inline Xbyak::Reg ABI_PARAM3 = Xbyak::util::rdx; +constexpr inline Xbyak::Reg ABI_PARAM4 = Xbyak::util::rcx; const std::bitset<32> ABI_ALL_CALLER_SAVED = BuildRegSet({ // GPRs diff --git a/src/common/zstd_compression.cpp b/src/common/zstd_compression.cpp index 978526492..5f45459da 100644 --- a/src/common/zstd_compression.cpp +++ b/src/common/zstd_compression.cpp @@ -5,7 +5,6 @@ #include <algorithm> #include <zstd.h> -#include "common/assert.h" #include "common/zstd_compression.h" namespace Common::Compression { diff --git a/src/common/zstd_compression.h b/src/common/zstd_compression.h index e9de941c8..c26a30ab9 100644 --- a/src/common/zstd_compression.h +++ b/src/common/zstd_compression.h @@ -13,24 +13,25 @@ namespace Common::Compression { /** * Compresses a source memory region with Zstandard and returns the compressed data in a vector. * - * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. - * @param compression_level the used compression level. Should be between 1 and 22. + * @param source The uncompressed source memory region. + * @param source_size The size of the uncompressed source memory region. + * @param compression_level The used compression level. Should be between 1 and 22. * * @return the compressed data. */ -std::vector<u8> CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level); +[[nodiscard]] std::vector<u8> CompressDataZSTD(const u8* source, std::size_t source_size, + s32 compression_level); /** * Compresses a source memory region with Zstandard with the default compression level and returns * the compressed data in a vector. * - * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. + * @param source The uncompressed source memory region. + * @param source_size The size of the uncompressed source memory region. * * @return the compressed data. */ -std::vector<u8> CompressDataZSTDDefault(const u8* source, std::size_t source_size); +[[nodiscard]] std::vector<u8> CompressDataZSTDDefault(const u8* source, std::size_t source_size); /** * Decompresses a source memory region with Zstandard and returns the uncompressed data in a vector. @@ -39,6 +40,6 @@ std::vector<u8> CompressDataZSTDDefault(const u8* source, std::size_t source_siz * * @return the decompressed data. */ -std::vector<u8> DecompressDataZSTD(const std::vector<u8>& compressed); +[[nodiscard]] std::vector<u8> DecompressDataZSTD(const std::vector<u8>& compressed); } // namespace Common::Compression
\ No newline at end of file |