From: Mikko Rasa Date: Wed, 19 Oct 2022 08:01:30 +0000 (+0300) Subject: Adjust some things to make header dependencies easier to manage X-Git-Url: http://git.tdb.fi/?p=libs%2Fgame.git;a=commitdiff_plain;h=c7d0e1aff305778f97974b329826628966380158 Adjust some things to make header dependencies easier to manage --- diff --git a/source/game/entity.cpp b/source/game/entity.cpp index 95ad686..055c8d6 100644 --- a/source/game/entity.cpp +++ b/source/game/entity.cpp @@ -1,6 +1,6 @@ #include "entity.h" #include "component.h" -#include "stage.h" +#include "root.h" using namespace std; @@ -36,4 +36,10 @@ void Entity::remove_child(Handle child) erase(children, child); } +Stage &Entity::get_stage() +{ + Handle root = get_root(); + return dynamic_cast(*root).get_stage(); +} + } // namespace Msp::Game diff --git a/source/game/entity.h b/source/game/entity.h index 922f552..7c7ee89 100644 --- a/source/game/entity.h +++ b/source/game/entity.h @@ -6,6 +6,7 @@ namespace Msp::Game { class Component; +class Stage; class hierarchy_error: public std::logic_error { @@ -32,6 +33,7 @@ public: Handle get_parent() const { return parent; } Handle get_root(); + Stage &get_stage(); }; diff --git a/source/game/owned.h b/source/game/owned.h index df3cd94..19daa7e 100644 --- a/source/game/owned.h +++ b/source/game/owned.h @@ -3,13 +3,12 @@ #include #include "handle.h" +#include "stage.h" namespace Msp::Game { class Component; class Entity; -class Root; -class Stage; template class Owned: public Handle @@ -68,13 +67,10 @@ template template Stage &Owned::get_stage(O &obj) { - using DependentRoot = std::conditional_t; if constexpr(std::is_base_of_v) return get_stage(*obj.get_entity()); - else if constexpr(std::is_base_of_v) - return dynamic_cast(*obj.get_root()).get_stage(); else - return obj; + return obj.get_stage(); } template diff --git a/source/game/stage.cpp b/source/game/stage.cpp index a6bd19a..a616d5a 100644 --- a/source/game/stage.cpp +++ b/source/game/stage.cpp @@ -1,13 +1,14 @@ #include "stage.h" +#include "root.h" #include "system.h" namespace Msp::Game { Stage::Stage(): - root(*this) + root(std::make_unique(*this)) { } -// Hide ~unique_ptr from the header +// Hide unique_ptr destructors from the header Stage::~Stage() { } diff --git a/source/game/stage.h b/source/game/stage.h index 0607596..47ca47d 100644 --- a/source/game/stage.h +++ b/source/game/stage.h @@ -4,17 +4,19 @@ #include #include #include "handle.h" -#include "root.h" namespace Msp::Game { +class Root; class System; class Stage { private: PoolPool pools; - Root root; + /* Use unique_ptr because there's only one root per stage so it's pointless + to put it in a pool. */ + std::unique_ptr root; std::vector> systems; public: @@ -22,7 +24,7 @@ public: ~Stage(); PoolPool &get_pools() { return pools; } - Handle get_root() { return Handle::from_object(&root); } + Handle get_root() { return Handle::from_object(root.get()); } template void iterate_objects(const F &);