Cleanup #9725: Replace cmd_helper related functions and remove cmd_helper.h.

pull/374/head
J0anJosep 2 years ago committed by Michael Lutz
parent 09b71c97aa
commit d9a37c915f

@ -87,7 +87,6 @@ add_files(
clear_cmd.cpp
clear_func.h
clear_map.h
cmd_helper.h
command.cpp
command_func.h
command_type.h

@ -20,7 +20,6 @@
#include "sortlist_type.h"
#include "widgets/dropdown_func.h"
#include "core/geometry_func.hpp"
#include "cmd_helper.h"
#include "tunnelbridge_map.h"
#include "road_gui.h"
#include "tunnelbridge_cmd.h"

@ -1,45 +0,0 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file cmd_helper.h Helper functions to extract data from command parameters. */
#ifndef CMD_HELPER_H
#define CMD_HELPER_H
#include "core/enum_type.hpp"
#include "core/bitmath_func.hpp"
/**
* Extracts a given type from a value.
* @tparam T The type of data we're looking for.
* @tparam S The offset in the data.
* @tparam N The amount of bits to read.
* @tparam U The type of data passed to us.
* @param v The data to extract the value from.
*/
template<typename T, uint S, uint N, typename U> static inline T Extract(U v)
{
/* Check if there are enough bits in v */
static_assert(N == EnumPropsT<T>::num_bits);
static_assert(S + N <= sizeof(U) * 8);
static_assert(EnumPropsT<T>::end <= (1 << N));
U masked = GB(v, S, N);
return IsInsideMM(masked, EnumPropsT<T>::begin, EnumPropsT<T>::end) ? (T)masked : EnumPropsT<T>::invalid;
}
/**
* Check if an enum value is inside it's valid values.
* @tparam T Type of enum.
* @param v The value to validate
* @return True if enum is valid.
*/
template <typename T> static constexpr inline bool IsEnumValid(T v) noexcept
{
return IsInsideMM(v, EnumPropsT<T>::begin, EnumPropsT<T>::end);
}
#endif /* CMD_HELPER_H */

@ -14,7 +14,6 @@
#include "core/backup_type.hpp"
#include "town.h"
#include "news_func.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "network/network.h"
#include "network/network_func.h"

@ -174,6 +174,17 @@ int GetCharacterHeight(FontSize size);
extern DrawPixelInfo *_cur_dpi;
/**
* Checks if a Colours value is valid.
*
* @param colours The value to check
* @return true if the given value is a valid Colours.
*/
static inline bool IsValidColours(Colours colours)
{
return colours < COLOUR_END;
}
TextColour GetContrastColour(uint8 background, uint8 threshold = 128);
/**

@ -8,7 +8,6 @@
/** @file group_cmd.cpp Handling of the engine groups */
#include "stdafx.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "train.h"
#include "vehiclelist.h"

@ -40,7 +40,6 @@
#include "object_base.h"
#include "game/game.hpp"
#include "error.h"
#include "cmd_helper.h"
#include "string_func.h"
#include "industry_cmd.h"
#include "landscape_cmd.h"

@ -10,7 +10,6 @@
#include "stdafx.h"
#include "command_func.h"
#include "economy_func.h"
#include "cmd_helper.h"
#include "window_func.h"
#include "textbuf_gui.h"
#include "network/network.h"

@ -9,7 +9,6 @@
#include "stdafx.h"
#include "debug.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "company_func.h"
#include "news_func.h"

@ -8,7 +8,6 @@
/** @file rail_cmd.cpp Handling of rail tiles. */
#include "stdafx.h"
#include "cmd_helper.h"
#include "viewport_func.h"
#include "command_func.h"
#include "depot_base.h"
@ -966,7 +965,7 @@ CommandCost CmdRemoveRailroadTrack(DoCommandFlag flags, TileIndex tile, TileInde
CommandCost CmdBuildTrainDepot(DoCommandFlag flags, TileIndex tile, RailType railtype, DiagDirection dir)
{
/* check railtype and valid direction for depot (0 through 3), 4 in total */
if (!ValParamRailtype(railtype) || !IsEnumValid(dir)) return CMD_ERROR;
if (!ValParamRailtype(railtype) || !IsValidDiagDirection(dir)) return CMD_ERROR;
Slope tileh = GetTileSlope(tile);

@ -8,7 +8,6 @@
/** @file road_cmd.cpp Commands related to road tiles. */
#include "stdafx.h"
#include "cmd_helper.h"
#include "road.h"
#include "road_internal.h"
#include "viewport_func.h"
@ -638,7 +637,7 @@ CommandCost CmdBuildRoad(DoCommandFlag flags, TileIndex tile, RoadBits pieces, R
}
/* do not allow building 'zero' road bits, code wouldn't handle it */
if (pieces == ROAD_NONE || !IsEnumValid(pieces) || !IsEnumValid(toggle_drd)) return CMD_ERROR;
if (pieces == ROAD_NONE || !IsValidRoadBits(pieces) || !IsValidDisallowedRoadDirections(toggle_drd)) return CMD_ERROR;
if (!ValParamRoadType(rt)) return CMD_ERROR;
Slope tileh = GetTileSlope(tile);
@ -979,7 +978,7 @@ CommandCost CmdBuildLongRoad(DoCommandFlag flags, TileIndex start_tile, TileInde
{
if (end_tile >= MapSize()) return CMD_ERROR;
if (!ValParamRoadType(rt) || !IsEnumValid(axis) || !IsEnumValid(drd)) return CMD_ERROR;
if (!ValParamRoadType(rt) || !IsValidAxis(axis) || !IsValidDisallowedRoadDirections(drd)) return CMD_ERROR;
/* Only drag in X or Y direction dictated by the direction variable */
if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
@ -1075,7 +1074,7 @@ std::tuple<CommandCost, Money> CmdRemoveLongRoad(DoCommandFlag flags, TileIndex
CommandCost cost(EXPENSES_CONSTRUCTION);
if (end_tile >= MapSize()) return { CMD_ERROR, 0 };
if (!ValParamRoadType(rt) || !IsEnumValid(axis)) return { CMD_ERROR, 0 };
if (!ValParamRoadType(rt) || !IsValidAxis(axis)) return { CMD_ERROR, 0 };
/* Only drag in X or Y direction dictated by the direction variable */
if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return { CMD_ERROR, 0 }; // x-axis
@ -1140,7 +1139,7 @@ std::tuple<CommandCost, Money> CmdRemoveLongRoad(DoCommandFlag flags, TileIndex
*/
CommandCost CmdBuildRoadDepot(DoCommandFlag flags, TileIndex tile, RoadType rt, DiagDirection dir)
{
if (!ValParamRoadType(rt) || !IsEnumValid(dir)) return CMD_ERROR;
if (!ValParamRoadType(rt) || !IsValidDiagDirection(dir)) return CMD_ERROR;
CommandCost cost(EXPENSES_CONSTRUCTION);

@ -282,6 +282,17 @@ static inline bool HasTownOwnedRoad(TileIndex t)
return HasTileRoadType(t, RTT_ROAD) && IsRoadOwner(t, RTT_ROAD, OWNER_TOWN);
}
/**
* Checks if a DisallowedRoadDirections is valid.
*
* @param wc The value to check
* @return true if the given value is a valid DisallowedRoadDirections.
*/
static inline bool IsValidDisallowedRoadDirections(DisallowedRoadDirections drt)
{
return drt < DRD_END;
}
/**
* Gets the disallowed directions
* @param t the tile to get the directions from

@ -10,7 +10,6 @@
#include "stdafx.h"
#include "aircraft.h"
#include "bridge_map.h"
#include "cmd_helper.h"
#include "viewport_func.h"
#include "viewport_kdtree.h"
#include "command_func.h"
@ -1257,7 +1256,7 @@ CommandCost CmdBuildRailStation(DoCommandFlag flags, TileIndex tile_org, RailTyp
CommandCost ret = CheckIfAuthorityAllowsNewStation(tile_org, flags);
if (ret.Failed()) return ret;
if (!ValParamRailtype(rt) || !IsEnumValid(axis)) return CMD_ERROR;
if (!ValParamRailtype(rt) || !IsValidAxis(axis)) return CMD_ERROR;
/* Check if the given station class is valid */
if ((uint)spec_class >= StationClass::GetClassCount() || spec_class == STAT_CLASS_WAYP) return CMD_ERROR;
@ -1818,7 +1817,7 @@ static CommandCost FindJoiningRoadStop(StationID existing_stop, StationID statio
*/
CommandCost CmdBuildRoadStop(DoCommandFlag flags, TileIndex tile, uint8 width, uint8 length, RoadStopType stop_type, bool is_drive_through, DiagDirection ddir, RoadType rt, StationID station_to_join, bool adjacent)
{
if (!ValParamRoadType(rt) || !IsEnumValid(ddir) || stop_type >= ROADSTOP_END) return CMD_ERROR;
if (!ValParamRoadType(rt) || !IsValidDiagDirection(ddir) || stop_type >= ROADSTOP_END) return CMD_ERROR;
bool reuse = (station_to_join != NEW_STATION);
if (!reuse) station_to_join = INVALID_STATION;
bool distant_join = (station_to_join != INVALID_STATION);

@ -10,7 +10,6 @@
#include "stdafx.h"
#include "story_base.h"
#include "core/pool_func.hpp"
#include "cmd_helper.h"
#include "command_func.h"
#include "company_base.h"
#include "company_func.h"
@ -144,7 +143,9 @@ void StoryPageButtonData::SetVehicleType(VehicleType vehtype)
/** Get the button background colour. */
Colours StoryPageButtonData::GetColour() const
{
return Extract<Colours, 0, 8>(this->referenced_id);
Colours colour = (Colours)GB(this->referenced_id, 0, 8);
if (!IsValidColours(colour)) return INVALID_COLOUR;
return colour;
}
StoryPageButtonFlags StoryPageButtonData::GetFlags() const
@ -155,7 +156,9 @@ StoryPageButtonFlags StoryPageButtonData::GetFlags() const
/** Get the mouse cursor used while waiting for input for the button. */
StoryPageButtonCursor StoryPageButtonData::GetCursor() const
{
return Extract<StoryPageButtonCursor, 8, 8>(this->referenced_id);
StoryPageButtonCursor cursor = (StoryPageButtonCursor)GB(this->referenced_id, 8, 8);
if (!IsValidStoryPageButtonCursor(cursor)) return INVALID_SPBC;
return cursor;
}
/** Get the type of vehicles that are accepted by the button */

@ -113,6 +113,17 @@ enum StoryPageButtonCursor : byte {
/** Define basic enum properties */
template <> struct EnumPropsT<StoryPageButtonCursor> : MakeEnumPropsT<StoryPageButtonCursor, byte, SPBC_MOUSE, SPBC_END, INVALID_SPBC, 8> {};
/**
* Checks if a StoryPageButtonCursor value is valid.
*
* @param wc The value to check
* @return true if the given value is a valid StoryPageButtonCursor.
*/
static inline bool IsValidStoryPageButtonCursor(StoryPageButtonCursor cursor)
{
return cursor < SPBC_END;
}
/** Helper to construct packed "id" values for button-type StoryPageElement */
struct StoryPageButtonData {
uint32 referenced_id;

@ -13,7 +13,6 @@
#include "date_func.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "cmd_helper.h"
#include "timetable_cmd.h"
#include "table/strings.h"

@ -14,7 +14,6 @@
#include "landscape.h"
#include "viewport_func.h"
#include "viewport_kdtree.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "industry.h"
#include "station_base.h"

@ -14,7 +14,6 @@
#include "stdafx.h"
#include "newgrf_object.h"
#include "viewport_func.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "town.h"
#include "train.h"

@ -11,7 +11,6 @@
#include "roadveh.h"
#include "news_func.h"
#include "airport.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "company_func.h"
#include "train.h"

@ -8,7 +8,6 @@
/** @file water_cmd.cpp Handling of water tiles. */
#include "stdafx.h"
#include "cmd_helper.h"
#include "landscape.h"
#include "viewport_func.h"
#include "command_func.h"
@ -100,7 +99,7 @@ static void MarkCanalsAndRiversAroundDirty(TileIndex tile)
*/
CommandCost CmdBuildShipDepot(DoCommandFlag flags, TileIndex tile, Axis axis)
{
if (!IsEnumValid(axis)) return CMD_ERROR;
if (!IsValidAxis(axis)) return CMD_ERROR;
TileIndex tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
if (!HasTileWaterGround(tile) || !HasTileWaterGround(tile2)) {
@ -439,7 +438,7 @@ bool RiverModifyDesertZone(TileIndex tile, void *)
*/
CommandCost CmdBuildCanal(DoCommandFlag flags, TileIndex tile, TileIndex start_tile, WaterClass wc, bool diagonal)
{
if (start_tile >= MapSize() || !IsEnumValid(wc)) return CMD_ERROR;
if (start_tile >= MapSize() || !IsValidWaterClass(wc)) return CMD_ERROR;
/* Outside of the editor you can only build canals, not oceans */
if (wc != WATER_CLASS_CANAL && _game_mode != GM_EDITOR) return CMD_ERROR;

@ -53,6 +53,17 @@ enum WaterClass : byte {
/** Helper information for extract tool. */
template <> struct EnumPropsT<WaterClass> : MakeEnumPropsT<WaterClass, byte, WATER_CLASS_SEA, WATER_CLASS_INVALID, WATER_CLASS_INVALID, 2> {};
/**
* Checks if a water class is valid.
*
* @param wc The value to check
* @return true if the given value is a valid water class.
*/
static inline bool IsValidWaterClass(WaterClass wc)
{
return wc < WATER_CLASS_INVALID;
}
/** Sections of the water depot. */
enum DepotPart {
DEPOT_PART_NORTH = 0, ///< Northern part of a depot.

@ -9,7 +9,6 @@
#include "stdafx.h"
#include "cmd_helper.h"
#include "command_func.h"
#include "landscape.h"
#include "bridge_map.h"
@ -175,7 +174,7 @@ extern CommandCost CanExpandRailStation(const BaseStation *st, TileArea &new_ta,
*/
CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis axis, byte width, byte height, StationClassID spec_class, byte spec_index, StationID station_to_join, bool adjacent)
{
if (!IsEnumValid(axis)) return CMD_ERROR;
if (!IsValidAxis(axis)) return CMD_ERROR;
/* Check if the given station class is valid */
if (spec_class != STAT_CLASS_WAYP) return CMD_ERROR;
if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR;

Loading…
Cancel
Save