From b36d072ad7a7b9c6e30fcb25d6bbb001a8393468 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 10 Apr 2022 17:20:49 +0200 Subject: refactor: add factory class & make DI container return unique ptrs --- src/DI/alloc_functor.hpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/DI/alloc_functor.hpp (limited to 'src/DI/alloc_functor.hpp') diff --git a/src/DI/alloc_functor.hpp b/src/DI/alloc_functor.hpp new file mode 100644 index 0000000..ea40b22 --- /dev/null +++ b/src/DI/alloc_functor.hpp @@ -0,0 +1,84 @@ +#pragma once + +#include "DI/allocation.hpp" +#include "DI/compressed_pair.hpp" + +#include +#include +#include + +template +struct InvokeReturnWrapper +{ + template + static auto call(Args &&...args) -> Return; +}; + +/** + * Holds a functor and a allocator. + */ +template +class AllocFunctor; + +template +class AllocFunctor +{ +public: + using Target = Function; + using Alloc = Allocator; + + explicit AllocFunctor(Target &&function); + + explicit AllocFunctor(const Target &function, const Alloc &allocator); + + explicit AllocFunctor(const Target &function, Alloc &&allocator); + + explicit AllocFunctor(Target &&function, Alloc &&allocator); + + auto operator()(Args &&...args) -> Return; + + [[nodiscard]] auto target() const -> const Target &; + + [[nodiscard]] auto get_allocator() const -> const Alloc &; + + [[nodiscard]] auto clone() const -> AllocFunctor *; + + void destroy() noexcept; + + static void destroy_and_delete(AllocFunctor *functor); + +private: + CompressedPair _function; +}; + +/** + * Holds a functor and a allocator. + */ +template +class DefaultAllocFunctor; + +template +class DefaultAllocFunctor +{ +public: + using Target = Function; + + explicit DefaultAllocFunctor(Target &&function); + + explicit DefaultAllocFunctor(const Target &function); + + auto operator()(Args &&...args) -> Return; + + auto target() const -> const Target &; + + auto clone() const -> DefaultAllocFunctor *; + + void destroy() noexcept; + + static void destroy_and_delete(DefaultAllocFunctor *function); + +private: + Function _function; +}; + +#include "alloc_functor.tpp" -- cgit v1.2.3-18-g5258