Skip to content

Commit

Permalink
fix: type safety around const "Thing" (#1854)
Browse files Browse the repository at this point in the history
Addresses the issue in const member functions where the getContainer method may return a nullptr
  • Loading branch information
luan authored Nov 20, 2023
1 parent 8993119 commit b6af793
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,27 @@ class Creature : virtual public Thing, public SharedObject {
std::shared_ptr<Creature> getCreature() override final {
return static_self_cast<Creature>();
}
std::shared_ptr<const Creature> getCreature() const override final {
return static_self_cast<Creature>();
}
virtual std::shared_ptr<Player> getPlayer() {
return nullptr;
}
virtual std::shared_ptr<const Player> getPlayer() const {
return nullptr;
}
virtual std::shared_ptr<Npc> getNpc() {
return nullptr;
}
virtual std::shared_ptr<const Npc> getNpc() const {
return nullptr;
}
virtual std::shared_ptr<Monster> getMonster() {
return nullptr;
}
virtual std::shared_ptr<const Monster> getMonster() const {
return nullptr;
}

virtual const std::string &getName() const = 0;
// Real creature name, set on creature creation "createNpcType(typeName) and createMonsterType(typeName)"
Expand Down
3 changes: 3 additions & 0 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Monster final : public Creature {
std::shared_ptr<Monster> getMonster() override {
return static_self_cast<Monster>();
}
std::shared_ptr<const Monster> getMonster() const override {
return static_self_cast<Monster>();
}

void setID() override {
if (id == 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/creatures/npcs/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Npc final : public Creature {
std::shared_ptr<Npc> getNpc() override {
return static_self_cast<Npc>();
}
std::shared_ptr<const Npc> getNpc() const override {
return static_self_cast<Npc>();
}

void setID() override {
if (id == 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class Player final : public Creature, public Cylinder, public Bankable {
std::shared_ptr<Player> getPlayer() override {
return static_self_cast<Player>();
}
std::shared_ptr<const Player> getPlayer() const override {
return static_self_cast<Player>();
}

static std::shared_ptr<Task> createPlayerTask(uint32_t delay, std::function<void(void)> f, std::string context);

Expand Down
4 changes: 4 additions & 0 deletions src/items/containers/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Container : public Item, public Cylinder {
return static_self_cast<Container>();
}

std::shared_ptr<const Container> getContainer() const override final {
return static_self_cast<Container>();
}

std::shared_ptr<Container> getRootContainer();

virtual std::shared_ptr<DepotLocker> getDepotLocker() {
Expand Down
3 changes: 3 additions & 0 deletions src/items/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class Item : virtual public Thing, public ItemProperties, public SharedObject {
std::shared_ptr<Item> getItem() override final {
return static_self_cast<Item>();
}
std::shared_ptr<const Item> getItem() const override final {
return static_self_cast<Item>();
}
virtual std::shared_ptr<Teleport> getTeleport() {
return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/items/thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ class Thing {
virtual std::shared_ptr<Container> getContainer() {
return nullptr;
}
virtual std::shared_ptr<Container> getContainer() const {
virtual std::shared_ptr<const Container> getContainer() const {
return nullptr;
}
virtual std::shared_ptr<Item> getItem() {
return nullptr;
}
virtual std::shared_ptr<Item> getItem() const {
virtual std::shared_ptr<const Item> getItem() const {
return nullptr;
}
virtual std::shared_ptr<Creature> getCreature() {
return nullptr;
}
virtual std::shared_ptr<Creature> getCreature() const {
virtual std::shared_ptr<const Creature> getCreature() const {
return nullptr;
}

Expand Down
5 changes: 5 additions & 0 deletions src/lua/global/shared_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class SharedObject : public std::enable_shared_from_this<SharedObject> {
return std::static_pointer_cast<T>(shared_from_this());
}

template <typename T>
std::shared_ptr<const T> static_self_cast() const {
return std::static_pointer_cast<const T>(shared_from_this());
}

template <typename T>
std::shared_ptr<T> dynamic_self_cast() {
return std::dynamic_pointer_cast<T>(shared_from_this());
Expand Down

0 comments on commit b6af793

Please sign in to comment.