3#include <litefx/app_api.hpp>
4#include <litefx/app_formatters.hpp>
73 std::type_index typeId() const noexcept {
return typeid(*this); }
107 template <typename TResult, typename... TArgs>
124 m_target(std::move(fn)), m_token(t) { }
132 inline TResult
invoke(TArgs... args)
const {
133 return m_target(std::move(args)...);
151 return this->
invoke(std::move(args)...);
167 template <
typename TEventArgs>
208 m_subscribers.
clear();
231 const auto match = std::max_element(m_subscribers.begin(), m_subscribers.end(), [](
const auto& lhs,
const auto& rhs) { return lhs.token() < rhs.token(); });
232 event_token_type token = match == m_subscribers.end() ? 0 : match->token() + 1;
233 m_subscribers.emplace_back(subscriber, token);
243 return this->
remove(subscriber.token());
252 const auto last = std::remove_if(m_subscribers.begin(), m_subscribers.end(), [&token](
const auto& s) { return s.token() == token; });
254 if (last == m_subscribers.end())
257 m_subscribers.erase(last, m_subscribers.end());
265 m_subscribers.clear();
273 void invoke(
const void* sender,
const TEventArgs& args)
const {
274 for (
const auto&
handler : m_subscribers)
284 return std::find_if(m_subscribers.begin(), m_subscribers.end(), [&token](
const auto& d) { return d.token() == token; }) != m_subscribers.end();
294 if (
auto match = std::find_if(m_subscribers.begin(), m_subscribers.end(), [&token](
const auto& d) { return d.token() == token; }); match != m_subscribers.end()) [[likely]]
305 explicit operator bool() const noexcept {
306 return !m_subscribers.empty();
315 return this->
add(subscriber);
324 return this->
remove(subscriber);
333 return this->
remove(token);
341 void operator ()(
const void* sender,
const TEventArgs& args)
const {
342 this->
invoke(sender, args);
362 int m_width, m_height;
402 class LITEFX_APPMODEL_API
App {
403 LITEFX_IMPLEMENTATION(
AppImpl);
413 auto operator=(const
App&) = delete;
414 auto operator=(
App&&) noexcept = delete;
416 virtual ~
App() noexcept;
442 const
IBackend* operator[](std::type_index type) const;
471 template <typename TBackend> requires
474 return dynamic_cast<TBackend*
>(this->
getBackend(
typeid(TBackend)));
487 void startBackend(std::type_index type)
const;
498 void stopBackend(std::type_index type)
const;
519 std::type_index activeBackendType(
BackendType type)
const;
527 void registerStartCallback(std::type_index type,
const std::function<
bool()>& callback);
534 void registerStopCallback(std::type_index type,
const std::function<
void()>& callback);
563 template <
typename TBackend>
requires
566 this->registerStartCallback(
typeid(TBackend), [
this, callback]() {
590 template <
typename TBackend>
requires
593 this->registerStopCallback(
typeid(TBackend), [
this, callback]() {
610 template <
typename TBackend>
requires
613 return dynamic_cast<const TBackend*
>(this->
getBackend(
typeid(TBackend)));
621 template <
typename TBackend>
requires
632 template <
typename TBackend>
requires
677 void resize(
int width,
int height);
683 template <
typename TApp,
typename ...TArgs>
701 template <
typename TSink,
typename ...TArgs>
requires
702 std::convertible_to<TSink*, ISink*>
712 template <
typename TBackend,
typename ...TArgs>
requires
720 template<
typename TApp,
typename ...TArgs>
Creates a new builder for an App.
Definition app.hpp:690
AppBuilder & logTo(TArgs &&... args)
Registers a sink for logging.
Definition app.hpp:703
AppBuilder & useBackend(TArgs &&... args)
Registers a new backend.
Definition app.hpp:714
void use(UniquePtr< IBackend > &&backend)
Definition app.cpp:225
The base class for an application.
Definition app.hpp:402
virtual void use(UniquePtr< IBackend > &&backend)
Adds a backend to the app.
Definition app.cpp:167
const TBackend * findBackend() const
Returns the registered backend instance for a type index.
Definition app.hpp:612
virtual StringView name() const noexcept=0
Returns the name of the app.
Event< EventArgs > shutdown
Invoked, if the application has is shutting down.
Definition app.hpp:652
Enumerable< const IBackend & > getBackends(const BackendType type) const
Returns all registered backend instances of a backend type.
Definition app.cpp:160
void startBackend()
Attempts to start a backend of type TBackend and stops the active backend of the same BackendType,...
Definition app.hpp:623
void stopBackend(std::type_index type) const
Stops a backend.
Definition app.cpp:101
Event< EventArgs > startup
Invoked, if the application has been started.
Definition app.hpp:642
Event< const IBackend * > backendStarted
Invoked, if a backend has been started.
Definition app.hpp:540
void startBackend(std::type_index type) const
Starts a backend.
Definition app.cpp:74
virtual AppVersion version() const noexcept=0
Returns the version of the app.
void onBackendStop(const std::function< void(TBackend *)> &callback)
Sets a callback that is called, if a backend is stopped.
Definition app.hpp:592
Event< ResizeEventArgs > resized
Invoked, if the app window or context gets resized.
Definition app.hpp:670
App(App &&) noexcept=delete
TBackend * findBackend()
Returns the registered backend instance for a type index.
Definition app.hpp:473
void stopBackend()
Stops a backend, if it is currently running.
Definition app.hpp:634
void onBackendStart(const std::function< bool(TBackend *)> &callback)
Sets a callback that is called, if a backend is started.
Definition app.hpp:565
const IBackend * getBackend(std::type_index type) const
Returns the registered backend instance for a type index.
Definition app.cpp:64
Event< const IBackend * > backendStopped
Invoked, if a backend has been stopped.
Definition app.hpp:545
void resize(int width, int height)
Called, if the application window resizes.
Definition app.cpp:210
static AppBuilder build(TArgs &&... _args)
Creates a new application builder.
Definition app.hpp:721
Platform platform() const noexcept
Returns the platform, the app is running on.
Definition app.cpp:50
App()
Initializes a new app instance.
Definition app.cpp:29
Event< EventArgs > initializing
Invoked during initialization.
Definition app.hpp:647
void run()
Starts the application.
Definition app.cpp:179
Contains the version of an App.
Definition app_api.hpp:50
constexpr Builder(TParent &parent, TPointer &&instance) noexcept
Definition containers.hpp:996
constexpr const App * instance() const noexcept
Definition containers.hpp:975
Represents a handler for an Event, that is assigned a unique token when created, so that it can be id...
Definition app.hpp:108
Delegate(function_type fn, token_type t) noexcept
Creates a new delegate.
Definition app.hpp:123
token_type token() const
Returns the unique token of the delegate.
Definition app.hpp:140
TResult invoke(TArgs... args) const
Invokes the delegate function.
Definition app.hpp:132
size_t token_type
Definition app.hpp:111
TResult operator()(TArgs... args) const
Invokes the delegate function.
Definition app.hpp:150
std::function< TResult(TArgs...)> function_type
Definition app.hpp:110
EventArgs(EventArgs &&) noexcept=default
EventArgs(const EventArgs &)=default
A class that is used to declare an event, which a number of listeners can subscribe to.
Definition app.hpp:168
bool remove(event_token_type token) noexcept
Unsubscribes an event handler from the event.
Definition app.hpp:251
typename delegate_type::token_type event_token_type
Definition app.hpp:173
constexpr ~Event() noexcept=default
Releases the event instance.
void clear() noexcept
Clears the event handlers.
Definition app.hpp:264
bool contains(event_token_type token) const noexcept
Returns true, if the event contains a subscriber with the provided token .
Definition app.hpp:283
event_token_type add(const function_type &subscriber)
Subscribes an event handler to the event.
Definition app.hpp:230
constexpr Event()=default
Initializes a new event.
TEventArgs event_args_type
Definition app.hpp:170
Delegate< void, const void *, TEventArgs > delegate_type
Definition app.hpp:171
constexpr Event & operator=(Event &&_other) noexcept=default
Assigns a event by taking it over.
bool remove(const delegate_type &subscriber) noexcept
Unsubscribes an event handler from the event.
Definition app.hpp:242
constexpr Event(const Event &_other)
Creates a copy of a event.
Definition app.hpp:191
constexpr Event & operator=(const Event &_other)
Assigns a event by copying it.
Definition app.hpp:207
typename delegate_type::function_type function_type
Definition app.hpp:172
void invoke(const void *sender, const TEventArgs &args) const
Invokes all event handlers of the event.
Definition app.hpp:273
const delegate_type & handler(event_token_type token) const
Returns the delegate associated with token .
Definition app.hpp:293
constexpr Event(Event &&_other) noexcept=default
Takes over another instance of a event.
The base class for an app backend.
Definition app.hpp:18
virtual StringView name() const noexcept=0
Gets the name of the backend.
friend class App
Definition app.hpp:19
const BackendState & state() const noexcept
Returns the state of the backend.
Definition app.hpp:47
IBackend() noexcept=default
virtual BackendType type() const noexcept=0
Gets the type of the backend.
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.
An exception that is thrown, if a provided argument is not valid.
Definition exceptions.hpp:60
static void sinkTo(const ISink *sink)
Allows a log to write messages to sink .
Definition logger_factory.cpp:38
Concept that can be used to refer to backend implementations.
Definition app.hpp:80
Definition logging.hpp:23
constexpr UniquePtr< T > makeUnique()
Creates a new unique pointer.
Definition containers.hpp:153
BackendState
Definition app_api.hpp:42
@ Inactive
Definition app_api.hpp:43
@ Active
Definition app_api.hpp:44
std::vector< T > Array
Represents a dynamic array.
Definition containers.hpp:73
std::unique_ptr< T, TDeleter > UniquePtr
Represents a unique pointer, that expresses exclusive ownership.
Definition containers.hpp:102
std::string_view StringView
Definition string.hpp:26
BackendType
Definition app_api.hpp:35
Platform
Definition app_api.hpp:29
An input range over another range, where the returned values of type T are covariants of the values ...
Definition containers.hpp:529
int height() const noexcept
Returns the new window height.
Definition app.hpp:392
ResizeEventArgs(int width, int height) noexcept
Creates a new set of window resize event arguments.
Definition app.hpp:370
ResizeEventArgs(ResizeEventArgs &&) noexcept=default
ResizeEventArgs(const ResizeEventArgs &)=default
int width() const noexcept
Returns the new window width.
Definition app.hpp:384