Skip to content

Commit

Permalink
Reduce casting for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Gymnasiast committed Oct 16, 2024
1 parent 5934dc6 commit 0a9a456
Show file tree
Hide file tree
Showing 33 changed files with 68 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/openrct2-ui/interface/LandTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void LandTool::ShowSurfaceStyleDropdown(WindowBase* w, Widget* widget, ObjectEnt
auto itemIndex = 0;
for (size_t i = 0; i < kMaxTerrainSurfaceObjects; i++)
{
const auto surfaceObj = static_cast<TerrainSurfaceObject*>(objManager.GetLoadedObject(ObjectType::TerrainSurface, i));
const auto surfaceObj = objManager.GetLoadedObject<TerrainSurfaceObject>(i);
// If fallback images are loaded, the RCT1 styles will just look like copies of already existing styles, so hide them.
if (surfaceObj != nullptr && !surfaceObj->UsesFallbackImages())
{
Expand Down Expand Up @@ -92,7 +92,7 @@ ObjectEntryIndex LandTool::GetSurfaceStyleFromDropdownIndex(size_t index)
auto itemIndex = 0U;
for (size_t i = 0; i < kMaxTerrainSurfaceObjects; i++)
{
const auto surfaceObj = static_cast<TerrainSurfaceObject*>(objManager.GetLoadedObject(ObjectType::TerrainSurface, i));
const auto surfaceObj = objManager.GetLoadedObject<TerrainSurfaceObject>(i);
// If fallback images are loaded, the RCT1 styles will just look like copies of already existing styles, so hide them.
if (surfaceObj != nullptr && !surfaceObj->UsesFallbackImages())
{
Expand All @@ -114,7 +114,7 @@ void LandTool::ShowEdgeStyleDropdown(WindowBase* w, Widget* widget, ObjectEntryI
auto itemIndex = 0;
for (size_t i = 0; i < kMaxTerrainEdgeObjects; i++)
{
const auto edgeObj = static_cast<TerrainEdgeObject*>(objManager.GetLoadedObject(ObjectType::TerrainEdge, i));
const auto edgeObj = objManager.GetLoadedObject<TerrainEdgeObject>(i);
// If fallback images are loaded, the RCT1 styles will just look like copies of already existing styles, so hide them.
if (edgeObj != nullptr && !edgeObj->UsesFallbackImages())
{
Expand Down Expand Up @@ -143,7 +143,7 @@ ObjectEntryIndex LandTool::GetEdgeStyleFromDropdownIndex(size_t index)
auto itemIndex = 0U;
for (size_t i = 0; i < kMaxTerrainEdgeObjects; i++)
{
const auto edgeObj = static_cast<TerrainEdgeObject*>(objManager.GetLoadedObject(ObjectType::TerrainEdge, i));
const auto edgeObj = objManager.GetLoadedObject<TerrainEdgeObject>(i);
// If fallback images are loaded, the RCT1 styles will just look like copies of already existing styles, so hide them.
if (edgeObj != nullptr && !edgeObj->UsesFallbackImages())
{
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2-ui/windows/EditorParkEntrance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace OpenRCT2::Ui::Windows
for (ObjectEntryIndex objectIndex = 0; objectIndex < kMaxParkEntranceObjects; objectIndex++)
{
auto& objManager = GetContext()->GetObjectManager();
auto* object = static_cast<EntranceObject*>(objManager.GetLoadedObject(ObjectType::ParkEntrance, objectIndex));
auto* object = objManager.GetLoadedObject<EntranceObject>(objectIndex);
if (object != nullptr)
{
const auto* legacyData = reinterpret_cast<const EntranceEntry*>(object->GetLegacyData());
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2-ui/windows/Footpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ namespace OpenRCT2::Ui::Windows

for (ObjectEntryIndex i = 0; i < kMaxPathObjects; i++)
{
auto* pathObj = static_cast<FootpathObject*>(objManager.GetLoadedObject(ObjectType::Paths, i));
auto* pathObj = objManager.GetLoadedObject<FootpathObject>(i);
if (pathObj == nullptr)
{
continue;
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2-ui/windows/NewRide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ namespace OpenRCT2::Ui::Windows
ImageIndex GetRideImage(RideSelection rideSelection)
{
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
auto obj = static_cast<RideObject*>(objMgr.GetLoadedObject(ObjectType::Ride, rideSelection.EntryIndex));
auto obj = objMgr.GetLoadedObject<RideObject>(rideSelection.EntryIndex);
return obj == nullptr ? ImageIndexUndefined : obj->GetPreviewImage(rideSelection.Type);
}

Expand Down Expand Up @@ -683,7 +683,7 @@ namespace OpenRCT2::Ui::Windows

// Ride entries
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
auto rideObj = static_cast<const RideObject*>(objMgr.GetLoadedObject(ObjectType::Ride, rideEntryIndex));
auto* rideObj = objMgr.GetLoadedObject<RideObject>(rideEntryIndex);

// Skip if the vehicle isn't the preferred vehicle for this generic track type
if (!Config::Get().interface.ListRideVehiclesSeparately
Expand Down Expand Up @@ -918,7 +918,7 @@ namespace OpenRCT2::Ui::Windows
void DrawRideInformation(DrawPixelInfo& dpi, RideSelection item, const ScreenCoordsXY& screenPos, int32_t textWidth)
{
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
auto rideObj = static_cast<const RideObject*>(objMgr.GetLoadedObject(ObjectType::Ride, item.EntryIndex));
const auto* rideObj = objMgr.GetLoadedObject<RideObject>(item.EntryIndex);
const auto& rideEntry = rideObj->GetEntry();
RideNaming rideNaming = GetRideNaming(item.Type, rideEntry);
auto ft = Formatter();
Expand Down
8 changes: 4 additions & 4 deletions src/openrct2-ui/windows/Ride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ namespace OpenRCT2::Ui::Windows

for (ObjectEntryIndex i = 0; i < kMaxStationObjects; i++)
{
auto stationObj = static_cast<StationObject*>(objManager.GetLoadedObject(ObjectType::Station, i));
auto stationObj = objManager.GetLoadedObject<StationObject>(i);
if (stationObj != nullptr)
{
auto name = stationObj->NameStringId;
Expand Down Expand Up @@ -5055,7 +5055,7 @@ namespace OpenRCT2::Ui::Windows
static std::string GetMusicString(ObjectEntryIndex musicObjectIndex)
{
auto& objManager = GetContext()->GetObjectManager();
auto musicObj = static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, musicObjectIndex));
auto musicObj = objManager.GetLoadedObject<MusicObject>(musicObjectIndex);

return LanguageGetString(musicObj->NameStringId);
}
Expand All @@ -5080,7 +5080,7 @@ namespace OpenRCT2::Ui::Windows
auto& objManager = GetContext()->GetObjectManager();
for (ObjectEntryIndex i = 0; i < kMaxMusicObjects; i++)
{
auto musicObj = static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, i));
auto musicObj = objManager.GetLoadedObject<MusicObject>(i);
if (musicObj != nullptr)
{
// Hide custom music if the WAV file does not exist
Expand Down Expand Up @@ -5119,7 +5119,7 @@ namespace OpenRCT2::Ui::Windows
auto numItems = musicOrder.size();
for (size_t i = 0; i < numItems; i++)
{
auto musicObj = static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, musicOrder[i]));
auto musicObj = objManager.GetLoadedObject<MusicObject>(musicOrder[i]);
gDropdownItems[i].Format = STR_DROPDOWN_MENU_LABEL;
gDropdownItems[i].Args = musicObj->NameStringId;
}
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/actions/SurfaceSetStyleAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ GameActions::Result SurfaceSetStyleAction::Query() const

if (_edgeStyle != OBJECT_ENTRY_INDEX_NULL)
{
const auto edgeObj = static_cast<TerrainEdgeObject*>(objManager.GetLoadedObject(ObjectType::TerrainEdge, _edgeStyle));
const auto edgeObj = objManager.GetLoadedObject<TerrainEdgeObject>(_edgeStyle);

if (edgeObj == nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/localisation/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ namespace OpenRCT2
if (IsRealNameStringId(id))
{
auto& objManager = GetContext()->GetObjectManager();
auto* peepNamesObj = static_cast<PeepNamesObject*>(objManager.GetLoadedObject(ObjectType::PeepNames, 0));
auto* peepNamesObj = objManager.GetLoadedObject<PeepNamesObject>(0);
if (peepNamesObj != nullptr)
{
auto realNameIndex = id - kRealNameStart;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/AudioObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AudioObject final : public Object
AudioSampleTable _loadedSampleTable;

public:
static constexpr ObjectType objectType = ObjectType::Audio;

void ReadJson(IReadObjectContext* context, json_t& root) override;
void Load() override;
void Unload() override;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/BannerObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class BannerObject final : public SceneryObject
BannerSceneryEntry _legacyType = {};

public:
static constexpr ObjectType objectType = ObjectType::Banners;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/EntranceObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class EntranceObject final : public Object
EntranceEntry _legacyType = {};

public:
static constexpr ObjectType objectType = ObjectType::ParkEntrance;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/FootpathObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class FootpathObject final : public Object
PathRailingsDescriptor _pathRailingsDescriptor = {};

public:
static constexpr ObjectType objectType = ObjectType::Paths;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/FootpathRailingsObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class FootpathRailingsObject final : public Object
PathRailingsDescriptor _descriptor = {};

public:
static constexpr ObjectType objectType = ObjectType::FootpathRailings;

void ReadJson(IReadObjectContext* context, json_t& root) override;
void Load() override;
void Unload() override;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/FootpathSurfaceObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class FootpathSurfaceObject final : public Object
PathSurfaceDescriptor _descriptor = {};

public:
static constexpr ObjectType objectType = ObjectType::FootpathSurface;

void ReadJson(IReadObjectContext* context, json_t& root) override;
void Load() override;
void Unload() override;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/LargeSceneryObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class LargeSceneryObject final : public SceneryObject
std::unique_ptr<LargeSceneryText> _3dFont;

public:
static constexpr ObjectType objectType = ObjectType::LargeScenery;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/MusicObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class MusicObject final : public Object
uint32_t _previewImageId{};

public:
static constexpr ObjectType objectType = ObjectType::Music;

StringId NameStringId{};

void ReadJson(IReadObjectContext* context, json_t& root) override;
Expand Down
4 changes: 4 additions & 0 deletions src/openrct2/object/ObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ struct IObjectManager
}

virtual Object* GetLoadedObject(ObjectType objectType, size_t index) = 0;
template<typename TClass> TClass* GetLoadedObject(size_t index)
{
return static_cast<TClass*>(GetLoadedObject(TClass::objectType, index));
}
virtual Object* GetLoadedObject(const ObjectEntryDescriptor& entry) = 0;
virtual ObjectEntryIndex GetLoadedObjectEntryIndex(std::string_view identifier) = 0;
virtual ObjectEntryIndex GetLoadedObjectEntryIndex(const ObjectEntryDescriptor& descriptor) = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/PathAdditionObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class PathAdditionObject final : public SceneryObject
PathAdditionEntry _legacyType = {};

public:
static constexpr ObjectType objectType = ObjectType::PathAdditions;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/PeepNamesObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class PeepNamesObject final : public Object
std::vector<std::string> _surnames;

public:
static constexpr ObjectType objectType = ObjectType::PeepNames;

void ReadJson(IReadObjectContext* context, json_t& root) override;
void Load() override;
void Unload() override;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/RideObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class RideObject final : public Object
std::vector<std::array<CoordsXY, 3>> _peepLoadingWaypoints[OpenRCT2::RCT2::ObjectLimits::MaxCarTypesPerRideEntry];

public:
static constexpr ObjectType objectType = ObjectType::Ride;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/SceneryGroupObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SceneryGroupObject final : public Object
std::vector<ObjectEntryDescriptor> _items;

public:
static constexpr ObjectType objectType = ObjectType::SceneryGroup;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/SmallSceneryObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class SmallSceneryObject final : public SceneryObject
std::vector<uint8_t> _frameOffsets;

public:
static constexpr ObjectType objectType = ObjectType::SmallScenery;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/StationObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace OpenRCT2::STATION_OBJECT_FLAGS
class StationObject final : public Object
{
public:
static constexpr ObjectType objectType = ObjectType::Station;

StringId NameStringId{};
ImageIndex BaseImageId = ImageIndexUndefined;
ImageIndex ShelterImageId = ImageIndexUndefined;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/TerrainEdgeObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TerrainEdgeObject final : public Object
{
private:
public:
static constexpr ObjectType objectType = ObjectType::TerrainEdge;

StringId NameStringId{};
uint32_t IconImageId{};
uint32_t BaseImageId{};
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/TerrainSurfaceObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class TerrainSurfaceObject final : public Object
static constexpr auto kNumImagesInEntry = 19;

public:
static constexpr ObjectType objectType = ObjectType::TerrainSurface;

static constexpr uint8_t kNoValue = 0xFF;
StringId NameStringId{};
uint32_t IconImageId{};
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/WallObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class WallObject final : public SceneryObject
WallSceneryEntry _legacyType = {};

public:
static constexpr ObjectType objectType = ObjectType::Walls;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/object/WaterObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class WaterObject final : public Object
WaterObjectEntry _legacyType = {};

public:
static constexpr ObjectType objectType = ObjectType::Water;

void* GetLegacyData() override
{
return &_legacyType;
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2/ride/Ride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1981,7 +1981,7 @@ void DefaultMusicUpdate(Ride& ride)
if (ride.music_tune_id == TUNE_ID_NULL)
{
auto& objManager = GetContext()->GetObjectManager();
auto musicObj = static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, ride.music));
auto musicObj = objManager.GetLoadedObject<MusicObject>(ride.music);
if (musicObj != nullptr)
{
auto numTracks = musicObj->GetTrackCount();
Expand Down Expand Up @@ -5543,13 +5543,13 @@ int32_t RideGetEntryIndex(int32_t rideType, int32_t rideSubType)
const StationObject* Ride::GetStationObject() const
{
auto& objManager = GetContext()->GetObjectManager();
return static_cast<StationObject*>(objManager.GetLoadedObject(ObjectType::Station, entrance_style));
return objManager.GetLoadedObject<StationObject>(entrance_style);
}

const MusicObject* Ride::GetMusicObject() const
{
auto& objManager = GetContext()->GetObjectManager();
return static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, music));
return objManager.GetLoadedObject<MusicObject>(music);
}

// Normally, a station has at most one entrance and one exit, which are at the same height
Expand Down
4 changes: 2 additions & 2 deletions src/openrct2/ride/RideAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace OpenRCT2::RideAudio
{
auto& objManager = GetContext()->GetObjectManager();
auto ride = GetRide(instance.RideId);
auto musicObj = static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, ride->music));
auto musicObj = objManager.GetLoadedObject<MusicObject>(ride->music);
if (musicObj != nullptr)
{
auto shouldLoop = musicObj->GetTrackCount() == 1;
Expand Down Expand Up @@ -276,7 +276,7 @@ namespace OpenRCT2::RideAudio
std::pair<size_t, size_t> RideMusicGetTrackOffsetLength_Default(const Ride& ride)
{
auto& objManager = GetContext()->GetObjectManager();
auto musicObj = static_cast<MusicObject*>(objManager.GetLoadedObject(ObjectType::Music, ride.music));
auto musicObj = objManager.GetLoadedObject<MusicObject>(ride.music);
if (musicObj != nullptr)
{
auto numTracks = musicObj->GetTrackCount();
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/scripting/bindings/object/ScObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ namespace OpenRCT2::Scripting
const RideObject* GetObject() const
{
auto& objManager = GetContext()->GetObjectManager();
return static_cast<RideObject*>(objManager.GetLoadedObject(_objectType, _objectIndex));
return objManager.GetLoadedObject<RideObject>(_objectIndex);
}

const CarEntry* GetEntry() const
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/world/Footpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,7 @@ static bool FootpathIsLegacyPathEntryOkay(ObjectEntryIndex index)
{
bool showEditorPaths = ((gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) || GetGameState().Cheats.SandboxMode);
auto& objManager = OpenRCT2::GetContext()->GetObjectManager();
auto footpathObj = static_cast<FootpathObject*>(objManager.GetLoadedObject(ObjectType::Paths, index));
auto footpathObj = objManager.GetLoadedObject<FootpathObject>(index);
if (footpathObj != nullptr)
{
auto pathEntry = reinterpret_cast<FootpathEntry*>(footpathObj->GetLegacyData());
Expand Down
4 changes: 2 additions & 2 deletions src/openrct2/world/tile_element/EntranceElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ ObjectEntryIndex EntranceElement::GetLegacyPathEntryIndex() const
const FootpathObject* EntranceElement::GetLegacyPathEntry() const
{
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
return static_cast<FootpathObject*>(objMgr.GetLoadedObject(ObjectType::Paths, GetLegacyPathEntryIndex()));
return objMgr.GetLoadedObject<FootpathObject>(GetLegacyPathEntryIndex());
}

void EntranceElement::SetLegacyPathEntryIndex(ObjectEntryIndex newPathType)
Expand All @@ -100,7 +100,7 @@ ObjectEntryIndex EntranceElement::GetSurfaceEntryIndex() const
const FootpathSurfaceObject* EntranceElement::GetSurfaceEntry() const
{
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
return static_cast<FootpathSurfaceObject*>(objMgr.GetLoadedObject(ObjectType::FootpathSurface, GetSurfaceEntryIndex()));
return objMgr.GetLoadedObject<FootpathSurfaceObject>(GetSurfaceEntryIndex());
}

void EntranceElement::SetSurfaceEntryIndex(ObjectEntryIndex newIndex)
Expand Down
Loading

0 comments on commit 0a9a456

Please sign in to comment.