From ff6aec9f700de8b9332767d982ea89bd4ebf3e18 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 12 Jan 2020 01:47:06 +0700 Subject: [PATCH] utils: variadic module::is_initialized and module::shutdown --- headers/enduro2d/utils/module.hpp | 10 +++---- sources/enduro2d/core/engine.cpp | 8 ++--- .../high/widgets/hierarchy_widget.cpp | 6 +--- .../high/widgets/inspector_widget.cpp | 6 +--- untests/sources/untests_utils/module.cpp | 29 +++++++++++++++++++ 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/headers/enduro2d/utils/module.hpp b/headers/enduro2d/utils/module.hpp index 75aa3772..315ec1c8 100644 --- a/headers/enduro2d/utils/module.hpp +++ b/headers/enduro2d/utils/module.hpp @@ -80,16 +80,14 @@ namespace e2d::modules return module::template initialize(std::forward(args)...); } - template < typename ImplT > + template < typename... ImplTs > void shutdown() noexcept { - using BaseT = typename ImplT::base_type; - module::shutdown(); + return (... , module::shutdown()); } - template < typename ImplT > + template < typename... ImplTs > bool is_initialized() noexcept { - using BaseT = typename ImplT::base_type; - return module::is_initialized(); + return (... && module::is_initialized()); } template < typename ImplT > diff --git a/sources/enduro2d/core/engine.cpp b/sources/enduro2d/core/engine.cpp index 27e207fc..c7faa61b 100644 --- a/sources/enduro2d/core/engine.cpp +++ b/sources/enduro2d/core/engine.cpp @@ -23,10 +23,10 @@ namespace using namespace e2d; template < typename Module, typename... Args > - void safe_module_initialize(Args&&... args) { - if ( !modules::is_initialized() ) { - modules::initialize(std::forward(args)...); - } + Module& safe_module_initialize(Args&&... args) { + return modules::is_initialized() + ? modules::instance() + : modules::initialize(std::forward(args)...); } void safe_register_predef_path( diff --git a/sources/enduro2d/high/widgets/hierarchy_widget.cpp b/sources/enduro2d/high/widgets/hierarchy_widget.cpp index 55ebdaf7..ac761854 100644 --- a/sources/enduro2d/high/widgets/hierarchy_widget.cpp +++ b/sources/enduro2d/high/widgets/hierarchy_widget.cpp @@ -86,11 +86,7 @@ namespace e2d::dbgui_widgets } bool hierarchy_widget::show() { - if ( !modules::is_initialized() ) { - return false; - } - - if ( !modules::is_initialized() ) { + if ( !modules::is_initialized() ) { return false; } diff --git a/sources/enduro2d/high/widgets/inspector_widget.cpp b/sources/enduro2d/high/widgets/inspector_widget.cpp index 9fd98be7..deced669 100644 --- a/sources/enduro2d/high/widgets/inspector_widget.cpp +++ b/sources/enduro2d/high/widgets/inspector_widget.cpp @@ -18,11 +18,7 @@ namespace e2d::dbgui_widgets } bool inspector_widget::show() { - if ( !modules::is_initialized() ) { - return false; - } - - if ( !modules::is_initialized() ) { + if ( !modules::is_initialized() ) { return false; } diff --git a/untests/sources/untests_utils/module.cpp b/untests/sources/untests_utils/module.cpp index 25ffa565..5565d8c4 100644 --- a/untests/sources/untests_utils/module.cpp +++ b/untests/sources/untests_utils/module.cpp @@ -26,6 +26,12 @@ namespace private: u32 m_; }; + + class empty_module : public module { + }; + + class empty_module_impl : public empty_module { + }; } TEST_CASE("module") { @@ -92,4 +98,27 @@ TEST_CASE("module") { REQUIRE_THROWS_AS(modules::instance(), module_not_initialized); REQUIRE_THROWS_AS(modules::instance(), module_not_initialized); } + { + REQUIRE_FALSE(modules::is_initialized()); + REQUIRE_FALSE(modules::is_initialized()); + REQUIRE_FALSE(modules::is_initialized()); + + REQUIRE_NOTHROW(modules::initialize(3)); + + REQUIRE(modules::is_initialized()); + REQUIRE_FALSE(modules::is_initialized()); + REQUIRE_FALSE(modules::is_initialized()); + + REQUIRE_NOTHROW(modules::initialize()); + + REQUIRE(modules::is_initialized()); + REQUIRE(modules::is_initialized()); + REQUIRE(modules::is_initialized()); + + REQUIRE_NOTHROW(modules::shutdown()); + + REQUIRE_FALSE(modules::is_initialized()); + REQUIRE_FALSE(modules::is_initialized()); + REQUIRE_FALSE(modules::is_initialized()); + } }