aboutsummaryrefslogtreecommitdiff
path: root/src/DI/copyable_functor.tpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/DI/copyable_functor.tpp')
-rw-r--r--src/DI/copyable_functor.tpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/DI/copyable_functor.tpp b/src/DI/copyable_functor.tpp
new file mode 100644
index 0000000..9895397
--- /dev/null
+++ b/src/DI/copyable_functor.tpp
@@ -0,0 +1,105 @@
+#pragma once
+
+#include "copyable_functor.hpp"
+
+#include <memory>
+#include <utility>
+
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define COPYABLE_FUNCTOR_TEMPLATE \
+ template <class Function, class Allocator, class Return, class... Args>
+
+// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
+#define COPYABLE_FUNCTOR CopyableFunctor<Function, Allocator, Return(Args...)>
+
+COPYABLE_FUNCTOR_TEMPLATE
+COPYABLE_FUNCTOR::CopyableFunctor(Function &&function) : _functor(std::move(function)) {}
+
+COPYABLE_FUNCTOR_TEMPLATE
+COPYABLE_FUNCTOR::CopyableFunctor(const Function &function, const Allocator &allocator)
+ : _functor(function, allocator)
+{
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+COPYABLE_FUNCTOR::CopyableFunctor(const Function &function, Allocator &&allocator)
+ : _functor(function, std::move(allocator))
+{
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+COPYABLE_FUNCTOR::CopyableFunctor(Function &&function, Allocator &&allocator)
+ : _functor(std::move(function), std::move(allocator))
+{
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+auto COPYABLE_FUNCTOR::operator()(Args &&...args) -> Return
+{
+ return _functor(std::forward<Args>(args)...);
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+auto COPYABLE_FUNCTOR::clone() const -> ICopyableFunctor<Return(Args...)> *
+{
+ using AllocTraits = std::allocator_traits<Allocator>;
+
+ using AllocHelper = typename RebindAllocHelper<AllocTraits, CopyableFunctor>::type;
+
+ auto alloc_helper = AllocHelper(_functor.get_allocator());
+
+ using Destructor = AllocDestructor<AllocHelper>;
+
+ auto hold = std::unique_ptr<CopyableFunctor, Destructor>(alloc_helper.allocate(1),
+ Destructor(alloc_helper, 1));
+
+ ::new (static_cast<void *>(hold.get()))
+ CopyableFunctor(_functor.target(), Allocator(alloc_helper));
+
+ return hold.release();
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+void COPYABLE_FUNCTOR::clone(ICopyableFunctor<Return(Args...)> *functor) const
+{
+ ::new (static_cast<void *>(functor))
+ CopyableFunctor(_functor.target(), _functor.get_allocator());
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+void COPYABLE_FUNCTOR::destroy() noexcept
+{
+ _functor.destroy();
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+void COPYABLE_FUNCTOR::destroy_deallocate() noexcept
+{
+ using AllocTraits = std::allocator_traits<Allocator>;
+
+ using AllocHelper = typename RebindAllocHelper<AllocTraits, CopyableFunctor>::type;
+
+ auto alloc_helper = AllocHelper(_functor.get_allocator());
+
+ _functor.destroy();
+
+ alloc_helper.deallocate(this, 1);
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+auto COPYABLE_FUNCTOR::target(const std::type_info &type_info) const noexcept -> const
+ void *
+{
+ if (type_info == typeid(Function))
+ {
+ return std::addressof(_functor.target());
+ }
+
+ return nullptr;
+}
+
+COPYABLE_FUNCTOR_TEMPLATE
+auto COPYABLE_FUNCTOR::target_type() const noexcept -> const std::type_info &
+{
+ return typeid(Function);
+}