5#pragma warning(disable: 4251)
24#include "exceptions.hpp"
26#ifndef LITEFX_DEFINE_FLAGS
27# define LITEFX_DEFINE_FLAGS(T) \
28 inline T operator| (const T lhs, const T rhs) { using _base_t = std::underlying_type_t<T>; return static_cast<T>(static_cast<_base_t>(lhs) | static_cast<_base_t>(rhs)); } \
29 inline T& operator|= (T& lhs, const T& rhs) { lhs = lhs | rhs; return lhs; } \
30 inline T operator& (const T lhs, const T rhs) { using _base_t = std::underlying_type_t<T>; return static_cast<T>(static_cast<_base_t>(lhs) & static_cast<_base_t>(rhs)); } \
31 inline T& operator&= (T& lhs, const T& rhs) { lhs = lhs & rhs; return lhs; }
34#ifndef LITEFX_FLAG_IS_SET
35# define LITEFX_FLAG_IS_SET(val, flag) static_cast<bool>((static_cast<UInt32>(val) & static_cast<UInt32>(flag)) == static_cast<UInt32>(flag))
50 template<
class TKey,
class TVal>
86 template<
class T,
class TDeleter = std::default_delete<T>>
107 template <
class... T>
114 template <
class... T>
124 return std::make_unique<T>();
132 template <
class T,
class... TArgs>
134 return std::make_unique<T>(std::forward<TArgs>(_args)...);
144 return std::make_shared<T>();
152 template <
class T,
class... TArgs>
154 return std::make_shared<T>(std::forward<TArgs>(_args)...);
165 return std::make_shared<T>(ptr.release());
173 template <
typename TContainer>
176 template <
typename TContainer, std::ranges::range TRange>
requires
177 std::convertible_to<std::ranges::range_value_t<TRange>,
typename TContainer::value_type>
179 auto it = range | std::views::common;
180 return TContainer{ it.begin(), it.end() };
190 template <std::ranges::range TContainer>
requires
191 (!std::ranges::view<TContainer>)
198#if (defined(BUILD_LITEFX_PIMPL) && BUILD_LITEFX_PIMPL) || (!defined(BUILD_LITEFX_PIMPL)) && !defined(LITEFX_IMPLEMENTATION)
203 template <
class pImpl>
255 PimplPtr(pImpl* pimpl) noexcept : m_ptr(pimpl) { }
267 pImpl*
release() noexcept { m_ptr.release(); }
283 template <
class T,
class... Arg>
294 template <
class T,
class... Arg>
296 return PimplPtr<T>(
new T(std::forward<Arg>(arg)...));
307# define LITEFX_IMPLEMENTATION(impl) private: \
309 PimplPtr<impl> m_impl;
317 template <
class TInterface>
332 if (parent ==
nullptr)
333 throw std::runtime_error(
"Initializing an implementation requires the parent to be provided.");
346 template <
class THandle>
363 virtual const THandle&
handle() const noexcept = 0;
370 template <class THandle>
389 THandle&
handle() noexcept
override {
return m_handle; }
393 const THandle&
handle() const noexcept
override {
return m_handle; }
421 template <
typename TDerived,
typename T,
typename TParent = std::
nullptr_t,
typename TPo
inter = UniquePtr<T>>
431 template <
typename TDerived,
typename T,
typename TPo
inter>
432 class Builder<TDerived, T, std::nullptr_t, typename TPointer> {
448 const T*
instance() const noexcept {
return m_instance.get(); }
455 T*
instance() noexcept {
return m_instance.get(); }
462 explicit Builder(TPointer&& instance) noexcept : m_instance(std::move(instance)) { }
477 virtual
void build() { };
488 template <
typename TInstance>
495 operator TPointer&& () {
497 return std::move(m_instance);
508 template <
typename TDerived,
typename T,
typename TParent,
typename TPo
inter>
526 const T*
instance() const noexcept {
return m_instance.get(); }
532 const TParent&
parent() const noexcept {
return m_parent; }
539 T*
instance() noexcept {
return m_instance.get(); }
547 explicit Builder(TParent& parent, TPointer&& instance) noexcept : m_parent(parent), m_instance(std::move(instance)) { }
553 Builder(
builder_type&& _other) noexcept : m_instance(std::move(_other.m_instance)), m_parent(_other.m_parent) { }
562 virtual
void build() { };
573 template <
typename TInstance>
582 m_parent.use(std::move(m_instance));
587#if !defined(LITEFX_BUILDER)
588# define LITEFX_BUILDER(BuilderType) public: \
589 using builder_type = BuilderType; \
590 friend class BuilderType;
TDerived derived_type
Definition: containers.hpp:437
void use(pointer_type &&)=delete
Called by child builders to pass a constructed object back to the parent builder.
TPointer pointer_type
Definition: containers.hpp:440
Builder(TPointer &&instance) noexcept
Initializes the builder instance.
Definition: containers.hpp:462
virtual ~Builder() noexcept=default
Builder(const builder_type &)=delete
T instance_type
Definition: containers.hpp:438
std::nullptr_t parent_type
Definition: containers.hpp:439
Builder(builder_type &&_other) noexcept
Initializes the builder instance by taking over another instance.
Definition: containers.hpp:468
T * instance() noexcept
Returns a pointer to the current instance of the object that is built by the builder.
Definition: containers.hpp:455
const T * instance() const noexcept
Returns a pointer to the current instance of the object that is built by the builder.
Definition: containers.hpp:448
Describes an generic builder type.
Definition: containers.hpp:509
T * instance() noexcept
Returns a pointer to the current instance of the object that is built by the builder.
Definition: containers.hpp:539
TParent & add()
First, calls build, then use on the parent builder using the current object instance and finally retu...
Definition: containers.hpp:580
Builder(builder_type &&_other) noexcept
Initializes the builder instance by taking over another instance.
Definition: containers.hpp:553
T instance_type
Definition: containers.hpp:516
Builder(const builder_type &)=delete
TPointer pointer_type
Definition: containers.hpp:518
TDerived derived_type
Definition: containers.hpp:515
virtual ~Builder() noexcept=default
Builder(TParent &parent, TPointer &&instance) noexcept
Initializes the builder instance.
Definition: containers.hpp:547
const T * instance() const noexcept
Returns a pointer to the current instance of the object that is built by the builder.
Definition: containers.hpp:526
TParent parent_type
Definition: containers.hpp:517
void use(pointer_type &&)=delete
Called by child builders to pass a constructed object back to the parent builder.
const TParent & parent() const noexcept
Returns a reference of the parent builder.
Definition: containers.hpp:532
Provides access to a resource managed by the class.
Definition: containers.hpp:347
virtual THandle & handle() noexcept=0
Returns the resource managed by the class.
virtual ~IResource() noexcept=default
Base class for an implementation of a public interface class.
Definition: containers.hpp:318
Implement(TInterface *parent)
Initializes the implementation instance.
Definition: containers.hpp:331
TInterface * m_parent
Definition: containers.hpp:324
TInterface interface_type
Definition: containers.hpp:320
Implement(const Implement< TInterface > &)=delete
virtual ~Implement()=default
Implement(Implement< TInterface > &&)=delete
A smart pointer that manages an implementation instance for a public interface class.
Definition: containers.hpp:204
pImpl * release() noexcept
Releases the implementation instance managed by this pointer and returns it.
Definition: containers.hpp:267
void destroy()
Destroys the implementation instance managed by this pointer.
Definition: containers.hpp:261
friend PimplPtr< T > makePimpl(Arg &&... arg)
Creates a pointer to an implementation.
Definition: containers.hpp:295
PimplPtr(PimplPtr &&src) noexcept=default
Initializes a new pointer by taking over the implementation instance managed by src .
PimplPtr() noexcept=default
Initializes a new pointer to an uninitialized implementation instance.
pImpl * operator->() const noexcept
Returns a pointer to the managed implementation instance.
Definition: containers.hpp:280
~PimplPtr() noexcept=default
PimplPtr & operator=(const PimplPtr &src) noexcept
Initializes a new pointer to a copy of the implementation instance managed by src .
Definition: containers.hpp:239
pImpl & operator*() const noexcept
Returns a reference to the managed implementation instance.
Definition: containers.hpp:274
Implements the IResource interface.
Definition: containers.hpp:371
virtual ~Resource() noexcept=default
Resource(Resource &&)=delete
const THandle & handle() const noexcept override
Returns the resource managed by the class.
Definition: containers.hpp:393
Resource(const THandle handle) noexcept
Initializes the managed resource.
Definition: containers.hpp:380
Resource(const Resource &)=delete
auto to()
Definition: containers.hpp:192
TContainer operator|(TRange &&range, to_container< TContainer >)
Definition: containers.hpp:178
SharedPtr< T > makeShared()
Creates a new shared pointer.
Definition: containers.hpp:143
UniquePtr< T > makeUnique()
Creates a new unique pointer.
Definition: containers.hpp:123
std::vector< T > Array
Represents a dynamic array.
Definition: containers.hpp:58
void * Handle
Represents a handle type.
Definition: containers.hpp:43
std::weak_ptr< T > WeakPtr
Represents a weak pointer, that expresses a reference to a shared pointer instance.
Definition: containers.hpp:101
std::span< T > Span
Represents a view of an array.
Definition: containers.hpp:72
std::queue< T > Queue
Represents a queue.
Definition: containers.hpp:65
std::shared_ptr< T > SharedPtr
Represents a shared pointer, that expresses non-exclusive ownership.
Definition: containers.hpp:94
PimplPtr< T > makePimpl(Arg &&... arg)
Creates a pointer to an implementation.
Definition: containers.hpp:295
std::unordered_map< TKey, TVal > Dictionary
Represents a dictionary that maps a key to a certain value.
Definition: containers.hpp:51
std::unique_ptr< T, TDeleter > UniquePtr
Represents a unique pointer, that expresses exclusive ownership.
Definition: containers.hpp:87
std::variant< T... > Variant
Represents a variant of objects.
Definition: containers.hpp:115
std::optional< T > Optional
Represents an optional value.
Definition: containers.hpp:79
std::tuple< T... > Tuple
Represents a tuple of multiple objects.
Definition: containers.hpp:108
Definition: containers.hpp:174