diff --git a/CMakeLists.txt b/CMakeLists.txt index ffda29459..b1c256ca4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ endif() add_library( appbase application.cpp + ${HEADERS} ) target_link_libraries( appbase ${Boost_LIBRARIES}) diff --git a/application.cpp b/application.cpp index 3931ad84b..0f5e6a76b 100644 --- a/application.cpp +++ b/application.cpp @@ -22,7 +22,10 @@ class application_impl { options_description _app_options; options_description _cfg_options; - bfs::path _data_dir; + bfs::path _data_dir; + + string _version; + uint64_t _version_int; }; application::application() @@ -32,6 +35,27 @@ application::application() application::~application() { } +void application::set_version(string version) { + my->_version = version; +} + +void application::set_version(uint64_t version) { + my->_version_int = version; +} + +string application::version() const { + return my->_version; +} + +uint64_t application::version_int() const { + return my->_version_int; +} + +void application::startup() { + for (auto plugin : initialized_plugins) + plugin->startup(); +} + application& application::instance() { static application _app; return _app; @@ -69,16 +93,19 @@ void application::set_program_options() my->_app_options.add(app_cli_opts); } - -bool application::initialize( int argc, char** argv ) -{ +bool application::initialize_impl(int argc, char** argv, vector autostart_plugins) { set_program_options(); bpo::variables_map options; bpo::store(bpo::parse_command_line(argc, argv, my->_app_options), options); if( options.count( "help" ) ) { - cout << my->_app_options << "\n"; + cout << my->_app_options << std::endl; + return false; + } + + if( options.count( "version" ) ) { + cout << my->_version << std::endl; return false; } @@ -116,18 +143,15 @@ bool application::initialize( int argc, char** argv ) get_plugin(name).initialize(options); } } + for (auto plugin : autostart_plugins) + if (plugin != nullptr && plugin->get_state() == abstract_plugin::registered) + plugin->initialize(options); bpo::notify(options); return true; } -void application::startup() -{ - for(auto plug : initialized_plugins) - plug->startup(); -} - void application::shutdown() { for(auto ritr = running_plugins.rbegin(); ritr != running_plugins.rend(); ++ritr) { diff --git a/include/appbase/application.hpp b/include/appbase/application.hpp index 95bd5c33c..1d024f537 100644 --- a/include/appbase/application.hpp +++ b/include/appbase/application.hpp @@ -13,22 +13,48 @@ namespace appbase { public: ~application(); + /** @brief Sets version string + * + * @param version Version string output verbatim with -v/--version + */ + void set_version(string version); + /** @overload + * + * @param version Integer version independent of the string output with -v/--version + */ + void set_version(uint64_t version); + /** @brief Gets version string + * + * @return Version string output with -v/--version + */ + string version() const; + /** @brief Gets version integer + * + * @return Version integer independent of the string output with -v/--version + */ + uint64_t version_int() const; /** - * Looks for the --plugin commandline / config option and calls initialize on those plugins + * @brief Looks for the --plugin commandline / config option and calls initialize on those plugins * - * @return true if the application and plugins were initialized, false or exception on error + * @tparam Plugin List of plugins to initalize even if not mentioned by configuration. For plugins started by + * configuration settings or dependency resolution, this template has no effect. + * @return true if the application and plugins were initialized, false or exception on error */ - bool initialize(int argc, char** argv); - void startup(); - void shutdown(); + template + bool initialize(int argc, char** argv) { + return initialize_impl(argc, argv, {find_plugin()...}); + } + + void startup(); + void shutdown(); /** * Wait until quit(), SIGINT or SIGTERM and then shutdown */ - void exec(); - void quit(); + void exec(); + void quit(); - static application& instance(); + static application& instance(); abstract_plugin* find_plugin(const string& name)const; abstract_plugin& get_plugin(const string& name)const; @@ -65,6 +91,8 @@ namespace appbase { template friend class plugin; + bool initialize_impl(int argc, char** argv, vector autostart_plugins); + /** these notifications get called from the plugin when their state changes so that * the application can call shutdown in the reverse order. */