Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ template <class R, class... Args, size_t MaxSize> class Function<R(Args...), Max

Function(const Function &other) {
if (other) {
other.manager(data, other.data, Operation::Clone);
other.manager(&data, &other.data, Operation::Clone);
invoker = other.invoker;
manager = other.manager;
}
}

Function(Function &other) : Function(const_cast<const Function&>(other)) {}

Function(Function &&other) { other.swap(*this); }

template <class F> Function(F &&f) {
Expand Down Expand Up @@ -108,7 +110,7 @@ template <class R, class... Args, size_t MaxSize> class Function<R(Args...), Max
enum class Operation { Clone, Destroy };

using Invoker = R (*)(void *, Args &&...);
using Manager = void (*)(void *, void *, Operation);
using Manager = void (*)(void *, const void *, Operation);
using Storage = typename std::aligned_storage<MaxSize - sizeof(Invoker) - sizeof(Manager), 8>::type;

template <typename F>
Expand All @@ -118,10 +120,10 @@ template <class R, class... Args, size_t MaxSize> class Function<R(Args...), Max
}

template <typename F>
static void manage(void *dest, void *src, Operation op) {
static void manage(void *dest, const void *src, Operation op) {
switch (op) {
case Operation::Clone:
new (dest) F(*static_cast<F *>(src));
new (dest) F(*static_cast<const F *>(src));
break;
case Operation::Destroy:
static_cast<F *>(dest)->~F();
Expand Down