flatten 20260225
This commit is contained in:
9
tjp/core/runnable/Runnable.h
Normal file
9
tjp/core/runnable/Runnable.h
Normal file
@@ -0,0 +1,9 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
struct Endable;
|
||||
|
||||
} // namespace
|
||||
78
tjp/core/runnable/Runnable.hpp
Normal file
78
tjp/core/runnable/Runnable.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Runnable.h"
|
||||
|
||||
#include <tjp/core/ptr/Ptr.hpp>
|
||||
#include <tjp/core/future/Future.hpp>
|
||||
#include <tjp/core/containers/Optional.hpp>
|
||||
|
||||
#include <tjp/core/future/Future.h>
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
struct Endable;
|
||||
|
||||
struct Runnable : StrongThis<Runnable>
|
||||
{
|
||||
using End = Function<void(Runnable &)>;
|
||||
|
||||
Runnable(End &&end) :
|
||||
end_(std::move(end))
|
||||
{}
|
||||
|
||||
Function<void(Runnable &)> end_;
|
||||
|
||||
bool end = false;
|
||||
Promise<int> ended;
|
||||
|
||||
Endable getEndable();
|
||||
} ;
|
||||
|
||||
struct Endable
|
||||
{
|
||||
StrongPtr<Runnable> 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<int> get_future()
|
||||
{
|
||||
return runnable->ended.get_future();
|
||||
}
|
||||
} ;
|
||||
|
||||
inline
|
||||
Endable Runnable::getEndable()
|
||||
{
|
||||
return Endable {
|
||||
strong_this(this)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user