1
0
Fork 0

Codechange: introduce ConvertibleThroughBase helpers

pull/13582/head
Rubidium 2025-02-09 17:25:40 +01:00 committed by rubidium42
parent bdd14063a0
commit b0eb8fe4db
8 changed files with 37 additions and 4 deletions

View File

@ -36,11 +36,17 @@ concept ConvertibleThroughBaseOrTo = std::is_convertible_v<T, TTo> || Convertibl
template <typename Container>
class ReferenceThroughBaseContainer : public Container {
public:
Container::reference at(ConvertibleThroughBase auto pos) { return this->Container::at(pos.base()); }
Container::const_reference at(ConvertibleThroughBase auto pos) const { return this->Container::at(pos.base()); }
Container::reference at(size_t pos) { return this->Container::at(pos); }
Container::reference at(const ConvertibleThroughBase auto &pos) { return this->Container::at(pos.base()); }
Container::reference operator[](ConvertibleThroughBase auto pos) { return this->Container::operator[](pos.base()); }
Container::const_reference operator[](ConvertibleThroughBase auto pos) const { return this->Container::operator[](pos.base()); }
Container::const_reference at(size_t pos) const { return this->Container::at(pos); }
Container::const_reference at(const ConvertibleThroughBase auto &pos) const { return this->Container::at(pos.base()); }
Container::reference operator[](size_t pos) { return this->Container::operator[](pos); }
Container::reference operator[](const ConvertibleThroughBase auto &pos) { return this->Container::operator[](pos.base()); }
Container::const_reference operator[](size_t pos) const { return this->Container::operator[](pos); }
Container::const_reference operator[](const ConvertibleThroughBase auto &pos) const { return this->Container::operator[](pos.base()); }
};
#endif /* CONVERTIBLE_THROUGH_BASE_HPP */

View File

@ -61,6 +61,8 @@ struct EMPTY_BASES PoolID : PoolIDBase {
constexpr auto operator++() { ++this->value; return this; }
constexpr auto operator+(const std::integral auto &val) const { return this->value + val; }
constexpr auto operator-(const std::integral auto &val) const { return this->value - val; }
constexpr auto operator%(const std::integral auto &val) const { return this->value % val; }
constexpr bool operator==(const PoolID<TBaseType, TTag, TEnd, TInvalid> &rhs) const { return this->value == rhs.value; }
constexpr auto operator<=>(const PoolID<TBaseType, TTag, TEnd, TInvalid> &rhs) const { return this->value <=> rhs.value; }
@ -72,6 +74,11 @@ private:
TBaseType value;
};
template <typename T> requires std::is_base_of_v<PoolIDBase, T>
constexpr auto operator+(const std::integral auto &val, const T &pool_id) { return pool_id + val; }
template <typename Te, typename Tp> requires std::is_enum_v<Te> && std::is_base_of_v<PoolIDBase, Tp>
constexpr auto operator+(const Te &val, const Tp &pool_id) { return pool_id + to_underlying(val); }
/** Base class for base of all pools. */
struct PoolBase {
const PoolType type; ///< Type of this pool.

View File

@ -15,6 +15,7 @@
#include "os_abstraction.h"
#include "config.h"
#include "core.h"
#include "../../core/convertible_through_base.hpp"
#include "../../string_type.h"
typedef uint16_t PacketSize; ///< Size of the whole packet.
@ -63,6 +64,7 @@ public:
bool CanWriteToPacket(size_t bytes_to_write);
void Send_bool (bool data);
void Send_uint8 (uint8_t data);
void Send_uint8 (const ConvertibleThroughBase auto &data) { this->Send_uint8(data.base()); }
void Send_uint16(uint16_t data);
void Send_uint32(uint32_t data);
void Send_uint64(uint64_t data);

View File

@ -33,7 +33,9 @@ extern NewGrfDebugSpritePicker _newgrf_debug_sprite_picker;
bool IsNewGRFInspectable(GrfSpecFeature feature, uint index);
void ShowNewGRFInspectWindow(GrfSpecFeature feature, uint index, const uint32_t grfid = 0);
void InvalidateNewGRFInspectWindow(GrfSpecFeature feature, uint index);
void InvalidateNewGRFInspectWindow(GrfSpecFeature feature, ConvertibleThroughBase auto index) { InvalidateNewGRFInspectWindow(feature, index.base()); }
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index);
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, ConvertibleThroughBase auto index) { DeleteNewGRFInspectWindow(feature, index.base()); }
GrfSpecFeature GetGrfSpecFeature(TileIndex tile);
GrfSpecFeature GetGrfSpecFeature(VehicleType type);

View File

@ -73,6 +73,8 @@ static inline uint GetInspectWindowNumber(GrfSpecFeature feature, uint index)
return (feature << 24) | index;
}
static inline uint GetInspectWindowNumber(GrfSpecFeature feature, ConvertibleThroughBase auto index) { return GetInspectWindowNumber(feature, index.base()); }
/**
* The type of a property to show. This is used to
* provide an appropriate representation in the GUI.
@ -226,6 +228,11 @@ protected:
SetDParam(2, index);
SetDParam(3, tile);
}
void SetObjectAtStringParameters(StringID string, ConvertibleThroughBase auto index, TileIndex tile) const
{
this->SetObjectAtStringParameters(string, index.base(), tile);
}
};

View File

@ -34,12 +34,17 @@ public:
SourceID id; ///< Index of industry/town/HQ, Source::Invalid if unknown/invalid.
SourceType type; ///< Type of \c source_id.
Source() = default;
Source(ConvertibleThroughBase auto id, SourceType type) : id(id.base()), type(type) {}
Source(SourceID id, SourceType type) : id(id), type(type) {}
constexpr CompanyID ToCompanyID() const { assert(this->type == SourceType::Headquarters); return static_cast<CompanyID>(this->id); }
constexpr IndustryID ToIndustryID() const { assert(this->type == SourceType::Industry); return static_cast<IndustryID>(this->id); }
constexpr TownID ToTownID() const { assert(this->type == SourceType::Town); return static_cast<TownID>(this->id); }
constexpr void MakeInvalid() { this->id = Source::Invalid; }
constexpr void SetIndex(SourceID index) { this->id = index; }
constexpr void SetIndex(ConvertibleThroughBase auto index) { this->id = index.base(); }
constexpr bool IsValid() const noexcept { return this->id != Source::Invalid; }
auto operator<=>(const Source &source) const = default;

View File

@ -35,6 +35,7 @@ void SetupColoursAndInitialWindow();
void InputLoop();
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data = 0, bool gui_scope = false);
void InvalidateWindowData(WindowClass cls, WindowNumber number, ConvertibleThroughBase auto data, bool gui_scope = false) { InvalidateWindowData(cls, number, data.base(), gui_scope); }
void InvalidateWindowClassesData(WindowClass cls, int data = 0, bool gui_scope = false);
void InvalidateWindowClassesData(WindowClass cls, ConvertibleThroughBase auto data, bool gui_scope = false) { InvalidateWindowClassesData(cls, data.base(), gui_scope); }

View File

@ -763,6 +763,9 @@ public:
/* Automatically convert to any other type that might be requested. */
template <typename T> requires (std::is_enum_v<T> || std::is_class_v<T>)
operator T() const { return static_cast<T>(value); };
constexpr bool operator==(const std::integral auto &rhs) const { return this->value == static_cast<int32_t>(rhs); }
constexpr bool operator==(const ConvertibleThroughBase auto &rhs) const { return this->value == static_cast<int32_t>(rhs.base()); }
};
/** State of handling an event. */