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)
275 handler(sender, args);
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);
352 return this->handler(token);
362 int m_width, m_height;
370 ResizeEventArgs(
int width,
int height) noexcept : m_width(width), m_height(height) { }
384 inline
int width() const noexcept {
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;
449 const
IBackend* getBackend(std::type_index type) const;
464 IBackend* getBackend(std::type_index type);
471 template <typename TBackend> requires
472 meta::implements<TBackend,
IBackend>
473 TBackend* findBackend() {
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]() {
567 auto backend = this->findBackend<TBackend>();
572 if (
backend->state() == BackendState::Active)
590 template <
typename TBackend>
requires
593 this->registerStopCallback(
typeid(TBackend), [
this, callback]() {
594 auto backend = this->findBackend<TBackend>();
599 if (
backend->state() != BackendState::Inactive)
610 template <
typename TBackend>
requires
613 return dynamic_cast<const TBackend*
>(this->getBackend(
typeid(TBackend)));
621 template <
typename TBackend>
requires
624 this->startBackend(
typeid(TBackend));
632 template <
typename TBackend>
requires
635 this->stopBackend(
typeid(TBackend));
677 void resize(
int width,
int height);
683 template <
typename TApp,
typename ...TArgs>
684 [[nodiscard]]
static AppBuilder build(TArgs&&... _args);
701 template <
typename TSink,
typename ...TArgs>
requires
702 std::convertible_to<TSink*, ISink*>
704 auto sink = makeUnique<TSink>(std::forward<TArgs>(args)...);
705 Logger::sinkTo(sink.get());
712 template <
typename TBackend,
typename ...TArgs>
requires
715 this->use(makeUnique<TBackend>(*this->instance(), std::forward<TArgs>(args)...));
720 template<
typename TApp,
typename ...TArgs>
723 return AppBuilder(makeUnique<TApp>(std::forward<TArgs>(_args)...));
Creates a new builder for an App.
Definition app.hpp:690
AppBuilder & useBackend(TArgs &&... args)
Registers a new backend.
Definition app.hpp:714
AppBuilder & logTo(TArgs &&... args)
Registers a sink for logging.
Definition app.hpp:703
The base class for an application.
Definition app.hpp:402
Event< EventArgs > shutdown
Invoked, if the application has is shutting down.
Definition app.hpp:652
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
Event< ResizeEventArgs > resized
Invoked, if the app window or context gets resized.
Definition app.hpp:670
App(App &&) noexcept=delete
void onBackendStop(const std::function< void(TBackend *)> &callback)
Sets a callback that is called, if a backend is stopped.
Definition app.hpp:592
void onBackendStart(const std::function< bool(TBackend *)> &callback)
Sets a callback that is called, if a backend is started.
Definition app.hpp:565
void stopBackend()
Stops a backend, if it is currently running.
Definition app.hpp:634
Event< const IBackend * > backendStopped
Invoked, if a backend has been stopped.
Definition app.hpp:545
const TBackend * findBackend() const
Returns the registered backend instance for a type index.
Definition app.hpp:612
void startBackend()
Attempts to start a backend of type TBackend and stops the active backend of the same BackendType,...
Definition app.hpp:623
Event< EventArgs > initializing
Invoked during initialization.
Definition app.hpp:647
Contains the version of an App.
Definition app_api.hpp:50
Describes an generic builder type.
Definition containers.hpp:960
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
Base class for additional event arguments.
Definition app.hpp:86
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
constexpr Event()=default
Initializes a new event.
TEventArgs event_args_type
Definition app.hpp:170
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.
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.
An exception that is thrown, if a provided argument is not valid.
Definition exceptions.hpp:60
Concept that can be used to refer to backend implementations.
Definition app.hpp:80
Definition logging.hpp:23
BackendState
Definition app_api.hpp:42
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
Stores event arguments of a window resize event.
Definition app.hpp:360
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