82 lines
1.2 KiB
C++
82 lines
1.2 KiB
C++
// License: Modified MIT (NON-AI)
|
|
// See the LICENSE file in the root directory for license information.
|
|
// Copyright 2026 Timothy Prepscius
|
|
|
|
#pragma once
|
|
|
|
#include <tjp/core/future/Future.hpp>
|
|
#include <tjp/core/ptr/Ptr.hpp>
|
|
#include <tjp/core/ptr/strong_of.hpp>
|
|
#include <tjp/core/ptr/strong_emplace.hpp>
|
|
|
|
namespace tjp::core {
|
|
namespace futures::v2 {
|
|
|
|
struct FutureErase
|
|
{
|
|
StrongPtr<Future<void>> future;
|
|
|
|
FutureErase()
|
|
{}
|
|
|
|
template<typename T>
|
|
FutureErase(Future<T> &&future_)
|
|
{
|
|
set(std::move(future_));
|
|
}
|
|
|
|
~FutureErase()
|
|
{
|
|
clear();
|
|
}
|
|
|
|
template<typename T>
|
|
void set(Future<T> &&future_)
|
|
{
|
|
strong_emplace(future);
|
|
|
|
*future = future_.then([future_ = weak(future)](auto &&f){
|
|
if (auto future = strong(future_))
|
|
(*future) = {};
|
|
});
|
|
|
|
if (future->is_set())
|
|
future = {};
|
|
}
|
|
|
|
template<typename T>
|
|
void operator =(Future<T> &&future_)
|
|
{
|
|
return set(std::move(future_));
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
future.reset();
|
|
}
|
|
|
|
bool has_value () const
|
|
{
|
|
return future && future->valid();
|
|
}
|
|
|
|
bool empty() const
|
|
{
|
|
return !has_value();
|
|
}
|
|
|
|
Future<void> get_future()
|
|
{
|
|
auto future_ = future;
|
|
return future_ ?
|
|
*future_ : future_of_value();
|
|
}
|
|
} ;
|
|
|
|
} // namespace
|
|
|
|
using FutureErase = futures::v2::FutureErase;
|
|
|
|
} // namespace
|
|
|