3#include <litefx/app_api.hpp>
4#include <litefx/app_formatters.hpp>
22 BackendState m_state = BackendState::Inactive;
33 virtual BackendType type() const noexcept = 0;
40 const BackendState& state() const noexcept {
return m_state; }
53 BackendState& state() noexcept {
return m_state; }
66 std::type_index typeId() const noexcept {
return typeid(*this); }
74 class LITEFX_APPMODEL_API
App {
86 virtual ~App() noexcept;
93 virtual
String name() const noexcept = 0;
105 Platform platform() const noexcept;
112 virtual const
IBackend* operator[](std::type_index type) const;
119 virtual const
IBackend* getBackend(std::type_index type) const;
126 virtual
Array<const
IBackend*> getBackends(const BackendType type) const noexcept;
134 virtual
IBackend* getBackend(std::type_index type);
141 template <typename TBackend> requires
142 rtti::implements<TBackend,
IBackend>
143 TBackend* findBackend() {
144 return dynamic_cast<TBackend*
>(this->getBackend(
typeid(TBackend)));
157 virtual void startBackend(std::type_index type)
const;
168 virtual void stopBackend(std::type_index type)
const;
175 virtual void stopActiveBackends(
const BackendType& type)
const;
182 virtual IBackend* activeBackend(
const BackendType& type)
const;
189 virtual std::type_index activeBackendType(
const BackendType& type)
const;
197 void registerStartCallback(std::type_index type,
const std::function<
bool()>& callback);
204 void registerStopCallback(std::type_index type,
const std::function<
void()>& callback);
218 template <
typename TBackend>
requires
221 this->registerStartCallback(
typeid(TBackend), [
this, callback]() {
222 auto backend = this->findBackend<TBackend>();
224 if (backend ==
nullptr)
225 throw InvalidArgumentException(
"No backend of type {0} has been registered.",
typeid(TBackend).name());
227 if (backend->state() == BackendState::Active)
230 return callback(backend);
240 template <
typename TBackend>
requires
243 this->registerStopCallback(
typeid(TBackend), [
this, callback]() {
244 auto backend = this->findBackend<TBackend>();
246 if (backend ==
nullptr)
247 throw InvalidArgumentException(
"No backend of type {0} has been registered.",
typeid(TBackend).name());
249 if (backend->state() != BackendState::Inactive)
260 template <
typename TBackend>
requires
263 return dynamic_cast<const TBackend*
>(this->getBackend(
typeid(TBackend)));
271 template <
typename TBackend>
requires
274 this->startBackend(
typeid(TBackend));
282 template <
typename TBackend>
requires
285 this->stopBackend(
typeid(TBackend));
315 virtual void resize(
int& width,
int& height);
321 template <
typename TApp,
typename ...TArgs>
323 return AppBuilder(makeUnique<TApp>(std::forward<TArgs>(_args)...));
332 using builder_type::Builder;
336 virtual void build()
override;
345 template <
typename TSink,
typename ...TArgs>
requires
346 std::convertible_to<TSink*, ISink*>
348 auto sink = makeUnique<TSink>(std::forward<TArgs>(args)...);
356 template <
typename TBackend,
typename ...TArgs>
requires
359 this->use(makeUnique<TBackend>(*this->instance(), std::forward<TArgs>(args)...));
378 explicit AppVersion(
int major = 1,
int minor = 0,
int patch = 0,
int revision = 0)
noexcept;
389 int major() const noexcept;
395 int minor() const noexcept;
401 int patch() const noexcept;
407 int revision() const noexcept;
413 int engineMajor() const noexcept;
419 int engineMinor() const noexcept;
425 int engineRevision() const noexcept;
431 int engineStatus() const noexcept;
437 String engineIdentifier() const noexcept;
443 String engineVersion() const noexcept;
450 bool engineVersion =
false;
452 constexpr auto parse(format_parse_context& ctx) {
453 auto it = ctx.begin(), end = ctx.end();
455 if (it != end && (*it ==
'e'))
457 engineVersion =
true;
461 if (it != end && *it !=
'}')
462 throw format_error(
"Invalid version format: expected: `}`.");
467 template <
typename FormatContext>
469 return engineVersion ?
Definition: appversion.cpp:9
Creates a new builder for an App.
Definition: app.hpp:330
AppBuilder & logTo(TArgs &&... args)
Registers a sink for logging.
Definition: app.hpp:347
AppBuilder & useBackend(TArgs &&... args)
Registers a new backend.
Definition: app.hpp:358
The base class for an application.
Definition: app.hpp:74
const TBackend * findBackend() const
Returns the registered backend instance for a type index.
Definition: app.hpp:262
static AppBuilder build(TArgs &&... _args)
Creates a new application builder.
Definition: app.hpp:322
void onBackendStop(const std::function< void(TBackend *)> &callback)
Sets a callback that is called, if a backend is stopped.
Definition: app.hpp:242
void startBackend()
Attempts to start a backend of type TBackend and stops the active backend of the same BackendType,...
Definition: app.hpp:273
virtual void run()=0
Starts the application.
void onBackendStart(const std::function< bool(TBackend *)> &callback)
Sets a callback that is called, if a backend is started.
Definition: app.hpp:220
void stopBackend()
Stops a backend, if it is currently running.
Definition: app.hpp:284
virtual void initialize()=0
Called to initialize the application state.
Contains the version of an App.
Definition: app.hpp:367
int major() const noexcept
Gets the major version of the app.
Definition: appversion.cpp:34
int patch() const noexcept
Gets the patch number of the app.
Definition: appversion.cpp:44
String engineIdentifier() const noexcept
Gets the identifier of the engine build.
Definition: appversion.cpp:74
int revision() const noexcept
Gets the revision of the app.
Definition: appversion.cpp:49
virtual ~AppVersion() noexcept
AppVersion(const AppVersion &)=delete
int minor() const noexcept
Gets the minor version of the app.
Definition: appversion.cpp:39
String engineVersion() const noexcept
Gets the version string of the engine build.
Definition: appversion.cpp:79
AppVersion(AppVersion &&)=delete
Describes an generic builder type.
Definition: containers.hpp:509
The base class for an app backend.
Definition: app.hpp:18
virtual ~IBackend() noexcept=default
virtual void deactivate()=0
Called by the parent App, if the backend is stopped.
virtual void activate()=0
Called by the parent App, if the backend is started.
virtual String name() const noexcept=0
Gets the name of the backend.
static void sinkTo(const ISink *sink)
Definition: logger_factory.cpp:32
Definition: traits.hpp:95
Definition: logging.hpp:22
std::string String
Definition: string.hpp:19
std::vector< T > Array
Represents a dynamic array.
Definition: containers.hpp:58
std::unique_ptr< T, TDeleter > UniquePtr
Represents a unique pointer, that expresses exclusive ownership.
Definition: containers.hpp:87
Definition: app_formatters.hpp:6