From 4dc99fc8d9958486810dc28b58d059afc45aad5d Mon Sep 17 00:00:00 2001 From: Rubidium Date: Wed, 17 Jan 2024 04:06:51 +0100 Subject: [PATCH] Codechange: use std::source_location over __FILE__ and __LINE__ for TILE_ADD(XY) --- src/map.cpp | 26 ++++++++++---------------- src/map_func.h | 32 +++++++++++++++++--------------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/map.cpp b/src/map.cpp index 91d02b3f9d..291f4b5992 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -69,32 +69,26 @@ extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned); #ifdef _DEBUG -TileIndex TileAdd(TileIndex tile, TileIndexDiff add, - const char *exp, const char *file, int line) +TileIndex TILE_ADD(TileIndex tile, TileIndexDiff offset, std::source_location location) { - int dx; - int dy; - uint x; - uint y; - - dx = add & Map::MaxX(); + int dx = offset & Map::MaxX(); if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX(); - dy = (add - dx) / (int)Map::SizeX(); + int dy = (offset - dx) / (int)Map::SizeX(); - x = TileX(tile) + dx; - y = TileY(tile) + dy; + uint x = TileX(tile) + dx; + uint y = TileY(tile) + dy; if (x >= Map::SizeX() || y >= Map::SizeY()) { - std::string message = fmt::format("TILE_ADD({}) when adding 0x{:04X} and 0x{:04X} failed", - exp, tile, add); + std::string message = fmt::format("TILE_ADD when adding 0x{:04X} and 0x{:04X} failed", + tile, offset); #if !defined(_MSC_VER) - fmt::print(stderr, "{}:{} {}\n", file, line, message); + fmt::print(stderr, "{}:{}:{} {}\n", location.file_name(), location.line(), location.column(), message); #else - _assert(message.data(), (char*)file, line); + _assert(message.data(), (char*)location.file_name(), location.line()); #endif } - assert(TileXY(x, y) == Map::WrapToMap(tile + add)); + assert(TileXY(x, y) == Map::WrapToMap(tile + offset)); return TileXY(x, y); } diff --git a/src/map_func.h b/src/map_func.h index 14d65db690..8a232e4c6e 100644 --- a/src/map_func.h +++ b/src/map_func.h @@ -455,29 +455,31 @@ inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc) } +/** + * Adds a given offset to a tile. + * + * @param tile The tile to add an offset to. + * @param offset The offset to add. + * @return The resulting tile. + */ #ifndef _DEBUG - /** - * Adds two tiles together. - * - * @param x One tile - * @param y Another tile to add - * @return The resulting tile(index) - */ -# define TILE_ADD(x, y) ((x) + (y)) + constexpr TileIndex TILE_ADD(TileIndex tile, TileIndexDiff offset, [[maybe_unused]] const std::source_location location = std::source_location::current()) { return tile + offset; } #else - extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add, - const char *exp, const char *file, int line); -# define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__)) + TileIndex TILE_ADD(TileIndex tile, TileIndexDiff offset, const std::source_location location = std::source_location::current()); #endif /** * Adds a given offset to a tile. * - * @param tile The tile to add an offset on it - * @param x The x offset to add to the tile - * @param y The y offset to add to the tile + * @param tile The tile to add an offset to. + * @param x The x offset to add to the tile. + * @param y The y offset to add to the tile. + * @return The resulting tile. */ -#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y)) +inline TileIndex TILE_ADDXY(TileIndex tile, int x, int y, const std::source_location location = std::source_location::current()) +{ + return TILE_ADD(tile, TileDiffXY(x, y), location); +} TileIndex TileAddWrap(TileIndex tile, int addx, int addy);