1
0
Fork 0

Codechange: remove uses of MemCmpT (and MemMoveT)

pull/13847/head
Rubidium 2025-03-18 19:41:04 +01:00 committed by rubidium42
parent e55f54ce08
commit a908c7bed0
7 changed files with 10 additions and 37 deletions

View File

@ -25,19 +25,6 @@ inline void MemCpyT(T *destination, const T *source, size_t num = 1)
memcpy(destination, source, num * sizeof(T));
}
/**
* Type-safe version of memmove().
*
* @param destination Pointer to the destination buffer
* @param source Pointer to the source buffer
* @param num number of items to be copied. (!not number of bytes!)
*/
template <typename T>
inline void MemMoveT(T *destination, const T *source, size_t num = 1)
{
memmove(destination, source, num * sizeof(T));
}
/**
* Type-safe version of memset().
*
@ -51,18 +38,4 @@ inline void MemSetT(T *ptr, uint8_t value, size_t num = 1)
memset(ptr, value, num * sizeof(T));
}
/**
* Type-safe version of memcmp().
*
* @param ptr1 Pointer to the first buffer
* @param ptr2 Pointer to the second buffer
* @param num Number of items to compare. (!not number of bytes!)
* @return an int value indicating the relationship between the content of the two buffers
*/
template <typename T>
inline int MemCmpT(const T *ptr1, const T *ptr2, size_t num = 1)
{
return memcmp(ptr1, ptr2, num * sizeof(T));
}
#endif /* MEM_FUNC_HPP */

View File

@ -22,6 +22,8 @@ typedef uint32_t CursorID; ///< The number of the cursor (sprite)
struct PalSpriteID {
SpriteID sprite{}; ///< The 'real' sprite
PaletteID pal{}; ///< The palette (use \c PAL_NONE) if not needed)
auto operator<=>(const PalSpriteID&) const = default;
};
enum WindowKeyCodes : uint16_t {

View File

@ -12,7 +12,6 @@
#include "../fileio_type.h"
#include "../string_func.h"
#include "../core/endian_func.hpp"
#include "../core/mem_func.hpp"
#include "../base_media_base.h"
#include "midi.h"
@ -427,7 +426,7 @@ bool MidiFile::ReadSMFHeader(FileHandle &file, SMFHeader &header)
/* Check magic, 'MThd' followed by 4 byte length indicator (always = 6 in SMF) */
const uint8_t magic[] = { 'M', 'T', 'h', 'd', 0x00, 0x00, 0x00, 0x06 };
if (MemCmpT(buffer, magic, sizeof(magic)) != 0) {
if (std::ranges::equal(std::span(buffer, std::size(magic)), magic)) {
return false;
}

View File

@ -231,12 +231,12 @@ void UpdateNewGRFConfigPalette(int32_t)
*/
size_t GRFGetSizeOfDataSection(FileHandle &f)
{
extern const uint8_t _grf_cont_v2_sig[];
extern const std::array<uint8_t, 8> _grf_cont_v2_sig;
static const uint header_len = 14;
uint8_t data[header_len];
if (fread(data, 1, header_len, f) == header_len) {
if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
if (data[0] == 0 && data[1] == 0 && std::ranges::equal(std::span(data + 2, _grf_cont_v2_sig.size()), _grf_cont_v2_sig)) {
/* Valid container version 2, get data section size. */
size_t offset = (static_cast<size_t>(data[13]) << 24) | (static_cast<size_t>(data[12]) << 16) | (static_cast<size_t>(data[11]) << 8) | static_cast<size_t>(data[10]);
if (offset >= 1 * 1024 * 1024 * 1024) {

View File

@ -1100,7 +1100,7 @@ void GetCustomEngineSprite(EngineID engine, const Vehicle *v, Direction directio
result->Clear();
bool sprite_stack = EngInfo(engine)->misc_flags.Test(EngineMiscFlag::SpriteStack);
uint max_stack = sprite_stack ? lengthof(result->seq) : 1;
uint max_stack = sprite_stack ? static_cast<uint>(std::size(result->seq)) : 1;
for (uint stack = 0; stack < max_stack; ++stack) {
object.ResetState();
object.callback_param1 = image_type | (stack << 8);
@ -1134,7 +1134,7 @@ void GetRotorOverrideSprite(EngineID engine, const struct Aircraft *v, EngineIma
uint rotor_pos = v == nullptr || rotor_in_gui ? 0 : v->Next()->Next()->state;
bool sprite_stack = e->info.misc_flags.Test(EngineMiscFlag::SpriteStack);
uint max_stack = sprite_stack ? lengthof(result->seq) : 1;
uint max_stack = sprite_stack ? static_cast<uint>(std::size(result->seq)) : 1;
for (uint stack = 0; stack < max_stack; ++stack) {
object.ResetState();
object.callback_param1 = image_type | (stack << 8);

View File

@ -11,7 +11,7 @@
#include "sprite_file_type.hpp"
/** Signature of a container version 2 GRF. */
extern const uint8_t _grf_cont_v2_sig[8] = {'G', 'R', 'F', 0x82, 0x0D, 0x0A, 0x1A, 0x0A};
extern const std::array<uint8_t, 8> _grf_cont_v2_sig = {'G', 'R', 'F', 0x82, 0x0D, 0x0A, 0x1A, 0x0A};
/**
* Get the container version of the currently opened GRF file.

View File

@ -23,7 +23,6 @@
#include "network/network.h"
#include "saveload/saveload.h"
#include "timer/timer_game_calendar.h"
#include "core/mem_func.hpp"
const uint TILE_AXIAL_DISTANCE = 192; // Logical length of the tile in any DiagDirection used in vehicle movement.
const uint TILE_CORNER_DISTANCE = 128; // Logical length of the tile corner crossing in any non-diagonal direction used in vehicle movement.
@ -120,12 +119,12 @@ struct VehicleCache {
/** Sprite sequence for a vehicle part. */
struct VehicleSpriteSeq {
PalSpriteID seq[8];
std::array<PalSpriteID, 8> seq;
uint count;
bool operator==(const VehicleSpriteSeq &other) const
{
return this->count == other.count && MemCmpT<PalSpriteID>(this->seq, other.seq, this->count) == 0;
return std::ranges::equal(std::span(this->seq.data(), this->count), std::span(other.seq.data(), other.count));
}
/**