diff options
Diffstat (limited to 'include/yacppdic/detail/internal/functor/alloc_functor.hpp')
-rw-r--r-- | include/yacppdic/detail/internal/functor/alloc_functor.hpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/include/yacppdic/detail/internal/functor/alloc_functor.hpp b/include/yacppdic/detail/internal/functor/alloc_functor.hpp new file mode 100644 index 0000000..687f182 --- /dev/null +++ b/include/yacppdic/detail/internal/functor/alloc_functor.hpp @@ -0,0 +1,89 @@ +#pragma once + +#include "yacppdic/detail/internal/alloc_destructor.hpp" +#include "yacppdic/detail/internal/compressed_pair.hpp" + +#include <memory> +#include <type_traits> +#include <utility> + +namespace yacppdic::internal +{ + +template <typename Return> +struct InvokeReturnWrapper +{ + template <typename... Args> + static auto call(Args &&...args) -> Return; +}; + +/** + * Holds a functor and a allocator. + */ +template <class Function, class Allocator, class FunctionSignature> +class AllocFunctor; + +template <class Function, class Allocator, class Return, class... Args> +class AllocFunctor<Function, Allocator, Return(Args...)> +{ +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, Allocator> _function; +}; + +/** + * Holds a functor and a allocator. + */ +template <class Function, class FB> +class DefaultAllocFunctor; + +template <class Function, class Return, class... Args> +class DefaultAllocFunctor<Function, Return(Args...)> +{ +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; +}; + +} // namespace yacppdic::internal + +#include "alloc_functor-impl.hpp" |