Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Implement new getX methods, allowing direct access instead of casting. #4809

Open
1 task done
ramon-bernardo opened this issue Oct 15, 2024 · 0 comments
Open
1 task done
Labels
feature New feature or functionality

Comments

@ramon-bernardo
Copy link
Contributor

ramon-bernardo commented Oct 15, 2024

By submitting this feature issue, you agree to the following.

  • There is no existing issue for this feature (or similar) already

Request

Currently, some parts of the forgottenserver use dynamic_cast to check and convert object types. I propose the implementation of new getX() methods that directly return the desired type. Derived classes can implement these methods to provide specific access to the type, while the base class will return nullptr when the type is not applicable.

This approach have any problem?

Example: Instead of using:

if (!dynamic_cast<HouseTile*>(this)) {
	g_game.addTileToClean(this);
}
if (Player* player = dynamic_cast<Player*>(topParent)) { ... }

The code would become:

if (!this->getHouseTile()) {
	g_game.addTileToClean(this);
}
if (auto player = topParent->getPlayer()) { ... }

Thing would implement:

class Thing
{
public:
	constexpr Thing() = default;
	virtual ~Thing() = default;

	// non-copyable
	Thing(const Thing&) = delete;
	Thing& operator=(const Thing&) = delete;

	virtual int32_t getThrowRange() const = 0;
	virtual std::string getDescription(int32_t lookDistance) const = 0;
	virtual bool isRemoved() const { return true; }
	virtual bool isPushable() const = 0;

	virtual Cylinder* getParent() const { return nullptr; }
	virtual Cylinder* getRealParent() const { return getParent(); }
	virtual void setParent(Cylinder*) {}

	virtual const Position& getPosition() const;

	virtual Cylinder* getCylinder() { return nullptr; }
	virtual const Cylinder* getCylinder() const { return nullptr; }

	// Tiles
	virtual Tile* getTile();
	virtual const Tile* getTile() const;

	virtual DynamicTile* getDynamicTile() { return nullptr; }
	virtual const DynamicTile* getDynamicTile() const { return nullptr; }

	virtual StaticTile* getStaticTile() { return nullptr; }
	virtual const StaticTile* getStaticTile() const { return nullptr; }

	virtual HouseTile* getHouseTile() { return nullptr; }
	virtual const HouseTile* getHouseTile() const { return nullptr; }

	// Containers
	virtual Container* getContainer() { return nullptr; }
	virtual const Container* getContainer() const { return nullptr; }

	virtual DepotChest* getDepotChest() { return nullptr; }
	virtual const DepotChest* getDepotChest() const { return nullptr; }

	virtual DepotLocker* getDepotLocker() { return nullptr; }
	virtual const DepotLocker* getDepotLocker() const { return nullptr; }

	virtual Inbox* getInbox() { return nullptr; }
	virtual const Inbox* getInbox() const { return nullptr; }

	virtual StoreInbox* getStoreInbox() { return nullptr; }
	virtual const StoreInbox* getStoreInbox() const { return nullptr; }

	// Creatures
	virtual Creature* getCreature() { return nullptr; }
	virtual const Creature* getCreature() const { return nullptr; }

	virtual Monster* getMonster() { return nullptr; }
	virtual const Monster* getMonster() const { return nullptr; }

	virtual Npc* getNpc() { return nullptr; }
	virtual const Npc* getNpc() const { return nullptr; }

	virtual Player* getPlayer() { return nullptr; }
	virtual const Player* getPlayer() const { return nullptr; }

	// Items
	virtual Item* getItem() { return nullptr; }
	virtual const Item* getItem() const { return nullptr; }

	virtual BedItem* getBed() { return nullptr; }
	virtual const BedItem* getBed() const { return nullptr; }

	virtual HouseTransferItem* getHouseTransferItem() { return nullptr; }
	virtual const HouseTransferItem* getHouseTransferItem() const { return nullptr; }

	virtual MagicField* getMagicField() { return nullptr; }
	virtual const MagicField* getMagicField() const { return nullptr; }

	virtual Podium* getPodium() { return nullptr; }
	virtual const Podium* getPodium() const { return nullptr; }

	virtual Teleport* getTeleport() { return nullptr; }
	virtual const Teleport* getTeleport() const { return nullptr; }

	virtual TrashHolder* getTrashHolder() { return nullptr; }
	virtual const TrashHolder* getTrashHolder() const { return nullptr; }

	virtual Mailbox* getMailbox() { return nullptr; }
	virtual const Mailbox* getMailbox() const { return nullptr; }

	virtual Door* getDoor() { return nullptr; }
	virtual const Door* getDoor() const { return nullptr; }
};
class StaticTile final : public Tile
{
	// non-copyable
	StaticTile(const StaticTile&) = delete;
	StaticTile& operator=(const StaticTile&) = delete;

	StaticTile* getStaticTile() override final { return this; }
	const StaticTile* getStaticTile() const override final { return this; }
}
@ramon-bernardo ramon-bernardo added the feature New feature or functionality label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality
Projects
None yet
Development

No branches or pull requests

1 participant