// TJP COPYRIGHT HEADER #pragma once #include "Runnable.h" #include #include #include #include namespace tjp::core { struct Endable; struct Runnable : StrongThis { using End = Function; Runnable(End &&end) : end_(std::move(end)) {} Function end_; bool end = false; Promise ended; Endable getEndable(); } ; struct Endable { StrongPtr runnable; bool endOnDestruct_ = false; ~Endable() { if (endOnDestruct_) { end(); get_future().wait(); } } void endOnDestruct() { endOnDestruct_ = true; } void end() { runnable->end = true; if (runnable->end_) { runnable->end_(*runnable); runnable->end_ = {}; } } Future get_future() { return runnable->ended.get_future(); } } ; inline Endable Runnable::getEndable() { return Endable { strong_this(this) }; } } // namespace