+ ~Task()
+ {
+ this->clear();
+ }
+
+ Task(Task const&) = delete;
+
+ Task(Task&& that)
+ {
+ if (that.vtable_ && that.vtable_->move)
+ that.vtable_->move(buffer_, that.buffer_);
+ else
+ std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
+ vtable_ = that.vtable_;
+ that.vtable_ = nullptr;
+ }
+ Task& operator=(Task that)
+ {
+ this->clear();
+ if (that.vtable_ && that.vtable_->move)
+ that.vtable_->move(buffer_, that.buffer_);
+ else
+ std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
+ vtable_ = that.vtable_;
+ that.vtable_ = nullptr;
+ return *this;
+ }
+
+private:
+
+ template<class F>
+ typename std::enable_if<canSBO<F>()>::type
+ init(F code)
+ {
+ const static TaskVtable vtable {
+ // Call:
+ [](TaskUnion& buffer, Args... args) -> R {
+ F* src = reinterpret_cast<F*>(&buffer);
+ F code = std::move(*src);
+ src->~F();
+ code(std::forward<Args>(args)...);
+ },
+ // Destroy:
+ std::is_trivially_destructible<F>::value ?
+ static_cast<destroy_function>(nullptr) :
+ [](TaskUnion& buffer) {
+ F* code = reinterpret_cast<F*>(&buffer);
+ code->~F();
+ },
+ // Move:
+ [](TaskUnion& dst, TaskUnion& src) {
+ F* src_code = reinterpret_cast<F*>(&src);
+ F* dst_code = reinterpret_cast<F*>(&dst);
+ new(dst_code) F(std::move(*src_code));
+ src_code->~F();
+ }
+ };
+ new(&buffer_) F(std::move(code));
+ vtable_ = &vtable;
+ }