diff --git a/src/ruisapp/application.cpp b/src/ruisapp/application.cpp index c748c86..4673a8f 100644 --- a/src/ruisapp/application.cpp +++ b/src/ruisapp/application.cpp @@ -145,7 +145,18 @@ const application_factory::factory_type& application_factory::get_factory() return f; } -application_factory::application_factory(factory_type&& factory) +std::unique_ptr application_factory::create_application(int argc, const char** argv) +{ + auto args = utki::make_span(argv, argc); + + if (args.empty()) { + return get_factory()(std::string_view(), nullptr); + } + + return get_factory()(args.front(), args.subspan(1)); +} + +application_factory::application_factory(factory_type factory) { auto& f = this->get_factory_internal(); if (f) { diff --git a/src/ruisapp/application.hpp b/src/ruisapp/application.hpp index 260fa77..6849e6a 100644 --- a/src/ruisapp/application.hpp +++ b/src/ruisapp/application.hpp @@ -306,7 +306,8 @@ inline application& inst() class application_factory { public: - using factory_type = std::function(utki::span)>; + using factory_type = + std::function(std::string_view executable, utki::span)>; /** * @brief Constructor. @@ -315,10 +316,18 @@ class application_factory * @param factory - application factory function. * @throw std::logic_error - in case a factory is already registered. */ - application_factory(factory_type&& factory); + application_factory(factory_type factory); static const factory_type& get_factory(); + /** + * @brief Create application object. + * @param argc - number of command line arguments. + * @param argv - array of command line arguments. First argument is the + * executable filename. + */ + static std::unique_ptr create_application(int argc, const char** argv); + private: static factory_type& get_factory_internal(); }; diff --git a/src/ruisapp/glue/android/glue.cxx b/src/ruisapp/glue/android/glue.cxx index 764f80b..7bc8ea8 100644 --- a/src/ruisapp/glue/android/glue.cxx +++ b/src/ruisapp/glue/android/glue.cxx @@ -1551,7 +1551,7 @@ void on_native_window_created(ANativeActivity* activity, ANativeWindow* window) // retrieve current configuration AConfiguration_fromAssetManager(cfg->android_configuration, native_activity->assetManager); - application* app = ruisapp::application_factory::get_factory()(nullptr).release(); + application* app = ruisapp::application_factory::create_application(0, nullptr).release(); activity->instance = app; diff --git a/src/ruisapp/glue/unix_common.cxx b/src/ruisapp/glue/unix_common.cxx index 5a20c6b..7637136 100644 --- a/src/ruisapp/glue/unix_common.cxx +++ b/src/ruisapp/glue/unix_common.cxx @@ -25,7 +25,7 @@ namespace { std::unique_ptr create_app_unix(int argc, const char** argv) { - return ruisapp::application_factory::get_factory()(utki::make_span(argv, argc)); + return ruisapp::application_factory::create_application(argc, argv); } std::string initialize_storage_dir(const std::string& app_name) diff --git a/src/ruisapp/glue/windows/glue.cxx b/src/ruisapp/glue/windows/glue.cxx index 32d10bc..284412c 100644 --- a/src/ruisapp/glue/windows/glue.cxx +++ b/src/ruisapp/glue/windows/glue.cxx @@ -728,7 +728,7 @@ void application::quit() noexcept namespace ruisapp { void winmain(int argc, const char** argv) { - auto app = ruisapp::application_factory::get_factory()(utki::make_span(argv, argc)); + auto app = ruisapp::application_factory::create_application(argc, argv); if (!app) { return; } diff --git a/tests/app/src/main.cpp b/tests/app/src/main.cpp index 1907b51..bf2e122 100644 --- a/tests/app/src/main.cpp +++ b/tests/app/src/main.cpp @@ -846,6 +846,6 @@ class application : public ruisapp::application{ } }; -const ruisapp::application_factory app_fac([](auto args){ +const ruisapp::application_factory app_fac([](auto executable, auto args){ return std::make_unique<::application>(); });