Skip to content

Commit

Permalink
[World] getSystem() can now be called from a constant World
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Sep 29, 2020
1 parent 381961b commit bd0cd14
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion include/RaZ/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ class World {
/// Gets a given system contained by the world.
/// This system must be present within the world. If not, an exception is thrown.
/// \tparam Sys Type of the system to be fetched.
/// \return Constant reference to the found system.
template <typename Sys> const Sys& getSystem() const;
/// Gets a given system contained by the world.
/// This system must be present within the world. If not, an exception is thrown.
/// \tparam Sys Type of the system to be fetched.
/// \return Reference to the found system.
template <typename Sys> Sys& getSystem();
template <typename Sys> Sys& getSystem() { return const_cast<Sys&>(static_cast<const World*>(this)->getSystem<Sys>()); }
/// Adds a given system to the world.
/// \tparam Sys Type of the system to be added.
/// \tparam Args Types of the arguments to be forwarded to the given system.
Expand Down
4 changes: 2 additions & 2 deletions include/RaZ/World.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ bool World::hasSystem() const {
}

template <typename Sys>
Sys& World::getSystem() {
const Sys& World::getSystem() const {
static_assert(std::is_base_of_v<System, Sys>, "Error: Fetched system must be derived from System.");

if (hasSystem<Sys>())
return static_cast<Sys&>(*m_systems[System::getId<Sys>()]);
return static_cast<const Sys&>(*m_systems[System::getId<Sys>()]);

throw std::runtime_error("Error: No system available of specified type");
}
Expand Down

0 comments on commit bd0cd14

Please sign in to comment.