Merge branch 'master' of https://git.mewin.de/mewin/mijin2
This commit is contained in:
commit
d8b03893b3
50
source/mijin/async/boxed_signal.hpp
Normal file
50
source/mijin/async/boxed_signal.hpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_ASYNC_BOXED_SIGNAL_HPP_INCLUDED)
|
||||||
|
#define MIJIN_ASYNC_BOXED_SIGNAL_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "./signal.hpp"
|
||||||
|
#include "../container/boxed_object.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
|
||||||
|
//
|
||||||
|
// public defines
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// public constants
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// public types
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
|
class BaseBoxedSignal : private BoxedObject<BaseSignal<TAllocator, TArgs...>>
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
using base_t = BoxedObject<BaseSignal<TAllocator, TArgs...>>;
|
||||||
|
public:
|
||||||
|
using base_t::construct;
|
||||||
|
using base_t::destroy;
|
||||||
|
using base_t::moveTo;
|
||||||
|
|
||||||
|
MIJIN_BOXED_PROXY_FUNC(connect)
|
||||||
|
MIJIN_BOXED_PROXY_FUNC(disconnect)
|
||||||
|
MIJIN_BOXED_PROXY_FUNC(emit)
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename... TArgs>
|
||||||
|
using BoxedSignal = BaseBoxedSignal<MIJIN_DEFAULT_ALLOCATOR, TArgs...>;
|
||||||
|
|
||||||
|
//
|
||||||
|
// public functions
|
||||||
|
//
|
||||||
|
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_ASYNC_BOXED_SIGNAL_HPP_INCLUDED)
|
||||||
@ -26,205 +26,15 @@ namespace mijin
|
|||||||
|
|
||||||
namespace impl
|
namespace impl
|
||||||
{
|
{
|
||||||
thread_local TaskLoop::StoredTask* gCurrentTask = nullptr;
|
thread_local std::shared_ptr<TaskSharedState> gCurrentTaskState;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// internal functions
|
// internal functions
|
||||||
//
|
//
|
||||||
|
|
||||||
void MultiThreadedTaskLoop::managerThread(std::stop_token stopToken) // NOLINT(performance-unnecessary-value-param)
|
|
||||||
{
|
|
||||||
setCurrentThreadName("Task Manager");
|
|
||||||
|
|
||||||
while (!stopToken.stop_requested())
|
|
||||||
{
|
|
||||||
// first clear out any parked tasks that are actually finished
|
|
||||||
auto itRem = std::remove_if(parkedTasks_.begin(), parkedTasks_.end(), [](StoredTask& task) {
|
|
||||||
return !task.task || task.task->status() == TaskStatus::FINISHED;
|
|
||||||
});
|
|
||||||
parkedTasks_.erase(itRem, parkedTasks_.end());
|
|
||||||
|
|
||||||
// then try to push any task from the buffer into the queue, if possible
|
|
||||||
for (auto it = parkedTasks_.begin(); it != parkedTasks_.end();)
|
|
||||||
{
|
|
||||||
if (!it->task->canResume())
|
|
||||||
{
|
|
||||||
++it;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (readyTasks_.tryPushMaybeMove(*it)) {
|
|
||||||
it = parkedTasks_.erase(it);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// then clear the incoming task queue
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
std::optional<StoredTask> task = queuedTasks_.tryPop();
|
|
||||||
if (!task.has_value()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to directly move it into the next queue
|
|
||||||
if (readyTasks_.tryPushMaybeMove(*task)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise park it
|
|
||||||
parkedTasks_.push_back(std::move(*task));
|
|
||||||
}
|
|
||||||
|
|
||||||
// next collect tasks returning from the worker threads
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
std::optional<StoredTask> task = returningTasks_.tryPop();
|
|
||||||
if (!task.has_value()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task->task == nullptr || task->task->status() == TaskStatus::FINISHED) {
|
|
||||||
continue; // task has been transferred or finished
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task->task->canResume() && readyTasks_.tryPushMaybeMove(*task)) {
|
|
||||||
continue; // instantly resume, no questions asked
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise park it for future processing
|
|
||||||
parkedTasks_.push_back(std::move(*task));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MultiThreadedTaskLoop::workerThread(std::stop_token stopToken, std::size_t workerId) // NOLINT(performance-unnecessary-value-param)
|
|
||||||
{
|
|
||||||
currentLoopStorage() = this; // forever (on this thread)
|
|
||||||
|
|
||||||
std::array<char, 16> threadName;
|
|
||||||
(void) std::snprintf(threadName.data(), 16, "Task Worker %lu", static_cast<unsigned long>(workerId));
|
|
||||||
setCurrentThreadName(threadName.data());
|
|
||||||
|
|
||||||
while (!stopToken.stop_requested())
|
|
||||||
{
|
|
||||||
// try to fetch a task to run
|
|
||||||
std::optional<StoredTask> task = readyTasks_.tryPop();
|
|
||||||
if (!task.has_value())
|
|
||||||
{
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// run it
|
|
||||||
impl::gCurrentTask = &*task;
|
|
||||||
tickTask(*task);
|
|
||||||
impl::gCurrentTask = nullptr;
|
|
||||||
|
|
||||||
// and give it back
|
|
||||||
returningTasks_.push(std::move(*task));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
void SimpleTaskLoop::transferCurrentTask(TaskLoop& otherLoop) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
assertCorrectThread();
|
|
||||||
|
|
||||||
if (&otherLoop == this) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MIJIN_ASSERT_FATAL(currentTask_ != tasks_.end(), "Trying to call transferCurrentTask() while not running a task!");
|
|
||||||
|
|
||||||
// now start the transfer, first disown the task
|
|
||||||
StoredTask storedTask = std::move(*currentTask_);
|
|
||||||
currentTask_->task = nullptr; // just to be sure
|
|
||||||
|
|
||||||
// then send it over to the other loop
|
|
||||||
otherLoop.addStoredTask(std::move(storedTask));
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleTaskLoop::addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
storedTask.task->setLoop(this);
|
|
||||||
if (threadId_ == std::thread::id() || threadId_ == std::this_thread::get_id())
|
|
||||||
{
|
|
||||||
// same thread, just copy it over
|
|
||||||
if (currentLoopStorage() != nullptr) {
|
|
||||||
// currently running, can't append to tasks_ directly
|
|
||||||
newTasks_.push_back(std::move(storedTask));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tasks_.push_back(std::move(storedTask));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// other thread, better be safe
|
|
||||||
queuedTasks_.push(std::move(storedTask));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t SimpleTaskLoop::getActiveTasks() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
std::size_t sum = 0;
|
|
||||||
for (const StoredTask& task : mijin::chain(tasks_, newTasks_))
|
|
||||||
{
|
|
||||||
const TaskStatus status = task.task ? task.task->status() : TaskStatus::FINISHED;
|
|
||||||
if (status == TaskStatus::SUSPENDED || status == TaskStatus::RUNNING)
|
|
||||||
{
|
|
||||||
++sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MultiThreadedTaskLoop::transferCurrentTask(TaskLoop& otherLoop) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
if (&otherLoop == this) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MIJIN_ASSERT_FATAL(impl::gCurrentTask != nullptr, "Trying to call transferCurrentTask() while not running a task!");
|
|
||||||
|
|
||||||
// now start the transfer, first disown the task
|
|
||||||
StoredTask storedTask = std::move(*impl::gCurrentTask);
|
|
||||||
impl::gCurrentTask->task = nullptr; // just to be sure
|
|
||||||
|
|
||||||
// then send it over to the other loop
|
|
||||||
otherLoop.addStoredTask(std::move(storedTask));
|
|
||||||
}
|
|
||||||
|
|
||||||
void MultiThreadedTaskLoop::addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
storedTask.task->setLoop(this);
|
|
||||||
|
|
||||||
// just assume we are not on the manager thread, as that wouldn't make sense
|
|
||||||
queuedTasks_.push(std::move(storedTask));
|
|
||||||
}
|
|
||||||
|
|
||||||
void MultiThreadedTaskLoop::start(std::size_t numWorkerThreads)
|
|
||||||
{
|
|
||||||
managerThread_ = std::jthread([this](std::stop_token stopToken) { managerThread(std::move(stopToken)); });
|
|
||||||
workerThreads_.reserve(numWorkerThreads);
|
|
||||||
for (std::size_t workerId = 0; workerId < numWorkerThreads; ++workerId) {
|
|
||||||
workerThreads_.emplace_back([this, workerId](std::stop_token stopToken) { workerThread(std::move(stopToken), workerId); });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MultiThreadedTaskLoop::stop()
|
|
||||||
{
|
|
||||||
workerThreads_.clear(); // will also set the stop token
|
|
||||||
managerThread_ = {}; // this too
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <any>
|
#include <any>
|
||||||
|
#include <chrono>
|
||||||
#include <coroutine>
|
#include <coroutine>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -20,8 +21,10 @@
|
|||||||
#include "./message_queue.hpp"
|
#include "./message_queue.hpp"
|
||||||
#include "../container/optional.hpp"
|
#include "../container/optional.hpp"
|
||||||
#include "../internal/common.hpp"
|
#include "../internal/common.hpp"
|
||||||
|
#include "../memory/memutil.hpp"
|
||||||
#include "../util/flag.hpp"
|
#include "../util/flag.hpp"
|
||||||
#include "../util/iterators.hpp"
|
#include "../util/iterators.hpp"
|
||||||
|
#include "../util/misc.hpp"
|
||||||
#include "../util/traits.hpp"
|
#include "../util/traits.hpp"
|
||||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||||
#include "../debug/stacktrace.hpp"
|
#include "../debug/stacktrace.hpp"
|
||||||
@ -63,9 +66,10 @@ enum class TaskStatus
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct TaskState;
|
struct TaskState;
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
class TaskLoop;
|
class TaskLoop;
|
||||||
|
|
||||||
template<typename TResult = void>
|
template<typename TResult = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
class TaskBase;
|
class TaskBase;
|
||||||
|
|
||||||
#if MIJIN_COROUTINE_ENABLE_CANCEL
|
#if MIJIN_COROUTINE_ENABLE_CANCEL
|
||||||
@ -103,6 +107,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void cancel() const MIJIN_NOEXCEPT;
|
inline void cancel() const MIJIN_NOEXCEPT;
|
||||||
|
[[nodiscard]] inline Optional<std::source_location> getLocation() const MIJIN_NOEXCEPT;
|
||||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||||
inline Optional<Stacktrace> getCreationStack() const MIJIN_NOEXCEPT;
|
inline Optional<Stacktrace> getCreationStack() const MIJIN_NOEXCEPT;
|
||||||
#endif
|
#endif
|
||||||
@ -111,6 +116,7 @@ struct TaskSharedState
|
|||||||
{
|
{
|
||||||
std::atomic_bool cancelled_ = false;
|
std::atomic_bool cancelled_ = false;
|
||||||
TaskHandle subTask;
|
TaskHandle subTask;
|
||||||
|
std::source_location sourceLoc;
|
||||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||||
Stacktrace creationStack_;
|
Stacktrace creationStack_;
|
||||||
#endif
|
#endif
|
||||||
@ -245,16 +251,68 @@ struct TaskAwaitableSuspend
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TTraits>
|
namespace impl
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
using default_is_valid = T::default_is_valid_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
struct TaskAllocatorTraits
|
||||||
|
{
|
||||||
|
static constexpr bool default_is_valid_v = detect_or_t<std::true_type, impl::default_is_valid, TAllocator<void>>::value;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static TAllocator<T> create()
|
||||||
|
{
|
||||||
|
auto taskLoop = TaskLoop<TAllocator>::currentOpt();
|
||||||
|
if (taskLoop != nullptr)
|
||||||
|
{
|
||||||
|
return TAllocator<T>(taskLoop->getAllocator());
|
||||||
|
}
|
||||||
|
if constexpr (std::is_default_constructible_v<TAllocator<T>>)
|
||||||
|
{
|
||||||
|
return TAllocator<T>();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MIJIN_FATAL("Could not create task allocator.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct TaskAllocatorTraits<std::allocator>
|
||||||
|
{
|
||||||
|
static constexpr bool default_is_valid_v = true;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::allocator<T> create() noexcept
|
||||||
|
{
|
||||||
|
return std::allocator<T>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator, typename T>
|
||||||
|
TAllocator<T> makeTaskAllocator()
|
||||||
|
{
|
||||||
|
return TaskAllocatorTraits<TAllocator>::template create<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TTraits, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TTraits>>
|
struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TTraits>>
|
||||||
{
|
{
|
||||||
using handle_t = std::coroutine_handle<TaskPromise>;
|
using handle_t = std::coroutine_handle<TaskPromise>;
|
||||||
using task_t = typename TTraits::task_t;
|
using task_t = typename TTraits::task_t;
|
||||||
using result_t = typename TTraits::result_t;
|
using result_t = typename TTraits::result_t;
|
||||||
|
|
||||||
|
[[no_unique_address]] TAllocator<std::max_align_t> allocator_;
|
||||||
TaskState<result_t> state_;
|
TaskState<result_t> state_;
|
||||||
std::shared_ptr<TaskSharedState> sharedState_ = std::make_shared<TaskSharedState>();
|
std::shared_ptr<TaskSharedState> sharedState_;
|
||||||
TaskLoop* loop_ = nullptr;
|
TaskLoop<TAllocator>* loop_ = nullptr;
|
||||||
|
|
||||||
|
explicit TaskPromise(TAllocator<std::max_align_t> allocator = makeTaskAllocator<TAllocator, std::max_align_t>()) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TAllocator<TaskSharedState>>)
|
||||||
|
: allocator_(std::move(allocator)), sharedState_(std::allocate_shared<TaskSharedState>(TAllocator<TaskSharedState>(allocator_))) {}
|
||||||
|
|
||||||
constexpr task_t get_return_object() MIJIN_NOEXCEPT { return task_t(handle_t::from_promise(*this)); }
|
constexpr task_t get_return_object() MIJIN_NOEXCEPT { return task_t(handle_t::from_promise(*this)); }
|
||||||
constexpr TaskAwaitableSuspend initial_suspend() MIJIN_NOEXCEPT { return {}; }
|
constexpr TaskAwaitableSuspend initial_suspend() MIJIN_NOEXCEPT { return {}; }
|
||||||
@ -271,9 +329,10 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
|||||||
// constexpr void unhandled_exception() MIJIN_NOEXCEPT {}
|
// constexpr void unhandled_exception() MIJIN_NOEXCEPT {}
|
||||||
|
|
||||||
template<typename TValue>
|
template<typename TValue>
|
||||||
auto await_transform(FuturePtr<TValue> future) MIJIN_NOEXCEPT
|
auto await_transform(FuturePtr<TValue> future, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(loop_ != nullptr, "Cannot await future outside of a loop!");
|
MIJIN_ASSERT(loop_ != nullptr, "Cannot await future outside of a loop!");
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
TaskAwaitableFuture<TValue> awaitable{future};
|
TaskAwaitableFuture<TValue> awaitable{future};
|
||||||
if (!awaitable.await_ready())
|
if (!awaitable.await_ready())
|
||||||
{
|
{
|
||||||
@ -287,17 +346,18 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename TResultOther>
|
template<typename TResultOther>
|
||||||
auto await_transform(TaskBase<TResultOther> task) MIJIN_NOEXCEPT
|
auto await_transform(TaskBase<TResultOther> task, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(loop_ != nullptr, "Cannot await another task outside of a loop!"); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult)
|
MIJIN_ASSERT(loop_ != nullptr, "Cannot await another task outside of a loop!"); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult)
|
||||||
auto future = delayEvaluation<TResultOther>(loop_)->addTask(std::move(task), &sharedState_->subTask); // hackidyhack: delay evaluation of the type of loop_ as it is only forward-declared here
|
auto future = delayEvaluation<TResultOther>(loop_)->addTaskImpl(std::move(task), &sharedState_->subTask); // hackidyhack: delay evaluation of the type of loop_ as it is only forward-declared here
|
||||||
return await_transform(future);
|
return await_transform(future, std::move(sourceLoc));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TFirstArg, typename TSecondArg, typename... TArgs>
|
template<typename TFirstArg, typename TSecondArg, typename... TArgs>
|
||||||
auto await_transform(Signal<TFirstArg, TSecondArg, TArgs...>& signal) MIJIN_NOEXCEPT
|
auto await_transform(Signal<TFirstArg, TSecondArg, TArgs...>& signal, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
auto data = std::make_shared<std::tuple<TFirstArg, TSecondArg, TArgs...>>();
|
auto data = std::make_shared<std::tuple<TFirstArg, TSecondArg, TArgs...>>();
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
signal.connect([this, data](TFirstArg arg0, TSecondArg arg1, TArgs... args) mutable
|
signal.connect([this, data](TFirstArg arg0, TSecondArg arg1, TArgs... args) mutable
|
||||||
{
|
{
|
||||||
*data = std::make_tuple(std::move(arg0), std::move(arg1), std::move(args)...);
|
*data = std::make_tuple(std::move(arg0), std::move(arg1), std::move(args)...);
|
||||||
@ -309,9 +369,10 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename TFirstArg>
|
template<typename TFirstArg>
|
||||||
auto await_transform(Signal<TFirstArg>& signal) MIJIN_NOEXCEPT
|
auto await_transform(Signal<TFirstArg>& signal, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
auto data = std::make_shared<TFirstArg>();
|
auto data = std::make_shared<TFirstArg>();
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
signal.connect([this, data](TFirstArg arg0) mutable
|
signal.connect([this, data](TFirstArg arg0) mutable
|
||||||
{
|
{
|
||||||
*data = std::move(arg0);
|
*data = std::move(arg0);
|
||||||
@ -322,8 +383,9 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
|||||||
return awaitable;
|
return awaitable;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto await_transform(Signal<>& signal) MIJIN_NOEXCEPT
|
auto await_transform(Signal<>& signal, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
signal.connect([this]()
|
signal.connect([this]()
|
||||||
{
|
{
|
||||||
state_.status = TaskStatus::SUSPENDED;
|
state_.status = TaskStatus::SUSPENDED;
|
||||||
@ -333,24 +395,39 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
|||||||
return awaitable;
|
return awaitable;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::suspend_always await_transform(std::suspend_always) MIJIN_NOEXCEPT
|
std::suspend_always await_transform(std::suspend_always, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
state_.status = TaskStatus::SUSPENDED;
|
state_.status = TaskStatus::SUSPENDED;
|
||||||
return std::suspend_always();
|
return std::suspend_always();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::suspend_never await_transform(std::suspend_never) MIJIN_NOEXCEPT {
|
std::suspend_never await_transform(std::suspend_never, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT {
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
return std::suspend_never();
|
return std::suspend_never();
|
||||||
}
|
}
|
||||||
|
|
||||||
TaskAwaitableSuspend await_transform(TaskAwaitableSuspend) MIJIN_NOEXCEPT
|
TaskAwaitableSuspend await_transform(TaskAwaitableSuspend, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
state_.status = TaskStatus::SUSPENDED;
|
state_.status = TaskStatus::SUSPENDED;
|
||||||
return TaskAwaitableSuspend();
|
return TaskAwaitableSuspend();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure the allocators are also used for the promise itself
|
||||||
|
void* operator new(std::size_t size)
|
||||||
|
{
|
||||||
|
return makeTaskAllocator<TAllocator, std::max_align_t>().allocate((size - 1) / sizeof(std::max_align_t) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void* ptr, std::size_t size) noexcept
|
||||||
|
{
|
||||||
|
TaskPromise* self = static_cast<TaskPromise*>(ptr);
|
||||||
|
self->allocator_.deallocate(static_cast<std::max_align_t*>(ptr), (size - 1) / sizeof(std::max_align_t) + 1);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TResult>
|
template<typename TResult, template<typename> typename TAllocator>
|
||||||
class [[nodiscard("Tasks should either we awaited or added to a loop.")]] TaskBase
|
class [[nodiscard("Tasks should either we awaited or added to a loop.")]] TaskBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -362,7 +439,7 @@ public:
|
|||||||
using result_t = TResult;
|
using result_t = TResult;
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
using promise_type = TaskPromise<Traits>;
|
using promise_type = TaskPromise<Traits, TAllocator>;
|
||||||
using handle_t = typename promise_type::handle_t;
|
using handle_t = typename promise_type::handle_t;
|
||||||
private:
|
private:
|
||||||
handle_t handle_;
|
handle_t handle_;
|
||||||
@ -415,11 +492,11 @@ private:
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr handle_t handle() const MIJIN_NOEXCEPT { return handle_; }
|
constexpr handle_t handle() const MIJIN_NOEXCEPT { return handle_; }
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr TaskLoop* getLoop() MIJIN_NOEXCEPT
|
constexpr TaskLoop<TAllocator>* getLoop() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return handle_.promise().loop_;
|
return handle_.promise().loop_;
|
||||||
}
|
}
|
||||||
constexpr void setLoop(TaskLoop* loop) MIJIN_NOEXCEPT
|
constexpr void setLoop(TaskLoop<TAllocator>* loop) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
// MIJIN_ASSERT(handle_.promise().loop_ == nullptr
|
// MIJIN_ASSERT(handle_.promise().loop_ == nullptr
|
||||||
// || handle_.promise().loop_ == loop
|
// || handle_.promise().loop_ == loop
|
||||||
@ -427,12 +504,24 @@ private:
|
|||||||
handle_.promise().loop_ = loop;
|
handle_.promise().loop_ = loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend class TaskLoop;
|
friend class TaskLoop<TAllocator>;
|
||||||
|
|
||||||
template<typename TTask>
|
template<typename TTask, template<typename> typename TAllocator2>
|
||||||
friend class WrappedTask;
|
friend class WrappedTask;
|
||||||
};
|
};
|
||||||
|
template<typename T>
|
||||||
|
struct is_task : std::false_type {};
|
||||||
|
|
||||||
|
template<typename TResult, template<typename...> typename TAllocator>
|
||||||
|
struct is_task<TaskBase<TResult, TAllocator>> : std::true_type {};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool is_task_v = is_task<T>::value;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept task_type = is_task_v<T>;
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
class WrappedTaskBase
|
class WrappedTaskBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -444,7 +533,7 @@ public:
|
|||||||
virtual void resume() = 0;
|
virtual void resume() = 0;
|
||||||
virtual void* raw() MIJIN_NOEXCEPT = 0;
|
virtual void* raw() MIJIN_NOEXCEPT = 0;
|
||||||
virtual std::coroutine_handle<> handle() MIJIN_NOEXCEPT = 0;
|
virtual std::coroutine_handle<> handle() MIJIN_NOEXCEPT = 0;
|
||||||
virtual void setLoop(TaskLoop* loop) MIJIN_NOEXCEPT = 0;
|
virtual void setLoop(TaskLoop<TAllocator>* loop) MIJIN_NOEXCEPT = 0;
|
||||||
virtual std::shared_ptr<TaskSharedState>& sharedState() MIJIN_NOEXCEPT = 0;
|
virtual std::shared_ptr<TaskSharedState>& sharedState() MIJIN_NOEXCEPT = 0;
|
||||||
|
|
||||||
[[nodiscard]] inline bool canResume() {
|
[[nodiscard]] inline bool canResume() {
|
||||||
@ -453,8 +542,8 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TTask>
|
template<typename TTask, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
class WrappedTask : public WrappedTaskBase
|
class WrappedTask : public WrappedTaskBase<TAllocator>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
TTask task_;
|
TTask task_;
|
||||||
@ -480,57 +569,95 @@ public:
|
|||||||
void resume() override { task_.resume(); }
|
void resume() override { task_.resume(); }
|
||||||
void* raw() MIJIN_NOEXCEPT override { return &task_; }
|
void* raw() MIJIN_NOEXCEPT override { return &task_; }
|
||||||
std::coroutine_handle<> handle() MIJIN_NOEXCEPT override { return task_.handle(); }
|
std::coroutine_handle<> handle() MIJIN_NOEXCEPT override { return task_.handle(); }
|
||||||
void setLoop(TaskLoop* loop) MIJIN_NOEXCEPT override { task_.setLoop(loop); }
|
void setLoop(TaskLoop<TAllocator>* loop) MIJIN_NOEXCEPT override { task_.setLoop(loop); }
|
||||||
virtual std::shared_ptr<TaskSharedState>& sharedState() MIJIN_NOEXCEPT override { return task_.sharedState(); }
|
virtual std::shared_ptr<TaskSharedState>& sharedState() MIJIN_NOEXCEPT override { return task_.sharedState(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TTask>
|
template<typename TTask, template<typename> typename TAllocator>
|
||||||
std::unique_ptr<WrappedTask<TTask>> wrapTask(TTask&& task) MIJIN_NOEXCEPT
|
auto wrapTask(TAllocator<WrappedTask<TTask, TAllocator>> allocator, TTask&& task)
|
||||||
{
|
{
|
||||||
return std::make_unique<WrappedTask<TTask>>(std::forward<TTask>(task));
|
using wrapped_task_t = WrappedTask<TTask, TAllocator>;
|
||||||
|
using deleter_t = AllocatorDeleter<TAllocator<wrapped_task_t>>;
|
||||||
|
using allocator_t = TAllocator<wrapped_task_t>;
|
||||||
|
|
||||||
|
wrapped_task_t* ptr = ::new (allocator.allocate(1)) wrapped_task_t(std::forward<TTask>(task));
|
||||||
|
return std::unique_ptr<wrapped_task_t, deleter_t>(ptr, AllocatorDeleter<allocator_t>(std::move(allocator)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
class TaskLoop
|
class TaskLoop
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MIJIN_DEFINE_FLAG(CanContinue);
|
MIJIN_DEFINE_FLAG(CanContinue);
|
||||||
MIJIN_DEFINE_FLAG(IgnoreWaiting);
|
MIJIN_DEFINE_FLAG(IgnoreWaiting);
|
||||||
|
|
||||||
using wrapped_task_t = WrappedTaskBase;
|
using wrapped_task_t = WrappedTaskBase<TAllocator>;
|
||||||
using wrapped_task_base_ptr_t = std::unique_ptr<wrapped_task_t>;
|
using wrapped_allocator_t = TAllocator<wrapped_task_t>;
|
||||||
|
using wrapped_deleter_t = AllocatorDeleter<wrapped_allocator_t >;
|
||||||
|
using wrapped_task_base_ptr_t = std::unique_ptr<wrapped_task_t, wrapped_deleter_t>;
|
||||||
struct StoredTask
|
struct StoredTask
|
||||||
{
|
{
|
||||||
|
using set_future_t = std::function<void(StoredTask&)>;
|
||||||
wrapped_task_base_ptr_t task;
|
wrapped_task_base_ptr_t task;
|
||||||
std::function<void(StoredTask&)> setFuture;
|
set_future_t setFuture;
|
||||||
std::any resultData;
|
std::any resultData;
|
||||||
|
|
||||||
|
StoredTask(wrapped_task_base_ptr_t&& task_, set_future_t&& setFuture_, std::any&& resultData_)
|
||||||
|
: task(std::move(task_)), setFuture(std::move(setFuture_)), resultData(std::move(resultData_)) {}
|
||||||
|
template<typename T>
|
||||||
|
StoredTask(TAllocator<T> allocator_) : task(nullptr, wrapped_deleter_t(wrapped_allocator_t(allocator_))) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
using exception_handler_t = std::function<void(std::exception_ptr)>;
|
using exception_handler_t = std::function<void(std::exception_ptr)>;
|
||||||
|
using allocator_t = TAllocator<void>;
|
||||||
protected:
|
protected:
|
||||||
using task_vector_t = std::vector<StoredTask>;
|
using task_vector_t = std::vector<StoredTask, TAllocator<StoredTask>>;
|
||||||
|
|
||||||
template<typename TTask>
|
template<typename TTask>
|
||||||
using wrapped_task_ptr_t = std::unique_ptr<WrappedTask<TTask>>;
|
using wrapped_task_ptr_t = std::unique_ptr<WrappedTask<TTask>>;
|
||||||
|
|
||||||
exception_handler_t uncaughtExceptionHandler_;
|
exception_handler_t uncaughtExceptionHandler_;
|
||||||
|
[[no_unique_address]] allocator_t allocator_;
|
||||||
public:
|
public:
|
||||||
TaskLoop() MIJIN_NOEXCEPT = default;
|
explicit TaskLoop(allocator_t allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||||
|
: allocator_(std::move(allocator)) {};
|
||||||
TaskLoop(const TaskLoop&) = delete;
|
TaskLoop(const TaskLoop&) = delete;
|
||||||
TaskLoop(TaskLoop&&) = delete;
|
TaskLoop(TaskLoop&&) = delete;
|
||||||
virtual ~TaskLoop() MIJIN_NOEXCEPT = default;
|
virtual ~TaskLoop() MIJIN_NOEXCEPT = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const allocator_t& getAllocator() const MIJIN_NOEXCEPT { return allocator_; }
|
||||||
|
|
||||||
TaskLoop& operator=(const TaskLoop&) = delete;
|
TaskLoop& operator=(const TaskLoop&) = delete;
|
||||||
TaskLoop& operator=(TaskLoop&&) = delete;
|
TaskLoop& operator=(TaskLoop&&) = delete;
|
||||||
|
|
||||||
void setUncaughtExceptionHandler(exception_handler_t handler) MIJIN_NOEXCEPT { uncaughtExceptionHandler_ = std::move(handler); }
|
void setUncaughtExceptionHandler(exception_handler_t handler) MIJIN_NOEXCEPT { uncaughtExceptionHandler_ = std::move(handler); }
|
||||||
|
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
inline FuturePtr<TResult> addTask(TaskBase<TResult> task, TaskHandle* outHandle = nullptr) MIJIN_NOEXCEPT;
|
FuturePtr<TResult> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
template<typename TResult>
|
||||||
|
FuturePtr<TResult> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TCoro, typename... TArgs>
|
||||||
|
auto makeTask(TCoro&& coro, TaskHandle& outHandle, TArgs&&... args) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
template<typename TCoro, typename... TArgs>
|
||||||
|
auto makeTask(TCoro&& coro, TArgs&&... args) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
TaskHandle dummy;
|
||||||
|
return makeTask(std::forward<TCoro>(coro), dummy, std::forward<TArgs>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
virtual void transferCurrentTask(TaskLoop& otherLoop) MIJIN_NOEXCEPT = 0;
|
virtual void transferCurrentTask(TaskLoop& otherLoop) MIJIN_NOEXCEPT = 0;
|
||||||
virtual void addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT = 0;
|
virtual void addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT = 0;
|
||||||
|
|
||||||
[[nodiscard]] static TaskLoop& current() MIJIN_NOEXCEPT;
|
[[nodiscard]] static TaskLoop& current() MIJIN_NOEXCEPT;
|
||||||
|
[[nodiscard]] static TaskLoop* currentOpt() MIJIN_NOEXCEPT;
|
||||||
protected:
|
protected:
|
||||||
inline TaskStatus tickTask(StoredTask& task);
|
inline TaskStatus tickTask(StoredTask& task);
|
||||||
protected:
|
protected:
|
||||||
@ -542,17 +669,29 @@ protected:
|
|||||||
template<typename TResult = void>
|
template<typename TResult = void>
|
||||||
using Task = TaskBase<TResult>;
|
using Task = TaskBase<TResult>;
|
||||||
|
|
||||||
class SimpleTaskLoop : public TaskLoop
|
template<template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
|
class BaseSimpleTaskLoop : public TaskLoop<TAllocator>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
using base_t = TaskLoop<TAllocator>;
|
||||||
|
using typename TaskLoop<TAllocator>::task_vector_t;
|
||||||
|
using typename TaskLoop<TAllocator>::allocator_t;
|
||||||
|
using typename TaskLoop<TAllocator>::StoredTask;
|
||||||
|
using typename TaskLoop<TAllocator>::CanContinue;
|
||||||
|
using typename TaskLoop<TAllocator>::IgnoreWaiting;
|
||||||
|
|
||||||
|
using base_t::allocator_;
|
||||||
task_vector_t tasks_;
|
task_vector_t tasks_;
|
||||||
task_vector_t newTasks_;
|
task_vector_t newTasks_;
|
||||||
task_vector_t::iterator currentTask_;
|
task_vector_t::iterator currentTask_;
|
||||||
MessageQueue<StoredTask> queuedTasks_;
|
MessageQueue<StoredTask> queuedTasks_;
|
||||||
std::thread::id threadId_;
|
std::thread::id threadId_;
|
||||||
|
public:
|
||||||
|
explicit BaseSimpleTaskLoop(const allocator_t& allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<allocator_t>)
|
||||||
|
: base_t(std::move(allocator)), tasks_(TAllocator<StoredTask>(allocator_)), newTasks_(TAllocator<StoredTask>(allocator_)),
|
||||||
|
queuedTasks_(constructArray<StoredTask, MessageQueue<StoredTask>::BUFFER_SIZE>(allocator_)) {}
|
||||||
public: // TaskLoop implementation
|
public: // TaskLoop implementation
|
||||||
void transferCurrentTask(TaskLoop& otherLoop) MIJIN_NOEXCEPT override;
|
void transferCurrentTask(TaskLoop<TAllocator>& otherLoop) MIJIN_NOEXCEPT override;
|
||||||
void addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT override;
|
void addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT override;
|
||||||
|
|
||||||
public: // public interface
|
public: // public interface
|
||||||
@ -562,23 +701,39 @@ public: // public interface
|
|||||||
inline CanContinue tick();
|
inline CanContinue tick();
|
||||||
inline void runUntilDone(IgnoreWaiting ignoreWaiting = IgnoreWaiting::NO);
|
inline void runUntilDone(IgnoreWaiting ignoreWaiting = IgnoreWaiting::NO);
|
||||||
inline void cancelAllTasks() MIJIN_NOEXCEPT;
|
inline void cancelAllTasks() MIJIN_NOEXCEPT;
|
||||||
[[nodiscard]] inline std::vector<TaskHandle> getAllTasks() const MIJIN_NOEXCEPT;
|
[[nodiscard]] inline std::vector<TaskHandle, TAllocator<TaskHandle>> getAllTasks() const MIJIN_NOEXCEPT;
|
||||||
private:
|
private:
|
||||||
inline void assertCorrectThread() { MIJIN_ASSERT(threadId_ == std::thread::id() || threadId_ == std::this_thread::get_id(), "Unsafe to TaskLoop from different thread!"); }
|
inline void assertCorrectThread() { MIJIN_ASSERT(threadId_ == std::thread::id() || threadId_ == std::this_thread::get_id(), "Unsafe to TaskLoop from different thread!"); }
|
||||||
};
|
};
|
||||||
|
using SimpleTaskLoop = BaseSimpleTaskLoop<>;
|
||||||
|
|
||||||
class MultiThreadedTaskLoop : public TaskLoop
|
template<template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
|
class BaseMultiThreadedTaskLoop : public TaskLoop<TAllocator>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
using base_t = TaskLoop<TAllocator>;
|
||||||
|
using typename base_t::task_vector_t;
|
||||||
|
using typename base_t::allocator_t;
|
||||||
|
using typename base_t::StoredTask;
|
||||||
|
|
||||||
|
using base_t::allocator_;
|
||||||
task_vector_t parkedTasks_; // buffer for tasks that don't fit into readyTasks_
|
task_vector_t parkedTasks_; // buffer for tasks that don't fit into readyTasks_
|
||||||
MessageQueue<StoredTask> queuedTasks_; // tasks that should be appended to parked tasks
|
MessageQueue<StoredTask> queuedTasks_; // tasks that should be appended to parked tasks
|
||||||
MessageQueue<StoredTask> readyTasks_; // task queue to send tasks to a worker thread
|
MessageQueue<StoredTask> readyTasks_; // task queue to send tasks to a worker thread
|
||||||
MessageQueue<StoredTask> returningTasks_; // task that have executed on a worker thread and return for further processing
|
MessageQueue<StoredTask> returningTasks_; // task that have executed on a worker thread and return for further processing
|
||||||
std::jthread managerThread_;
|
std::jthread managerThread_;
|
||||||
std::vector<std::jthread> workerThreads_;
|
std::vector<std::jthread, TAllocator<std::jthread>> workerThreads_;
|
||||||
|
public:
|
||||||
|
explicit BaseMultiThreadedTaskLoop(allocator_t allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<allocator_t>)
|
||||||
|
: base_t(std::move(allocator)),
|
||||||
|
parkedTasks_(TAllocator<StoredTask>(allocator_)),
|
||||||
|
queuedTasks_(constructArray<StoredTask, MessageQueue<StoredTask>::BUFFER_SIZE>(allocator_)),
|
||||||
|
readyTasks_(constructArray<StoredTask, MessageQueue<StoredTask>::BUFFER_SIZE>(allocator_)),
|
||||||
|
returningTasks_(constructArray<StoredTask, MessageQueue<StoredTask>::BUFFER_SIZE>(allocator_)),
|
||||||
|
workerThreads_(TAllocator<std::jthread>(allocator_)) {}
|
||||||
|
|
||||||
public: // TaskLoop implementation
|
public: // TaskLoop implementation
|
||||||
void transferCurrentTask(TaskLoop& otherLoop) MIJIN_NOEXCEPT override;
|
void transferCurrentTask(TaskLoop<TAllocator>& otherLoop) MIJIN_NOEXCEPT override;
|
||||||
void addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT override;
|
void addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT override;
|
||||||
|
|
||||||
public: // public interface
|
public: // public interface
|
||||||
@ -587,7 +742,14 @@ public: // public interface
|
|||||||
private: // private stuff
|
private: // private stuff
|
||||||
void managerThread(std::stop_token stopToken);
|
void managerThread(std::stop_token stopToken);
|
||||||
void workerThread(std::stop_token stopToken, std::size_t workerId);
|
void workerThread(std::stop_token stopToken, std::size_t workerId);
|
||||||
|
|
||||||
|
static StoredTask*& getCurrentTask()
|
||||||
|
{
|
||||||
|
static thread_local StoredTask* task = nullptr;
|
||||||
|
return task;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
using MultiThreadedTaskLoop = BaseMultiThreadedTaskLoop<>;
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
@ -595,12 +757,12 @@ private: // private stuff
|
|||||||
|
|
||||||
namespace impl
|
namespace impl
|
||||||
{
|
{
|
||||||
extern thread_local TaskLoop::StoredTask* gCurrentTask;
|
extern thread_local std::shared_ptr<TaskSharedState> gCurrentTaskState;
|
||||||
|
|
||||||
inline void throwIfCancelled()
|
inline void throwIfCancelled()
|
||||||
{
|
{
|
||||||
#if MIJIN_COROUTINE_ENABLE_CANCEL
|
#if MIJIN_COROUTINE_ENABLE_CANCEL
|
||||||
if (gCurrentTask->task->sharedState()->cancelled_)
|
if (gCurrentTaskState->cancelled_)
|
||||||
{
|
{
|
||||||
throw TaskCancelled();
|
throw TaskCancelled();
|
||||||
}
|
}
|
||||||
@ -617,6 +779,15 @@ void TaskHandle::cancel() const MIJIN_NOEXCEPT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<std::source_location> TaskHandle::getLocation() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (std::shared_ptr<TaskSharedState> state = state_.lock())
|
||||||
|
{
|
||||||
|
return state->sourceLoc;
|
||||||
|
}
|
||||||
|
return NULL_OPTIONAL;
|
||||||
|
}
|
||||||
|
|
||||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||||
Optional<Stacktrace> TaskHandle::getCreationStack() const MIJIN_NOEXCEPT
|
Optional<Stacktrace> TaskHandle::getCreationStack() const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
@ -628,8 +799,8 @@ Optional<Stacktrace> TaskHandle::getCreationStack() const MIJIN_NOEXCEPT
|
|||||||
}
|
}
|
||||||
#endif // MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
#endif // MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||||
|
|
||||||
template<typename TResult>
|
template<typename TResult, template<typename> typename TAllocator>
|
||||||
TaskBase<TResult>::~TaskBase() MIJIN_NOEXCEPT
|
TaskBase<TResult, TAllocator>::~TaskBase() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (handle_)
|
if (handle_)
|
||||||
{
|
{
|
||||||
@ -637,13 +808,14 @@ TaskBase<TResult>::~TaskBase() MIJIN_NOEXCEPT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
inline FuturePtr<TResult> TaskLoop::addTask(TaskBase<TResult> task, TaskHandle* outHandle) MIJIN_NOEXCEPT
|
FuturePtr<TResult> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(!task.getLoop(), "Attempting to add task that already has a loop!");
|
MIJIN_ASSERT(!task.getLoop(), "Attempting to add task that already has a loop!");
|
||||||
task.setLoop(this);
|
task.setLoop(this);
|
||||||
|
|
||||||
auto future = std::make_shared<Future<TResult>>();
|
FuturePtr<TResult> future = std::allocate_shared<Future<TResult>>(TAllocator<Future<TResult>>(allocator_), allocator_);
|
||||||
auto setFuture = &setFutureHelper<TResult>;
|
auto setFuture = &setFutureHelper<TResult>;
|
||||||
|
|
||||||
if (outHandle != nullptr)
|
if (outHandle != nullptr)
|
||||||
@ -652,26 +824,35 @@ inline FuturePtr<TResult> TaskLoop::addTask(TaskBase<TResult> task, TaskHandle*
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add tasks to a seperate vector first as we might be running another task right now
|
// add tasks to a seperate vector first as we might be running another task right now
|
||||||
addStoredTask(StoredTask{
|
TAllocator<WrappedTask<TaskBase<TResult, TAllocator>>> allocator(allocator_);
|
||||||
.task = wrapTask(std::move(task)),
|
addStoredTask(StoredTask(wrapTask(std::move(allocator), std::move(task)), std::move(setFuture), future));
|
||||||
.setFuture = setFuture,
|
|
||||||
.resultData = future
|
|
||||||
});
|
|
||||||
|
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline TaskStatus TaskLoop::tickTask(StoredTask& task)
|
template<template<typename> typename TAllocator>
|
||||||
|
template<typename TCoro, typename... TArgs>
|
||||||
|
auto TaskLoop<TAllocator>::makeTask(TCoro&& coro, TaskHandle& outHandle, TArgs&&... args) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
TaskLoop<TAllocator>* previousLoop = currentLoopStorage();
|
||||||
|
currentLoopStorage() = this;
|
||||||
|
auto result = addTaskImpl(std::invoke(std::forward<TCoro>(coro), std::forward<TArgs>(args)...), &outHandle);
|
||||||
|
currentLoopStorage() = previousLoop;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
||||||
{
|
{
|
||||||
TaskStatus status = {};
|
TaskStatus status = {};
|
||||||
impl::gCurrentTask = &task;
|
impl::gCurrentTaskState = task.task->sharedState();
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
task.task->resume();
|
task.task->resume();
|
||||||
status = task.task ? task.task->status() : TaskStatus::WAITING; // no inner task -> task switch context (and will be removed later)
|
status = task.task ? task.task->status() : TaskStatus::WAITING; // no inner task -> task switch context (and will be removed later)
|
||||||
}
|
}
|
||||||
while (status == TaskStatus::RUNNING);
|
while (status == TaskStatus::RUNNING);
|
||||||
impl::gCurrentTask = nullptr;
|
impl::gCurrentTaskState = nullptr;
|
||||||
|
|
||||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||||
if (task.task && task.task->exception())
|
if (task.task && task.task->exception())
|
||||||
@ -706,22 +887,31 @@ inline TaskStatus TaskLoop::tickTask(StoredTask& task)
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */ inline auto TaskLoop::current() MIJIN_NOEXCEPT -> TaskLoop&
|
template<template<typename> typename TAllocator>
|
||||||
|
/* static */ inline auto TaskLoop<TAllocator>::current() MIJIN_NOEXCEPT -> TaskLoop&
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(currentLoopStorage() != nullptr, "Attempting to fetch current loop while no coroutine is running!");
|
MIJIN_ASSERT(currentLoopStorage() != nullptr, "Attempting to fetch current loop while no coroutine is running!");
|
||||||
return *currentLoopStorage();
|
return *currentLoopStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */ auto TaskLoop::currentLoopStorage() MIJIN_NOEXCEPT -> TaskLoop*&
|
template<template<typename> typename TAllocator>
|
||||||
|
/* static */ inline auto TaskLoop<TAllocator>::currentOpt() MIJIN_NOEXCEPT -> TaskLoop*
|
||||||
|
{
|
||||||
|
return currentLoopStorage();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
/* static */ auto TaskLoop<TAllocator>::currentLoopStorage() MIJIN_NOEXCEPT -> TaskLoop*&
|
||||||
{
|
{
|
||||||
static thread_local TaskLoop* storage = nullptr;
|
static thread_local TaskLoop* storage = nullptr;
|
||||||
return storage;
|
return storage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
/* static */ inline void TaskLoop::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT
|
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
TaskBase<TResult>& task = *static_cast<TaskBase<TResult>*>(storedTask.task->raw());
|
TaskBase<TResult, TAllocator>& task = *static_cast<TaskBase<TResult, TAllocator>*>(storedTask.task->raw());
|
||||||
auto future = std::any_cast<FuturePtr<TResult>>(storedTask.resultData);
|
auto future = std::any_cast<FuturePtr<TResult>>(storedTask.resultData);
|
||||||
|
|
||||||
if constexpr (!std::is_same_v<TResult, void>)
|
if constexpr (!std::is_same_v<TResult, void>)
|
||||||
@ -734,9 +924,10 @@ template<typename TResult>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::suspend_always switchContext(TaskLoop& taskLoop)
|
template<template<typename> typename TAllocator>
|
||||||
|
inline std::suspend_always switchContext(TaskLoop<TAllocator>& taskLoop)
|
||||||
{
|
{
|
||||||
TaskLoop& currentTaskLoop = TaskLoop::current();
|
TaskLoop<TAllocator>& currentTaskLoop = TaskLoop<TAllocator>::current();
|
||||||
if (¤tTaskLoop == &taskLoop) {
|
if (¤tTaskLoop == &taskLoop) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@ -744,11 +935,68 @@ inline std::suspend_always switchContext(TaskLoop& taskLoop)
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
inline auto SimpleTaskLoop::tick() -> CanContinue
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseSimpleTaskLoop<TAllocator>::transferCurrentTask(TaskLoop<TAllocator>& otherLoop) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
assertCorrectThread();
|
||||||
|
|
||||||
|
if (&otherLoop == this) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MIJIN_ASSERT_FATAL(currentTask_ != tasks_.end(), "Trying to call transferCurrentTask() while not running a task!");
|
||||||
|
|
||||||
|
// now start the transfer, first disown the task
|
||||||
|
StoredTask storedTask = std::move(*currentTask_);
|
||||||
|
currentTask_->task = nullptr; // just to be sure
|
||||||
|
|
||||||
|
// then send it over to the other loop
|
||||||
|
otherLoop.addStoredTask(std::move(storedTask));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseSimpleTaskLoop<TAllocator>::addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
storedTask.task->setLoop(this);
|
||||||
|
if (threadId_ == std::thread::id() || threadId_ == std::this_thread::get_id())
|
||||||
|
{
|
||||||
|
// same thread, just copy it over
|
||||||
|
if (TaskLoop<TAllocator>::currentLoopStorage() != nullptr) {
|
||||||
|
// currently running, can't append to tasks_ directly
|
||||||
|
newTasks_.push_back(std::move(storedTask));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tasks_.push_back(std::move(storedTask));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// other thread, better be safe
|
||||||
|
queuedTasks_.push(std::move(storedTask));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
std::size_t BaseSimpleTaskLoop<TAllocator>::getActiveTasks() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
std::size_t sum = 0;
|
||||||
|
for (const StoredTask& task : mijin::chain(tasks_, newTasks_))
|
||||||
|
{
|
||||||
|
const TaskStatus status = task.task ? task.task->status() : TaskStatus::FINISHED;
|
||||||
|
if (status == TaskStatus::SUSPENDED || status == TaskStatus::RUNNING)
|
||||||
|
{
|
||||||
|
++sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
inline auto BaseSimpleTaskLoop<TAllocator>::tick() -> CanContinue
|
||||||
{
|
{
|
||||||
// set current taskloop
|
// set current taskloop
|
||||||
MIJIN_ASSERT(currentLoopStorage() == nullptr, "Trying to tick a loop from a coroutine, this is not supported.");
|
MIJIN_ASSERT(TaskLoop<TAllocator>::currentLoopStorage() == nullptr, "Trying to tick a loop from a coroutine, this is not supported.");
|
||||||
currentLoopStorage() = this;
|
TaskLoop<TAllocator>::currentLoopStorage() = this;
|
||||||
threadId_ = std::this_thread::get_id();
|
threadId_ = std::this_thread::get_id();
|
||||||
|
|
||||||
// move over all tasks from newTasks
|
// move over all tasks from newTasks
|
||||||
@ -791,7 +1039,7 @@ inline auto SimpleTaskLoop::tick() -> CanContinue
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = tickTask(task);
|
status = base_t::tickTask(task);
|
||||||
|
|
||||||
if (status == TaskStatus::SUSPENDED || status == TaskStatus::YIELDED)
|
if (status == TaskStatus::SUSPENDED || status == TaskStatus::YIELDED)
|
||||||
{
|
{
|
||||||
@ -799,7 +1047,7 @@ inline auto SimpleTaskLoop::tick() -> CanContinue
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// reset current loop
|
// reset current loop
|
||||||
currentLoopStorage() = nullptr;
|
TaskLoop<TAllocator>::currentLoopStorage() = nullptr;
|
||||||
|
|
||||||
// remove any tasks that have been transferred to another queue
|
// remove any tasks that have been transferred to another queue
|
||||||
it = std::remove_if(tasks_.begin(), tasks_.end(), [](const StoredTask& task) {
|
it = std::remove_if(tasks_.begin(), tasks_.end(), [](const StoredTask& task) {
|
||||||
@ -810,7 +1058,8 @@ inline auto SimpleTaskLoop::tick() -> CanContinue
|
|||||||
return canContinue;
|
return canContinue;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void SimpleTaskLoop::runUntilDone(IgnoreWaiting ignoreWaiting)
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseSimpleTaskLoop<TAllocator>::runUntilDone(IgnoreWaiting ignoreWaiting)
|
||||||
{
|
{
|
||||||
while (!tasks_.empty() || !newTasks_.empty())
|
while (!tasks_.empty() || !newTasks_.empty())
|
||||||
{
|
{
|
||||||
@ -822,7 +1071,8 @@ inline void SimpleTaskLoop::runUntilDone(IgnoreWaiting ignoreWaiting)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void SimpleTaskLoop::cancelAllTasks() MIJIN_NOEXCEPT
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseSimpleTaskLoop<TAllocator>::cancelAllTasks() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
for (StoredTask& task : mijin::chain(tasks_, newTasks_))
|
for (StoredTask& task : mijin::chain(tasks_, newTasks_))
|
||||||
{
|
{
|
||||||
@ -835,9 +1085,10 @@ inline void SimpleTaskLoop::cancelAllTasks() MIJIN_NOEXCEPT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::vector<TaskHandle> SimpleTaskLoop::getAllTasks() const MIJIN_NOEXCEPT
|
template<template<typename> typename TAllocator>
|
||||||
|
std::vector<TaskHandle, TAllocator<TaskHandle>> BaseSimpleTaskLoop<TAllocator>::getAllTasks() const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::vector<TaskHandle> result;
|
std::vector<TaskHandle, TAllocator<TaskHandle>> result((TAllocator<TaskHandle>(TaskLoop<TAllocator>::allocator_)));
|
||||||
for (const StoredTask& task : mijin::chain(tasks_, newTasks_))
|
for (const StoredTask& task : mijin::chain(tasks_, newTasks_))
|
||||||
{
|
{
|
||||||
result.emplace_back(task.task->sharedState());
|
result.emplace_back(task.task->sharedState());
|
||||||
@ -845,6 +1096,151 @@ inline std::vector<TaskHandle> SimpleTaskLoop::getAllTasks() const MIJIN_NOEXCEP
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseMultiThreadedTaskLoop<TAllocator>::managerThread(std::stop_token stopToken) // NOLINT(performance-unnecessary-value-param)
|
||||||
|
{
|
||||||
|
// setCurrentThreadName("Task Manager");
|
||||||
|
|
||||||
|
while (!stopToken.stop_requested())
|
||||||
|
{
|
||||||
|
// first clear out any parked tasks that are actually finished
|
||||||
|
auto itRem = std::remove_if(parkedTasks_.begin(), parkedTasks_.end(), [](StoredTask& task) {
|
||||||
|
return !task.task || task.task->status() == TaskStatus::FINISHED;
|
||||||
|
});
|
||||||
|
parkedTasks_.erase(itRem, parkedTasks_.end());
|
||||||
|
|
||||||
|
// then try to push any task from the buffer into the queue, if possible
|
||||||
|
for (auto it = parkedTasks_.begin(); it != parkedTasks_.end();)
|
||||||
|
{
|
||||||
|
if (!it->task->canResume())
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (readyTasks_.tryPushMaybeMove(*it)) {
|
||||||
|
it = parkedTasks_.erase(it);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// then clear the incoming task queue
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::optional<StoredTask> task = queuedTasks_.tryPop();
|
||||||
|
if (!task.has_value()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to directly move it into the next queue
|
||||||
|
if (readyTasks_.tryPushMaybeMove(*task)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise park it
|
||||||
|
parkedTasks_.push_back(std::move(*task));
|
||||||
|
}
|
||||||
|
|
||||||
|
// next collect tasks returning from the worker threads
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::optional<StoredTask> task = returningTasks_.tryPop();
|
||||||
|
if (!task.has_value()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task->task == nullptr || task->task->status() == TaskStatus::FINISHED) {
|
||||||
|
continue; // task has been transferred or finished
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task->task->canResume() && readyTasks_.tryPushMaybeMove(*task)) {
|
||||||
|
continue; // instantly resume, no questions asked
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise park it for future processing
|
||||||
|
parkedTasks_.push_back(std::move(*task));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseMultiThreadedTaskLoop<TAllocator>::workerThread(std::stop_token stopToken, std::size_t workerId) // NOLINT(performance-unnecessary-value-param)
|
||||||
|
{
|
||||||
|
TaskLoop<TAllocator>::currentLoopStorage() = this; // forever (on this thread)
|
||||||
|
|
||||||
|
std::array<char, 16> threadName;
|
||||||
|
(void) std::snprintf(threadName.data(), 16, "Task Worker %lu", static_cast<unsigned long>(workerId));
|
||||||
|
// setCurrentThreadName(threadName.data());
|
||||||
|
|
||||||
|
while (!stopToken.stop_requested())
|
||||||
|
{
|
||||||
|
// try to fetch a task to run
|
||||||
|
std::optional<StoredTask> task = readyTasks_.tryPop();
|
||||||
|
if (!task.has_value())
|
||||||
|
{
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// run it
|
||||||
|
getCurrentTask() = &*task;
|
||||||
|
impl::gCurrentTaskState = task->task->sharedState();
|
||||||
|
tickTask(*task);
|
||||||
|
getCurrentTask() = nullptr;
|
||||||
|
impl::gCurrentTaskState = nullptr;
|
||||||
|
|
||||||
|
// and give it back
|
||||||
|
returningTasks_.push(std::move(*task));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseMultiThreadedTaskLoop<TAllocator>::transferCurrentTask(TaskLoop<TAllocator>& otherLoop) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (&otherLoop == this) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MIJIN_ASSERT_FATAL(getCurrentTask() != nullptr, "Trying to call transferCurrentTask() while not running a task!");
|
||||||
|
|
||||||
|
// now start the transfer, first disown the task
|
||||||
|
StoredTask storedTask = std::move(*getCurrentTask());
|
||||||
|
getCurrentTask()->task = nullptr; // just to be sure
|
||||||
|
|
||||||
|
// then send it over to the other loop
|
||||||
|
otherLoop.addStoredTask(std::move(storedTask));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseMultiThreadedTaskLoop<TAllocator>::addStoredTask(StoredTask&& storedTask) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
storedTask.task->setLoop(this);
|
||||||
|
|
||||||
|
// just assume we are not on the manager thread, as that wouldn't make sense
|
||||||
|
queuedTasks_.push(std::move(storedTask));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseMultiThreadedTaskLoop<TAllocator>::start(std::size_t numWorkerThreads)
|
||||||
|
{
|
||||||
|
managerThread_ = std::jthread([this](std::stop_token stopToken) { managerThread(std::move(stopToken)); });
|
||||||
|
workerThreads_.reserve(numWorkerThreads);
|
||||||
|
for (std::size_t workerId = 0; workerId < numWorkerThreads; ++workerId) {
|
||||||
|
workerThreads_.emplace_back([this, workerId](std::stop_token stopToken) { workerThread(std::move(stopToken), workerId); });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
void BaseMultiThreadedTaskLoop<TAllocator>::stop()
|
||||||
|
{
|
||||||
|
workerThreads_.clear(); // will also set the stop token
|
||||||
|
managerThread_ = {}; // this too
|
||||||
|
}
|
||||||
|
|
||||||
// utility stuff
|
// utility stuff
|
||||||
|
|
||||||
inline TaskAwaitableSuspend c_suspend() {
|
inline TaskAwaitableSuspend c_suspend() {
|
||||||
@ -869,10 +1265,40 @@ Task<> c_allDone(const TCollection<FuturePtr<TType>, TTemplateArgs...>& futures)
|
|||||||
} while (!allDone);
|
} while (!allDone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator, typename... TResult>
|
||||||
|
struct AllDoneHelper
|
||||||
|
{
|
||||||
|
TaskLoop<TAllocator>& currentTaskLoop;
|
||||||
|
|
||||||
|
template<typename T, std::size_t index>
|
||||||
|
auto makeFuture(TaskBase<T, TAllocator>&& task, std::array<TaskHandle, sizeof...(TResult)>& outHandles)
|
||||||
|
{
|
||||||
|
return currentTaskLoop.addTaskImpl(std::move(task), &outHandles[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t... indices>
|
||||||
|
auto makeFutures(TaskBase<TResult, TAllocator>&&... tasks, std::array<TaskHandle, sizeof...(TResult)>& outHandles, std::index_sequence<indices...>)
|
||||||
|
{
|
||||||
|
return std::make_tuple(makeFuture<TResult, indices>(std::move(tasks), outHandles)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator, typename... TResult>
|
||||||
|
TaskBase<std::tuple<TResult...>, TAllocator> c_allDone(TaskBase<TResult, TAllocator>&&... tasks)
|
||||||
|
{
|
||||||
|
TaskLoop<TAllocator>& currentTaskLoop = TaskLoop<TAllocator>::current();
|
||||||
|
std::tuple futures = std::make_tuple(currentTaskLoop.addTaskImpl(std::move(tasks), nullptr)...);
|
||||||
|
while (!allReady(futures)) {
|
||||||
|
co_await c_suspend();
|
||||||
|
}
|
||||||
|
co_return getAll(futures);
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] inline TaskHandle getCurrentTask() MIJIN_NOEXCEPT
|
[[nodiscard]] inline TaskHandle getCurrentTask() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(impl::gCurrentTask != nullptr, "Attempt to call getCurrentTask() outside of task.");
|
MIJIN_ASSERT(impl::gCurrentTaskState != nullptr, "Attempt to call getCurrentTask() outside of task.");
|
||||||
return TaskHandle(impl::gCurrentTask->task->sharedState());
|
return TaskHandle(impl::gCurrentTaskState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,9 @@
|
|||||||
#if !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
|
#if !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
|
||||||
#define MIJIN_ASYNC_FUTURE_HPP_INCLUDED 1
|
#define MIJIN_ASYNC_FUTURE_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include "./signal.hpp"
|
#include "./signal.hpp"
|
||||||
#include "../container/optional.hpp"
|
#include "../container/optional.hpp"
|
||||||
@ -26,7 +27,7 @@ namespace mijin
|
|||||||
//
|
//
|
||||||
// public types
|
// public types
|
||||||
//
|
//
|
||||||
template<typename TValue>
|
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
class Future;
|
class Future;
|
||||||
|
|
||||||
// TODO: add support for mutexes and waiting for futures
|
// TODO: add support for mutexes and waiting for futures
|
||||||
@ -57,16 +58,17 @@ struct FutureStorage<void>
|
|||||||
};
|
};
|
||||||
} // namespace impl
|
} // namespace impl
|
||||||
|
|
||||||
template<typename TValue>
|
template<typename TValue, template<typename> typename TAllocator>
|
||||||
class Future
|
class Future
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
impl::FutureStorage<TValue> value_;
|
[[no_unique_address]] impl::FutureStorage<TValue> value_;
|
||||||
bool isSet_ = false;
|
bool isSet_ = false;
|
||||||
public:
|
public:
|
||||||
Future() = default;
|
Future() = default;
|
||||||
Future(const Future&) = delete;
|
Future(const Future&) = delete;
|
||||||
Future(Future&&) MIJIN_NOEXCEPT = default;
|
Future(Future&&) MIJIN_NOEXCEPT = default;
|
||||||
|
explicit Future(TAllocator<void> allocator) : sigSet(std::move(allocator)) {}
|
||||||
public:
|
public:
|
||||||
Future& operator=(const Future&) = delete;
|
Future& operator=(const Future&) = delete;
|
||||||
Future& operator=(Future&&) MIJIN_NOEXCEPT = default;
|
Future& operator=(Future&&) MIJIN_NOEXCEPT = default;
|
||||||
@ -126,16 +128,52 @@ public: // modification
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public: // signals
|
public: // signals
|
||||||
Signal<> sigSet;
|
BaseSignal<TAllocator> sigSet;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TValue>
|
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
using FuturePtr = std::shared_ptr<Future<TValue>>;
|
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator>>;
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template<typename... TResult>
|
||||||
|
struct MultiFutureHelper
|
||||||
|
{
|
||||||
|
template<std::size_t... indices>
|
||||||
|
static bool allReady(const std::tuple<FuturePtr<TResult>...>& futures, std::index_sequence<indices...>) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return (std::get<indices>(futures)->ready() && ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t... indices>
|
||||||
|
static std::tuple<std::remove_reference_t<TResult>...> getAll(const std::tuple<FuturePtr<TResult>...>& futures, std::index_sequence<indices...>) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::make_tuple(std::move(std::get<indices>(futures)->get())...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
|
constexpr FuturePtr<T> makeSharedFuture(TAllocator<Future<T>> allocator = {}) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::allocate_shared<Future<T>>(std::move(allocator));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... TResult>
|
||||||
|
constexpr bool allReady(const std::tuple<FuturePtr<TResult>...>& futures) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return impl::MultiFutureHelper<TResult...>::allReady(futures, std::index_sequence_for<TResult...>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... TResult>
|
||||||
|
constexpr std::tuple<std::remove_reference_t<TResult>...> getAll(const std::tuple<FuturePtr<TResult>...>& futures) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return impl::MultiFutureHelper<TResult...>::getAll(futures, std::index_sequence_for<TResult...>());
|
||||||
|
}
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
|
#endif // !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
|
||||||
|
|||||||
@ -79,15 +79,20 @@ class MessageQueue
|
|||||||
public:
|
public:
|
||||||
using message_t = TMessage;
|
using message_t = TMessage;
|
||||||
using iterator_t = MessageQueueIterator<MessageQueue<TMessage, bufferSize>>;
|
using iterator_t = MessageQueueIterator<MessageQueue<TMessage, bufferSize>>;
|
||||||
|
static constexpr std::size_t BUFFER_SIZE = bufferSize;
|
||||||
private:
|
private:
|
||||||
std::array<TMessage, bufferSize> messages;
|
std::array<TMessage, bufferSize> messages_;
|
||||||
mijin::BitArray<bufferSize, true> messageReady;
|
mijin::BitArray<bufferSize, true> messageReady_;
|
||||||
std::atomic_uint writePos = 0;
|
std::atomic_uint writePos_ = 0;
|
||||||
std::atomic_uint readPos = 0;
|
std::atomic_uint readPos_ = 0;
|
||||||
public:
|
public:
|
||||||
MessageQueue() = default;
|
MessageQueue() = default;
|
||||||
MessageQueue(const MessageQueue&) = delete;
|
MessageQueue(const MessageQueue&) = delete;
|
||||||
MessageQueue(MessageQueue&&) = delete;
|
MessageQueue(MessageQueue&&) = delete;
|
||||||
|
explicit MessageQueue(const std::array<TMessage, bufferSize>& messages) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<TMessage>)
|
||||||
|
: messages_(messages) {}
|
||||||
|
explicit MessageQueue(std::array<TMessage, bufferSize>&& messages) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TMessage>)
|
||||||
|
: messages_(std::move(messages)) {}
|
||||||
|
|
||||||
MessageQueue& operator=(const MessageQueue&) = delete;
|
MessageQueue& operator=(const MessageQueue&) = delete;
|
||||||
MessageQueue& operator=(MessageQueue&&) = delete;
|
MessageQueue& operator=(MessageQueue&&) = delete;
|
||||||
@ -118,22 +123,22 @@ struct TaskMessageQueue
|
|||||||
template<typename TMessage, std::size_t bufferSize>
|
template<typename TMessage, std::size_t bufferSize>
|
||||||
bool MessageQueue<TMessage, bufferSize>::tryPushMaybeMove(TMessage& message)
|
bool MessageQueue<TMessage, bufferSize>::tryPushMaybeMove(TMessage& message)
|
||||||
{
|
{
|
||||||
unsigned oldWritePos = writePos.load(std::memory_order_relaxed);
|
unsigned oldWritePos = writePos_.load(std::memory_order_relaxed);
|
||||||
unsigned newWritePos = 0;
|
unsigned newWritePos = 0;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
newWritePos = (oldWritePos + 1) % bufferSize;
|
newWritePos = (oldWritePos + 1) % bufferSize;
|
||||||
if (newWritePos == readPos) {
|
if (newWritePos == readPos_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} while (!writePos.compare_exchange_weak(oldWritePos, newWritePos, std::memory_order_release, std::memory_order_relaxed));
|
} while (!writePos_.compare_exchange_weak(oldWritePos, newWritePos, std::memory_order_release, std::memory_order_relaxed));
|
||||||
|
|
||||||
while (messageReady.get(oldWritePos)) {
|
while (messageReady_.get(oldWritePos)) {
|
||||||
std::this_thread::yield(); // someone is still reading, wait...
|
std::this_thread::yield(); // someone is still reading, wait...
|
||||||
}
|
}
|
||||||
|
|
||||||
messages[oldWritePos] = std::move(message);
|
messages_[oldWritePos] = std::move(message);
|
||||||
messageReady.set(oldWritePos, true);
|
messageReady_.set(oldWritePos, true);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -149,22 +154,22 @@ void MessageQueue<TMessage, bufferSize>::push(TMessage message)
|
|||||||
template<typename TMessage, std::size_t bufferSize>
|
template<typename TMessage, std::size_t bufferSize>
|
||||||
std::optional<TMessage> MessageQueue<TMessage, bufferSize>::tryPop()
|
std::optional<TMessage> MessageQueue<TMessage, bufferSize>::tryPop()
|
||||||
{
|
{
|
||||||
unsigned oldReadPos = readPos.load(std::memory_order_relaxed);
|
unsigned oldReadPos = readPos_.load(std::memory_order_relaxed);
|
||||||
unsigned newReadPos = 0;
|
unsigned newReadPos = 0;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (oldReadPos == writePos) {
|
if (oldReadPos == writePos_) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
newReadPos = (oldReadPos + 1) % bufferSize;
|
newReadPos = (oldReadPos + 1) % bufferSize;
|
||||||
} while (!readPos.compare_exchange_weak(oldReadPos, newReadPos, std::memory_order_release, std::memory_order_relaxed));
|
} while (!readPos_.compare_exchange_weak(oldReadPos, newReadPos, std::memory_order_release, std::memory_order_relaxed));
|
||||||
|
|
||||||
while (!messageReady.get(oldReadPos)) {
|
while (!messageReady_.get(oldReadPos)) {
|
||||||
std::this_thread::yield(); // no harm in busy-waiting here, should be fast
|
std::this_thread::yield(); // no harm in busy-waiting here, should be fast
|
||||||
};
|
};
|
||||||
|
|
||||||
TMessage message = std::move(messages[oldReadPos]);
|
TMessage message = std::move(messages_[oldReadPos]);
|
||||||
messageReady.set(oldReadPos, false);
|
messageReady_.set(oldReadPos, false);
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,11 +33,11 @@ inline constexpr signal_token_t INVALID_SIGNAL_TOKEN = std::numeric_limits<signa
|
|||||||
|
|
||||||
MIJIN_DEFINE_FLAG(Oneshot);
|
MIJIN_DEFINE_FLAG(Oneshot);
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
class Signal
|
class BaseSignal
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using handler_t = std::function<void(TArgs...)>;
|
using handler_t = std::function<void(TArgs...)>; // TODO: write a custom function wrapper with allocator support
|
||||||
using token_t = signal_token_t;
|
using token_t = signal_token_t;
|
||||||
private:
|
private:
|
||||||
struct RegisteredHandler
|
struct RegisteredHandler
|
||||||
@ -47,36 +47,41 @@ private:
|
|||||||
token_t token;
|
token_t token;
|
||||||
Oneshot oneshot = Oneshot::NO;
|
Oneshot oneshot = Oneshot::NO;
|
||||||
};
|
};
|
||||||
using handler_vector_t = std::vector<RegisteredHandler>;
|
using handler_vector_t = std::vector<RegisteredHandler, TAllocator<RegisteredHandler>>;
|
||||||
private:
|
private:
|
||||||
handler_vector_t handlers_;
|
handler_vector_t handlers_;
|
||||||
token_t nextToken = 1;
|
token_t nextToken = 1;
|
||||||
std::mutex handlersMutex_;
|
std::mutex handlersMutex_;
|
||||||
public:
|
public:
|
||||||
Signal() = default;
|
explicit BaseSignal(TAllocator<void> allocator = {}) : handlers_(TAllocator<RegisteredHandler>(std::move(allocator))) {}
|
||||||
Signal(const Signal&) = delete;
|
BaseSignal(const BaseSignal&) = delete;
|
||||||
Signal(Signal&&) MIJIN_NOEXCEPT = default;
|
BaseSignal(BaseSignal&&) MIJIN_NOEXCEPT = default;
|
||||||
public:
|
public:
|
||||||
Signal& operator=(const Signal&) = delete;
|
BaseSignal& operator=(const BaseSignal&) = delete;
|
||||||
Signal& operator=(Signal&&) MIJIN_NOEXCEPT = default;
|
BaseSignal& operator=(BaseSignal&&) MIJIN_NOEXCEPT = default;
|
||||||
public:
|
public:
|
||||||
template<typename THandler, typename TWeak = void>
|
template<typename THandler, typename TWeak = void>
|
||||||
inline token_t connect(THandler handler, Oneshot oneshot = Oneshot::NO, std::weak_ptr<TWeak> referenced = std::weak_ptr<TWeak>()) MIJIN_NOEXCEPT;
|
inline token_t connect(THandler handler, Oneshot oneshot = Oneshot::NO, std::weak_ptr<TWeak> referenced = std::weak_ptr<TWeak>()) MIJIN_NOEXCEPT;
|
||||||
template<typename TObject, typename TWeak = void>
|
template<typename TObject, typename TWeak = void>
|
||||||
inline token_t connect(TObject& object, void (TObject::* handler)(TArgs...), Oneshot oneshot = Oneshot::NO, std::weak_ptr<TWeak> referenced = std::weak_ptr<TWeak>()) MIJIN_NOEXCEPT;
|
inline token_t connect(TObject& object, void (TObject::* handler)(TArgs...), Oneshot oneshot = Oneshot::NO, std::weak_ptr<TWeak> referenced = std::weak_ptr<TWeak>()) MIJIN_NOEXCEPT;
|
||||||
|
template<typename TObject, typename TWeak = void>
|
||||||
|
inline token_t connect(TObject& object, void (TObject::* handler)(TArgs...) const, Oneshot oneshot = Oneshot::NO, std::weak_ptr<TWeak> referenced = std::weak_ptr<TWeak>()) MIJIN_NOEXCEPT;
|
||||||
inline void disconnect(token_t token) MIJIN_NOEXCEPT;
|
inline void disconnect(token_t token) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
template<typename... TArgs2>
|
template<typename... TArgs2>
|
||||||
inline void emit(TArgs2&&... args) MIJIN_NOEXCEPT;
|
inline void emit(TArgs2&&... args) MIJIN_NOEXCEPT;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... TArgs>
|
||||||
|
using Signal = BaseSignal<MIJIN_DEFAULT_ALLOCATOR, TArgs...>;
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
template<typename THandler, typename TWeak>
|
template<typename THandler, typename TWeak>
|
||||||
inline auto Signal<TArgs...>::connect(THandler handler, Oneshot oneshot, std::weak_ptr<TWeak> referenced) MIJIN_NOEXCEPT -> token_t
|
inline auto BaseSignal<TAllocator, TArgs...>::connect(THandler handler, Oneshot oneshot, std::weak_ptr<TWeak> referenced) MIJIN_NOEXCEPT -> token_t
|
||||||
{
|
{
|
||||||
std::lock_guard lock(handlersMutex_);
|
std::lock_guard lock(handlersMutex_);
|
||||||
|
|
||||||
@ -91,9 +96,9 @@ inline auto Signal<TArgs...>::connect(THandler handler, Oneshot oneshot, std::we
|
|||||||
return nextToken++;
|
return nextToken++;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
template<typename TObject, typename TWeak>
|
template<typename TObject, typename TWeak>
|
||||||
inline auto Signal<TArgs...>::connect(TObject& object, void (TObject::* handler)(TArgs...), Oneshot oneshot, std::weak_ptr<TWeak> referenced) MIJIN_NOEXCEPT -> token_t
|
inline auto BaseSignal<TAllocator, TArgs...>::connect(TObject& object, void (TObject::* handler)(TArgs...), Oneshot oneshot, std::weak_ptr<TWeak> referenced) MIJIN_NOEXCEPT -> token_t
|
||||||
{
|
{
|
||||||
std::lock_guard lock(handlersMutex_);
|
std::lock_guard lock(handlersMutex_);
|
||||||
|
|
||||||
@ -111,8 +116,28 @@ inline auto Signal<TArgs...>::connect(TObject& object, void (TObject::* handler)
|
|||||||
return nextToken++;
|
return nextToken++;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
inline void Signal<TArgs...>::disconnect(token_t token) MIJIN_NOEXCEPT
|
template<typename TObject, typename TWeak>
|
||||||
|
inline auto BaseSignal<TAllocator, TArgs...>::connect(TObject& object, void (TObject::* handler)(TArgs...) const, Oneshot oneshot, std::weak_ptr<TWeak> referenced) MIJIN_NOEXCEPT -> token_t
|
||||||
|
{
|
||||||
|
std::lock_guard lock(handlersMutex_);
|
||||||
|
|
||||||
|
auto callable = [object = &object, handler](TArgs... args)
|
||||||
|
{
|
||||||
|
std::invoke(handler, object, std::forward<TArgs>(args)...);
|
||||||
|
};
|
||||||
|
handlers_.push_back({
|
||||||
|
.callable = std::move(callable),
|
||||||
|
.referenced = std::move(referenced),
|
||||||
|
.token = nextToken,
|
||||||
|
.oneshot = oneshot
|
||||||
|
});
|
||||||
|
|
||||||
|
return nextToken++;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
|
inline void BaseSignal<TAllocator, TArgs...>::disconnect(token_t token) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::lock_guard lock(handlersMutex_);
|
std::lock_guard lock(handlersMutex_);
|
||||||
|
|
||||||
@ -123,9 +148,9 @@ inline void Signal<TArgs...>::disconnect(token_t token) MIJIN_NOEXCEPT
|
|||||||
handlers_.erase(it, handlers_.end());
|
handlers_.erase(it, handlers_.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
template<typename... TArgs2>
|
template<typename... TArgs2>
|
||||||
inline void Signal<TArgs...>::emit(TArgs2&&... args) MIJIN_NOEXCEPT
|
inline void BaseSignal<TAllocator, TArgs...>::emit(TArgs2&&... args) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::lock_guard lock(handlersMutex_);
|
std::lock_guard lock(handlersMutex_);
|
||||||
|
|
||||||
|
|||||||
@ -16,8 +16,26 @@ namespace mijin
|
|||||||
//
|
//
|
||||||
|
|
||||||
#if !defined(MIJIN_BOXED_OBJECT_DEBUG)
|
#if !defined(MIJIN_BOXED_OBJECT_DEBUG)
|
||||||
|
#if defined(MIJIN_DEBUG)
|
||||||
#define MIJIN_BOXED_OBJECT_DEBUG MIJIN_DEBUG
|
#define MIJIN_BOXED_OBJECT_DEBUG MIJIN_DEBUG
|
||||||
|
#else
|
||||||
|
#define MIJIN_BOXED_OBJECT_DEBUG 0
|
||||||
#endif
|
#endif
|
||||||
|
#endif // !defined(MIJIN_BOXED_OBJECT_DEBUG)
|
||||||
|
|
||||||
|
#define MIJIN_BOXED_PROXY_FUNC(funcname) \
|
||||||
|
template<typename... TFuncArgs> \
|
||||||
|
decltype(auto) funcname(TFuncArgs&&... args) \
|
||||||
|
{ \
|
||||||
|
return base_t::get().funcname(std::forward<TFuncArgs>(args)...); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MIJIN_BOXED_PROXY_FUNC_CONST(funcname) \
|
||||||
|
template<typename... TFuncArgs> \
|
||||||
|
decltype(auto) funcname(TFuncArgs&&... args) const \
|
||||||
|
{ \
|
||||||
|
return base_t::get().funcname(std::forward<TFuncArgs>(args)...); \
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// public constants
|
// public constants
|
||||||
@ -39,8 +57,8 @@ private:
|
|||||||
bool constructed = false;
|
bool constructed = false;
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
BoxedObject() noexcept : placeholder_() {};
|
constexpr BoxedObject() MIJIN_NOEXCEPT : placeholder_() {};
|
||||||
explicit BoxedObject(T object)
|
explicit constexpr BoxedObject(T object) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
: constructed(true)
|
: constructed(true)
|
||||||
#endif
|
#endif
|
||||||
@ -51,7 +69,7 @@ public:
|
|||||||
BoxedObject(BoxedObject&&) = delete;
|
BoxedObject(BoxedObject&&) = delete;
|
||||||
|
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
~BoxedObject()
|
constexpr ~BoxedObject() noexcept
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(!constructed, "BoxedObject::~BoxedObject(): Object has not been destroyed prior to destructor!");
|
MIJIN_ASSERT(!constructed, "BoxedObject::~BoxedObject(): Object has not been destroyed prior to destructor!");
|
||||||
}
|
}
|
||||||
@ -60,13 +78,13 @@ public:
|
|||||||
BoxedObject& operator=(const BoxedObject&) = delete;
|
BoxedObject& operator=(const BoxedObject&) = delete;
|
||||||
BoxedObject& operator=(BoxedObject&&) = delete;
|
BoxedObject& operator=(BoxedObject&&) = delete;
|
||||||
|
|
||||||
T& operator*() noexcept { return get(); }
|
constexpr T& operator*() noexcept { return get(); }
|
||||||
const T& operator*() const noexcept { return get(); }
|
constexpr const T& operator*() const noexcept { return get(); }
|
||||||
T* operator->() noexcept { return &get(); }
|
constexpr T* operator->() noexcept { return &get(); }
|
||||||
const T* operator->() const noexcept { return &get(); }
|
constexpr const T* operator->() const noexcept { return &get(); }
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<typename... TArgs>
|
||||||
void construct(TArgs&&... args)
|
constexpr void construct(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
||||||
{
|
{
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
MIJIN_ASSERT(!constructed, "BoxedObject::construct(): Attempt to construct an already constructed object!");
|
MIJIN_ASSERT(!constructed, "BoxedObject::construct(): Attempt to construct an already constructed object!");
|
||||||
@ -75,7 +93,7 @@ public:
|
|||||||
std::construct_at(&object_, std::forward<TArgs>(args)...);
|
std::construct_at(&object_, std::forward<TArgs>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy()
|
constexpr void destroy() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
MIJIN_ASSERT(constructed, "BoxedObject::destroy(): Attempt to destroy a not constructed object!");
|
MIJIN_ASSERT(constructed, "BoxedObject::destroy(): Attempt to destroy a not constructed object!");
|
||||||
@ -83,8 +101,8 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
std::destroy_at(&object_);
|
std::destroy_at(&object_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void copyTo(BoxedObject& other) const
|
constexpr void copyTo(BoxedObject& other) const MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<T>)
|
||||||
{
|
{
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!");
|
MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!");
|
||||||
@ -92,7 +110,7 @@ public:
|
|||||||
other.construct(object_);
|
other.construct(object_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveTo(BoxedObject& other)
|
constexpr void moveTo(BoxedObject& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||||
{
|
{
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!");
|
MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!");
|
||||||
@ -101,7 +119,7 @@ public:
|
|||||||
destroy();
|
destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] T& get()
|
[[nodiscard]] constexpr T& get() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!");
|
MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!");
|
||||||
@ -109,7 +127,7 @@ public:
|
|||||||
return object_;
|
return object_;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const T& get() const
|
[[nodiscard]] constexpr const T& get() const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||||
MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!");
|
MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!");
|
||||||
@ -131,38 +149,38 @@ private:
|
|||||||
BoxedObject<T>* end_ = nullptr;
|
BoxedObject<T>* end_ = nullptr;
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
BoxedObjectIterator() = default;
|
BoxedObjectIterator() noexcept = default;
|
||||||
explicit constexpr BoxedObjectIterator(BoxedObject<T>* box, BoxedObject<T>* start, BoxedObject<T>* end)
|
explicit constexpr BoxedObjectIterator(BoxedObject<T>* box, BoxedObject<T>* start, BoxedObject<T>* end) MIJIN_NOEXCEPT
|
||||||
: box_(box)
|
: box_(box)
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
, start_(start), end_(end)
|
, start_(start), end_(end)
|
||||||
#endif
|
#endif
|
||||||
{}
|
{}
|
||||||
BoxedObjectIterator(const BoxedObjectIterator&) = default;
|
constexpr BoxedObjectIterator(const BoxedObjectIterator&) noexcept = default;
|
||||||
BoxedObjectIterator(BoxedObjectIterator&&) noexcept = default;
|
constexpr BoxedObjectIterator(BoxedObjectIterator&&) noexcept = default;
|
||||||
|
|
||||||
BoxedObjectIterator& operator=(const BoxedObjectIterator&) = default;
|
constexpr BoxedObjectIterator& operator=(const BoxedObjectIterator&) noexcept = default;
|
||||||
BoxedObjectIterator& operator=(BoxedObjectIterator&&) noexcept = default;
|
constexpr BoxedObjectIterator& operator=(BoxedObjectIterator&&) noexcept = default;
|
||||||
BoxedObjectIterator& operator+=(difference_type diff);
|
constexpr BoxedObjectIterator& operator+=(difference_type diff) MIJIN_NOEXCEPT;
|
||||||
BoxedObjectIterator& operator-=(difference_type diff) { return (*this += -diff); }
|
constexpr BoxedObjectIterator& operator-=(difference_type diff) MIJIN_NOEXCEPT { return (*this += -diff); }
|
||||||
|
|
||||||
constexpr auto operator<=>(const BoxedObjectIterator& other) const noexcept = default;
|
constexpr auto operator<=>(const BoxedObjectIterator& other) const noexcept = default;
|
||||||
|
|
||||||
[[nodiscard]] T& operator*() const;
|
[[nodiscard]] constexpr T& operator*() const MIJIN_NOEXCEPT;
|
||||||
[[nodiscard]] T* operator->() const;
|
[[nodiscard]] constexpr T* operator->() const MIJIN_NOEXCEPT;
|
||||||
BoxedObjectIterator& operator++();
|
constexpr BoxedObjectIterator& operator++() MIJIN_NOEXCEPT;
|
||||||
BoxedObjectIterator operator++(int);
|
constexpr BoxedObjectIterator operator++(int) MIJIN_NOEXCEPT;
|
||||||
BoxedObjectIterator& operator--();
|
constexpr BoxedObjectIterator& operator--() MIJIN_NOEXCEPT;
|
||||||
BoxedObjectIterator operator--(int);
|
constexpr BoxedObjectIterator operator--(int) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
[[nodiscard]] difference_type operator-(const BoxedObjectIterator& other) const { return box_ - other.box_; }
|
[[nodiscard]] constexpr difference_type operator-(const BoxedObjectIterator& other) const MIJIN_NOEXCEPT { return box_ - other.box_; }
|
||||||
[[nodiscard]] BoxedObjectIterator operator+(difference_type diff) const;
|
[[nodiscard]] constexpr BoxedObjectIterator operator+(difference_type diff) const MIJIN_NOEXCEPT;
|
||||||
[[nodiscard]] BoxedObjectIterator operator-(difference_type diff) const { return (*this + -diff); }
|
[[nodiscard]] constexpr BoxedObjectIterator operator-(difference_type diff) const MIJIN_NOEXCEPT { return (*this + -diff); }
|
||||||
|
|
||||||
[[nodiscard]] T& operator[](difference_type diff) const { return *(*this + diff); }
|
[[nodiscard]] T& operator[](difference_type diff) const MIJIN_NOEXCEPT { return *(*this + diff); }
|
||||||
};
|
};
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline BoxedObjectIterator<T> operator+(std::iter_difference_t<T> diff, const BoxedObjectIterator<T>& iter) {
|
constexpr BoxedObjectIterator<T> operator+(std::iter_difference_t<T> diff, const BoxedObjectIterator<T>& iter) MIJIN_NOEXCEPT {
|
||||||
return iter + diff;
|
return iter + diff;
|
||||||
}
|
}
|
||||||
static_assert(std::random_access_iterator<BoxedObjectIterator<int>>);
|
static_assert(std::random_access_iterator<BoxedObjectIterator<int>>);
|
||||||
@ -172,7 +190,7 @@ static_assert(std::random_access_iterator<BoxedObjectIterator<int>>);
|
|||||||
//
|
//
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator+=(difference_type diff)
|
constexpr BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator+=(difference_type diff) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
MIJIN_ASSERT(diff <= (end_ - box_) && diff >= (box_ - start_), "BoxedObjectIterator::operator+=(): Attempt to iterate out of range.");
|
MIJIN_ASSERT(diff <= (end_ - box_) && diff >= (box_ - start_), "BoxedObjectIterator::operator+=(): Attempt to iterate out of range.");
|
||||||
@ -182,7 +200,7 @@ BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator+=(difference_type diff)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T& BoxedObjectIterator<T>::operator*() const
|
constexpr T& BoxedObjectIterator<T>::operator*() const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
MIJIN_ASSERT(box_ >= start_ && box_ < end_, "BoxedObjectIterator::operator->(): Attempt to dereference out-of-range iterator.");
|
MIJIN_ASSERT(box_ >= start_ && box_ < end_, "BoxedObjectIterator::operator->(): Attempt to dereference out-of-range iterator.");
|
||||||
@ -191,7 +209,7 @@ T& BoxedObjectIterator<T>::operator*() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator++()
|
constexpr BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator++() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(): Attempt to iterator past the end.");
|
MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(): Attempt to iterator past the end.");
|
||||||
@ -200,7 +218,7 @@ BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator++()
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
BoxedObjectIterator<T> BoxedObjectIterator<T>::operator++(int)
|
constexpr BoxedObjectIterator<T> BoxedObjectIterator<T>::operator++(int) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(int): Attempt to iterator past the end.");
|
MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(int): Attempt to iterator past the end.");
|
||||||
@ -211,7 +229,7 @@ BoxedObjectIterator<T> BoxedObjectIterator<T>::operator++(int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator--()
|
constexpr BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator--() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(): Attempt to iterator past start.");
|
MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(): Attempt to iterator past start.");
|
||||||
@ -220,7 +238,7 @@ BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator--()
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
BoxedObjectIterator<T> BoxedObjectIterator<T>::operator--(int)
|
constexpr BoxedObjectIterator<T> BoxedObjectIterator<T>::operator--(int) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if MIJIN_CHECKED_ITERATORS
|
#if MIJIN_CHECKED_ITERATORS
|
||||||
MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(int): Attempt to iterator past start.");
|
MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(int): Attempt to iterator past start.");
|
||||||
@ -231,7 +249,7 @@ BoxedObjectIterator<T> BoxedObjectIterator<T>::operator--(int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
BoxedObjectIterator<T> BoxedObjectIterator<T>::operator+(difference_type diff) const
|
constexpr BoxedObjectIterator<T> BoxedObjectIterator<T>::operator+(difference_type diff) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
BoxedObjectIterator copy(*this);
|
BoxedObjectIterator copy(*this);
|
||||||
copy += diff;
|
copy += diff;
|
||||||
|
|||||||
@ -5,10 +5,10 @@
|
|||||||
#define MIJIN_CONTAINER_MEMORY_VIEW_HPP_INCLUDED 1
|
#define MIJIN_CONTAINER_MEMORY_VIEW_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <bit>
|
#include <bit>
|
||||||
#include <cstddef>
|
|
||||||
#include <span>
|
#include <span>
|
||||||
|
#include <string_view>
|
||||||
#include "../debug/assert.hpp"
|
#include "../debug/assert.hpp"
|
||||||
#include "../util/traits.hpp"
|
#include "../internal/common.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@ -25,60 +25,172 @@ namespace mijin
|
|||||||
// public types
|
// public types
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename TBytes>
|
template<typename T>
|
||||||
class MemoryViewBase
|
concept MemoryViewable = requires(const T& object)
|
||||||
|
{
|
||||||
|
{ object.data() } -> std::convertible_to<const void*>;
|
||||||
|
{ object.byteSize() } -> std::convertible_to<std::size_t>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept RWMemoryViewable = MemoryViewable<T> && requires(T& object)
|
||||||
|
{
|
||||||
|
{ object.data() } -> std::convertible_to<void*>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TConcrete>
|
||||||
|
class MixinMemoryView
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static constexpr bool WRITABLE = requires(TConcrete& object) { { object.data() } -> std::convertible_to<void*>; };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]]
|
||||||
|
auto makeSpan();
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]]
|
||||||
|
auto makeSpan() const;
|
||||||
|
|
||||||
|
template<typename TChar = char, typename TTraits = std::char_traits<TChar>>
|
||||||
|
[[nodiscard]]
|
||||||
|
std::basic_string_view<TChar, TTraits> makeStringView() const ;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]]
|
||||||
|
auto& dataAt(std::size_t offset) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]]
|
||||||
|
auto& dataAt(std::size_t offset) const MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
auto bytes() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
using return_t = mijin::copy_const_t<std::remove_pointer_t<decltype(static_cast<TConcrete*>(this)->data())>, std::byte>;
|
||||||
|
return static_cast<return_t*>(static_cast<TConcrete*>(this)->data());
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
auto bytes() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
using return_t = mijin::copy_const_t<std::remove_pointer_t<decltype(static_cast<const TConcrete*>(this)->data())>, std::byte>;
|
||||||
|
return static_cast<return_t*>(static_cast<const TConcrete*>(this)->data());
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::size_t byteSizeImpl() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return static_cast<const TConcrete*>(this)->byteSize();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MemoryView : public MixinMemoryView<MemoryView>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using size_type = std::size_t;
|
using size_type = std::size_t;
|
||||||
private:
|
private:
|
||||||
std::span<TBytes> bytes_;
|
void* data_ = nullptr;
|
||||||
|
std::size_t byteSize_ = 0;
|
||||||
public:
|
public:
|
||||||
MemoryViewBase() noexcept = default;
|
MemoryView() noexcept = default;
|
||||||
MemoryViewBase(const MemoryViewBase&) noexcept = default;
|
MemoryView(const MemoryView&) = default;
|
||||||
MemoryViewBase(copy_const_t<TBytes, void>* data, std::size_t size) noexcept : bytes_(static_cast<TBytes*>(data), size) {}
|
MemoryView(void* data, std::size_t byteSize) MIJIN_NOEXCEPT : data_(data), byteSize_(byteSize) {}
|
||||||
|
template<typename T> requires(!std::is_const_v<T>)
|
||||||
|
MemoryView(std::span<T> span) MIJIN_NOEXCEPT : data_(span.data()), byteSize_(span.size_bytes()) {}
|
||||||
|
template<RWMemoryViewable T>
|
||||||
|
MemoryView(T& memoryViewable) MIJIN_NOEXCEPT : data_(memoryViewable.data()), byteSize_(memoryViewable.byteSize()) {}
|
||||||
|
|
||||||
MemoryViewBase& operator=(const MemoryViewBase&) noexcept = default;
|
MemoryView& operator=(const MemoryView&) = default;
|
||||||
|
|
||||||
[[nodiscard]] void* data() noexcept { return bytes_.data(); }
|
|
||||||
[[nodiscard]] const void* data() const noexcept { return bytes_.data(); }
|
|
||||||
[[nodiscard]] size_type byteSize() const noexcept { return bytes_.size(); }
|
|
||||||
[[nodiscard]] bool empty() const noexcept { return bytes_.empty(); }
|
|
||||||
|
|
||||||
template<typename T>
|
[[nodiscard]]
|
||||||
[[nodiscard]] std::span<T> makeSpan();
|
void* data() const MIJIN_NOEXCEPT { return data_; }
|
||||||
|
|
||||||
template<typename T>
|
[[nodiscard]]
|
||||||
[[nodiscard]] std::span<const T> makeSpan() const;
|
size_type byteSize() const MIJIN_NOEXCEPT { return byteSize_; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class ConstMemoryView : public MixinMemoryView<ConstMemoryView>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using size_type = std::size_t;
|
||||||
|
private:
|
||||||
|
const void* data_;
|
||||||
|
std::size_t byteSize_;
|
||||||
|
public:
|
||||||
|
ConstMemoryView() noexcept = default;
|
||||||
|
ConstMemoryView(const ConstMemoryView&) = default;
|
||||||
|
ConstMemoryView(void* data, std::size_t byteSize) MIJIN_NOEXCEPT : data_(data), byteSize_(byteSize) {}
|
||||||
|
template<typename T>
|
||||||
|
ConstMemoryView(std::span<T> span) MIJIN_NOEXCEPT : data_(span.data()), byteSize_(span.size_bytes()) {}
|
||||||
|
template<MemoryViewable T>
|
||||||
|
ConstMemoryView(const T& memoryViewable) MIJIN_NOEXCEPT : data_(memoryViewable.data()), byteSize_(memoryViewable.byteSize()) {}
|
||||||
|
|
||||||
|
ConstMemoryView& operator=(const ConstMemoryView& other) MIJIN_NOEXCEPT = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const void* data() const MIJIN_NOEXCEPT { return data_; }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
size_type byteSize() const MIJIN_NOEXCEPT { return byteSize_; }
|
||||||
};
|
};
|
||||||
using MemoryView = MemoryViewBase<std::byte>;
|
|
||||||
using ConstMemoryView = MemoryViewBase<const std::byte>;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename TBytes>
|
template<typename TConcrete>
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::span<T> MemoryViewBase<TBytes>::makeSpan()
|
auto MixinMemoryView<TConcrete>::makeSpan()
|
||||||
{
|
{
|
||||||
static_assert(!std::is_const_v<TBytes>, "Cannot create writable spans from const memory views.");
|
MIJIN_ASSERT(byteSizeImpl() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
||||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "MemoryView cannot be divided into elements of this type.");
|
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||||
return {
|
return std::span<return_t>{
|
||||||
std::bit_cast<T*>(bytes_.data()),
|
std::bit_cast<return_t*>(bytes()),
|
||||||
std::bit_cast<T*>(bytes_.data() + bytes_.size())
|
std::bit_cast<return_t*>(bytes() + byteSizeImpl())
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TBytes>
|
template<typename TConcrete>
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::span<const T> MemoryViewBase<TBytes>::makeSpan() const
|
auto MixinMemoryView<TConcrete>::makeSpan() const
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "MemoryView cannot be divided into elements of this type.");
|
MIJIN_ASSERT(byteSizeImpl() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
||||||
return {
|
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||||
std::bit_cast<const T*>(bytes_.data()),
|
return std::span<return_t>{
|
||||||
std::bit_cast<const T*>(bytes_.data() + bytes_.size())
|
std::bit_cast<return_t*>(bytes()),
|
||||||
|
std::bit_cast<return_t*>(bytes() + byteSizeImpl())
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TConcrete>
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
std::basic_string_view<TChar, TTraits> MixinMemoryView<TConcrete>::makeStringView() const
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(byteSizeImpl() % sizeof(TChar) == 0, "Buffer cannot be divided into elements of this char type.");
|
||||||
|
return {std::bit_cast<const TChar*>(bytes()), byteSizeImpl() / sizeof(TChar)};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TConcrete>
|
||||||
|
template<typename T>
|
||||||
|
auto& MixinMemoryView<TConcrete>::dataAt(std::size_t offset) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
||||||
|
MIJIN_ASSERT(offset + sizeof(T) < byteSizeImpl(), "Buffer access out-of-range.");
|
||||||
|
|
||||||
|
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||||
|
return *std::bit_cast<return_t*>(bytes().data() + offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TConcrete>
|
||||||
|
template<typename T>
|
||||||
|
auto& MixinMemoryView<TConcrete>::dataAt(std::size_t offset) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
||||||
|
MIJIN_ASSERT(offset + sizeof(T) < byteSizeImpl(), "Buffer access out-of-range.");
|
||||||
|
|
||||||
|
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||||
|
return *std::bit_cast<return_t*>(bytes().data() + offset);
|
||||||
|
}
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_CONTAINER_MEMORY_VIEW_HPP_INCLUDED)
|
#endif // !defined(MIJIN_CONTAINER_MEMORY_VIEW_HPP_INCLUDED)
|
||||||
|
|||||||
@ -10,7 +10,9 @@
|
|||||||
#include <span>
|
#include <span>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "./memory_view.hpp"
|
||||||
#include "../debug/assert.hpp"
|
#include "../debug/assert.hpp"
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@ -27,17 +29,26 @@ namespace mijin
|
|||||||
// public types
|
// public types
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename T>
|
template<typename T, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
class BufferView;
|
class BufferView;
|
||||||
|
|
||||||
class TypelessBuffer
|
template<template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
|
class BaseTypelessBuffer : public MixinMemoryView<BaseTypelessBuffer<TAllocator>>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using size_type = std::size_t;
|
using size_type = std::size_t;
|
||||||
private:
|
private:
|
||||||
std::vector<std::byte> bytes_;
|
std::vector<std::byte, TAllocator<std::byte>> bytes_;
|
||||||
public:
|
public:
|
||||||
auto operator<=>(const TypelessBuffer&) const noexcept = default;
|
BaseTypelessBuffer() noexcept = default;
|
||||||
|
BaseTypelessBuffer(const BaseTypelessBuffer&) = default;
|
||||||
|
BaseTypelessBuffer(BaseTypelessBuffer&&) = default;
|
||||||
|
explicit BaseTypelessBuffer(TAllocator<std::byte> allocator) : bytes_(std::move(allocator)) {}
|
||||||
|
|
||||||
|
BaseTypelessBuffer& operator=(const BaseTypelessBuffer&) = default;
|
||||||
|
BaseTypelessBuffer& operator=(BaseTypelessBuffer&&) = default;
|
||||||
|
|
||||||
|
auto operator<=>(const BaseTypelessBuffer&) const noexcept = default;
|
||||||
|
|
||||||
[[nodiscard]] void* data() noexcept { return bytes_.data(); }
|
[[nodiscard]] void* data() noexcept { return bytes_.data(); }
|
||||||
[[nodiscard]] const void* data() const noexcept { return bytes_.data(); }
|
[[nodiscard]] const void* data() const noexcept { return bytes_.data(); }
|
||||||
@ -49,27 +60,14 @@ public:
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
[[nodiscard]] BufferView<T> makeBufferView() { return BufferView<T>(this); }
|
[[nodiscard]] BufferView<T> makeBufferView() { return BufferView<T>(this); }
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] std::span<T> makeSpan();
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] std::span<const T> makeSpan() const;
|
|
||||||
|
|
||||||
template<typename TChar = char, typename TTraits = std::char_traits<TChar>>
|
|
||||||
[[nodiscard]] std::basic_string_view<TChar, TTraits> makeStringView() const ;
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void append(std::span<const T> data);
|
void append(std::span<const T> data);
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] T& dataAt(size_type offset) MIJIN_NOEXCEPT;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] const T& dataAt(size_type offset) const MIJIN_NOEXCEPT;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
using TypelessBuffer = BaseTypelessBuffer<>;
|
||||||
|
|
||||||
|
template<typename T, template<typename> typename TAllocator>
|
||||||
class BufferView
|
class BufferView
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -84,10 +82,10 @@ public:
|
|||||||
using iterator = T*;
|
using iterator = T*;
|
||||||
using const_iterator = const T*;
|
using const_iterator = const T*;
|
||||||
private:
|
private:
|
||||||
class TypelessBuffer* buffer_ = nullptr;
|
class BaseTypelessBuffer<TAllocator>* buffer_ = nullptr;
|
||||||
public:
|
public:
|
||||||
BufferView() = default;
|
BufferView() = default;
|
||||||
explicit BufferView(class TypelessBuffer* buffer) : buffer_(buffer) {}
|
explicit BufferView(class BaseTypelessBuffer<TAllocator>* buffer) : buffer_(buffer) {}
|
||||||
BufferView(const BufferView&) = default;
|
BufferView(const BufferView&) = default;
|
||||||
|
|
||||||
BufferView& operator=(const BufferView&) = default;
|
BufferView& operator=(const BufferView&) = default;
|
||||||
@ -131,57 +129,13 @@ public:
|
|||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::span<T> TypelessBuffer::makeSpan()
|
void BaseTypelessBuffer<TAllocator>::append(std::span<const T> data)
|
||||||
{
|
|
||||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
|
||||||
return {
|
|
||||||
std::bit_cast<T*>(bytes_.data()),
|
|
||||||
std::bit_cast<T*>(bytes_.data() + bytes_.size())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::span<const T> TypelessBuffer::makeSpan() const
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
|
||||||
return {
|
|
||||||
std::bit_cast<const T*>(bytes_.data()),
|
|
||||||
std::bit_cast<const T*>(bytes_.data() + bytes_.size())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits>
|
|
||||||
std::basic_string_view<TChar, TTraits> TypelessBuffer::makeStringView() const
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(bytes_.size() % sizeof(TChar) == 0, "Buffer cannot be divided into elements of this char type.");
|
|
||||||
return {std::bit_cast<const TChar*>(bytes_.data()), bytes_.size() / sizeof(TChar)};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void TypelessBuffer::append(std::span<const T> data)
|
|
||||||
{
|
{
|
||||||
bytes_.resize(bytes_.size() + data.size_bytes());
|
bytes_.resize(bytes_.size() + data.size_bytes());
|
||||||
std::memcpy(bytes_.data() + bytes_.size() - data.size_bytes(), data.data(), data.size_bytes());
|
std::memcpy(bytes_.data() + bytes_.size() - data.size_bytes(), data.data(), data.size_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T& TypelessBuffer::dataAt(size_type offset) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
|
||||||
MIJIN_ASSERT(offset + sizeof(T) < bytes_.size(), "Buffer access out-of-range.");
|
|
||||||
|
|
||||||
return *std::bit_cast<T*>(bytes_.data() + offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
const T& TypelessBuffer::dataAt(size_type offset) const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
|
||||||
MIJIN_ASSERT(offset + sizeof(T) < bytes_.size(), "Buffer access out-of-range.");
|
|
||||||
|
|
||||||
return *std::bit_cast<const T*>(bytes_.data() + offset);
|
|
||||||
}
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_CONTAINER_TYPELESS_BUFFER_HPP_INCLUDED)
|
#endif // !defined(MIJIN_CONTAINER_TYPELESS_BUFFER_HPP_INCLUDED)
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "./boxed_object.hpp"
|
#include "./boxed_object.hpp"
|
||||||
#include "./optional.hpp"
|
#include "./optional.hpp"
|
||||||
|
|
||||||
@ -102,7 +103,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TKey, typename TValue, typename TKeyAllocator = std::allocator<TKey>, typename TValueAllocator = std::allocator<TValue>>
|
template<typename TKey, typename TValue, typename TKeyAllocator = MIJIN_DEFAULT_ALLOCATOR<TKey>, typename TValueAllocator = MIJIN_DEFAULT_ALLOCATOR<TValue>>
|
||||||
class VectorMap
|
class VectorMap
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -119,12 +120,17 @@ private:
|
|||||||
std::vector<TKey, TKeyAllocator> keys_;
|
std::vector<TKey, TKeyAllocator> keys_;
|
||||||
std::vector<TValue, TValueAllocator> values_;
|
std::vector<TValue, TValueAllocator> values_;
|
||||||
public:
|
public:
|
||||||
VectorMap() noexcept = default;
|
explicit VectorMap(TKeyAllocator keyAllocator = {})
|
||||||
|
MIJIN_NOEXCEPT_IF((std::is_nothrow_move_constructible_v<TKeyAllocator> && std::is_nothrow_constructible_v<TValueAllocator, const TKeyAllocator&>))
|
||||||
|
: keys_(std::move(keyAllocator)), values_(TValueAllocator(keys_.get_allocator())) {}
|
||||||
|
VectorMap(TKeyAllocator keyAllocator, TValueAllocator valueAllocator)
|
||||||
|
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TKeyAllocator> && std::is_nothrow_move_constructible_v<TValueAllocator>)
|
||||||
|
: keys_(std::move(keyAllocator)), values_(std::move(valueAllocator)) {}
|
||||||
VectorMap(const VectorMap&) = default;
|
VectorMap(const VectorMap&) = default;
|
||||||
VectorMap(VectorMap&&) MIJIN_NOEXCEPT = default;
|
VectorMap(VectorMap&&) = default;
|
||||||
|
|
||||||
VectorMap& operator=(const VectorMap&) = default;
|
VectorMap& operator=(const VectorMap&) = default;
|
||||||
VectorMap& operator=(VectorMap&&) MIJIN_NOEXCEPT = default;
|
VectorMap& operator=(VectorMap&&) = default;
|
||||||
auto operator<=>(const VectorMap& other) const noexcept = default;
|
auto operator<=>(const VectorMap& other) const noexcept = default;
|
||||||
|
|
||||||
TValue& operator[](const TKey& key)
|
TValue& operator[](const TKey& key)
|
||||||
|
|||||||
@ -45,7 +45,7 @@ namespace mijin
|
|||||||
|
|
||||||
#if MIJIN_DEBUG
|
#if MIJIN_DEBUG
|
||||||
|
|
||||||
#define MIJIN_RAISE_ERROR(msg, source_loc) \
|
#define MIJIN_RAISE_ERROR(msg, source_loc) \
|
||||||
switch (mijin::handleError(msg, source_loc)) \
|
switch (mijin::handleError(msg, source_loc)) \
|
||||||
{ \
|
{ \
|
||||||
case mijin::ErrorHandling::CONTINUE: \
|
case mijin::ErrorHandling::CONTINUE: \
|
||||||
@ -99,10 +99,10 @@ if (!static_cast<bool>(condition)) \
|
|||||||
MIJIN_FATAL("Debug assertion failed: " #condition "\nMessage: " msg); \
|
MIJIN_FATAL("Debug assertion failed: " #condition "\nMessage: " msg); \
|
||||||
}
|
}
|
||||||
#else // MIJIN_DEBUG
|
#else // MIJIN_DEBUG
|
||||||
#define MIJIN_ERROR(...)
|
#define MIJIN_ERROR(...) ((void)0)
|
||||||
#define MIJIN_FATAL(...) std::abort()
|
#define MIJIN_FATAL(...) std::abort()
|
||||||
#define MIJIN_ASSERT(...)
|
#define MIJIN_ASSERT(...) ((void)0)
|
||||||
#define MIJIN_ASSERT_FATAL(...)
|
#define MIJIN_ASSERT_FATAL(...) ((void)0)
|
||||||
#endif // !MIJIN_DEBUG
|
#endif // !MIJIN_DEBUG
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@ -4,15 +4,24 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../detect.hpp"
|
#include "../detect.hpp"
|
||||||
|
#include "../util/string.hpp"
|
||||||
|
|
||||||
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG || MIJIN_COMPILER == MIJIN_COMPILER_GCC
|
#if MIJIN_TARGET_OS != MIJIN_OS_WINDOWS && (MIJIN_COMPILER == MIJIN_COMPILER_CLANG || MIJIN_COMPILER == MIJIN_COMPILER_GCC)
|
||||||
#define MIJIN_USE_LIBBACKTRACE 1
|
#define MIJIN_USE_LIBBACKTRACE 1
|
||||||
#else
|
#else
|
||||||
#define MIJIN_USE_LIBBACKTRACE 0
|
#define MIJIN_USE_LIBBACKTRACE 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MIJIN_USE_LIBBACKTRACE
|
#if MIJIN_USE_LIBBACKTRACE
|
||||||
#include <backtrace.h>
|
#include <backtrace.h>
|
||||||
|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <mutex>
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <DbgHelp.h>
|
||||||
|
#include "../util/winundef.hpp"
|
||||||
|
#pragma comment(lib, "dbghelp")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -32,11 +41,15 @@ namespace
|
|||||||
// internal types
|
// internal types
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#if MIJIN_USE_LIBBACKTRACE
|
||||||
struct BacktraceData
|
struct BacktraceData
|
||||||
{
|
{
|
||||||
std::optional<std::string> error;
|
std::optional<std::string> error;
|
||||||
std::vector<Stackframe> stackframes;
|
std::vector<Stackframe> stackframes;
|
||||||
};
|
};
|
||||||
|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
HANDLE gProcessHandle = nullptr;
|
||||||
|
#endif
|
||||||
|
|
||||||
//
|
//
|
||||||
// internal variables
|
// internal variables
|
||||||
@ -44,6 +57,11 @@ struct BacktraceData
|
|||||||
|
|
||||||
thread_local Optional<Stacktrace> gCurrentExceptionStackTrace;
|
thread_local Optional<Stacktrace> gCurrentExceptionStackTrace;
|
||||||
|
|
||||||
|
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
std::mutex gDbgHelpMutex;
|
||||||
|
bool gDbgHelpInitCalled = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
//
|
//
|
||||||
// internal functions
|
// internal functions
|
||||||
//
|
//
|
||||||
@ -68,6 +86,49 @@ void backtraceErrorCallback(void* data, const char* msg, int /* errnum */)
|
|||||||
}
|
}
|
||||||
|
|
||||||
thread_local backtrace_state* gBacktraceState = nullptr;
|
thread_local backtrace_state* gBacktraceState = nullptr;
|
||||||
|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
void cleanupDbgHelp() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (!SymCleanup(gProcessHandle))
|
||||||
|
{
|
||||||
|
[[maybe_unused]] const DWORD error = GetLastError();
|
||||||
|
MIJIN_ERROR("Error cleaning up DbgHelp.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
bool initDbgHelp() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (gDbgHelpInitCalled)
|
||||||
|
{
|
||||||
|
return gProcessHandle != nullptr; // if init was successful, process handle is not null
|
||||||
|
}
|
||||||
|
gDbgHelpInitCalled = true;
|
||||||
|
|
||||||
|
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
|
||||||
|
|
||||||
|
HANDLE hCurrentProcess = GetCurrentProcess();
|
||||||
|
HANDLE hCopy = nullptr;
|
||||||
|
if (!DuplicateHandle(hCurrentProcess, hCurrentProcess, hCurrentProcess, &hCopy, 0, FALSE, DUPLICATE_SAME_ACCESS))
|
||||||
|
{
|
||||||
|
[[maybe_unused]] const DWORD error = GetLastError();
|
||||||
|
MIJIN_ERROR("Error duplicating process handle.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!SymInitialize(hCopy, nullptr, true))
|
||||||
|
{
|
||||||
|
[[maybe_unused]] const DWORD error = GetLastError();
|
||||||
|
MIJIN_ERROR("Error initializing DbHelp.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
gProcessHandle = hCopy;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
#endif // MIJIN_USE_LIBBACKTRACE
|
#endif // MIJIN_USE_LIBBACKTRACE
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@ -98,9 +159,87 @@ Result<Stacktrace> captureStacktrace(unsigned skipFrames) MIJIN_NOEXCEPT
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Stacktrace(std::move(btData.stackframes));
|
return Stacktrace(std::move(btData.stackframes));
|
||||||
#else // MIJIN_USE_LIBBACKTRACE
|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
if (!initDbgHelp())
|
||||||
|
{
|
||||||
|
return ResultError("error initializing DbgHelp");
|
||||||
|
}
|
||||||
|
|
||||||
|
const HANDLE hThread = GetCurrentThread();
|
||||||
|
|
||||||
|
CONTEXT context;
|
||||||
|
RtlCaptureContext(&context);
|
||||||
|
|
||||||
|
STACKFRAME64 stackFrame = {
|
||||||
|
.AddrPC = {
|
||||||
|
.Offset = context.Rip,
|
||||||
|
.Mode = AddrModeFlat
|
||||||
|
},
|
||||||
|
.AddrFrame = {
|
||||||
|
.Offset = context.Rbp,
|
||||||
|
.Mode = AddrModeFlat
|
||||||
|
},
|
||||||
|
.AddrStack = {
|
||||||
|
.Offset = context.Rsp,
|
||||||
|
.Mode = AddrModeFlat
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
++skipFrames; // always skip the first frame (the current function)
|
||||||
|
|
||||||
|
// for symbol info
|
||||||
|
DWORD64 displacement64 = 0;
|
||||||
|
static constexpr std::size_t SYMBOL_BUFFER_SIZE = sizeof(SYMBOL_INFO) + (MAX_SYM_NAME * sizeof(char));
|
||||||
|
std::array<std::byte, SYMBOL_BUFFER_SIZE> symbolBuffer alignas(SYMBOL_INFO);
|
||||||
|
SYMBOL_INFO& symbolInfo = *std::bit_cast<SYMBOL_INFO*>(symbolBuffer.data());
|
||||||
|
symbolInfo.SizeOfStruct = sizeof(SYMBOL_BUFFER_SIZE);
|
||||||
|
symbolInfo.MaxNameLen = MAX_SYM_NAME;
|
||||||
|
|
||||||
|
// for file and line info
|
||||||
|
DWORD displacement = 0;
|
||||||
|
IMAGEHLP_LINE64 line64;
|
||||||
|
line64.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
||||||
|
|
||||||
|
std::vector<Stackframe> stackframes;
|
||||||
|
while (StackWalk64(
|
||||||
|
/* MachineType = */ IMAGE_FILE_MACHINE_AMD64,
|
||||||
|
/* hProcess = */ gProcessHandle,
|
||||||
|
/* hThread = */ hThread,
|
||||||
|
/* StackFrame = */ &stackFrame,
|
||||||
|
/* ContextRecord = */ &context,
|
||||||
|
/* ReadMemoryRoutine = */ nullptr,
|
||||||
|
/* FunctionTableAccessRoutine = */ &SymFunctionTableAccess64,
|
||||||
|
/* GetModuleBaseRoutine = */ &SymGetModuleBase64,
|
||||||
|
/* TranslateAddress = */ nullptr
|
||||||
|
))
|
||||||
|
{
|
||||||
|
if (skipFrames > 0)
|
||||||
|
{
|
||||||
|
--skipFrames;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Stackframe& frame = stackframes.emplace_back();
|
||||||
|
const DWORD64 baseAddress = SymGetModuleBase64(gProcessHandle, stackFrame.AddrPC.Offset);
|
||||||
|
const DWORD64 relativeAddress = stackFrame.AddrPC.Offset - baseAddress;
|
||||||
|
|
||||||
|
frame.address = std::bit_cast<void*>(relativeAddress);
|
||||||
|
|
||||||
|
if (SymFromAddr(gProcessHandle, stackFrame.AddrPC.Offset, &displacement64, &symbolInfo))
|
||||||
|
{
|
||||||
|
frame.function = symbolInfo.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SymGetLineFromAddr64(gProcessHandle, stackFrame.AddrPC.Offset, &displacement, &line64))
|
||||||
|
{
|
||||||
|
frame.filename = line64.FileName;
|
||||||
|
frame.lineNumber = static_cast<int>(line64.LineNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Stacktrace(std::move(stackframes));
|
||||||
|
#else // MIJIN_USE_LIBBACKTRACE || (MIJIN_TARGET_OS == MIJIN_OS_WINDOWS)
|
||||||
(void) skipFrames;
|
(void) skipFrames;
|
||||||
return {}; // TODO
|
return ResultError("not implemented");
|
||||||
#endif // MIJIN_USE_LIBBACKTRACE
|
#endif // MIJIN_USE_LIBBACKTRACE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#define MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED 1
|
#define MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <format>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#if __has_include(<fmt/format.h>)
|
#if __has_include(<fmt/format.h>)
|
||||||
@ -87,6 +88,68 @@ TStream& operator<<(TStream& stream, const Stacktrace& stacktrace)
|
|||||||
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
|
template<typename TChar>
|
||||||
|
struct std::formatter<mijin::Stackframe, TChar>
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
|
||||||
|
template<typename TContext>
|
||||||
|
constexpr TContext::iterator parse(TContext& ctx)
|
||||||
|
{
|
||||||
|
auto it = ctx.begin();
|
||||||
|
auto end = ctx.end();
|
||||||
|
|
||||||
|
if (it != end && *it != MIJIN_SMART_QUOTE(char_t, '}'))
|
||||||
|
{
|
||||||
|
throw std::format_error("invalid format");
|
||||||
|
}
|
||||||
|
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TContext>
|
||||||
|
TContext::iterator format(const mijin::Stackframe& stackframe, TContext& ctx) const
|
||||||
|
{
|
||||||
|
auto it = ctx.out();
|
||||||
|
it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "[{}] {}:{} in {}"), stackframe.address, stackframe.filename,
|
||||||
|
stackframe.lineNumber, mijin::demangleCPPIdentifier(stackframe.function.c_str()));
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TChar>
|
||||||
|
struct std::formatter<mijin::Stacktrace, TChar>
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
|
||||||
|
template<class TContext>
|
||||||
|
constexpr TContext::iterator parse(TContext& ctx)
|
||||||
|
{
|
||||||
|
auto it = ctx.begin();
|
||||||
|
auto end = ctx.end();
|
||||||
|
|
||||||
|
if (it != end && *it != MIJIN_SMART_QUOTE(char_t, '}'))
|
||||||
|
{
|
||||||
|
throw std::format_error("invalid format");
|
||||||
|
}
|
||||||
|
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TContext>
|
||||||
|
TContext::iterator format(const mijin::Stacktrace& stacktrace, TContext& ctx) const
|
||||||
|
{
|
||||||
|
const int numDigits = static_cast<int>(std::ceil(std::log10(stacktrace.getFrames().size())));
|
||||||
|
auto it = ctx.out();
|
||||||
|
it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "[{} frames]"), stacktrace.getFrames().size());
|
||||||
|
for (const auto& [idx, frame] : mijin::enumerate(stacktrace.getFrames()))
|
||||||
|
{
|
||||||
|
it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "\n #{:<{}} at {}"), idx, numDigits, frame);
|
||||||
|
}
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#if __has_include(<fmt/format.h>)
|
#if __has_include(<fmt/format.h>)
|
||||||
template<>
|
template<>
|
||||||
struct fmt::formatter<mijin::Stackframe>
|
struct fmt::formatter<mijin::Stackframe>
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "./config.hpp"
|
||||||
|
#include "./helpers.hpp"
|
||||||
#include "./exception.hpp"
|
#include "./exception.hpp"
|
||||||
|
#include "./version_support.hpp"
|
||||||
|
|||||||
22
source/mijin/internal/config.hpp
Normal file
22
source/mijin/internal/config.hpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_INTERNAL_CONFIG_HPP_INCLUDED)
|
||||||
|
#define MIJIN_INTERNAL_CONFIG_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#define MIJIN_QUOTED_ACTUAL(x) #x
|
||||||
|
#define MIJIN_QUOTED(x) MIJIN_QUOTED_ACTUAL(x)
|
||||||
|
|
||||||
|
#if defined(MIJIN_CONFIG_HEADER)
|
||||||
|
#include MIJIN_QUOTED(MIJIN_CONFIG_HEADER)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_DEFAULT_ALLOCATOR)
|
||||||
|
#define MIJIN_DEFAULT_ALLOCATOR std::allocator
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_DEFAULT_CHAR_TYPE)
|
||||||
|
#define MIJIN_DEFAULT_CHAR_TYPE char
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_INTERNAL_CONFIG_HPP_INCLUDED)
|
||||||
@ -23,10 +23,12 @@
|
|||||||
#else
|
#else
|
||||||
#if defined(MIJIN_TEST_NO_NOEXCEPT) // only use for testing
|
#if defined(MIJIN_TEST_NO_NOEXCEPT) // only use for testing
|
||||||
#define MIJIN_NOEXCEPT
|
#define MIJIN_NOEXCEPT
|
||||||
|
#define MIJIN_NOEXCEPT_IF(x)
|
||||||
#define MIJIN_THROWS
|
#define MIJIN_THROWS
|
||||||
#define MIJIN_CONDITIONAL_NOEXCEPT(...)
|
#define MIJIN_CONDITIONAL_NOEXCEPT(...)
|
||||||
#else
|
#else
|
||||||
#define MIJIN_NOEXCEPT noexcept
|
#define MIJIN_NOEXCEPT noexcept
|
||||||
|
#define MIJIN_NOEXCEPT_IF(x) noexcept(x)
|
||||||
#define MIJIN_THROWS noexcept
|
#define MIJIN_THROWS noexcept
|
||||||
#define MIJIN_CONDITIONAL_NOEXCEPT(...) noexcept(__VA_ARGS__)
|
#define MIJIN_CONDITIONAL_NOEXCEPT(...) noexcept(__VA_ARGS__)
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
57
source/mijin/internal/helpers.hpp
Normal file
57
source/mijin/internal/helpers.hpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_INTERNAL_HELPERS_HPP_INCLUDED)
|
||||||
|
#define MIJIN_INTERNAL_HELPERS_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
|
#define MIJIN_IDENTITY(what) what
|
||||||
|
#define MIJIN_NULLIFY(what)
|
||||||
|
|
||||||
|
#define MIJIN_SMART_QUOTE(chr_type, text) \
|
||||||
|
[]<typename TChar__>(TChar__) consteval \
|
||||||
|
{ \
|
||||||
|
if constexpr (std::is_same_v<TChar__, char>) \
|
||||||
|
{ \
|
||||||
|
return text; \
|
||||||
|
} \
|
||||||
|
else if constexpr (std::is_same_v<TChar__, wchar_t>) \
|
||||||
|
{ \
|
||||||
|
return L ## text; \
|
||||||
|
} \
|
||||||
|
else if constexpr (std::is_same_v<TChar__, char8_t>) \
|
||||||
|
{ \
|
||||||
|
return u8 ## text; \
|
||||||
|
} \
|
||||||
|
else \
|
||||||
|
{ \
|
||||||
|
static_assert(::mijin::always_false_v<TChar__>, "Invalid char type."); \
|
||||||
|
} \
|
||||||
|
}(chr_type())
|
||||||
|
|
||||||
|
#define MIJIN_SMART_STRINGIFY(chr_type, text) MIJIN_SMART_QUOTE(chr_type, #text)
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_CHAR_VERSIONS_IMPL(type_name, prefix_a, prefix_b, prefix_c, set_args) \
|
||||||
|
prefix_a prefix_b(char) prefix_c \
|
||||||
|
using C ## type_name = Base ## type_name <set_args(char)>; \
|
||||||
|
\
|
||||||
|
prefix_a prefix_b(wchar_t) prefix_c \
|
||||||
|
using W ## type_name = Base ## type_name <set_args(wchar_t)>; \
|
||||||
|
\
|
||||||
|
prefix_a prefix_b(char8_t) prefix_c \
|
||||||
|
using U ## type_name = Base ## type_name <set_args(char8_t)>; \
|
||||||
|
\
|
||||||
|
using type_name = Base ## type_name<>;
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_CHAR_VERSIONS_TMPL(type_name, remaining_args, set_args) \
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_IMPL(type_name, template<, remaining_args, >, set_args)
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_CHAR_VERSIONS_CUSTOM(type_name, set_args) \
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_IMPL(type_name, , MIJIN_NULLIFY, , set_args)
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_CHAR_VERSIONS(type_name) \
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_CUSTOM(type_name, MIJIN_IDENTITY)
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_INTERNAL_HELPERS_HPP_INCLUDED)
|
||||||
19
source/mijin/internal/version_support.hpp
Normal file
19
source/mijin/internal/version_support.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_INTERNAL_VERSION_SUPPORT_HPP_INCLUDED)
|
||||||
|
#define MIJIN_INTERNAL_VERSION_SUPPORT_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "../detect.hpp"
|
||||||
|
|
||||||
|
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG
|
||||||
|
#pragma clang diagnostic ignored "-Wc++26-extensions"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__cpp_deleted_function)
|
||||||
|
#define MIJIN_DELETE(reason) = delete(reason)
|
||||||
|
#else
|
||||||
|
#define MIJIN_DELETE(reason) = delete
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_INTERNAL_VERSION_SUPPORT_HPP_INCLUDED)
|
||||||
@ -197,76 +197,6 @@ StreamError Stream::getTotalLength(std::size_t& outLength)
|
|||||||
return StreamError::SUCCESS;
|
return StreamError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamError Stream::readRest(TypelessBuffer& outBuffer)
|
|
||||||
{
|
|
||||||
// first try to allocate everything at once
|
|
||||||
std::size_t length = 0;
|
|
||||||
if (const StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
|
||||||
length -= tell();
|
|
||||||
outBuffer.resize(length);
|
|
||||||
if (const StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
return StreamError::SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
// could not determine the size, read chunk-wise
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
|
||||||
const std::span<std::byte> bufferBytes = outBuffer.makeSpan<std::byte>();
|
|
||||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
|
||||||
}
|
|
||||||
return StreamError::SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
mijin::Task<StreamError> Stream::c_readRest(TypelessBuffer& outBuffer)
|
|
||||||
{
|
|
||||||
// first try to allocate everything at once
|
|
||||||
std::size_t length = 0;
|
|
||||||
if (StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
|
||||||
length -= tell();
|
|
||||||
outBuffer.resize(length);
|
|
||||||
if (StreamError error = co_await c_readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
co_return error;
|
|
||||||
}
|
|
||||||
co_return StreamError::SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
// could not determine the size, read chunk-wise
|
|
||||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
|
||||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
|
||||||
|
|
||||||
while (!isAtEnd())
|
|
||||||
{
|
|
||||||
std::size_t bytesRead = 0;
|
|
||||||
if (StreamError error = co_await c_readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
co_return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
|
||||||
std::span<std::byte> bufferBytes = outBuffer.makeSpan<std::byte>();
|
|
||||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
|
||||||
}
|
|
||||||
co_return StreamError::SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamError Stream::readLine(std::string& outString)
|
StreamError Stream::readLine(std::string& outString)
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(getFeatures().readOptions.peek, "Stream needs to support peeking.");
|
MIJIN_ASSERT(getFeatures().readOptions.peek, "Stream needs to support peeking.");
|
||||||
|
|||||||
@ -132,8 +132,9 @@ public:
|
|||||||
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
||||||
return readRaw(&*range.begin(), bytes, {.partial = partial}, outBytesRead);
|
return readRaw(&*range.begin(), bytes, {.partial = partial}, outBytesRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamError readRaw(TypelessBuffer& buffer, const ReadOptions& options = {})
|
template<template<typename> typename TAllocator>
|
||||||
|
StreamError readRaw(BaseTypelessBuffer<TAllocator>& buffer, const ReadOptions& options = {})
|
||||||
{
|
{
|
||||||
return readRaw(buffer.data(), buffer.byteSize(), options);
|
return readRaw(buffer.data(), buffer.byteSize(), options);
|
||||||
}
|
}
|
||||||
@ -144,8 +145,9 @@ public:
|
|||||||
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
||||||
return c_readRaw(&*range.begin(), bytes, options, outBytesRead);
|
return c_readRaw(&*range.begin(), bytes, options, outBytesRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
mijin::Task<StreamError> c_readRaw(TypelessBuffer& buffer, const ReadOptions& options = {})
|
template<template<typename> typename TAllocator>
|
||||||
|
mijin::Task<StreamError> c_readRaw(BaseTypelessBuffer<TAllocator>& buffer, const ReadOptions& options = {})
|
||||||
{
|
{
|
||||||
return c_readRaw(buffer.data(), buffer.byteSize(), options);
|
return c_readRaw(buffer.data(), buffer.byteSize(), options);
|
||||||
}
|
}
|
||||||
@ -272,17 +274,21 @@ public:
|
|||||||
inline StreamError writeString(std::string_view str) { return writeBinaryString(str); }
|
inline StreamError writeString(std::string_view str) { return writeBinaryString(str); }
|
||||||
|
|
||||||
StreamError getTotalLength(std::size_t& outLength);
|
StreamError getTotalLength(std::size_t& outLength);
|
||||||
StreamError readRest(TypelessBuffer& outBuffer);
|
|
||||||
mijin::Task<StreamError> c_readRest(TypelessBuffer& outBuffer);
|
template<template<typename> typename TAllocator>
|
||||||
|
StreamError readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
mijin::Task<StreamError> c_readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||||
|
|
||||||
StreamError readLine(std::string& outString);
|
StreamError readLine(std::string& outString);
|
||||||
mijin::Task<StreamError> c_readLine(std::string& outString);
|
mijin::Task<StreamError> c_readLine(std::string& outString);
|
||||||
|
|
||||||
template<typename TChar = char>
|
template<typename TChar = char, typename TTraits = std::char_traits<TChar>, typename TAllocator = MIJIN_DEFAULT_ALLOCATOR<char>>
|
||||||
StreamError readAsString(std::basic_string<TChar>& outString);
|
StreamError readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString);
|
||||||
|
|
||||||
template<typename TChar = char>
|
template<typename TChar = char, typename TTraits = std::char_traits<TChar>, typename TAllocator = MIJIN_DEFAULT_ALLOCATOR<char>>
|
||||||
mijin::Task<StreamError> c_readAsString(std::basic_string<TChar>& outString);
|
mijin::Task<StreamError> c_readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString);
|
||||||
|
|
||||||
StreamError writeText(std::string_view str)
|
StreamError writeText(std::string_view str)
|
||||||
{
|
{
|
||||||
@ -363,8 +369,80 @@ using StreamResult = ResultBase<TSuccess, StreamError>;
|
|||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename TChar>
|
template<template<typename> typename TAllocator>
|
||||||
StreamError Stream::readAsString(std::basic_string<TChar>& outString)
|
StreamError Stream::readRest(BaseTypelessBuffer<TAllocator>& outBuffer)
|
||||||
|
{
|
||||||
|
// first try to allocate everything at once
|
||||||
|
std::size_t length = 0;
|
||||||
|
if (const StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||||
|
length -= tell();
|
||||||
|
outBuffer.resize(length);
|
||||||
|
if (const StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// could not determine the size, read chunk-wise
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||||
|
const std::span<std::byte> bufferBytes = outBuffer.template makeSpan<std::byte>();
|
||||||
|
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||||
|
}
|
||||||
|
return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<template<typename> typename TAllocator>
|
||||||
|
mijin::Task<StreamError> Stream::c_readRest(BaseTypelessBuffer<TAllocator>& outBuffer)
|
||||||
|
{
|
||||||
|
// first try to allocate everything at once
|
||||||
|
std::size_t length = 0;
|
||||||
|
if (StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||||
|
length -= tell();
|
||||||
|
outBuffer.resize(length);
|
||||||
|
if (StreamError error = co_await c_readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
co_return error;
|
||||||
|
}
|
||||||
|
co_return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// could not determine the size, read chunk-wise
|
||||||
|
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||||
|
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||||
|
|
||||||
|
while (!isAtEnd())
|
||||||
|
{
|
||||||
|
std::size_t bytesRead = 0;
|
||||||
|
if (StreamError error = co_await c_readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
co_return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||||
|
std::span<std::byte> bufferBytes = outBuffer.template makeSpan<std::byte>();
|
||||||
|
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||||
|
}
|
||||||
|
co_return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits, typename TAllocator>
|
||||||
|
StreamError Stream::readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(TChar) == 1, "Can only read to 8-bit character types (char, unsigned char or char8_t");
|
static_assert(sizeof(TChar) == 1, "Can only read to 8-bit character types (char, unsigned char or char8_t");
|
||||||
|
|
||||||
@ -399,8 +477,8 @@ StreamError Stream::readAsString(std::basic_string<TChar>& outString)
|
|||||||
return StreamError::SUCCESS;
|
return StreamError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TChar>
|
template<typename TChar, typename TTraits, typename TAllocator>
|
||||||
mijin::Task<StreamError> Stream::c_readAsString(std::basic_string<TChar>& outString)
|
mijin::Task<StreamError> Stream::c_readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(TChar) == 1, "Can only read to 8-bit character types (char, unsigned char or char8_t");
|
static_assert(sizeof(TChar) == 1, "Can only read to 8-bit character types (char, unsigned char or char8_t");
|
||||||
|
|
||||||
|
|||||||
323
source/mijin/logging/formatting.hpp
Normal file
323
source/mijin/logging/formatting.hpp
Normal file
@ -0,0 +1,323 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOGGING_FORMATTING_HPP_INCLUDED)
|
||||||
|
#define MIJIN_LOGGING_FORMATTING_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <format>
|
||||||
|
#include <variant>
|
||||||
|
#include "./logger.hpp"
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../memory/dynamic_pointer.hpp"
|
||||||
|
#include "../memory/memutil.hpp"
|
||||||
|
#include "../memory/virtual_allocator.hpp"
|
||||||
|
#include "../util/annot.hpp"
|
||||||
|
#include "../util/ansi_colors.hpp"
|
||||||
|
#include "../util/concepts.hpp"
|
||||||
|
#include "../util/string.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
#define FORMATTER_COMMON_ARGS(chr_type) allocator_type_for<chr_type> TAllocator = MIJIN_DEFAULT_ALLOCATOR<chr_type>
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>,
|
||||||
|
FORMATTER_COMMON_ARGS(TChar)>
|
||||||
|
class BaseLogFormatter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using char_t = TChar;
|
||||||
|
using traits_t = TTraits;
|
||||||
|
using allocator_t = TAllocator;
|
||||||
|
using string_t = std::basic_string<char_t, traits_t, allocator_t>;
|
||||||
|
|
||||||
|
virtual ~BaseLogFormatter() noexcept = default;
|
||||||
|
|
||||||
|
virtual void format(const LogMessage& message, string_t& outFormatted) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>,
|
||||||
|
FORMATTER_COMMON_ARGS(TChar)>
|
||||||
|
class BaseSimpleLogFormatter : public BaseLogFormatter<TChar, TTraits, TAllocator>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using char_t = TChar;
|
||||||
|
using traits_t = TTraits;
|
||||||
|
using allocator_t = TAllocator;
|
||||||
|
using base_t = BaseLogFormatter<char_t, traits_t, allocator_t>;
|
||||||
|
using typename base_t::string_t;
|
||||||
|
using string_view_t = std::basic_string_view<char_t, traits_t>;
|
||||||
|
private:
|
||||||
|
string_t mFormat;
|
||||||
|
string_t mFormatBuffer;
|
||||||
|
public:
|
||||||
|
explicit BaseSimpleLogFormatter(string_t format) MIJIN_NOEXCEPT : mFormat(std::move(format)), mFormatBuffer(mFormat.get_allocator()) {}
|
||||||
|
|
||||||
|
void format(const LogMessage& message, string_t& outFormatted) override;
|
||||||
|
private:
|
||||||
|
void formatAnsiSequence(const LogMessage& message, string_view_t ansiName, string_t& outFormatted);
|
||||||
|
};
|
||||||
|
|
||||||
|
#define FORMATTER_SET_ARGS(chr_type) chr_type, std::char_traits<chr_type>, TAllocator
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(LogFormatter, FORMATTER_COMMON_ARGS, FORMATTER_SET_ARGS)
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(SimpleLogFormatter, FORMATTER_COMMON_ARGS, FORMATTER_SET_ARGS)
|
||||||
|
|
||||||
|
#undef FORMATTER_COMMON_ARGS
|
||||||
|
#undef FORMATTER_SET_ARGS
|
||||||
|
|
||||||
|
#define MIJIN_FORMATTING_SINK_COMMON_ARGS(chr_type) \
|
||||||
|
typename TTraits = std::char_traits<chr_type>, \
|
||||||
|
template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, \
|
||||||
|
deleter_type<BaseLogFormatter<chr_type, TTraits, TAllocator<chr_type>>> TDeleter \
|
||||||
|
= AllocatorDeleter<TAllocator<BaseLogFormatter<chr_type, TTraits, TAllocator<chr_type>>>>
|
||||||
|
|
||||||
|
#define MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT \
|
||||||
|
typename TChar = MIJIN_DEFAULT_CHAR_TYPE, \
|
||||||
|
MIJIN_FORMATTING_SINK_COMMON_ARGS(TChar)
|
||||||
|
|
||||||
|
#define MIJIN_FORMATTING_SINK_TMP_ARG_NAMES TChar, TTraits, TAllocator, TDeleter
|
||||||
|
|
||||||
|
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
||||||
|
requires(allocator_type<TAllocator<TChar>>)
|
||||||
|
class BaseFormattingLogSink : public BaseLogSink<TChar>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using base_t = BaseLogSink<TChar>;
|
||||||
|
|
||||||
|
using char_t = TChar;
|
||||||
|
using traits_t = TTraits;
|
||||||
|
using allocator_t = TAllocator<TChar>;
|
||||||
|
using formatter_t = BaseLogFormatter<char_t, traits_t, allocator_t>;
|
||||||
|
using formatter_deleter_t = TDeleter;
|
||||||
|
using formatter_ptr_t = DynamicPointer<formatter_t, formatter_deleter_t>;
|
||||||
|
using string_t = formatter_t::string_t;
|
||||||
|
using typename base_t::message_t;
|
||||||
|
private:
|
||||||
|
not_null_t<formatter_ptr_t> mFormatter;
|
||||||
|
string_t mBuffer;
|
||||||
|
public:
|
||||||
|
explicit BaseFormattingLogSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||||
|
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||||
|
: mFormatter(std::move(formatter)), mBuffer(std::move(allocator))
|
||||||
|
{}
|
||||||
|
|
||||||
|
virtual void handleMessageFormatted(const message_t& message, const char_t* formatted) MIJIN_NOEXCEPT = 0;
|
||||||
|
|
||||||
|
void handleMessage(const message_t& message) noexcept override
|
||||||
|
{
|
||||||
|
mBuffer.clear();
|
||||||
|
mFormatter->format(message, mBuffer);
|
||||||
|
handleMessageFormatted(message, mBuffer.c_str());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define SINK_SET_ARGS(chr_type) chr_type, std::char_traits<chr_type>, TAllocator, TDeleter
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(FormattingLogSink, MIJIN_FORMATTING_SINK_COMMON_ARGS, SINK_SET_ARGS)
|
||||||
|
|
||||||
|
#undef SINK_SET_ARGS
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits, allocator_type_for<TChar> TAllocator>
|
||||||
|
void BaseSimpleLogFormatter<TChar, TTraits, TAllocator>::format(const LogMessage& message, string_t& outFormatted)
|
||||||
|
{
|
||||||
|
mFormatBuffer.clear();
|
||||||
|
for (auto pos = mFormat.begin(); pos != mFormat.end(); ++pos)
|
||||||
|
{
|
||||||
|
if (*pos == MIJIN_SMART_QUOTE(char_t, '{'))
|
||||||
|
{
|
||||||
|
++pos;
|
||||||
|
if (*pos == MIJIN_SMART_QUOTE(char_t, '{'))
|
||||||
|
{
|
||||||
|
// double {
|
||||||
|
outFormatted += MIJIN_SMART_QUOTE(char_t, '{');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const auto argStart = pos;
|
||||||
|
static const string_view_t endChars = MIJIN_SMART_QUOTE(char_t, ":}");
|
||||||
|
pos = std::find_first_of(pos, mFormat.end(), endChars.begin(), endChars.end());
|
||||||
|
MIJIN_ASSERT(pos != mFormat.end(), "Invalid format.");
|
||||||
|
|
||||||
|
const string_view_t argName(argStart, pos);
|
||||||
|
string_view_t argFormat;
|
||||||
|
if (*pos == ':')
|
||||||
|
{
|
||||||
|
const auto formatStart = pos;
|
||||||
|
pos = std::find(pos, mFormat.end(), MIJIN_SMART_QUOTE(char_t, '}'));
|
||||||
|
MIJIN_ASSERT(pos != mFormat.end(), "Invalid format.");
|
||||||
|
argFormat = string_view_t(formatStart, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// small utility that uses the provided string buffer for storing the format string
|
||||||
|
auto formatInline = [&](const auto& value)
|
||||||
|
{
|
||||||
|
using type_t = std::decay_t<decltype(value)>;
|
||||||
|
|
||||||
|
// if there is no format, just directly print the value
|
||||||
|
if (argFormat.empty())
|
||||||
|
{
|
||||||
|
if constexpr (is_char_v<type_t>)
|
||||||
|
{
|
||||||
|
convertStringType(string_view_t(&value, 1), outFormatted);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_arithmetic_v<type_t>)
|
||||||
|
{
|
||||||
|
std::format_to(std::back_inserter(outFormatted), MIJIN_SMART_QUOTE(char_t, "{}"), value);
|
||||||
|
}
|
||||||
|
else if constexpr (is_string_v<type_t> || is_cstring_v<type_t>)
|
||||||
|
{
|
||||||
|
convertStringType(value, outFormatted);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static_assert(always_false_v<type_t>);
|
||||||
|
outFormatted += value;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// first copy the format string + braces into the buffer
|
||||||
|
const auto formatStart = mFormatBuffer.size();
|
||||||
|
mFormatBuffer += '{';
|
||||||
|
mFormatBuffer += argFormat;
|
||||||
|
mFormatBuffer += '}';
|
||||||
|
const auto formatEnd = mFormatBuffer.size();
|
||||||
|
|
||||||
|
auto doFormatTo = [](string_t& string, string_view_t format, const auto& value)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<char_t, char>)
|
||||||
|
{
|
||||||
|
std::vformat_to(std::back_inserter(string), format, std::make_format_args(value));
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<char_t, wchar_t>)
|
||||||
|
{
|
||||||
|
std::vformat_to(std::back_inserter(string), format, std::make_wformat_args(value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static_assert(always_false_v<char_t>, "Cannot format this char type.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto doFormat = [&](const auto& value)
|
||||||
|
{
|
||||||
|
auto format = string_view_t(mFormatBuffer).substr(formatStart, formatEnd - formatStart);
|
||||||
|
doFormatTo(outFormatted, format, value);
|
||||||
|
};
|
||||||
|
|
||||||
|
if constexpr (is_char_v<type_t> && !std::is_same_v<char_t, type_t>)
|
||||||
|
{
|
||||||
|
static_assert(always_false_v<type_t>, "TODO...");
|
||||||
|
}
|
||||||
|
else if constexpr ((is_string_v<type_t> || is_cstring_v<type_t>) && !std::is_same_v<str_char_type_t<type_t>, char_t>)
|
||||||
|
{
|
||||||
|
// different string type, needs to be converted
|
||||||
|
const auto convertedStart = mFormatBuffer.size();
|
||||||
|
convertStringType(value, mFormatBuffer);
|
||||||
|
const auto convertedEnd = mFormatBuffer.size();
|
||||||
|
|
||||||
|
// then we can format it
|
||||||
|
auto converted = string_view_t(mFormatBuffer).substr(convertedStart, mFormatBuffer.size() - convertedEnd);
|
||||||
|
doFormat(converted);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// nothing special
|
||||||
|
doFormat(value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (argName == MIJIN_SMART_QUOTE(char_t, "text"))
|
||||||
|
{
|
||||||
|
formatInline(message.text);
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "channel"))
|
||||||
|
{
|
||||||
|
formatInline(message.channel->name);
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "file"))
|
||||||
|
{
|
||||||
|
formatInline(message.sourceLocation.file_name());
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "function"))
|
||||||
|
{
|
||||||
|
formatInline(message.sourceLocation.function_name());
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "line"))
|
||||||
|
{
|
||||||
|
formatInline(message.sourceLocation.line());
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "column"))
|
||||||
|
{
|
||||||
|
formatInline(message.sourceLocation.column());
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "level"))
|
||||||
|
{
|
||||||
|
formatInline(message.level->name);
|
||||||
|
}
|
||||||
|
else if (argName == MIJIN_SMART_QUOTE(char_t, "ansi"))
|
||||||
|
{
|
||||||
|
formatAnsiSequence(message, argFormat.substr(1), outFormatted);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MIJIN_ERROR("Invalid format argument name.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outFormatted += *pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits, allocator_type_for<TChar> TAllocator>
|
||||||
|
void BaseSimpleLogFormatter<TChar, TTraits, TAllocator>::formatAnsiSequence(const LogMessage& message, string_view_t ansiName, string_t& outFormatted)
|
||||||
|
{
|
||||||
|
std::size_t numParts = 0;
|
||||||
|
const auto formatParts = splitFixed<4>(ansiName, ",", {}, &numParts);
|
||||||
|
|
||||||
|
outFormatted += MIJIN_SMART_QUOTE(char_t, "\033[");
|
||||||
|
for (std::size_t partIdx = 0; partIdx < numParts; ++partIdx)
|
||||||
|
{
|
||||||
|
const string_view_t& part = formatParts[partIdx];
|
||||||
|
if (partIdx > 0)
|
||||||
|
{
|
||||||
|
outFormatted += MIJIN_SMART_QUOTE(char_t, ',');
|
||||||
|
}
|
||||||
|
if (part == MIJIN_SMART_QUOTE(char_t, "reset"))
|
||||||
|
{
|
||||||
|
outFormatted += BaseAnsiFontEffects<char_t>::RESET;
|
||||||
|
}
|
||||||
|
else if (part == MIJIN_SMART_QUOTE(char_t, "level_color"))
|
||||||
|
{
|
||||||
|
const int levelValue = message.level->value;
|
||||||
|
if (levelValue < MIJIN_LOG_LEVEL_VALUE_VERBOSE)
|
||||||
|
{
|
||||||
|
outFormatted += BaseAnsiFontEffects<char_t>::FG_CYAN;
|
||||||
|
}
|
||||||
|
else if (levelValue < MIJIN_LOG_LEVEL_VALUE_INFO)
|
||||||
|
{
|
||||||
|
outFormatted += BaseAnsiFontEffects<char_t>::FG_WHITE;
|
||||||
|
}
|
||||||
|
else if (levelValue < MIJIN_LOG_LEVEL_VALUE_WARNING)
|
||||||
|
{
|
||||||
|
outFormatted += BaseAnsiFontEffects<char_t>::FG_BRIGHT_WHITE;
|
||||||
|
}
|
||||||
|
else if (levelValue < MIJIN_LOG_LEVEL_VALUE_ERROR)
|
||||||
|
{
|
||||||
|
outFormatted += BaseAnsiFontEffects<char_t>::FG_YELLOW;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outFormatted += BaseAnsiFontEffects<char_t>::FG_RED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MIJIN_ERROR("Invalid format ansi font effect name.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outFormatted += MIJIN_SMART_QUOTE(char_t, 'm');
|
||||||
|
}
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_LOGGING_FORMATTING_HPP_INCLUDED)
|
||||||
256
source/mijin/logging/logger.hpp
Normal file
256
source/mijin/logging/logger.hpp
Normal file
@ -0,0 +1,256 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOGGING_LOGGER_HPP_INCLUDED)
|
||||||
|
#define MIJIN_LOGGING_LOGGER_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <format>
|
||||||
|
#include <source_location>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../util/annot.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOG_LEVEL_VALUE_DEBUG)
|
||||||
|
#define MIJIN_LOG_LEVEL_VALUE_DEBUG -1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOG_LEVEL_VALUE_VERBOSE)
|
||||||
|
#define MIJIN_LOG_LEVEL_VALUE_VERBOSE -500
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOG_LEVEL_VALUE_INFO)
|
||||||
|
#define MIJIN_LOG_LEVEL_VALUE_INFO 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOG_LEVEL_VALUE_WARNING)
|
||||||
|
#define MIJIN_LOG_LEVEL_VALUE_WARNING 500
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOG_LEVEL_VALUE_ERROR)
|
||||||
|
#define MIJIN_LOG_LEVEL_VALUE_ERROR 1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_FUNCNAME_GET_LOGGER)
|
||||||
|
#define MIJIN_FUNCNAME_GET_LOGGER mijin__getLogger__
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE)
|
||||||
|
#define MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE mijin__getMinLogLevelCompile
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_NSNAME_LOG_LEVEL)
|
||||||
|
#define MIJIN_NSNAME_LOG_LEVEL mijin_log_level
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_NSNAME_LOG_CHANNEL)
|
||||||
|
#define MIJIN_NSNAME_LOG_CHANNEL mijin_log_channel
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
|
struct BaseLogLevel
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
|
||||||
|
const char_t* name;
|
||||||
|
int value;
|
||||||
|
|
||||||
|
explicit operator int() const MIJIN_NOEXCEPT { return value; }
|
||||||
|
|
||||||
|
auto operator<=>(const BaseLogLevel& other) const MIJIN_NOEXCEPT { return value <=> other.value; }
|
||||||
|
};
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS(LogLevel)
|
||||||
|
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
|
struct BaseLogChannel
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
|
||||||
|
const char_t* name;
|
||||||
|
};
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS(LogChannel)
|
||||||
|
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
|
struct BaseLogMessage
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
|
||||||
|
const char_t* text;
|
||||||
|
const BaseLogChannel<char_t>* channel;
|
||||||
|
const BaseLogLevel<char_t>* level;
|
||||||
|
std::source_location sourceLocation;
|
||||||
|
};
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS(LogMessage)
|
||||||
|
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
|
class BaseLogSink
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using char_t = TChar;
|
||||||
|
using message_t = BaseLogMessage<char_t>;
|
||||||
|
|
||||||
|
virtual ~BaseLogSink() noexcept = default;
|
||||||
|
|
||||||
|
virtual void handleMessage(const message_t& message) MIJIN_NOEXCEPT = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS(LogSink)
|
||||||
|
|
||||||
|
#define LOGGER_COMMON_ARGS(chr_type) template<typename T> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>, LOGGER_COMMON_ARGS(TChar)>
|
||||||
|
class BaseLogger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using char_t = TChar;
|
||||||
|
using traits_t = TTraits;
|
||||||
|
using allocator_t = TAllocator<char_t>;
|
||||||
|
|
||||||
|
using sink_t = BaseLogSink<char_t>;
|
||||||
|
using level_t = BaseLogLevel<char_t>;
|
||||||
|
using channel_t = BaseLogChannel<char_t>;
|
||||||
|
using message_t = BaseLogMessage<char_t>;
|
||||||
|
using string_t = std::basic_string<char_t, traits_t, allocator_t>;
|
||||||
|
private:
|
||||||
|
std::vector<sink_t*, TAllocator<sink_t*>> mSinks;
|
||||||
|
public:
|
||||||
|
explicit BaseLogger(TAllocator<sink_t*> allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TAllocator<sink_t*>>)
|
||||||
|
: mSinks(std::move(allocator))
|
||||||
|
{}
|
||||||
|
|
||||||
|
BaseLogger(const BaseLogger&) = default;
|
||||||
|
|
||||||
|
BaseLogger(BaseLogger&&) = default;
|
||||||
|
|
||||||
|
BaseLogger& operator=(const BaseLogger&) = default;
|
||||||
|
|
||||||
|
BaseLogger& operator=(BaseLogger&&) = default;
|
||||||
|
|
||||||
|
void addSink(sink_t& sink)
|
||||||
|
{
|
||||||
|
mSinks.push_back(&sink);
|
||||||
|
}
|
||||||
|
|
||||||
|
void postMessage(const message_t& message) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
for (sink_t* sink: mSinks)
|
||||||
|
{
|
||||||
|
sink->handleMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void log(const level_t& level, const channel_t& channel, std::source_location sourceLocation, const char_t* msg) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
postMessage({
|
||||||
|
.text = msg,
|
||||||
|
.channel = &channel,
|
||||||
|
.level = &level,
|
||||||
|
.sourceLocation = std::move(sourceLocation)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... TArgs>
|
||||||
|
void log(const level_t& level, const channel_t& channel, std::source_location sourceLocation,
|
||||||
|
std::basic_format_string<char_t, std::type_identity_t<TArgs>...> fmt, TArgs&& ... args) const
|
||||||
|
MIJIN_NOEXCEPT_IF(noexcept(std::declval<allocator_t>().allocate(1)))
|
||||||
|
{
|
||||||
|
string_t buffer(allocator_t(mSinks.get_allocator()));
|
||||||
|
std::format_to(std::back_inserter(buffer), fmt, std::forward<TArgs>(args)...);
|
||||||
|
log(level, channel, std::move(sourceLocation), buffer.c_str());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LOGGER_SET_ARGS(chr_type) chr_type, std::char_traits<chr_type>, TAllocator
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(Logger, LOGGER_COMMON_ARGS, LOGGER_SET_ARGS)
|
||||||
|
|
||||||
|
#undef LOGGER_COMMON_ARGS
|
||||||
|
#undef LOGGER_SET_ARGS
|
||||||
|
|
||||||
|
#define MIJIN_DECLARE_LOG_CHANNEL_BASE(chr_type, cnlName) \
|
||||||
|
namespace MIJIN_NSNAME_LOG_CHANNEL \
|
||||||
|
{ \
|
||||||
|
extern const ::mijin::BaseLogChannel<chr_type> cnlName; \
|
||||||
|
}
|
||||||
|
#define MIJIN_DECLARE_LOG_CHANNEL(cnlName) MIJIN_DECLARE_LOG_CHANNEL_BASE(MIJIN_DEFAULT_CHAR_TYPE, cnlName)
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_LOG_CHANNEL_BASE(chr_type, cnlName) \
|
||||||
|
namespace MIJIN_NSNAME_LOG_CHANNEL \
|
||||||
|
{ \
|
||||||
|
const ::mijin::BaseLogChannel<chr_type> cnlName { \
|
||||||
|
.name = MIJIN_SMART_STRINGIFY(chr_type, cnlName) \
|
||||||
|
}; \
|
||||||
|
}
|
||||||
|
#define MIJIN_DEFINE_LOG_CHANNEL(cnlName) MIJIN_DEFINE_LOG_CHANNEL_BASE(MIJIN_DEFAULT_CHAR_TYPE, cnlName)
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_LOG_LEVEL_BASE(chr_type, lvlName, lvlValue) \
|
||||||
|
namespace MIJIN_NSNAME_LOG_LEVEL \
|
||||||
|
{ \
|
||||||
|
inline constexpr ::mijin::BaseLogLevel<chr_type> lvlName{ \
|
||||||
|
.name = MIJIN_SMART_STRINGIFY(chr_type, lvlName), \
|
||||||
|
.value = lvlValue \
|
||||||
|
}; \
|
||||||
|
}
|
||||||
|
#define MIJIN_DEFINE_LOG_LEVEL(lvlName, lvlValue) MIJIN_DEFINE_LOG_LEVEL_BASE(MIJIN_DEFAULT_CHAR_TYPE, lvlName, lvlValue)
|
||||||
|
|
||||||
|
#if defined(MIJIN_MIN_LOGLEVEL_COMPILE)
|
||||||
|
inline constexpr int MIN_LOG_LEVEL_COMPILE = static_cast<int>(MIJIN_MIN_LOGLEVEL_COMPILE);
|
||||||
|
#elif defined(MIJIN_DEBUG)
|
||||||
|
inline constexpr int MIN_LOG_LEVEL_COMPILE = MIJIN_LOG_LEVEL_VALUE_DEBUG;
|
||||||
|
#else
|
||||||
|
inline constexpr int MIN_LOG_LEVEL_COMPILE = MIJIN_LOG_LEVEL_VALUE_VERBOSE;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MIJIN_DEFINE_DEFAULT_LOG_LEVELS \
|
||||||
|
MIJIN_DEFINE_LOG_LEVEL(DEBUG, MIJIN_LOG_LEVEL_VALUE_DEBUG) \
|
||||||
|
MIJIN_DEFINE_LOG_LEVEL(VERBOSE, MIJIN_LOG_LEVEL_VALUE_VERBOSE) \
|
||||||
|
MIJIN_DEFINE_LOG_LEVEL(INFO, MIJIN_LOG_LEVEL_VALUE_INFO) \
|
||||||
|
MIJIN_DEFINE_LOG_LEVEL(WARNING, MIJIN_LOG_LEVEL_VALUE_WARNING) \
|
||||||
|
MIJIN_DEFINE_LOG_LEVEL(ERROR, MIJIN_LOG_LEVEL_VALUE_ERROR)
|
||||||
|
|
||||||
|
#define MIJIN_LOG_LEVEL_OBJECT(level) MIJIN_NSNAME_LOG_LEVEL::level
|
||||||
|
#define MIJIN_LOG_CHANNEL_OBJECT(channel) MIJIN_NSNAME_LOG_CHANNEL::channel
|
||||||
|
|
||||||
|
#define MIJIN_LOG_ALWAYS(level, channel, ...) MIJIN_FUNCNAME_GET_LOGGER().log( \
|
||||||
|
MIJIN_LOG_LEVEL_OBJECT(level), MIJIN_LOG_CHANNEL_OBJECT(channel), std::source_location::current(), __VA_ARGS__ \
|
||||||
|
)
|
||||||
|
#define MIJIN_LOG(level, channel, ...) \
|
||||||
|
if constexpr (MIJIN_LOG_LEVEL_OBJECT(level).value < MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE()) {} \
|
||||||
|
else MIJIN_LOG_ALWAYS(level, channel, __VA_ARGS__)
|
||||||
|
|
||||||
|
#define MIJIN_SET_CLASS_LOGGER(loggerExpr) \
|
||||||
|
const auto& MIJIN_FUNCNAME_GET_LOGGER() const noexcept \
|
||||||
|
{ \
|
||||||
|
return loggerExpr; \
|
||||||
|
}
|
||||||
|
#define MIJIN_SET_SCOPE_LOGGER(loggerExpr) \
|
||||||
|
auto MIJIN_FUNCNAME_GET_LOGGER = [&]() -> const auto& \
|
||||||
|
{ \
|
||||||
|
return loggerExpr; \
|
||||||
|
};
|
||||||
|
#define MIJIN_SET_CLASS_MIN_LOG_LEVEL_COMPILE(level) \
|
||||||
|
int MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE() MIJIN_NOEXCEPT \
|
||||||
|
{ \
|
||||||
|
return MIJIN_LOG_LEVEL_OBJECT(level).value; \
|
||||||
|
}
|
||||||
|
#define MIJIN_SET_SCOPE_MIN_LOG_LEVEL_COMPILE(level) \
|
||||||
|
auto MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE = []() -> int \
|
||||||
|
{ \
|
||||||
|
return MIJIN_LOG_LEVEL_OBJECT(level).value; \
|
||||||
|
};
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
inline constexpr int MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return ::mijin::MIN_LOG_LEVEL_COMPILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_LOGGING_LOGGER_HPP_INCLUDED)
|
||||||
71
source/mijin/logging/stdio_sink.hpp
Normal file
71
source/mijin/logging/stdio_sink.hpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_LOGGING_STDIO_SINK_HPP_INCLUDED)
|
||||||
|
#define MIJIN_LOGGING_STDIO_SINK_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "./formatting.hpp"
|
||||||
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
||||||
|
requires(allocator_type<TAllocator<TChar>>)
|
||||||
|
class BaseStdioSink : public BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG_NAMES>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using base_t = BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG_NAMES>;
|
||||||
|
using typename base_t::char_t;
|
||||||
|
using typename base_t::allocator_t;
|
||||||
|
using typename base_t::formatter_ptr_t;
|
||||||
|
using typename base_t::message_t;
|
||||||
|
private:
|
||||||
|
int mMinStderrLevel = MIJIN_LOG_LEVEL_VALUE_WARNING;
|
||||||
|
public:
|
||||||
|
explicit BaseStdioSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||||
|
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||||
|
: base_t(std::move(formatter), std::move(allocator)) {}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
int getMinStderrLevel() const MIJIN_NOEXCEPT { return mMinStderrLevel; }
|
||||||
|
|
||||||
|
void setMinStderrLevel(int level) MIJIN_NOEXCEPT { mMinStderrLevel = level; }
|
||||||
|
|
||||||
|
void setMinStderrLevel(const BaseLogLevel<char_t>& level) MIJIN_NOEXCEPT { mMinStderrLevel = level.value; }
|
||||||
|
|
||||||
|
void handleMessageFormatted(const message_t& message, const char_t* formatted) MIJIN_NOEXCEPT override
|
||||||
|
{
|
||||||
|
FILE* stream = (message.level->value >= mMinStderrLevel) ? stderr : stdout;
|
||||||
|
if constexpr (std::is_same_v<char_t, char>)
|
||||||
|
{
|
||||||
|
std::fputs(formatted, stream);
|
||||||
|
std::fputc('\n', stream);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<char_t, wchar_t>)
|
||||||
|
{
|
||||||
|
std::fputws(formatted, stream);
|
||||||
|
std::fputwc(L'\n', stream);
|
||||||
|
}
|
||||||
|
else if constexpr (sizeof(char_t) == sizeof(char))
|
||||||
|
{
|
||||||
|
// char8_t etc.
|
||||||
|
std::fputs(std::bit_cast<const char*>(formatted), stream);
|
||||||
|
std::fputc('\n', stream);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static_assert(always_false_v<char_t>, "Character type not supported.");
|
||||||
|
}
|
||||||
|
std::fflush(stream);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define SINK_SET_ARGS(chr_type) chr_type, std::char_traits<chr_type>, TAllocator, TDeleter
|
||||||
|
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(StdioSink, MIJIN_FORMATTING_SINK_COMMON_ARGS, SINK_SET_ARGS)
|
||||||
|
|
||||||
|
#undef SINK_SET_ARGS
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_LOGGING_STDIO_SINK_HPP_INCLUDED)
|
||||||
176
source/mijin/memory/dynamic_pointer.hpp
Normal file
176
source/mijin/memory/dynamic_pointer.hpp
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_MEMORY_DYNAMIC_POINTER_HPP_INCLUDED)
|
||||||
|
#define MIJIN_MEMORY_DYNAMIC_POINTER_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../memory/memutil.hpp"
|
||||||
|
#include "../util/concepts.hpp"
|
||||||
|
#include "../util/flag.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
MIJIN_DEFINE_FLAG(Owning);
|
||||||
|
|
||||||
|
template<typename T, deleter_type<T> TDeleter = std::default_delete<T>>
|
||||||
|
class DynamicPointer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using pointer = T*;
|
||||||
|
using element_type = T;
|
||||||
|
using deleter_t = TDeleter;
|
||||||
|
private:
|
||||||
|
std::uintptr_t mData = 0;
|
||||||
|
[[no_unique_address]] TDeleter mDeleter;
|
||||||
|
public:
|
||||||
|
constexpr DynamicPointer(std::nullptr_t = nullptr) MIJIN_NOEXCEPT {}
|
||||||
|
DynamicPointer(const DynamicPointer&) = delete;
|
||||||
|
constexpr DynamicPointer(pointer ptr, Owning owning, TDeleter deleter = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TDeleter>)
|
||||||
|
: mData(std::bit_cast<std::uintptr_t>(ptr) | (owning ? 1 : 0)), mDeleter(std::move(deleter))
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT((std::bit_cast<std::uintptr_t>(ptr) & 1) == 0, "Invalid address, DynamicPointer requires addresses to be divisible by two.");
|
||||||
|
}
|
||||||
|
template<typename TOther, typename TOtherDeleter> requires (std::is_constructible_v<TDeleter, TOtherDeleter&&>)
|
||||||
|
constexpr DynamicPointer(DynamicPointer<TOther, TOtherDeleter>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_convertible_v<TOtherDeleter, TDeleter>))
|
||||||
|
: mData(std::exchange(other.mData, 0)), mDeleter(std::move(other.mDeleter)) {
|
||||||
|
MIJIN_ASSERT(other.mData == 0, "");
|
||||||
|
}
|
||||||
|
constexpr ~DynamicPointer() noexcept
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
DynamicPointer& operator=(const DynamicPointer&) = delete;
|
||||||
|
DynamicPointer& operator=(std::nullptr_t) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TOther, typename TOtherDeleter> requires(std::is_assignable_v<TDeleter, TOtherDeleter>)
|
||||||
|
DynamicPointer& operator=(DynamicPointer<TOther, TOtherDeleter>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_assignable_v<TDeleter, TOtherDeleter>))
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
mData = std::exchange(other.mData, 0);
|
||||||
|
mDeleter = std::move(other.mDeleter);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TOther, typename TOtherDeleter> requires(std::equality_comparable_with<T, TOther>)
|
||||||
|
auto operator<=>(const DynamicPointer<TOther, TOtherDeleter>& other) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return mData <=> other.mData;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator==(std::nullptr_t) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(std::nullptr_t) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr operator bool() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr pointer operator->() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr element_type& operator*() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return *get();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr bool isOwning() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return (mData & 1) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr bool empty() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return mData == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr pointer get() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::bit_cast<pointer>(mData & ~1);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void reset(pointer ptr, Owning owning) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (isOwning())
|
||||||
|
{
|
||||||
|
mDeleter(get());
|
||||||
|
}
|
||||||
|
mData = std::bit_cast<std::uintptr_t>(ptr) | (owning ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void reset() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
reset(nullptr, Owning::NO);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
pointer release() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::bit_cast<pointer>(std::exchange(mData, 0) & ~1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TOther, deleter_type<TOther> TOtherDeleter>
|
||||||
|
friend class DynamicPointer;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, typename TDeleter>
|
||||||
|
bool operator==(std::nullptr_t, const DynamicPointer<T, TDeleter>& pointer) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return pointer == nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename TDeleter>
|
||||||
|
bool operator!=(std::nullptr_t, const DynamicPointer<T, TDeleter>& pointer) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return pointer != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename... TArgs>
|
||||||
|
DynamicPointer<T, std::default_delete<T>> makeDynamic(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
||||||
|
{
|
||||||
|
return DynamicPointer<T, std::default_delete<T>>(new T(std::forward<TArgs>(args)...), Owning::YES);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, allocator_type_for<T> TAllocator, typename... TArgs>
|
||||||
|
DynamicPointer<T, AllocatorDeleter<TAllocator>> makeDynamicWithAllocator(TAllocator allocator, TArgs&&... args)
|
||||||
|
MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...> && std::is_nothrow_move_constructible_v<TAllocator>))
|
||||||
|
{
|
||||||
|
T* obj = allocator.allocate(1);
|
||||||
|
if (obj != nullptr)
|
||||||
|
{
|
||||||
|
::new(obj) T(std::forward<TArgs>(args)...);
|
||||||
|
}
|
||||||
|
return DynamicPointer<T, AllocatorDeleter<TAllocator>>(obj, Owning::YES, AllocatorDeleter<TAllocator>(std::move(allocator)));
|
||||||
|
}
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_MEMORY_DYNAMIC_POINTER_HPP_INCLUDED)
|
||||||
90
source/mijin/memory/memutil.hpp
Normal file
90
source/mijin/memory/memutil.hpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_MEMORY_MEMUTIL_HPP_INCLUDED)
|
||||||
|
#define MIJIN_MEMORY_MEMUTIL_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename TAllocator>
|
||||||
|
class AllocatorDeleter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = std::allocator_traits<TAllocator>::value_type;
|
||||||
|
using pointer = std::allocator_traits<TAllocator>::pointer;
|
||||||
|
|
||||||
|
private:
|
||||||
|
[[no_unique_address]] TAllocator allocator_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AllocatorDeleter() = default;
|
||||||
|
explicit AllocatorDeleter(TAllocator allocator) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TAllocator>)
|
||||||
|
: allocator_(std::move(allocator)) {}
|
||||||
|
|
||||||
|
template<typename TOtherAllocator> requires (std::is_constructible_v<TAllocator, const TOtherAllocator&>)
|
||||||
|
AllocatorDeleter(const AllocatorDeleter<TOtherAllocator>& other)
|
||||||
|
MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TAllocator, TOtherAllocator>))
|
||||||
|
: allocator_(other.allocator_) {}
|
||||||
|
|
||||||
|
template<typename TOtherAllocator> requires (std::is_constructible_v<TAllocator, TOtherAllocator&&>)
|
||||||
|
AllocatorDeleter(AllocatorDeleter<TOtherAllocator>&& other)
|
||||||
|
MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TAllocator, TOtherAllocator&&>))
|
||||||
|
: allocator_(std::move(other.allocator_)) {}
|
||||||
|
|
||||||
|
template<typename TOtherAllocator> requires (std::is_assignable_v<TAllocator&, const TOtherAllocator&>)
|
||||||
|
AllocatorDeleter& operator=(const AllocatorDeleter<TOtherAllocator>& other)
|
||||||
|
MIJIN_NOEXCEPT_IF((std::is_nothrow_assignable_v<TAllocator&, const TOtherAllocator&>))
|
||||||
|
{
|
||||||
|
if (this != static_cast<const void*>(&other))
|
||||||
|
{
|
||||||
|
allocator_ = other.allocator_;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TOtherAllocator> requires (std::is_assignable_v<TAllocator&, TOtherAllocator&&>)
|
||||||
|
AllocatorDeleter& operator=(AllocatorDeleter<TOtherAllocator>&& other)
|
||||||
|
MIJIN_NOEXCEPT_IF((std::is_nothrow_assignable_v<TAllocator&, TOtherAllocator&&>))
|
||||||
|
{
|
||||||
|
if (this != static_cast<const void*>(&other))
|
||||||
|
{
|
||||||
|
allocator_ = std::move(other.allocator_);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(pointer ptr) MIJIN_NOEXCEPT_IF(noexcept(allocator_.deallocate(ptr, sizeof(value_type))))
|
||||||
|
{
|
||||||
|
allocator_.deallocate(ptr, sizeof(value_type));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TOtherAllocator>
|
||||||
|
friend class AllocatorDeleter;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class AllocatorDeleter<std::allocator<T>>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AllocatorDeleter() noexcept = default;
|
||||||
|
|
||||||
|
template<typename TOther>
|
||||||
|
AllocatorDeleter(std::allocator<TOther>) noexcept {}
|
||||||
|
|
||||||
|
template<typename TOther>
|
||||||
|
AllocatorDeleter(const AllocatorDeleter<std::allocator<TOther>>&) noexcept {}
|
||||||
|
|
||||||
|
template<typename TOther>
|
||||||
|
AllocatorDeleter& operator=(const AllocatorDeleter<std::allocator<TOther>>&) noexcept { return *this; }
|
||||||
|
|
||||||
|
void operator()(T* ptr) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
delete ptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_MEMORY_MEMUTIL_HPP_INCLUDED)
|
||||||
486
source/mijin/memory/stack_allocator.hpp
Normal file
486
source/mijin/memory/stack_allocator.hpp
Normal file
@ -0,0 +1,486 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_MEMORY_STACK_ALLOCATOR_HPP_INCLUDED)
|
||||||
|
#define MIJIN_MEMORY_STACK_ALLOCATOR_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
#include "../debug/assert.hpp"
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../util/align.hpp"
|
||||||
|
#include "../util/concepts.hpp"
|
||||||
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
|
#if !defined(MIJIN_STACK_ALLOCATOR_DEBUG)
|
||||||
|
#if defined(MIJIN_DEBUG)
|
||||||
|
#define MIJIN_STACK_ALLOCATOR_DEBUG 1
|
||||||
|
#else
|
||||||
|
#define MIJIN_STACK_ALLOCATOR_DEBUG 0
|
||||||
|
#endif
|
||||||
|
#endif // !defined(MIJIN_STACK_ALLOCATOR_DEBUG)
|
||||||
|
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
#include <print>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include "../debug/stacktrace.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename TValue, typename TStackAllocator>
|
||||||
|
class StlStackAllocator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = TValue;
|
||||||
|
private:
|
||||||
|
TStackAllocator* base_;
|
||||||
|
public:
|
||||||
|
explicit StlStackAllocator(TStackAllocator& base) MIJIN_NOEXCEPT : base_(&base) {}
|
||||||
|
template<typename TOtherValue>
|
||||||
|
StlStackAllocator(const StlStackAllocator<TOtherValue, TStackAllocator>& other) MIJIN_NOEXCEPT : base_(other.base_) {}
|
||||||
|
|
||||||
|
template<typename TOtherValue>
|
||||||
|
StlStackAllocator& operator=(const StlStackAllocator<TOtherValue, TStackAllocator>& other) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
base_ = other.base_;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator<=>(const StlStackAllocator&) const noexcept = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
TValue* allocate(std::size_t count) const
|
||||||
|
{
|
||||||
|
void* result = base_->allocate(alignof(TValue), count * sizeof(TValue));
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
++base_->numAllocations_;
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
base_->activeAllocations_.emplace(result, captureStacktrace(1));
|
||||||
|
#endif
|
||||||
|
return static_cast<TValue*>(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate([[maybe_unused]] TValue* ptr, std::size_t /* count */) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
MIJIN_ASSERT(base_->numAllocations_ > 0, "Unbalanced allocations in stack allocators!");
|
||||||
|
--base_->numAllocations_;
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
auto it = base_->activeAllocations_.find(ptr);
|
||||||
|
if (it != base_->activeAllocations_.end())
|
||||||
|
{
|
||||||
|
base_->activeAllocations_.erase(it);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MIJIN_ERROR("Deallocating invalid pointer from StackAllocator.");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TOtherValue, typename TOtherAllocator>
|
||||||
|
friend class StlStackAllocator;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
struct StackAllocatorSnapshotData
|
||||||
|
{
|
||||||
|
struct ChunkSnapshot
|
||||||
|
{
|
||||||
|
std::size_t allocated;
|
||||||
|
};
|
||||||
|
std::size_t numChunks;
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
std::size_t numAllocations_ = 0;
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
// just for debugging, so we don't care what memory this uses...
|
||||||
|
std::unordered_map<void*, Result<Stacktrace>> activeAllocations_;
|
||||||
|
#endif
|
||||||
|
ChunkSnapshot chunks[1]; // may be bigger than that
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class StackAllocatorSnapshot
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
impl::StackAllocatorSnapshotData* data = nullptr;
|
||||||
|
|
||||||
|
impl::StackAllocatorSnapshotData* operator->() const MIJIN_NOEXCEPT { return data; }
|
||||||
|
|
||||||
|
|
||||||
|
template<std::size_t chunkSize, template<typename> typename TBacking> requires (allocator_tmpl<TBacking>)
|
||||||
|
friend class StackAllocator;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TStackAllocator>
|
||||||
|
class StackAllocatorScope
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
TStackAllocator* allocator_;
|
||||||
|
StackAllocatorSnapshot snapshot_;
|
||||||
|
public:
|
||||||
|
explicit StackAllocatorScope(TStackAllocator& allocator) : allocator_(&allocator), snapshot_(allocator_->createSnapshot()) {}
|
||||||
|
StackAllocatorScope(const StackAllocatorScope&) = delete;
|
||||||
|
StackAllocatorScope(StackAllocatorScope&&) = delete;
|
||||||
|
~StackAllocatorScope() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
allocator_->restoreSnapshot(snapshot_);
|
||||||
|
}
|
||||||
|
|
||||||
|
StackAllocatorScope& operator=(const StackAllocatorScope&) = delete;
|
||||||
|
StackAllocatorScope& operator=(StackAllocatorScope&&) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<std::size_t chunkSize = 4096, template<typename> typename TBacking = MIJIN_DEFAULT_ALLOCATOR> requires (allocator_tmpl<TBacking>)
|
||||||
|
class StackAllocator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using backing_t = TBacking<void>;
|
||||||
|
static constexpr std::size_t ACTUAL_CHUNK_SIZE = chunkSize - sizeof(void*) - sizeof(std::size_t);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using stl_allocator_t = StlStackAllocator<T, StackAllocator<chunkSize, TBacking>>;
|
||||||
|
private:
|
||||||
|
struct Chunk
|
||||||
|
{
|
||||||
|
std::array<std::byte, ACTUAL_CHUNK_SIZE> data;
|
||||||
|
Chunk* next;
|
||||||
|
std::size_t allocated;
|
||||||
|
};
|
||||||
|
[[no_unique_address]] TBacking<Chunk> backing_;
|
||||||
|
Chunk* firstChunk_ = nullptr;
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
std::size_t numAllocations_ = 0;
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
// just for debugging, so we don't care what memory this uses...
|
||||||
|
std::unordered_map<void*, Result<Stacktrace>> activeAllocations_;
|
||||||
|
#endif
|
||||||
|
public:
|
||||||
|
StackAllocator() MIJIN_NOEXCEPT_IF(std::is_nothrow_default_constructible_v<backing_t>) = default;
|
||||||
|
explicit StackAllocator(backing_t backing) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TBacking<Chunk>, backing_t&&>))
|
||||||
|
: backing_(std::move(backing)) {}
|
||||||
|
StackAllocator(const StackAllocator&) = delete;
|
||||||
|
StackAllocator(StackAllocator&& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TBacking<Chunk>>)
|
||||||
|
: firstChunk_(std::exchange(other.firstChunk_, nullptr)) {}
|
||||||
|
~StackAllocator() noexcept
|
||||||
|
{
|
||||||
|
Chunk* chunk = firstChunk_;
|
||||||
|
while (chunk != nullptr)
|
||||||
|
{
|
||||||
|
Chunk* nextChunk = firstChunk_->next;
|
||||||
|
backing_.deallocate(chunk, 1);
|
||||||
|
chunk = nextChunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StackAllocator& operator=(const StackAllocator&) = delete;
|
||||||
|
StackAllocator& operator=(StackAllocator&& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_assignable_v<TBacking<Chunk>>)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
backing_ = std::move(other.backing_);
|
||||||
|
firstChunk_ = std::exchange(other.firstChunk_, nullptr);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* allocate(std::size_t alignment, std::size_t size)
|
||||||
|
{
|
||||||
|
// first check if this can ever fit
|
||||||
|
if (size > ACTUAL_CHUNK_SIZE)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// then try to find space in the current chunks
|
||||||
|
for (Chunk* chunk = firstChunk_; chunk != nullptr; chunk = chunk->next)
|
||||||
|
{
|
||||||
|
const std::size_t remaining = ACTUAL_CHUNK_SIZE - chunk->allocated;
|
||||||
|
if (remaining < size)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::byte* start = &chunk->data[chunk->allocated];
|
||||||
|
std::byte* pos = mijin::alignUp(start, alignment);
|
||||||
|
|
||||||
|
const std::ptrdiff_t alignmentBytes = pos - start;
|
||||||
|
const std::size_t combinedSize = size + alignmentBytes;
|
||||||
|
|
||||||
|
if (remaining < combinedSize)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
chunk->allocated += combinedSize;
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no free space in any chunk? allocate a new one
|
||||||
|
Chunk* newChunk = backing_.allocate(1);
|
||||||
|
if (newChunk == nullptr)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
initAndAddChunk(newChunk);
|
||||||
|
|
||||||
|
// now try with the new chunk
|
||||||
|
std::byte* start = newChunk->data.data();
|
||||||
|
std::byte* pos = mijin::alignUp(start, alignment);
|
||||||
|
|
||||||
|
const std::ptrdiff_t alignmentBytes = pos - start;
|
||||||
|
const std::size_t combinedSize = size + alignmentBytes;
|
||||||
|
|
||||||
|
// doesn't fit (due to alignment), time to give up
|
||||||
|
if (ACTUAL_CHUNK_SIZE < combinedSize)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
newChunk->allocated = combinedSize;
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() noexcept
|
||||||
|
{
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
MIJIN_ASSERT(numAllocations_ == 0, "Missing deallocation in StackAllocator!");
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
if (!activeAllocations_.empty())
|
||||||
|
{
|
||||||
|
std::println(stderr, "{} active allocations in StackAllocator when resetting!", activeAllocations_.size());
|
||||||
|
for (const auto& [ptr, stack] : activeAllocations_)
|
||||||
|
{
|
||||||
|
if (stack.isError())
|
||||||
|
{
|
||||||
|
std::println(stderr, "at {}, no stacktrace ({})", ptr, stack.getError().message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::println(stderr, "at 0x{}:\n{}", ptr, stack.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MIJIN_TRAP();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
for (Chunk* chunk = firstChunk_; chunk != nullptr; chunk = chunk->next)
|
||||||
|
{
|
||||||
|
chunk->allocated = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool createChunks(std::size_t count)
|
||||||
|
{
|
||||||
|
if (count == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Chunk* newChunks = backing_.allocate(count);
|
||||||
|
if (newChunks == nullptr)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reverse so the chunks are chained from 0 to count (new chunks are inserted in front)
|
||||||
|
for (std::size_t pos = count; pos > 0; --pos)
|
||||||
|
{
|
||||||
|
initAndAddChunk(&newChunks[pos-1]);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
stl_allocator_t<T> makeStlAllocator() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return stl_allocator_t<T>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
std::size_t getNumChunks() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
std::size_t num = 0;
|
||||||
|
for (Chunk* chunk = firstChunk_; chunk != nullptr; chunk = chunk->next)
|
||||||
|
{
|
||||||
|
++num;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
StackAllocatorSnapshot createSnapshot()
|
||||||
|
{
|
||||||
|
if (firstChunk_ == nullptr)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
using impl::StackAllocatorSnapshotData;
|
||||||
|
|
||||||
|
std::size_t numChunks = getNumChunks();
|
||||||
|
std::size_t snapshotSize = calcSnapshotSize(numChunks);
|
||||||
|
Chunk* prevFirst = firstChunk_;
|
||||||
|
StackAllocatorSnapshotData* snapshotData = static_cast<StackAllocatorSnapshotData*>(allocate(alignof(StackAllocatorSnapshotData), snapshotSize));
|
||||||
|
if (snapshotData == nullptr)
|
||||||
|
{
|
||||||
|
// couldn't allocate the snapshot
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
::new (snapshotData) StackAllocatorSnapshotData;
|
||||||
|
StackAllocatorSnapshot snapshot;
|
||||||
|
snapshot.data = snapshotData;
|
||||||
|
if (firstChunk_ != prevFirst)
|
||||||
|
{
|
||||||
|
// new chunk has been added, adjust the snapshot
|
||||||
|
// the snapshot must be inside the new chunk (but not necessarily at the very beginning, due to alignment)
|
||||||
|
MIJIN_ASSERT(static_cast<const void*>(snapshot.data) == alignUp(firstChunk_->data.data(), alignof(StackAllocatorSnapshot)), "Snapshot not where it was expected.");
|
||||||
|
|
||||||
|
// a chunk might be too small for the snapshot if we grow it (unlikely, but not impossible)
|
||||||
|
if (ACTUAL_CHUNK_SIZE - firstChunk_->allocated < MIJIN_STRIDEOF(StackAllocatorSnapshotData::ChunkSnapshot)) [[unlikely]]
|
||||||
|
{
|
||||||
|
firstChunk_->allocated = 0;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// looking good, adjust the numbers
|
||||||
|
++numChunks;
|
||||||
|
snapshotSize += MIJIN_STRIDEOF(StackAllocatorSnapshotData::ChunkSnapshot);
|
||||||
|
firstChunk_->allocated += MIJIN_STRIDEOF(StackAllocatorSnapshotData::ChunkSnapshot);
|
||||||
|
}
|
||||||
|
// now fill out the struct
|
||||||
|
snapshot->numChunks = numChunks;
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
snapshot->numAllocations_ = numAllocations_;
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
// just for debugging, so we don't care what memory this uses...
|
||||||
|
snapshot->activeAllocations_ = activeAllocations_;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::size_t pos = 0;
|
||||||
|
for (Chunk* chunk = firstChunk_; chunk != nullptr; chunk = chunk->next, ++pos)
|
||||||
|
{
|
||||||
|
snapshot->chunks[pos].allocated = chunk->allocated;
|
||||||
|
}
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
void restoreSnapshot(StackAllocatorSnapshot snapshot) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if (snapshot.data == nullptr)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::size_t numChunks = getNumChunks();
|
||||||
|
MIJIN_ASSERT_FATAL(snapshot->numChunks <= numChunks, "Snapshot contains more chunks than the allocator!");
|
||||||
|
|
||||||
|
#if MIJIN_STACK_ALLOCATOR_DEBUG == 1
|
||||||
|
MIJIN_ASSERT(snapshot->numAllocations_ >= numAllocations_, "Missing deallocation in StackAllocator!");
|
||||||
|
#elif MIJIN_STACK_ALLOCATOR_DEBUG > 1
|
||||||
|
// TODO: compare and print changes
|
||||||
|
unsigned numMismatches = 0;
|
||||||
|
for (const auto& [ptr, stack] : activeAllocations_)
|
||||||
|
{
|
||||||
|
if (snapshot->activeAllocations_.contains(ptr))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++numMismatches;
|
||||||
|
|
||||||
|
if (stack.isError())
|
||||||
|
{
|
||||||
|
std::println(stderr, "Missing deallocation at {}, no stacktrace ({})", ptr, stack.getError().message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::println(stderr, "Missing deallocation at 0x{}:\n{}", ptr, stack.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#if 0 // deallocating more than expected shouldn't be a problem
|
||||||
|
for (const auto& [ptr, stack] : snapshot->activeAllocations_)
|
||||||
|
{
|
||||||
|
if (activeAllocations_.contains(ptr))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++numMismatches;
|
||||||
|
|
||||||
|
if (stack.isError())
|
||||||
|
{
|
||||||
|
std::println(stderr, "Unexpected deallocation at {}, no stacktrace ({})", ptr, stack.getError().message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::println(stderr, "Unexpected deallocation at 0x{}:\n{}", ptr, stack.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (numMismatches > 0)
|
||||||
|
{
|
||||||
|
std::println(stderr, "{} mismatched deallocations when restoring stack allocator snapshot.", numMismatches);
|
||||||
|
MIJIN_TRAP();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
// if we allocated new chunks since the snapshot, these are completely empty now
|
||||||
|
const std::size_t emptyChunks = numChunks - snapshot->numChunks;
|
||||||
|
|
||||||
|
Chunk* chunk = firstChunk_;
|
||||||
|
for (std::size_t idx = 0; idx < emptyChunks; ++idx)
|
||||||
|
{
|
||||||
|
chunk->allocated = 0;
|
||||||
|
chunk = chunk->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the other values are in the snapshot
|
||||||
|
for (std::size_t idx = 0; idx < snapshot->numChunks; ++idx)
|
||||||
|
{
|
||||||
|
chunk->allocated = snapshot->chunks[idx].allocated;
|
||||||
|
chunk = chunk->next;
|
||||||
|
}
|
||||||
|
MIJIN_ASSERT(chunk == nullptr, "Something didn't add up.");
|
||||||
|
|
||||||
|
// finally free the space for the snapshot itself
|
||||||
|
Chunk* snapshotChunk = findChunk(snapshot.data);
|
||||||
|
MIJIN_ASSERT_FATAL(snapshotChunk != nullptr, "Snapshot not in chunks?");
|
||||||
|
snapshotChunk->allocated -= calcSnapshotSize(snapshot->numChunks); // note: this might miss the alignment bytes of the snapshot, but that should be fine
|
||||||
|
snapshot.data->~StackAllocatorSnapshotData();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
void initAndAddChunk(Chunk* newChunk) noexcept
|
||||||
|
{
|
||||||
|
::new (newChunk) Chunk();
|
||||||
|
|
||||||
|
// put it in the front
|
||||||
|
newChunk->next = firstChunk_;
|
||||||
|
firstChunk_ = newChunk;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isInChunk(const void* address, const Chunk& chunk) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
const std::byte* asByte = static_cast<const std::byte*>(address);
|
||||||
|
return asByte >= chunk.data.data() && asByte < chunk.data.data() + ACTUAL_CHUNK_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Chunk* findChunk(const void* address) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
for (Chunk* chunk = firstChunk_; chunk != nullptr; chunk = chunk->next)
|
||||||
|
{
|
||||||
|
if (isInChunk(address, *chunk))
|
||||||
|
{
|
||||||
|
return chunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::size_t calcSnapshotSize(std::size_t numChunks) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return sizeof(impl::StackAllocatorSnapshotData) + ((numChunks - 1) * MIJIN_STRIDEOF(impl::StackAllocatorSnapshotData::ChunkSnapshot));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TValue, typename TStackAllocator>
|
||||||
|
friend class StlStackAllocator;
|
||||||
|
};
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_MEMORY_STACK_ALLOCATOR_HPP_INCLUDED)
|
||||||
56
source/mijin/memory/virtual_allocator.hpp
Normal file
56
source/mijin/memory/virtual_allocator.hpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_MEMORY_VIRTUAL_ALLOCATOR_HPP_INCLUDED)
|
||||||
|
#define MIJIN_MEMORY_VIRTUAL_ALLOCATOR_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../util/annot.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
class VirtualAllocator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~VirtualAllocator() noexcept = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
virtual owner_t<T*> allocate(std::size_t count) noexcept;
|
||||||
|
virtual void deallocate(owner_t<T*> ptr, std::size_t count) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, typename TImpl>
|
||||||
|
class WrappedVirtualAllocator : public VirtualAllocator<T>
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
[[no_unique_address]] TImpl mImpl;
|
||||||
|
public:
|
||||||
|
explicit constexpr WrappedVirtualAllocator(TImpl impl = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TImpl>)
|
||||||
|
: mImpl(std::move(impl)) {}
|
||||||
|
constexpr WrappedVirtualAllocator(const WrappedVirtualAllocator&) = default;
|
||||||
|
constexpr WrappedVirtualAllocator(WrappedVirtualAllocator&&) = default;
|
||||||
|
|
||||||
|
WrappedVirtualAllocator& operator=(const WrappedVirtualAllocator&) = default;
|
||||||
|
WrappedVirtualAllocator& operator=(WrappedVirtualAllocator&&) = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
owner_t<T*> allocate(std::size_t count) noexcept override
|
||||||
|
{
|
||||||
|
return mImpl.allocate(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(owner_t<T*> ptr, std::size_t count) noexcept override
|
||||||
|
{
|
||||||
|
mImpl.deallocate(ptr, count);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
WrappedVirtualAllocator<typename T::value_type, T> makeVirtualAllocator(T allocator) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||||
|
{
|
||||||
|
return WrappedVirtualAllocator<typename T::value_type, T>(std::move(allocator));
|
||||||
|
}
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_MEMORY_VIRTUAL_ALLOCATOR_HPP_INCLUDED)
|
||||||
@ -44,7 +44,7 @@ struct HTTPRequestOptions
|
|||||||
{
|
{
|
||||||
std::string method = "GET";
|
std::string method = "GET";
|
||||||
std::multimap<std::string, std::string> headers;
|
std::multimap<std::string, std::string> headers;
|
||||||
TypelessBuffer body;
|
BaseTypelessBuffer<std::allocator> body;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HTTPResponse
|
struct HTTPResponse
|
||||||
@ -53,7 +53,7 @@ struct HTTPResponse
|
|||||||
unsigned status;
|
unsigned status;
|
||||||
std::string statusMessage;
|
std::string statusMessage;
|
||||||
std::multimap<std::string, std::string> headers;
|
std::multimap<std::string, std::string> headers;
|
||||||
TypelessBuffer body;
|
BaseTypelessBuffer<std::allocator> body;
|
||||||
};
|
};
|
||||||
|
|
||||||
class HTTPStream
|
class HTTPStream
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
template<typename TChar, typename TTraits = std::char_traits<TChar>, typename TAllocator = std::allocator<TChar>>
|
template<typename TChar, typename TTraits = std::char_traits<TChar>, typename TAllocator = MIJIN_DEFAULT_ALLOCATOR<TChar>>
|
||||||
class URLBase
|
class URLBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -33,8 +33,8 @@ public:
|
|||||||
constexpr URLBase(const URLBase&) = default;
|
constexpr URLBase(const URLBase&) = default;
|
||||||
constexpr URLBase(URLBase&&) MIJIN_NOEXCEPT = default;
|
constexpr URLBase(URLBase&&) MIJIN_NOEXCEPT = default;
|
||||||
constexpr URLBase(string_t base) MIJIN_NOEXCEPT : base_(std::move(base)) { parse(); }
|
constexpr URLBase(string_t base) MIJIN_NOEXCEPT : base_(std::move(base)) { parse(); }
|
||||||
constexpr URLBase(string_view_t base) : URLBase(string_t(base.begin(), base.end())) {}
|
constexpr URLBase(string_view_t base, TAllocator allocator = {}) : URLBase(string_t(base.begin(), base.end(), std::move(allocator))) {}
|
||||||
constexpr URLBase(const TChar* base) : URLBase(string_t(base)) {}
|
constexpr URLBase(const TChar* base, TAllocator allocator = {}) : URLBase(string_t(base, std::move(allocator))) {}
|
||||||
|
|
||||||
constexpr URLBase& operator=(const URLBase&) = default;
|
constexpr URLBase& operator=(const URLBase&) = default;
|
||||||
constexpr URLBase& operator=(URLBase&&) MIJIN_NOEXCEPT = default;
|
constexpr URLBase& operator=(URLBase&&) MIJIN_NOEXCEPT = default;
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
#if !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED)
|
#if !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED)
|
||||||
#define MIJIN_UTIL_ALIGN_HPP_INCLUDED 1
|
#define MIJIN_UTIL_ALIGN_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
|
#include <cstdint>
|
||||||
#include "../internal/common.hpp"
|
#include "../internal/common.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
@ -18,7 +20,13 @@ constexpr T alignUp(T value, T alignTo) MIJIN_NOEXCEPT
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* alignUp(T* pointer, std::uintptr_t alignTo) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::bit_cast<T*>(alignUp(std::bit_cast<std::uintptr_t>(pointer), alignTo));
|
||||||
|
}
|
||||||
|
|
||||||
#define MIJIN_STRIDEOF(T) mijin::alignUp(sizeof(T), alignof(T))
|
#define MIJIN_STRIDEOF(T) mijin::alignUp(sizeof(T), alignof(T))
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED)
|
#endif // !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED)
|
||||||
|
|||||||
179
source/mijin/util/annot.hpp
Normal file
179
source/mijin/util/annot.hpp
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_UTIL_ANNOT_HPP_INCLUDED)
|
||||||
|
#define MIJIN_UTIL_ANNOT_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../debug/assert.hpp"
|
||||||
|
|
||||||
|
#if !defined(__has_include)
|
||||||
|
#define __has_include(x) (false)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MIJIN_USE_GSL)
|
||||||
|
#if __has_include(<gsl/gsl>)
|
||||||
|
#define MIJIN_USE_GSL 1
|
||||||
|
#else
|
||||||
|
#define MIJIN_USE_GSL 0
|
||||||
|
#endif
|
||||||
|
#endif // !defined(MIJIN_USE_GSL)
|
||||||
|
|
||||||
|
#include <concepts>
|
||||||
|
#include "./concepts.hpp"
|
||||||
|
|
||||||
|
#if MIJIN_USE_GSL
|
||||||
|
#include <gsl/gsl>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
concept nullable_type = !std::is_same_v<T, std::nullptr_t> && requires(T t) { t == nullptr; };
|
||||||
|
|
||||||
|
template<nullable_type T>
|
||||||
|
class NotNullable
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
T base_;
|
||||||
|
public:
|
||||||
|
template<typename U> requires(std::is_same_v<T, U> && std::is_copy_constructible_v<T>)
|
||||||
|
constexpr NotNullable(U base) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<T>)
|
||||||
|
: base_(base)
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// some compilers apparently need this since they are unable to do proper pattern matching ...
|
||||||
|
NotNullable(const NotNullable&) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<T>) = default;
|
||||||
|
NotNullable(NotNullable&&) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>) = default;
|
||||||
|
|
||||||
|
template<typename TOther> requires(std::is_constructible_v<T, const TOther&>)
|
||||||
|
constexpr NotNullable(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, const TOther&>))
|
||||||
|
: base_(other.base_)
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
template<typename TOther> requires(std::is_constructible_v<T, TOther&&>)
|
||||||
|
constexpr NotNullable(NotNullable<TOther>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TOther&&>))
|
||||||
|
: base_(std::exchange(other.base_, nullptr))
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
template<typename TArg, typename... TArgs> requires(!std::is_same_v<TArg, std::nullptr_t>
|
||||||
|
&& (!std::is_same_v<TArg, T> && sizeof...(TArgs) == 0)
|
||||||
|
&& std::is_constructible_v<T, TArg&&, TArgs&&...>)
|
||||||
|
constexpr NotNullable(TArg&& arg, TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArg&&, TArgs&&...>))
|
||||||
|
: base_(std::forward<TArg>(arg), std::forward<TArgs>(args)...)
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
constexpr NotNullable(T&& base) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||||
|
requires(std::is_move_constructible_v<T>)
|
||||||
|
: base_(std::move(base))
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
constexpr NotNullable(NotNullable&& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<T>)
|
||||||
|
requires(std::is_copy_constructible_v<T>)
|
||||||
|
: base_(other.base_)
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
constexpr NotNullable(NotNullable&& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||||
|
requires(std::is_move_constructible_v<T>)
|
||||||
|
: base_(std::exchange(other.base_, nullptr))
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
|
constexpr NotNullable(std::nullptr_t) MIJIN_DELETE("Type is not nullable.");
|
||||||
|
|
||||||
|
// some compilers apparently need this since they are unable to do proper pattern matching ...
|
||||||
|
NotNullable& operator=(const NotNullable&) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<T>) = default;
|
||||||
|
NotNullable& operator=(NotNullable&&) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>) = default;
|
||||||
|
|
||||||
|
constexpr NotNullable& operator=(const NotNullable& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_assignable_v<T>)
|
||||||
|
requires(std::is_copy_assignable_v<T>)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
this->base_ = other.base_;
|
||||||
|
}
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Assigned nullptr to non-nullable type."); // might still happen if the other type was moved from
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr NotNullable& operator=(NotNullable&& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_assignable_v<T>)
|
||||||
|
requires(std::is_move_assignable_v<T>)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
this->base_ = std::exchange(other.base_, nullptr);
|
||||||
|
}
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Assigned nullptr to non-nullable type."); // might still happen if the other type was moved from
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr NotNullable& operator=(std::nullptr_t) MIJIN_DELETE("Type is not nullable.");
|
||||||
|
|
||||||
|
template<std::equality_comparable_with<T> TOther>
|
||||||
|
bool operator==(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF(noexcept(std::declval<T>() == std::declval<TOther>()))
|
||||||
|
{
|
||||||
|
return base_ == other.base_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::equality_comparable_with<T> TOther>
|
||||||
|
bool operator!=(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF(noexcept(std::declval<T>() != std::declval<TOther>()))
|
||||||
|
{
|
||||||
|
return base_ != other.base_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<nullable_type TOther> requires(std::equality_comparable_with<T, TOther> && !std::is_same_v<TOther, std::nullptr_t>)
|
||||||
|
bool operator==(const TOther& other) MIJIN_NOEXCEPT_IF(noexcept(std::declval<T>() == std::declval<TOther>()))
|
||||||
|
{
|
||||||
|
return base_ == other;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<nullable_type TOther> requires(std::equality_comparable_with<T, TOther> && !std::is_same_v<TOther, std::nullptr_t>)
|
||||||
|
bool operator!=(const TOther& other) MIJIN_NOEXCEPT_IF(noexcept(std::declval<T>() != std::declval<TOther>()))
|
||||||
|
{
|
||||||
|
return base_ != other;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(std::nullptr_t) MIJIN_DELETE("Type is not nullable.");
|
||||||
|
bool operator!=(std::nullptr_t) MIJIN_DELETE("Type is not nullable.");
|
||||||
|
|
||||||
|
constexpr operator const T&() const MIJIN_NOEXCEPT { return get(); }
|
||||||
|
constexpr operator std::nullptr_t() const MIJIN_DELETE("Type is not nullable.");
|
||||||
|
constexpr const T& operator->() const MIJIN_NOEXCEPT { return get(); }
|
||||||
|
constexpr decltype(auto) operator*() const MIJIN_NOEXCEPT_IF(noexcept(*get())) { return *get(); }
|
||||||
|
|
||||||
|
NotNullable& operator++() MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
NotNullable& operator--() MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
NotNullable operator++(int) MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
NotNullable operator--(int) MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
NotNullable& operator+=(std::ptrdiff_t) MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
NotNullable& operator-=(std::ptrdiff_t) MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
void operator[](std::ptrdiff_t) const MIJIN_DELETE("Operator disabled for non-nullable types.");
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr const T& get() const MIJIN_NOEXCEPT { return base_; }
|
||||||
|
|
||||||
|
template<nullable_type TOther>
|
||||||
|
friend class NotNullable;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if MIJIN_USE_GSL
|
||||||
|
template<mijin::pointer_type T>
|
||||||
|
using owner_t = gsl::owner<T>;
|
||||||
|
#else
|
||||||
|
template<mijin::pointer_type T>
|
||||||
|
using owner_t = T;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using not_null_t = NotNullable<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_UTIL_ANNOT_HPP_INCLUDED)
|
||||||
55
source/mijin/util/ansi_colors.hpp
Normal file
55
source/mijin/util/ansi_colors.hpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_UTIL_ANSI_COLORS_HPP_INCLUDED)
|
||||||
|
#define MIJIN_UTIL_ANSI_COLORS_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
|
struct BaseAnsiFontEffects
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
|
||||||
|
static constexpr const char_t* RESET = MIJIN_SMART_QUOTE(char_t, "0");
|
||||||
|
|
||||||
|
static constexpr const char_t* FG_BLACK = MIJIN_SMART_QUOTE(char_t, "30");
|
||||||
|
static constexpr const char_t* FG_RED = MIJIN_SMART_QUOTE(char_t, "31");
|
||||||
|
static constexpr const char_t* FG_GREEN = MIJIN_SMART_QUOTE(char_t, "32");
|
||||||
|
static constexpr const char_t* FG_YELLOW = MIJIN_SMART_QUOTE(char_t, "33");
|
||||||
|
static constexpr const char_t* FG_BLUE = MIJIN_SMART_QUOTE(char_t, "34");
|
||||||
|
static constexpr const char_t* FG_MAGENTA = MIJIN_SMART_QUOTE(char_t, "35");
|
||||||
|
static constexpr const char_t* FG_CYAN = MIJIN_SMART_QUOTE(char_t, "36");
|
||||||
|
static constexpr const char_t* FG_WHITE = MIJIN_SMART_QUOTE(char_t, "37");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_BLACK = MIJIN_SMART_QUOTE(char_t, "90");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_RED = MIJIN_SMART_QUOTE(char_t, "91");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_GREEN = MIJIN_SMART_QUOTE(char_t, "92");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_YELLOW = MIJIN_SMART_QUOTE(char_t, "93");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_BLUE = MIJIN_SMART_QUOTE(char_t, "94");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_MAGENTA = MIJIN_SMART_QUOTE(char_t, "95");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_CYAN = MIJIN_SMART_QUOTE(char_t, "96");
|
||||||
|
static constexpr const char_t* FG_BRIGHT_WHITE = MIJIN_SMART_QUOTE(char_t, "97");
|
||||||
|
|
||||||
|
static constexpr const char_t* BG_BLACK = MIJIN_SMART_QUOTE(char_t, "40");
|
||||||
|
static constexpr const char_t* BG_RED = MIJIN_SMART_QUOTE(char_t, "41");
|
||||||
|
static constexpr const char_t* BG_GREEN = MIJIN_SMART_QUOTE(char_t, "42");
|
||||||
|
static constexpr const char_t* BG_YELLOW = MIJIN_SMART_QUOTE(char_t, "43");
|
||||||
|
static constexpr const char_t* BG_BLUE = MIJIN_SMART_QUOTE(char_t, "44");
|
||||||
|
static constexpr const char_t* BG_MAGENTA = MIJIN_SMART_QUOTE(char_t, "45");
|
||||||
|
static constexpr const char_t* BG_CYAN = MIJIN_SMART_QUOTE(char_t, "46");
|
||||||
|
static constexpr const char_t* BG_WHITE = MIJIN_SMART_QUOTE(char_t, "47");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_BLACK = MIJIN_SMART_QUOTE(char_t, "100");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_RED = MIJIN_SMART_QUOTE(char_t, "101");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_GREEN = MIJIN_SMART_QUOTE(char_t, "102");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_YELLOW = MIJIN_SMART_QUOTE(char_t, "103");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_BLUE = MIJIN_SMART_QUOTE(char_t, "104");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_MAGENTA = MIJIN_SMART_QUOTE(char_t, "105");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_CYAN = MIJIN_SMART_QUOTE(char_t, "106");
|
||||||
|
static constexpr const char_t* BG_BRIGHT_WHITE = MIJIN_SMART_QUOTE(char_t, "107");
|
||||||
|
};
|
||||||
|
MIJIN_DEFINE_CHAR_VERSIONS(AnsiFontEffects)
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_UTIL_ANSI_COLORS_HPP_INCLUDED)
|
||||||
@ -5,6 +5,7 @@
|
|||||||
#define MIJIN_UTIL_CONCEPTS_HPP_INCLUDED 1
|
#define MIJIN_UTIL_CONCEPTS_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include "./traits.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@ -39,6 +40,32 @@ concept pointer_type = std::is_pointer_v<T>;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
concept reference_type = std::is_reference_v<T>;
|
concept reference_type = std::is_reference_v<T>;
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
using pointer_t = typename T::pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept allocator_type = requires(T alloc, typename T::value_type value, detect_or_t<typename T::value_type*, impl::pointer_t, T> pointer, int count)
|
||||||
|
{
|
||||||
|
typename T::value_type;
|
||||||
|
{ alloc.allocate(count) } -> std::same_as<decltype(pointer)>;
|
||||||
|
{ alloc.deallocate(pointer, count) } -> std::same_as<void>;
|
||||||
|
} && !std::is_const_v<typename T::value_type> && !std::is_volatile_v<typename T::value_type>;
|
||||||
|
|
||||||
|
template<typename T, typename TOther>
|
||||||
|
concept allocator_type_for = allocator_type<T> && std::is_same_v<typename T::value_type, TOther>;
|
||||||
|
|
||||||
|
template<template<typename> typename T>
|
||||||
|
concept allocator_tmpl = allocator_type<T<int>>;
|
||||||
|
|
||||||
|
template<typename T, typename TData>
|
||||||
|
concept deleter_type = requires(T deleter, TData* ptr)
|
||||||
|
{
|
||||||
|
deleter(ptr);
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|||||||
@ -6,6 +6,8 @@
|
|||||||
#define MIJIN_UTIL_HASH_HPP_INCLUDED 1
|
#define MIJIN_UTIL_HASH_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <tuple>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@ -16,4 +18,21 @@ inline void hashCombine(std::size_t& seed, const T& value, const THasher& hasher
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... T>
|
||||||
|
struct std::hash<std::tuple<T...>>
|
||||||
|
{
|
||||||
|
std::size_t operator()(const std::tuple<T...>& tuple) const noexcept
|
||||||
|
{
|
||||||
|
return hashImpl(tuple, std::index_sequence_for<T...>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t... indices>
|
||||||
|
std::size_t hashImpl(const std::tuple<T...>& tuple, std::index_sequence<indices...>) const noexcept
|
||||||
|
{
|
||||||
|
std::size_t result = 0;
|
||||||
|
(mijin::hashCombine(result, std::get<indices>(tuple)), ...);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif // MIJIN_UTIL_HASH_HPP_INCLUDED
|
#endif // MIJIN_UTIL_HASH_HPP_INCLUDED
|
||||||
|
|||||||
42
source/mijin/util/misc.hpp
Normal file
42
source/mijin/util/misc.hpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_UTIL_MISC_HPP_INCLUDED)
|
||||||
|
#define MIJIN_UTIL_MISC_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
|
||||||
|
//
|
||||||
|
// public functions
|
||||||
|
//
|
||||||
|
|
||||||
|
template<auto V, typename T>
|
||||||
|
constexpr decltype(auto) idValue(T&& value)
|
||||||
|
{
|
||||||
|
return std::forward<T>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template<typename T, typename... TArgs>
|
||||||
|
struct ConstructArrayHelper
|
||||||
|
{
|
||||||
|
template<std::size_t... I>
|
||||||
|
static constexpr std::array<T, sizeof...(I)> construct(const TArgs&... args, std::index_sequence<I...>)
|
||||||
|
{
|
||||||
|
return {idValue<I>(T(args...))...};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, std::size_t count, typename... TArgs>
|
||||||
|
constexpr std::array<T, count> constructArray(const TArgs&... args)
|
||||||
|
{
|
||||||
|
return impl::ConstructArrayHelper<T, TArgs...>::construct(args..., std::make_index_sequence<count>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif // !defined(MIJIN_UTIL_MISC_HPP_INCLUDED)
|
||||||
@ -5,11 +5,14 @@
|
|||||||
#include "../debug/assert.hpp"
|
#include "../debug/assert.hpp"
|
||||||
|
|
||||||
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||||
|
#include <bit>
|
||||||
|
#include <cstring>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <malloc.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include "../util/winundef.hpp"
|
#include "../util/winundef.hpp"
|
||||||
#endif
|
#endif
|
||||||
@ -139,4 +142,40 @@ std::string getExecutablePath() MIJIN_NOEXCEPT
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* alignedAlloc(std::size_t alignment, std::size_t size) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
return _aligned_malloc(size, alignment);
|
||||||
|
#else
|
||||||
|
return std::aligned_alloc(alignment, size);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void* alignedRealloc(void* ptr, std::size_t alignment, std::size_t size) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
return _aligned_realloc(ptr, size, alignment);
|
||||||
|
#else
|
||||||
|
void* newPtr = std::realloc(ptr, size);
|
||||||
|
if (newPtr == ptr || (std::bit_cast<std::uintptr_t>(newPtr) % alignment) == 0)
|
||||||
|
{
|
||||||
|
return newPtr;
|
||||||
|
}
|
||||||
|
// bad luck, have to copy a second time
|
||||||
|
void* newPtr2 = std::aligned_alloc(alignment, size);
|
||||||
|
std::memcpy(newPtr2, newPtr, size);
|
||||||
|
std::free(newPtr);
|
||||||
|
return newPtr2;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void alignedFree(void* ptr)
|
||||||
|
{
|
||||||
|
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
|
_aligned_free(ptr);
|
||||||
|
#else
|
||||||
|
std::free(ptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|||||||
@ -81,6 +81,10 @@ void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT;
|
|||||||
|
|
||||||
[[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT;
|
[[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
[[nodiscard]] void* alignedAlloc(std::size_t alignment, std::size_t size) MIJIN_NOEXCEPT;
|
||||||
|
[[nodiscard]] void* alignedRealloc(void* ptr, std::size_t alignment, std::size_t size) MIJIN_NOEXCEPT;
|
||||||
|
void alignedFree(void* ptr);
|
||||||
|
|
||||||
SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT
|
SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
|
|||||||
@ -7,6 +7,9 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
|
#include <climits>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
@ -17,6 +20,7 @@
|
|||||||
|
|
||||||
#include "./iterators.hpp"
|
#include "./iterators.hpp"
|
||||||
#include "../internal/common.hpp"
|
#include "../internal/common.hpp"
|
||||||
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@ -33,8 +37,56 @@ namespace mijin
|
|||||||
// public traits
|
// public traits
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename TString>
|
template<typename T>
|
||||||
using char_type_t = decltype(std::string_view(std::declval<TString>()))::value_type;
|
inline constexpr bool is_string_v = is_template_instance_v<std::basic_string, std::remove_cvref_t<T>>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept std_string_type = is_string_v<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool is_string_view_v = is_template_instance_v<std::basic_string_view, std::remove_cvref_t<T>>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept std_string_view_type = is_string_view_v<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool is_char_v = is_any_type_v<std::remove_cvref_t<T>, char, wchar_t, char8_t, char16_t, char32_t>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept char_type = is_char_v<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool is_cstring_v = std::is_pointer_v<T> && is_char_v<std::remove_pointer_t<T>>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept cstring_type = is_cstring_v<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct str_char_type
|
||||||
|
{
|
||||||
|
using type = void;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<char_type T>
|
||||||
|
struct str_char_type<T*>
|
||||||
|
{
|
||||||
|
using type = std::remove_cvref_t<T>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<std_string_view_type T>
|
||||||
|
struct str_char_type<T>
|
||||||
|
{
|
||||||
|
using type = typename std::remove_cvref_t<T>::value_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<std_string_type T>
|
||||||
|
struct str_char_type<T>
|
||||||
|
{
|
||||||
|
using type = typename std::remove_cvref_t<T>::value_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using str_char_type_t = str_char_type<T>::type;
|
||||||
|
|
||||||
//
|
//
|
||||||
// public types
|
// public types
|
||||||
@ -255,7 +307,7 @@ template<typename TString>
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trimPrefix(TString&& string)
|
auto trimPrefix(TString&& string)
|
||||||
{
|
{
|
||||||
return trimPrefix(string, detail::DEFAULT_TRIM_CHARS<char_type_t<TString>>);
|
return trimPrefix(string, detail::DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TString, typename TChars>
|
template<typename TString, typename TChars>
|
||||||
@ -269,7 +321,7 @@ template<typename TString>
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trimSuffix(TString&& string)
|
auto trimSuffix(TString&& string)
|
||||||
{
|
{
|
||||||
return trimSuffix(string, detail::DEFAULT_TRIM_CHARS<char_type_t<TString>>);
|
return trimSuffix(string, detail::DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TString, typename TChars>
|
template<typename TString, typename TChars>
|
||||||
@ -283,7 +335,7 @@ template<typename TString>
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trim(TString&& string)
|
auto trim(TString&& string)
|
||||||
{
|
{
|
||||||
return trim(string, detail::DEFAULT_TRIM_CHARS<char_type_t<TString>>);
|
return trim(string, detail::DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TLeft, typename TRight>
|
template<typename TLeft, typename TRight>
|
||||||
@ -425,7 +477,7 @@ struct Join
|
|||||||
{
|
{
|
||||||
const char* delimiter;
|
const char* delimiter;
|
||||||
|
|
||||||
explicit Join(const char* delimiter_) MIJIN_NOEXCEPT : delimiter(delimiter_) {}
|
explicit Join(const char* delimiter_) MIJIN_NOEXCEPT: delimiter(delimiter_) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TIterable>
|
template<typename TIterable>
|
||||||
@ -433,6 +485,142 @@ auto operator|(TIterable&& iterable, const Join& joiner)
|
|||||||
{
|
{
|
||||||
return join(std::forward<TIterable>(iterable), joiner.delimiter);
|
return join(std::forward<TIterable>(iterable), joiner.delimiter);
|
||||||
}
|
}
|
||||||
|
} // namespace pipe
|
||||||
|
|
||||||
|
struct [[nodiscard]] ConvertCharTypeResult
|
||||||
|
{
|
||||||
|
unsigned numRead = 0;
|
||||||
|
unsigned numWritten = 0;
|
||||||
|
|
||||||
|
constexpr operator bool() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return numRead != 0 || numWritten != 0;
|
||||||
|
}
|
||||||
|
constexpr bool operator !() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !static_cast<bool>(*this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TFrom, typename TTo>
|
||||||
|
ConvertCharTypeResult convertCharType(const TFrom* chrFrom, std::size_t numFrom, TTo* outTo, std::size_t numTo, std::mbstate_t& mbstate) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<TFrom, char>)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<TTo, wchar_t>)
|
||||||
|
{
|
||||||
|
const std::size_t result = std::mbrtowc(outTo, chrFrom, numFrom, &mbstate);
|
||||||
|
if (result == static_cast<std::size_t>(-1))
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
.numRead = static_cast<unsigned>(result),
|
||||||
|
.numWritten = 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if constexpr (std::is_same_v<TTo, char8_t>)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if constexpr (std::is_same_v<TFrom, wchar_t>)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<TTo, char>)
|
||||||
|
{
|
||||||
|
if (numTo < MB_CUR_MAX)
|
||||||
|
{
|
||||||
|
char tmpBuf[MB_LEN_MAX];
|
||||||
|
const ConvertCharTypeResult result = convertCharType(chrFrom, numFrom, tmpBuf, MB_LEN_MAX, mbstate);
|
||||||
|
if (result && result.numWritten <= numTo)
|
||||||
|
{
|
||||||
|
std::memcpy(outTo, tmpBuf, result.numWritten);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const std::size_t result = std::wcrtomb(outTo, *chrFrom, &mbstate);
|
||||||
|
if (result == static_cast<std::size_t>(-1))
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
.numRead = 1,
|
||||||
|
.numWritten = static_cast<unsigned>(result)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if constexpr (std::is_same_v<TTo, char8_t>)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if constexpr (std::is_same_v<TFrom, char8_t>)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename TFrom, typename TTo>
|
||||||
|
ConvertCharTypeResult convertCharType(const TFrom* chrFrom, std::size_t numFrom, TTo* outTo, std::size_t numTo) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
std::mbstate_t mbstate;
|
||||||
|
return convertCharType(chrFrom, numFrom, outTo, numTo, mbstate);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TIterator>
|
||||||
|
struct [[nodiscard]] ConvertStringTypeResult
|
||||||
|
{
|
||||||
|
TIterator iterator;
|
||||||
|
bool success;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TTo, typename TFrom, typename TFromTraits, std::output_iterator<TTo> TIterator>
|
||||||
|
ConvertStringTypeResult<TIterator> convertStringType(std::basic_string_view<TFrom, TFromTraits> strFrom, TIterator outIterator)
|
||||||
|
{
|
||||||
|
TTo outBuffer[MB_LEN_MAX];
|
||||||
|
|
||||||
|
std::mbstate_t mbstate = {};
|
||||||
|
for (auto it = strFrom.begin(); it != strFrom.end();)
|
||||||
|
{
|
||||||
|
const std::size_t remaining = std::distance(it, strFrom.end());
|
||||||
|
const ConvertCharTypeResult result = convertCharType(&*it, remaining, outBuffer, MB_LEN_MAX, mbstate);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
.iterator = outIterator,
|
||||||
|
.success = false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
for (unsigned pos = 0; pos < result.numWritten; ++pos)
|
||||||
|
{
|
||||||
|
*outIterator = outBuffer[pos];
|
||||||
|
++outIterator;
|
||||||
|
}
|
||||||
|
it = std::next(it, result.numRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
.iterator = outIterator,
|
||||||
|
.success = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TFrom, typename TFromTraits, typename TTo, typename TToTraits, typename TToAllocator>
|
||||||
|
bool convertStringType(std::basic_string_view<TFrom, TFromTraits> strFrom, std::basic_string<TTo, TToTraits, TToAllocator>& outString)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<TTo, TFrom>)
|
||||||
|
{
|
||||||
|
outString += strFrom;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return convertStringType<TTo>(strFrom, std::back_inserter(outString)).success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TFrom, typename TTo, typename TToTraits, typename TToAllocator>
|
||||||
|
bool convertStringType(const TFrom* strFrom, std::basic_string<TTo, TToTraits, TToAllocator>& outString)
|
||||||
|
{
|
||||||
|
return convertStringType(std::basic_string_view<TFrom>(strFrom), outString);
|
||||||
}
|
}
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,9 @@ namespace mijin
|
|||||||
// public types
|
// public types
|
||||||
//
|
//
|
||||||
|
|
||||||
|
struct Type_; // use as typevar
|
||||||
|
struct Any_; // use as placeholder in templates
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct always_false
|
struct always_false
|
||||||
{
|
{
|
||||||
@ -95,11 +98,88 @@ struct map_template {
|
|||||||
using type_t = TTemplate<TPredicate<T>>;
|
using type_t = TTemplate<TPredicate<T>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename... Types>
|
template<template<typename...> typename TTemplate, typename TType>
|
||||||
struct is_any_type : std::disjunction<std::is_same<T, Types>...> {};
|
struct is_template_instance : std::false_type {};
|
||||||
|
|
||||||
|
template<template<typename...> typename TTemplate, typename... TArgs>
|
||||||
|
struct is_template_instance<TTemplate, TTemplate<TArgs...>> : std::true_type {};
|
||||||
|
|
||||||
|
template<template<typename...> typename TTemplate, typename TType>
|
||||||
|
constexpr bool is_template_instance_v = is_template_instance<TTemplate, TType>::value;
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template<template<typename...> typename TTemplate, typename... TArgsTTmpl>
|
||||||
|
struct tmpl_param_comparator
|
||||||
|
{
|
||||||
|
template<typename TTmpl, typename TInstance>
|
||||||
|
static constexpr bool compare_single = std::is_same_v<TTmpl, TInstance> or std::is_same_v<TTmpl, Any_>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct matches : std::false_type {};
|
||||||
|
|
||||||
|
// original (which MSVC didn't like for some reason :/)
|
||||||
|
// template<typename... TArgsInstance>
|
||||||
|
// struct matches<TTemplate<TArgsInstance...>> : std::bool_constant<(compare_single<TArgsTTmpl, TArgsInstance> && ...)> {};
|
||||||
|
|
||||||
|
template<template<typename...> typename TOtherTemplate, typename... TArgsInstance> requires(is_template_instance_v<TTemplate, TOtherTemplate<TArgsInstance...>>)
|
||||||
|
struct matches<TOtherTemplate<TArgsInstance...>> : std::bool_constant<(compare_single<TArgsTTmpl, TArgsInstance> && ...)> {};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using matches_t = matches<T>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TTemplate, typename TType>
|
||||||
|
struct match_template_type : std::false_type {};
|
||||||
|
|
||||||
|
template<template<typename...> typename TTemplate, typename TType, typename... TArgs>
|
||||||
|
struct match_template_type<TTemplate<TArgs...>, TType> : impl::tmpl_param_comparator<TTemplate, TArgs...>::template matches_t<TType> {};
|
||||||
|
|
||||||
|
template<typename TTemplate, typename TType>
|
||||||
|
constexpr bool match_template_type_v = match_template_type<TTemplate, TType>::value;
|
||||||
|
|
||||||
|
// similar to std::is_same, but allows placeholders
|
||||||
|
// - is_type_or_impl<some_tmpl<Type_>, some_type> resolves to some_tmpl<some_type>
|
||||||
|
// - is_type_or_impl<some_tmpl<Any_, int>, some_type> checks if some_type is an instance of some_tmpl with int as 2nd parameter
|
||||||
|
template<typename TMatch, typename TConcrete>
|
||||||
|
struct type_matches
|
||||||
|
{
|
||||||
|
using type = std::is_same<TMatch, TConcrete>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<template<typename> typename TTmplMatch, typename TConcrete>
|
||||||
|
struct type_matches<TTmplMatch<Type_>, TConcrete>
|
||||||
|
{
|
||||||
|
using type = TTmplMatch<TConcrete>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<template<typename...> typename TTmplMatch, typename TConcrete, typename... TArgs>
|
||||||
|
struct type_matches<TTmplMatch<TArgs...>, TConcrete>
|
||||||
|
{
|
||||||
|
using type = match_template_type<TTmplMatch<TArgs...>, TConcrete>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TMatch, typename TConcrete>
|
||||||
|
using type_matches_t = type_matches<TMatch, TConcrete>::type;
|
||||||
|
|
||||||
|
template<typename TMatch, typename TConcrete>
|
||||||
|
inline constexpr bool type_matches_v = type_matches_t<TMatch, TConcrete>::value;
|
||||||
|
|
||||||
|
template<typename TConcrete, typename... TMatches>
|
||||||
|
struct is_any_type : std::disjunction<std::is_same<TConcrete, TMatches>...> {};
|
||||||
|
|
||||||
|
template<typename TConcrete, typename... TMatches>
|
||||||
|
static constexpr bool is_any_type_v = is_any_type<TConcrete, TMatches...>::value;
|
||||||
|
|
||||||
|
template<typename TConcrete, typename... TMatches>
|
||||||
|
struct match_any_type : std::disjunction<type_matches_t<TMatches, TConcrete>...> {};
|
||||||
|
|
||||||
|
template<typename TConcrete, typename... TMatches>
|
||||||
|
static constexpr bool match_any_type_v = match_any_type<TConcrete, TMatches...>::value;
|
||||||
|
|
||||||
template<typename T, typename... Types>
|
template<typename T, typename... Types>
|
||||||
static constexpr bool is_any_type_v = is_any_type<T, Types...>::value;
|
concept union_type = match_any_type_v<T, Types...>;
|
||||||
|
|
||||||
template<typename TElement, typename TCollection>
|
template<typename TElement, typename TCollection>
|
||||||
struct is_type_member;
|
struct is_type_member;
|
||||||
@ -139,14 +219,39 @@ struct TypeAtHelper<0, TArg, TArgs...>
|
|||||||
template<std::size_t I, typename... TArgs>
|
template<std::size_t I, typename... TArgs>
|
||||||
using type_at_t = TypeAtHelper<I, TArgs...>::type_t;
|
using type_at_t = TypeAtHelper<I, TArgs...>::type_t;
|
||||||
|
|
||||||
template<template<typename...> typename TTemplate, typename TType>
|
template<typename TDefault, template<typename...> typename TOper, typename... TArgs>
|
||||||
struct is_template_instance : std::false_type {};
|
struct detect_or
|
||||||
|
{
|
||||||
|
using type = TDefault;
|
||||||
|
static constexpr bool detected = false;
|
||||||
|
};
|
||||||
|
|
||||||
template<template<typename...> typename TTemplate, typename... TArgs>
|
template<typename TDefault, template<typename...> typename TOper, typename... TArgs>
|
||||||
struct is_template_instance<TTemplate, TTemplate<TArgs...>> : std::true_type {};
|
requires requires { typename TOper<TArgs...>; }
|
||||||
|
struct detect_or<TDefault, TOper, TArgs...>
|
||||||
|
{
|
||||||
|
using type = TOper<TArgs...>;
|
||||||
|
static constexpr bool detected = true;
|
||||||
|
};
|
||||||
|
template<typename TDefault, template<typename...> typename TOper, typename... TArgs>
|
||||||
|
using detect_or_t = detect_or<TDefault, TOper, TArgs...>::type;
|
||||||
|
|
||||||
template<template<typename...> typename TTemplate, typename TType>
|
struct empty_type {};
|
||||||
constexpr bool is_template_instance_v = is_template_instance<TTemplate, TType>::value;
|
|
||||||
|
template<typename T, bool enable>
|
||||||
|
struct optional_base
|
||||||
|
{
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct optional_base<T, false>
|
||||||
|
{
|
||||||
|
using type = empty_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, bool enable>
|
||||||
|
using optional_base_t = optional_base<T, enable>::type;
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
@ -158,6 +263,34 @@ decltype(auto) delayEvaluation(TType&& value)
|
|||||||
return static_cast<TType&&>(value);
|
return static_cast<TType&&>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MIJIN_STATIC_TESTS 1
|
||||||
|
#if MIJIN_STATIC_TESTS
|
||||||
|
namespace test
|
||||||
|
{
|
||||||
|
template<typename T, typename U>
|
||||||
|
struct MyTemplate {};
|
||||||
|
|
||||||
|
static_assert(match_template_type_v<MyTemplate<Any_, int>, MyTemplate<int, int>>);
|
||||||
|
static_assert(match_template_type_v<MyTemplate<Any_, Any_>, MyTemplate<int, int>>);
|
||||||
|
static_assert(type_matches_v<MyTemplate<Any_, int>, MyTemplate<int, int>>);
|
||||||
|
static_assert(type_matches_v<MyTemplate<Any_, Any_>, MyTemplate<int, int>>);
|
||||||
|
static_assert(!type_matches_v<MyTemplate<double, int>, MyTemplate<int, int>>);
|
||||||
|
static_assert(type_matches_v<MyTemplate<Any_, Any_>, MyTemplate<int, double>>);
|
||||||
|
static_assert(type_matches_v<std::is_pointer<Type_>, void*>);
|
||||||
|
|
||||||
|
static_assert(union_type<int, int>);
|
||||||
|
static_assert(!union_type<int>);
|
||||||
|
static_assert(!union_type<int, double>);
|
||||||
|
static_assert(union_type<MyTemplate<int, int>, MyTemplate<int, int>>);
|
||||||
|
static_assert(union_type<MyTemplate<int, int>, MyTemplate<Any_, int>>);
|
||||||
|
static_assert(union_type<MyTemplate<int, int>, MyTemplate<Any_, Any_>>);
|
||||||
|
static_assert(union_type<MyTemplate<int, int>, MyTemplate<double, double>, MyTemplate<Any_, Any_>>);
|
||||||
|
static_assert(!union_type<int*, int>);
|
||||||
|
static_assert(union_type<int*, std::is_pointer<Type_>>);
|
||||||
|
static_assert(!union_type<int, std::is_pointer<Type_>>);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_UTIL_TRAITS_HPP_INCLUDED)
|
#endif // !defined(MIJIN_UTIL_TRAITS_HPP_INCLUDED)
|
||||||
|
|||||||
@ -22,3 +22,7 @@
|
|||||||
#if defined(RELATIVE)
|
#if defined(RELATIVE)
|
||||||
#undef RELATIVE
|
#undef RELATIVE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(DEBUG)
|
||||||
|
#undef DEBUG
|
||||||
|
#endif
|
||||||
|
|||||||
@ -139,7 +139,7 @@ detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path,
|
|||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const noexcept
|
FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
.path = path,
|
.path = path,
|
||||||
@ -149,7 +149,7 @@ FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo MemoryFileSystemAdapter::fileInfo(const fs::path& path, const detail::MemoryFile& file) const noexcept
|
FileInfo MemoryFileSystemAdapter::fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
.path = path,
|
.path = path,
|
||||||
|
|||||||
@ -21,8 +21,8 @@ struct MemoryFile
|
|||||||
};
|
};
|
||||||
struct MemoryFolder
|
struct MemoryFolder
|
||||||
{
|
{
|
||||||
VectorMap<std::string, MemoryFile> files;
|
VectorMap<std::string, MemoryFile, std::allocator<std::string>, std::allocator<MemoryFile>> files; // TODO: make the FS library allocator aware
|
||||||
VectorMap<std::string, MemoryFolder> folders;
|
VectorMap<std::string, MemoryFolder, std::allocator<std::string>, std::allocator<MemoryFolder>> folders;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -39,10 +39,15 @@ fs::path StackedFileSystemAdapter::getHomeFolder()
|
|||||||
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
#pragma warning(disable : 4996) // yeah, we're using a deprecated function here, in order to implement another deprecated function ¯\_(ツ)_/¯
|
#pragma warning(disable : 4996) // yeah, we're using a deprecated function here, in order to implement another deprecated function ¯\_(ツ)_/¯
|
||||||
|
#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||||
#endif
|
#endif
|
||||||
return adapters_.front()->getHomeFolder();
|
return adapters_.front()->getHomeFolder();
|
||||||
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
|
#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user