src/application/app_state_manager.hpp
Namespaces
Classes
Source code
#ifndef SUPERGENIUS_APPLICATION_DISPATCHER
#define SUPERGENIUS_APPLICATION_DISPATCHER
#include <functional>
#include <memory>
#include <stdexcept>
#include "singleton/IComponent.hpp"
namespace sgns::application {
class AppStateManager : public std::enable_shared_from_this<AppStateManager>, public IComponent {
public:
using OnPrepare = std::function<bool()>;
using OnLaunch = std::function<bool()>;
using OnShutdown = std::function<void()>;
enum class State {
Init,
Prepare,
ReadyToStart,
Starting,
Works,
ShuttingDown,
ReadyToStop,
};
~AppStateManager() override = default;
virtual void atPrepare(OnPrepare &&cb) = 0;
virtual void atLaunch(OnLaunch &&cb) = 0;
virtual void atShutdown(OnShutdown &&cb) = 0;
void registerHandlers(OnPrepare &&prepare_cb,
OnLaunch &&launch_cb,
OnShutdown &&shutdown_cb) {
atPrepare(std::move(prepare_cb));
atLaunch(std::move(launch_cb));
atShutdown(std::move(shutdown_cb));
}
template <typename Controlled>
void takeControl(Controlled &entity) {
registerHandlers([&entity]() -> bool { return entity.prepare(); },
[&entity]() -> bool { return entity.start(); },
[&entity]() -> void { return entity.stop(); });
}
virtual void run() = 0;
virtual void shutdown() = 0;
[[nodiscard]] virtual State state() const = 0;
protected:
virtual void doPrepare() = 0;
virtual void doLaunch() = 0;
virtual void doShutdown() = 0;
};
struct AppStateException : public std::runtime_error {
explicit AppStateException(std::string message)
: std::runtime_error("Wrong workflow at " + std::move(message)) {}
};
} // namespace sgns::application
#endif // SUPERGENIUS_APPLICATION_DISPATCHER
Updated on 2026-03-04 at 13:10:44 -0800