Compare commits
6 Commits
3891c0f8ce
...
9ae424e968
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9ae424e968 | ||
![]() |
5a111df9ea | ||
![]() |
32ccaad00a | ||
![]() |
10d9b4c98f | ||
![]() |
cc20702249 | ||
![]() |
7d6fcc60fc |
@ -5,10 +5,6 @@
|
||||
#define MIJIN_ASYNC_COROUTINE_HPP_INCLUDED 1
|
||||
|
||||
|
||||
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
|
||||
# define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0 // Capture stack each time a coroutine is started. Warning, expensive! // TODO: maybe implement a lighter version only storing the return address?
|
||||
#endif
|
||||
|
||||
#include <any>
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
@ -27,6 +23,15 @@
|
||||
#include "../util/misc.hpp"
|
||||
#include "../util/scope_guard.hpp"
|
||||
#include "../util/traits.hpp"
|
||||
|
||||
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
|
||||
# define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0 // Capture stack each time a coroutine is started. Warning, expensive! // TODO: maybe implement a lighter version only storing the return address?
|
||||
#endif
|
||||
|
||||
#if !defined(MIJIN_COROUTINE_ENABLE_EXCEPTIONS)
|
||||
# define MIJIN_COROUTINE_ENABLE_EXCEPTIONS 0
|
||||
#endif
|
||||
|
||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||
#include "../debug/stacktrace.hpp"
|
||||
#endif
|
||||
@ -186,10 +191,16 @@ struct TaskReturn<void, TPromise>
|
||||
};
|
||||
}
|
||||
|
||||
template<typename TValue>
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using TaskFuture = Future<TValue, TAllocator, MIJIN_COROUTINE_ENABLE_EXCEPTIONS>;
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using TaskFuturePtr = FuturePtr<TValue, TAllocator, MIJIN_COROUTINE_ENABLE_EXCEPTIONS>;
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator>
|
||||
struct TaskAwaitableFuture
|
||||
{
|
||||
FuturePtr<TValue> future;
|
||||
TaskFuturePtr<TValue, TAllocator> future;
|
||||
|
||||
[[nodiscard]] constexpr bool await_ready() const MIJIN_NOEXCEPT { return future->ready(); }
|
||||
constexpr void await_suspend(std::coroutine_handle<>) const MIJIN_NOEXCEPT {}
|
||||
@ -329,12 +340,12 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
||||
|
||||
// constexpr void unhandled_exception() MIJIN_NOEXCEPT {}
|
||||
|
||||
template<typename TValue>
|
||||
auto await_transform(FuturePtr<TValue> future, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||
template<typename TValue, template<typename> typename TAllocator>
|
||||
auto await_transform(TaskFuturePtr<TValue, TAllocator> future, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(loop_ != nullptr, "Cannot await future outside of a loop!");
|
||||
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||
TaskAwaitableFuture<TValue> awaitable{future};
|
||||
TaskAwaitableFuture<TValue, TAllocator> awaitable{future};
|
||||
if (!awaitable.await_ready())
|
||||
{
|
||||
state_.status = TaskStatus::WAITING;
|
||||
@ -635,10 +646,10 @@ public:
|
||||
void setUncaughtExceptionHandler(exception_handler_t handler) MIJIN_NOEXCEPT { uncaughtExceptionHandler_ = std::move(handler); }
|
||||
|
||||
template<typename TResult>
|
||||
FuturePtr<TResult> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
||||
TaskFuturePtr<TResult, TAllocator> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
||||
|
||||
template<typename TResult>
|
||||
FuturePtr<TResult> addTask(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle = nullptr) MIJIN_NOEXCEPT
|
||||
TaskFuturePtr<TResult, TAllocator> addTask(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle = nullptr) MIJIN_NOEXCEPT
|
||||
{
|
||||
static_assert(TaskAllocatorTraits<TAllocator>::default_is_valid_v, "Allocator is not valid when default constructed, use makeTask() instead.");
|
||||
return addTaskImpl(std::move(task), outHandle);
|
||||
@ -664,7 +675,7 @@ protected:
|
||||
protected:
|
||||
static inline TaskLoop*& currentLoopStorage() MIJIN_NOEXCEPT;
|
||||
template<typename TResult>
|
||||
static inline void setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT;
|
||||
static inline void setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT_IF(!MIJIN_COROUTINE_ENABLE_EXCEPTIONS);
|
||||
};
|
||||
|
||||
template<typename TResult = void>
|
||||
@ -811,12 +822,12 @@ TaskBase<TResult, TAllocator>::~TaskBase() MIJIN_NOEXCEPT
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
template<typename TResult>
|
||||
FuturePtr<TResult> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT
|
||||
TaskFuturePtr<TResult, TAllocator> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(!task.getLoop(), "Attempting to add task that already has a loop!");
|
||||
task.setLoop(this);
|
||||
|
||||
FuturePtr<TResult> future = std::allocate_shared<Future<TResult>>(TAllocator<Future<TResult>>(allocator_), allocator_);
|
||||
TaskFuturePtr<TResult, TAllocator> future = std::allocate_shared<TaskFuture<TResult, TAllocator>>(TAllocator<Future<TResult, TAllocator>>(allocator_), allocator_);
|
||||
auto setFuture = &setFutureHelper<TResult>;
|
||||
|
||||
if (outHandle != nullptr)
|
||||
@ -855,7 +866,7 @@ TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
||||
while (status == TaskStatus::RUNNING);
|
||||
impl::gCurrentTaskState = nullptr;
|
||||
|
||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING && !MIJIN_COROUTINE_ENABLE_EXCEPTIONS
|
||||
if (task.task && task.task->exception())
|
||||
{
|
||||
try
|
||||
@ -883,7 +894,22 @@ TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
||||
#endif // MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||
if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED)
|
||||
{
|
||||
task.setFuture(task);
|
||||
try
|
||||
{
|
||||
task.setFuture(task);
|
||||
}
|
||||
catch(TaskCancelled&) {}
|
||||
catch(...)
|
||||
{
|
||||
if (uncaughtExceptionHandler_)
|
||||
{
|
||||
uncaughtExceptionHandler_(std::current_exception());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@ -910,10 +936,22 @@ template<template<typename> typename TAllocator>
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
template<typename TResult>
|
||||
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT
|
||||
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT_IF(!MIJIN_COROUTINE_ENABLE_EXCEPTIONS)
|
||||
{
|
||||
TaskBase<TResult, TAllocator>& task = *static_cast<TaskBase<TResult, TAllocator>*>(storedTask.task->raw());
|
||||
auto future = std::any_cast<FuturePtr<TResult>>(storedTask.resultData);
|
||||
const auto& future = std::any_cast<TaskFuturePtr<TResult, TAllocator>&>(storedTask.resultData);
|
||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTIONS
|
||||
if (task.state().exception)
|
||||
{
|
||||
if (future.use_count() < 2)
|
||||
{
|
||||
// future has been discarded, but someone must handle the exception
|
||||
std::rethrow_exception(task.state().exception);
|
||||
}
|
||||
future->setException(task.state().exception);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if constexpr (!std::is_same_v<TResult, void>)
|
||||
{
|
||||
@ -1249,14 +1287,14 @@ inline TaskAwaitableSuspend c_suspend() {
|
||||
return TaskAwaitableSuspend();
|
||||
}
|
||||
|
||||
template<template<typename...> typename TCollection, typename TType, typename... TTemplateArgs>
|
||||
Task<> c_allDone(const TCollection<FuturePtr<TType>, TTemplateArgs...>& futures)
|
||||
template<template<typename...> typename TCollection, FutureType TFuture, typename... TTemplateArgs>
|
||||
Task<> c_allDone(const TCollection<TFuture, TTemplateArgs...>& futures)
|
||||
{
|
||||
bool allDone = true;
|
||||
do
|
||||
{
|
||||
allDone = true;
|
||||
for (const FuturePtr<TType>& future : futures)
|
||||
for (const TFuture& future : futures)
|
||||
{
|
||||
if (future && !future->ready()) {
|
||||
allDone = false;
|
||||
|
@ -4,6 +4,7 @@
|
||||
#if !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
|
||||
#define MIJIN_ASYNC_FUTURE_HPP_INCLUDED 1
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <tuple>
|
||||
@ -27,43 +28,113 @@ namespace mijin
|
||||
//
|
||||
// public types
|
||||
//
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, bool exceptions = false>
|
||||
class Future;
|
||||
|
||||
// TODO: add support for mutexes and waiting for futures
|
||||
namespace impl
|
||||
{
|
||||
template<typename TValue>
|
||||
template<typename TValue, bool exceptions>
|
||||
struct FutureStorage
|
||||
{
|
||||
Optional<TValue> value;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return !value.empty();
|
||||
}
|
||||
|
||||
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
||||
[[nodiscard]] TValue& getValue() MIJIN_NOEXCEPT { return value.get(); }
|
||||
|
||||
};
|
||||
|
||||
// template<typename TValue>
|
||||
// struct FutureStorage<TValue&>
|
||||
// {
|
||||
// Optional<TValue*> value;
|
||||
//
|
||||
// void setValue(TValue& value_) MIJIN_NOEXCEPT { value = &value_; }
|
||||
// [[nodiscard]] TValue& getValue() const MIJIN_NOEXCEPT { return *value.get(); }
|
||||
// };
|
||||
template<>
|
||||
struct FutureStorage<void, false>
|
||||
{
|
||||
bool isSet = false;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return isSet;
|
||||
}
|
||||
void setValue() MIJIN_NOEXCEPT
|
||||
{
|
||||
isSet = true;
|
||||
}
|
||||
void getValue() MIJIN_NOEXCEPT {}
|
||||
};
|
||||
|
||||
#if MIJIN_ENABLE_EXCEPTIONS
|
||||
template<typename TValue>
|
||||
struct FutureStorage<TValue, true>
|
||||
{
|
||||
Optional<TValue> value;
|
||||
std::exception_ptr exception;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
if (exception) {
|
||||
return true;
|
||||
}
|
||||
return !value.empty();
|
||||
}
|
||||
|
||||
void setException(std::exception_ptr exc) MIJIN_NOEXCEPT
|
||||
{
|
||||
exception = std::move(exc);
|
||||
}
|
||||
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
||||
[[nodiscard]] TValue& getValue()
|
||||
{
|
||||
if (exception) {
|
||||
std::rethrow_exception(exception);
|
||||
}
|
||||
return value.get();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<>
|
||||
struct FutureStorage<void>
|
||||
struct FutureStorage<void, true>
|
||||
{
|
||||
bool isSet = false;
|
||||
std::exception_ptr exception;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
if (exception) {
|
||||
return true;
|
||||
}
|
||||
return isSet;
|
||||
}
|
||||
void setException(std::exception_ptr exc) MIJIN_NOEXCEPT
|
||||
{
|
||||
exception = std::move(exc);
|
||||
}
|
||||
void setValue() MIJIN_NOEXCEPT
|
||||
{
|
||||
isSet = true;
|
||||
}
|
||||
void getValue()
|
||||
{
|
||||
if (exception) {
|
||||
std::rethrow_exception(exception);
|
||||
}
|
||||
}
|
||||
};
|
||||
#endif
|
||||
} // namespace impl
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator>
|
||||
template<typename TValue, template<typename> typename TAllocator, bool exceptions>
|
||||
class Future
|
||||
{
|
||||
private:
|
||||
[[no_unique_address]] impl::FutureStorage<TValue> value_;
|
||||
bool isSet_ = false;
|
||||
impl::FutureStorage<TValue, exceptions> value_;
|
||||
public:
|
||||
Future() = default;
|
||||
Future(const Future&) = delete;
|
||||
@ -80,10 +151,12 @@ public:
|
||||
constexpr bool operator!() const MIJIN_NOEXCEPT { return !ready(); }
|
||||
public: // access
|
||||
[[nodiscard]]
|
||||
constexpr decltype(auto) get() MIJIN_NOEXCEPT
|
||||
constexpr decltype(auto) get() MIJIN_NOEXCEPT_IF(!exceptions)
|
||||
{
|
||||
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>) {
|
||||
MIJIN_ASSERT(ready(), "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>)
|
||||
{
|
||||
value_.getValue(); // in case of exceptions
|
||||
return;
|
||||
}
|
||||
else {
|
||||
@ -91,10 +164,12 @@ public: // access
|
||||
}
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr decltype(auto) get() const MIJIN_NOEXCEPT
|
||||
constexpr decltype(auto) get() const MIJIN_NOEXCEPT_IF(!exceptions)
|
||||
{
|
||||
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>) {
|
||||
MIJIN_ASSERT(ready(), "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>)
|
||||
{
|
||||
value_.getValue(); // in case of exceptions
|
||||
return;
|
||||
}
|
||||
else {
|
||||
@ -104,22 +179,22 @@ public: // access
|
||||
[[nodiscard]]
|
||||
constexpr bool ready() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return isSet_;
|
||||
return value_.hasValue();
|
||||
}
|
||||
public: // modification
|
||||
template<typename TArg> requires (!std::is_same_v<TValue, void>)
|
||||
constexpr void set(TArg&& value) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
|
||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
||||
value_.setValue(std::move(value));
|
||||
isSet_ = true;
|
||||
sigSet.emit();
|
||||
}
|
||||
constexpr void set() MIJIN_NOEXCEPT requires (std::is_same_v<TValue, void>)
|
||||
{
|
||||
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
|
||||
isSet_ = true;
|
||||
if constexpr (std::is_same_v<TValue, void>) {
|
||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
||||
if constexpr (std::is_same_v<TValue, void>)
|
||||
{
|
||||
value_.setValue();
|
||||
sigSet.emit();
|
||||
}
|
||||
else {
|
||||
@ -127,12 +202,38 @@ public: // modification
|
||||
MIJIN_ERROR("Attempting to call set(void) on future with value.");
|
||||
}
|
||||
}
|
||||
constexpr void setException(std::exception_ptr exception) requires (exceptions)
|
||||
{
|
||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
||||
if constexpr (exceptions)
|
||||
{
|
||||
value_.setException(std::move(exception));
|
||||
}
|
||||
}
|
||||
public: // signals
|
||||
BaseSignal<TAllocator> sigSet;
|
||||
};
|
||||
|
||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, bool exceptions = false>
|
||||
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator, exceptions>>;
|
||||
|
||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator>>;
|
||||
using ExceptFuture = Future<TValue, TAllocator, true>;
|
||||
|
||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using ExceptFuturePtr = std::shared_ptr<Future<TValue, TAllocator, true>>;
|
||||
|
||||
template<typename T>
|
||||
struct is_future : std::false_type {};
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator, bool exceptions>
|
||||
struct is_future<Future<TValue, TAllocator, exceptions>> : std::true_type {};
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_future_t = is_future<T>::value;
|
||||
|
||||
template<typename T>
|
||||
concept FutureType = is_future<T>::value;
|
||||
|
||||
//
|
||||
// public functions
|
||||
|
@ -133,7 +133,8 @@ public:
|
||||
VectorMap& operator=(VectorMap&&) = default;
|
||||
auto operator<=>(const VectorMap& other) const noexcept = default;
|
||||
|
||||
TValue& operator[](const TKey& key)
|
||||
template<typename TIndex>
|
||||
TValue& operator[](const TIndex& key)
|
||||
{
|
||||
auto it = find(key);
|
||||
if (it != end())
|
||||
@ -143,7 +144,8 @@ public:
|
||||
return emplace(key, TValue()).first->second;
|
||||
}
|
||||
|
||||
const TValue& operator[](const TKey& key) const
|
||||
template<typename TIndex>
|
||||
const TValue& operator[](const TIndex& key) const
|
||||
{
|
||||
return at(key);
|
||||
}
|
||||
@ -251,8 +253,9 @@ public:
|
||||
return eraseImpl(idx, count);
|
||||
}
|
||||
|
||||
template<typename TSearch>
|
||||
[[nodiscard]]
|
||||
iterator find(const TKey& key) MIJIN_NOEXCEPT
|
||||
iterator find(const TSearch& key) MIJIN_NOEXCEPT
|
||||
{
|
||||
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
||||
{
|
||||
@ -264,8 +267,9 @@ public:
|
||||
return end();
|
||||
}
|
||||
|
||||
template<typename TSearch>
|
||||
[[nodiscard]]
|
||||
const_iterator find(const TKey& key) const MIJIN_NOEXCEPT
|
||||
const_iterator find(const TSearch& key) const MIJIN_NOEXCEPT
|
||||
{
|
||||
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
||||
{
|
||||
|
@ -122,7 +122,7 @@ bool initDbgHelp() MIJIN_NOEXCEPT
|
||||
return false;
|
||||
}
|
||||
|
||||
const int result = std::atexit(&cleanupDbgHelp);
|
||||
[[maybe_unused]] const int result = std::atexit(&cleanupDbgHelp);
|
||||
MIJIN_ASSERT(result == 0, "Error registering DbgHelp cleanup handler.");
|
||||
|
||||
// only copy in the end so we can still figure out if initialization was successful
|
||||
|
@ -290,6 +290,29 @@ mijin::Task<StreamError> Stream::c_readLine(std::string& outString)
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
StreamError Stream::copyTo(Stream& other)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().read, "Stream must support reading.");
|
||||
MIJIN_ASSERT(other.getFeatures().write, "Other stream must support writing.");
|
||||
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
if (const StreamError error = other.writeRaw(chunk.data(), bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
if (handle) {
|
||||
@ -441,7 +464,7 @@ StreamFeatures FileStream::getFeatures()
|
||||
if (handle)
|
||||
{
|
||||
return {
|
||||
.read = (mode == FileOpenMode::READ),
|
||||
.read = (mode == FileOpenMode::READ || mode == FileOpenMode::READ_WRITE),
|
||||
.write = (mode == FileOpenMode::WRITE || mode == FileOpenMode::APPEND || mode == FileOpenMode::READ_WRITE),
|
||||
.tell = true,
|
||||
.seek = true,
|
||||
|
@ -275,6 +275,8 @@ public:
|
||||
|
||||
StreamError getTotalLength(std::size_t& outLength);
|
||||
|
||||
StreamError copyTo(Stream& otherStream);
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||
|
||||
@ -544,7 +546,7 @@ inline void throwOnError(mijin::StreamError error)
|
||||
if (error == mijin::StreamError::SUCCESS) {
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error(errorName(error));
|
||||
throw Exception(errorName(error));
|
||||
}
|
||||
|
||||
inline void throwOnError(mijin::StreamError error, std::string message)
|
||||
@ -552,7 +554,7 @@ inline void throwOnError(mijin::StreamError error, std::string message)
|
||||
if (error == mijin::StreamError::SUCCESS) {
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error(message + ": " + errorName(error));
|
||||
throw Exception(message + ": " + errorName(error));
|
||||
}
|
||||
|
||||
template<typename TSuccess>
|
||||
|
@ -60,6 +60,32 @@ public:
|
||||
[[nodiscard]] const TError& getError() const MIJIN_NOEXCEPT { return std::get<TError>(state_); }
|
||||
};
|
||||
|
||||
namespace impl
|
||||
{
|
||||
struct ResultSuccess {};
|
||||
}
|
||||
|
||||
template<typename TError>
|
||||
class ResultBase<void, TError> : public ResultBase<impl::ResultSuccess, TError>
|
||||
{
|
||||
public:
|
||||
ResultBase() MIJIN_NOEXCEPT : ResultBase<impl::ResultSuccess, TError>(impl::ResultSuccess{}) {}
|
||||
ResultBase(const ResultBase&) MIJIN_NOEXCEPT = default;
|
||||
ResultBase(ResultBase&&) MIJIN_NOEXCEPT = default;
|
||||
ResultBase(TError errorValue) MIJIN_NOEXCEPT : ResultBase<impl::ResultSuccess, TError>(std::move(errorValue)) {}
|
||||
|
||||
ResultBase& operator=(const ResultBase&) = default;
|
||||
ResultBase& operator=(ResultBase&&) = default;
|
||||
|
||||
impl::ResultSuccess& operator*() MIJIN_NOEXCEPT = delete;
|
||||
const impl::ResultSuccess& operator*() const MIJIN_NOEXCEPT = delete;
|
||||
impl::ResultSuccess* operator->() MIJIN_NOEXCEPT = delete;
|
||||
const impl::ResultSuccess* operator->() const MIJIN_NOEXCEPT = delete;
|
||||
|
||||
[[nodiscard]] impl::ResultSuccess& getValue() MIJIN_NOEXCEPT = delete;
|
||||
[[nodiscard]] const impl::ResultSuccess& getValue() const MIJIN_NOEXCEPT = delete;
|
||||
};
|
||||
|
||||
struct ResultError
|
||||
{
|
||||
std::string message;
|
||||
@ -73,7 +99,7 @@ struct ResultError
|
||||
ResultError& operator=(ResultError&&) MIJIN_NOEXCEPT = default;
|
||||
};
|
||||
|
||||
template<typename TSuccess>
|
||||
template<typename TSuccess = void>
|
||||
using Result = ResultBase<TSuccess, ResultError>;
|
||||
|
||||
//
|
||||
|
Loading…
x
Reference in New Issue
Block a user