diff options
author | HampusM <hampus@hampusmat.com> | 2022-04-10 17:20:49 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:58 +0200 |
commit | b36d072ad7a7b9c6e30fcb25d6bbb001a8393468 (patch) | |
tree | 7749fc877652bb2cf7bbd2b7c1fed5e3abe397fc /src/DI/factory.tpp | |
parent | b828d4799a84beb1afbd889e5c4cb939a8b3df78 (diff) |
refactor: add factory class & make DI container return unique ptrs
Diffstat (limited to 'src/DI/factory.tpp')
-rw-r--r-- | src/DI/factory.tpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/DI/factory.tpp b/src/DI/factory.tpp new file mode 100644 index 0000000..38b9c00 --- /dev/null +++ b/src/DI/factory.tpp @@ -0,0 +1,65 @@ +#pragma once + +#include "factory.hpp" + +template <class Return, class... Args> +Factory<Return(Args...)>::Factory(const Factory &factory) : _functor(factory._functor) +{ +} + +template <class Return, class... Args> +Factory<Return(Args...)>::Factory(Factory &&factory) noexcept + : _functor(std::move(factory._functor)) +{ +} + +template <class Return, class... Args> +auto Factory<Return(Args...)>::operator=(Factory &&factory) noexcept + -> Factory<Return(Args...)> & +{ + _functor = std::move(factory._functor); + return *this; +} + +template <class Return, class... Args> +auto Factory<Return(Args...)>::operator=(std::nullptr_t) noexcept + -> Factory<Return(Args...)> & +{ + _functor = nullptr; + return *this; +} + +template <class Return, class... Args> +Factory<Return(Args...)>::~Factory() = default; + +template <class Return, class... Args> +Factory<Return(Args...)>::operator bool() const noexcept +{ + return static_cast<bool>(_functor); +} + +template <class Return, class... Args> +auto Factory<Return(Args...)>::operator()(Args... args) const -> Return +{ + return _functor(std::forward<Args>(args)...); +} + +template <class Return, class... Args> +auto Factory<Return(Args...)>::target_type() const noexcept -> const std::type_info & +{ + return _functor.target_type(); +} + +template <class Return, class... Args> +template <typename Tp> +auto Factory<Return(Args...)>::target() noexcept -> Tp * +{ + return static_cast<Tp *>(_functor.template target<Tp>()); +} + +template <class Return, class... Args> +template <typename Tp> +auto Factory<Return(Args...)>::target() const noexcept -> const Tp * +{ + return _functor.template target<Tp>(); +} |