Compare commits
14 Commits
master
...
657b0d1ab1
| Author | SHA1 | Date | |
|---|---|---|---|
| 657b0d1ab1 | |||
|
|
4bd6843ba5 | ||
|
|
657d05341c | ||
|
|
9cefc52568 | ||
|
|
2d413764f0 | ||
|
|
50532459ce | ||
| 2d5a7316f1 | |||
| cf45aaa39a | |||
| def8d0b6b8 | |||
| f7daa58b38 | |||
| 1208f1f59f | |||
| 93a0b6cf58 | |||
| cae7fecb51 | |||
| 13f37a81b6 |
1
SModule
1
SModule
@@ -16,6 +16,7 @@ mijin_sources = Split("""
|
|||||||
source/mijin/platform/folders.cpp
|
source/mijin/platform/folders.cpp
|
||||||
source/mijin/util/os.cpp
|
source/mijin/util/os.cpp
|
||||||
source/mijin/types/name.cpp
|
source/mijin/types/name.cpp
|
||||||
|
source/mijin/types/path.cpp
|
||||||
source/mijin/virtual_filesystem/filesystem.cpp
|
source/mijin/virtual_filesystem/filesystem.cpp
|
||||||
source/mijin/virtual_filesystem/memory.cpp
|
source/mijin/virtual_filesystem/memory.cpp
|
||||||
source/mijin/virtual_filesystem/stacked.cpp
|
source/mijin/virtual_filesystem/stacked.cpp
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
#define MIJIN_ASYNC_COROUTINE_HPP_INCLUDED 1
|
#define MIJIN_ASYNC_COROUTINE_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
|
||||||
|
# define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0 // Capture stack each time a coroutine is started. Warning, expensive! // TODO: maybe implement a lighter version only storing the return address?
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <any>
|
#include <any>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <coroutine>
|
#include <coroutine>
|
||||||
@@ -21,17 +25,7 @@
|
|||||||
#include "../util/flag.hpp"
|
#include "../util/flag.hpp"
|
||||||
#include "../util/iterators.hpp"
|
#include "../util/iterators.hpp"
|
||||||
#include "../util/misc.hpp"
|
#include "../util/misc.hpp"
|
||||||
#include "../util/scope_guard.hpp"
|
|
||||||
#include "../util/traits.hpp"
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
|
|
||||||
# define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0 // Capture stack each time a coroutine is started. Warning, expensive! // TODO: maybe implement a lighter version only storing the return address?
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(MIJIN_COROUTINE_ENABLE_EXCEPTIONS)
|
|
||||||
# define MIJIN_COROUTINE_ENABLE_EXCEPTIONS 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||||
#include "../debug/stacktrace.hpp"
|
#include "../debug/stacktrace.hpp"
|
||||||
#endif
|
#endif
|
||||||
@@ -112,8 +106,6 @@ public:
|
|||||||
return !state_.expired();
|
return !state_.expired();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const void* getState() const MIJIN_NOEXCEPT { return state_.lock().get(); }
|
|
||||||
|
|
||||||
inline void cancel() const MIJIN_NOEXCEPT;
|
inline void cancel() const MIJIN_NOEXCEPT;
|
||||||
[[nodiscard]] inline Optional<std::source_location> getLocation() 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
|
||||||
@@ -193,16 +185,10 @@ struct TaskReturn<void, TPromise>
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
template<typename TValue>
|
||||||
using TaskFuture = Future<TValue, TAllocator, MIJIN_COROUTINE_ENABLE_EXCEPTIONS>;
|
|
||||||
|
|
||||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
|
||||||
using TaskFuturePtr = FuturePtr<TValue, TAllocator, MIJIN_COROUTINE_ENABLE_EXCEPTIONS>;
|
|
||||||
|
|
||||||
template<typename TValue, template<typename> typename TAllocator>
|
|
||||||
struct TaskAwaitableFuture
|
struct TaskAwaitableFuture
|
||||||
{
|
{
|
||||||
TaskFuturePtr<TValue, TAllocator> future;
|
FuturePtr<TValue> future;
|
||||||
|
|
||||||
[[nodiscard]] constexpr bool await_ready() const MIJIN_NOEXCEPT { return future->ready(); }
|
[[nodiscard]] constexpr bool await_ready() const MIJIN_NOEXCEPT { return future->ready(); }
|
||||||
constexpr void await_suspend(std::coroutine_handle<>) const MIJIN_NOEXCEPT {}
|
constexpr void await_suspend(std::coroutine_handle<>) const MIJIN_NOEXCEPT {}
|
||||||
@@ -342,12 +328,12 @@ 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> typename TAllocator2>
|
template<typename TValue>
|
||||||
auto await_transform(TaskFuturePtr<TValue, TAllocator2> future, std::source_location sourceLoc = std::source_location::current()) 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);
|
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||||
TaskAwaitableFuture<TValue, TAllocator> awaitable{future};
|
TaskAwaitableFuture<TValue> awaitable{future};
|
||||||
if (!awaitable.await_ready())
|
if (!awaitable.await_ready())
|
||||||
{
|
{
|
||||||
state_.status = TaskStatus::WAITING;
|
state_.status = TaskStatus::WAITING;
|
||||||
@@ -648,10 +634,10 @@ public:
|
|||||||
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>
|
||||||
TaskFuturePtr<TResult, TAllocator> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
FuturePtr<TResult> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
TaskFuturePtr<TResult, TAllocator> addTask(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle = nullptr) MIJIN_NOEXCEPT
|
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.");
|
static_assert(TaskAllocatorTraits<TAllocator>::default_is_valid_v, "Allocator is not valid when default constructed, use makeTask() instead.");
|
||||||
return addTaskImpl(std::move(task), outHandle);
|
return addTaskImpl(std::move(task), outHandle);
|
||||||
@@ -677,7 +663,7 @@ protected:
|
|||||||
protected:
|
protected:
|
||||||
static inline TaskLoop*& currentLoopStorage() MIJIN_NOEXCEPT;
|
static inline TaskLoop*& currentLoopStorage() MIJIN_NOEXCEPT;
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
static inline void setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT_IF(!MIJIN_COROUTINE_ENABLE_EXCEPTIONS);
|
static inline void setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TResult = void>
|
template<typename TResult = void>
|
||||||
@@ -824,12 +810,12 @@ TaskBase<TResult, TAllocator>::~TaskBase() MIJIN_NOEXCEPT
|
|||||||
|
|
||||||
template<template<typename> typename TAllocator>
|
template<template<typename> typename TAllocator>
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
TaskFuturePtr<TResult, TAllocator> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocator> 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);
|
||||||
|
|
||||||
TaskFuturePtr<TResult, TAllocator> future = std::allocate_shared<TaskFuture<TResult, TAllocator>>(TAllocator<Future<TResult, TAllocator>>(allocator_), allocator_);
|
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)
|
||||||
@@ -868,7 +854,7 @@ TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
|||||||
while (status == TaskStatus::RUNNING);
|
while (status == TaskStatus::RUNNING);
|
||||||
impl::gCurrentTaskState = nullptr;
|
impl::gCurrentTaskState = nullptr;
|
||||||
|
|
||||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING && !MIJIN_COROUTINE_ENABLE_EXCEPTIONS
|
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||||
if (task.task && task.task->exception())
|
if (task.task && task.task->exception())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -896,22 +882,7 @@ TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
|||||||
#endif // MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
#endif // MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||||
if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED)
|
if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED)
|
||||||
{
|
{
|
||||||
try
|
task.setFuture(task);
|
||||||
{
|
|
||||||
task.setFuture(task);
|
|
||||||
}
|
|
||||||
catch(TaskCancelled&) {}
|
|
||||||
catch(...)
|
|
||||||
{
|
|
||||||
if (uncaughtExceptionHandler_)
|
|
||||||
{
|
|
||||||
uncaughtExceptionHandler_(std::current_exception());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -938,22 +909,10 @@ template<template<typename> typename TAllocator>
|
|||||||
|
|
||||||
template<template<typename> typename TAllocator>
|
template<template<typename> typename TAllocator>
|
||||||
template<typename TResult>
|
template<typename TResult>
|
||||||
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT_IF(!MIJIN_COROUTINE_ENABLE_EXCEPTIONS)
|
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
TaskBase<TResult, TAllocator>& task = *static_cast<TaskBase<TResult, TAllocator>*>(storedTask.task->raw());
|
TaskBase<TResult, TAllocator>& task = *static_cast<TaskBase<TResult, TAllocator>*>(storedTask.task->raw());
|
||||||
const auto& future = std::any_cast<TaskFuturePtr<TResult, TAllocator>&>(storedTask.resultData);
|
auto future = std::any_cast<FuturePtr<TResult>>(storedTask.resultData);
|
||||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTIONS
|
|
||||||
if (task.state().exception)
|
|
||||||
{
|
|
||||||
if (future.use_count() < 2)
|
|
||||||
{
|
|
||||||
// future has been discarded, but someone must handle the exception
|
|
||||||
std::rethrow_exception(task.state().exception);
|
|
||||||
}
|
|
||||||
future->setException(task.state().exception);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if constexpr (!std::is_same_v<TResult, void>)
|
if constexpr (!std::is_same_v<TResult, void>)
|
||||||
{
|
{
|
||||||
@@ -966,7 +925,7 @@ template<typename TResult>
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<template<typename> typename TAllocator>
|
template<template<typename> typename TAllocator>
|
||||||
inline std::suspend_always c_switchContext(TaskLoop<TAllocator>& taskLoop)
|
inline std::suspend_always switchContext(TaskLoop<TAllocator>& taskLoop)
|
||||||
{
|
{
|
||||||
TaskLoop<TAllocator>& currentTaskLoop = TaskLoop<TAllocator>::current();
|
TaskLoop<TAllocator>& currentTaskLoop = TaskLoop<TAllocator>::current();
|
||||||
if (¤tTaskLoop == &taskLoop) {
|
if (¤tTaskLoop == &taskLoop) {
|
||||||
@@ -976,12 +935,6 @@ inline std::suspend_always c_switchContext(TaskLoop<TAllocator>& taskLoop)
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<template<typename> typename TAllocator>
|
|
||||||
inline std::suspend_always switchContext(TaskLoop<TAllocator>& taskLoop)
|
|
||||||
{
|
|
||||||
return c_switchContext(taskLoop);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<template<typename> typename TAllocator>
|
template<template<typename> typename TAllocator>
|
||||||
void BaseSimpleTaskLoop<TAllocator>::transferCurrentTask(TaskLoop<TAllocator>& otherLoop) MIJIN_NOEXCEPT
|
void BaseSimpleTaskLoop<TAllocator>::transferCurrentTask(TaskLoop<TAllocator>& otherLoop) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
@@ -1044,9 +997,6 @@ inline auto BaseSimpleTaskLoop<TAllocator>::tick() -> CanContinue
|
|||||||
// set current taskloop
|
// set current taskloop
|
||||||
MIJIN_ASSERT(TaskLoop<TAllocator>::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.");
|
||||||
TaskLoop<TAllocator>::currentLoopStorage() = this;
|
TaskLoop<TAllocator>::currentLoopStorage() = this;
|
||||||
MIJIN_SCOPE_EXIT {
|
|
||||||
TaskLoop<TAllocator>::currentLoopStorage() = nullptr;
|
|
||||||
};
|
|
||||||
threadId_ = std::this_thread::get_id();
|
threadId_ = std::this_thread::get_id();
|
||||||
|
|
||||||
// move over all tasks from newTasks
|
// move over all tasks from newTasks
|
||||||
@@ -1096,6 +1046,8 @@ inline auto BaseSimpleTaskLoop<TAllocator>::tick() -> CanContinue
|
|||||||
canContinue = CanContinue::YES;
|
canContinue = CanContinue::YES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// reset current loop
|
||||||
|
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) {
|
||||||
@@ -1237,7 +1189,7 @@ void BaseMultiThreadedTaskLoop<TAllocator>::workerThread(std::stop_token stopTok
|
|||||||
// run it
|
// run it
|
||||||
getCurrentTask() = &*task;
|
getCurrentTask() = &*task;
|
||||||
impl::gCurrentTaskState = task->task->sharedState();
|
impl::gCurrentTaskState = task->task->sharedState();
|
||||||
base_t::tickTask(*task);
|
tickTask(*task);
|
||||||
getCurrentTask() = nullptr;
|
getCurrentTask() = nullptr;
|
||||||
impl::gCurrentTaskState = nullptr;
|
impl::gCurrentTaskState = nullptr;
|
||||||
|
|
||||||
@@ -1295,14 +1247,14 @@ inline TaskAwaitableSuspend c_suspend() {
|
|||||||
return TaskAwaitableSuspend();
|
return TaskAwaitableSuspend();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<template<typename...> typename TCollection, FutureType TFuture, typename... TTemplateArgs>
|
template<template<typename...> typename TCollection, typename TType, typename... TTemplateArgs>
|
||||||
Task<> c_allDone(const TCollection<TFuture, TTemplateArgs...>& futures)
|
Task<> c_allDone(const TCollection<FuturePtr<TType>, TTemplateArgs...>& futures)
|
||||||
{
|
{
|
||||||
bool allDone = true;
|
bool allDone = true;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
allDone = true;
|
allDone = true;
|
||||||
for (const TFuture& future : futures)
|
for (const FuturePtr<TType>& future : futures)
|
||||||
{
|
{
|
||||||
if (future && !future->ready()) {
|
if (future && !future->ready()) {
|
||||||
allDone = false;
|
allDone = false;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#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 <exception>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
@@ -28,113 +27,43 @@ namespace mijin
|
|||||||
//
|
//
|
||||||
// public types
|
// public types
|
||||||
//
|
//
|
||||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, bool exceptions = false>
|
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
|
||||||
namespace impl
|
namespace impl
|
||||||
{
|
{
|
||||||
template<typename TValue, bool exceptions>
|
template<typename TValue>
|
||||||
struct FutureStorage
|
struct FutureStorage
|
||||||
{
|
{
|
||||||
Optional<TValue> value;
|
Optional<TValue> value;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
bool hasValue() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return !value.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
||||||
[[nodiscard]] TValue& getValue() MIJIN_NOEXCEPT { return value.get(); }
|
[[nodiscard]] TValue& getValue() MIJIN_NOEXCEPT { return value.get(); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
// template<typename TValue>
|
||||||
struct FutureStorage<void, false>
|
// struct FutureStorage<TValue&>
|
||||||
{
|
// {
|
||||||
bool isSet = false;
|
// Optional<TValue*> value;
|
||||||
|
//
|
||||||
[[nodiscard]]
|
// void setValue(TValue& value_) MIJIN_NOEXCEPT { value = &value_; }
|
||||||
bool hasValue() const MIJIN_NOEXCEPT
|
// [[nodiscard]] TValue& getValue() const MIJIN_NOEXCEPT { return *value.get(); }
|
||||||
{
|
// };
|
||||||
return isSet;
|
|
||||||
}
|
|
||||||
void setValue() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
isSet = true;
|
|
||||||
}
|
|
||||||
void getValue() MIJIN_NOEXCEPT {}
|
|
||||||
};
|
|
||||||
|
|
||||||
#if MIJIN_ENABLE_EXCEPTIONS
|
|
||||||
template<typename TValue>
|
|
||||||
struct FutureStorage<TValue, true>
|
|
||||||
{
|
|
||||||
Optional<TValue> value;
|
|
||||||
std::exception_ptr exception;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
bool hasValue() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
if (exception) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return !value.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setException(std::exception_ptr exc) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
exception = std::move(exc);
|
|
||||||
}
|
|
||||||
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
|
||||||
[[nodiscard]] TValue& getValue()
|
|
||||||
{
|
|
||||||
if (exception) {
|
|
||||||
std::rethrow_exception(exception);
|
|
||||||
}
|
|
||||||
return value.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct FutureStorage<void, true>
|
struct FutureStorage<void>
|
||||||
{
|
{
|
||||||
bool isSet = false;
|
|
||||||
std::exception_ptr exception;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
bool hasValue() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
if (exception) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return isSet;
|
|
||||||
}
|
|
||||||
void setException(std::exception_ptr exc) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
exception = std::move(exc);
|
|
||||||
}
|
|
||||||
void setValue() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
isSet = true;
|
|
||||||
}
|
|
||||||
void getValue()
|
|
||||||
{
|
|
||||||
if (exception) {
|
|
||||||
std::rethrow_exception(exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
} // namespace impl
|
} // namespace impl
|
||||||
|
|
||||||
template<typename TValue, template<typename> typename TAllocator, bool exceptions>
|
template<typename TValue, template<typename> typename TAllocator>
|
||||||
class Future
|
class Future
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
impl::FutureStorage<TValue, exceptions> value_;
|
[[no_unique_address]] impl::FutureStorage<TValue> value_;
|
||||||
|
bool isSet_ = false;
|
||||||
public:
|
public:
|
||||||
Future() = default;
|
Future() = default;
|
||||||
Future(const Future&) = delete;
|
Future(const Future&) = delete;
|
||||||
@@ -151,12 +80,10 @@ public:
|
|||||||
constexpr bool operator!() const MIJIN_NOEXCEPT { return !ready(); }
|
constexpr bool operator!() const MIJIN_NOEXCEPT { return !ready(); }
|
||||||
public: // access
|
public: // access
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr decltype(auto) get() MIJIN_NOEXCEPT_IF(!exceptions)
|
constexpr decltype(auto) get() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(ready(), "Attempting to get from future that is not ready.");
|
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
|
||||||
if constexpr(std::is_same_v<TValue, void>)
|
if constexpr(std::is_same_v<TValue, void>) {
|
||||||
{
|
|
||||||
value_.getValue(); // in case of exceptions
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -164,12 +91,10 @@ public: // access
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr decltype(auto) get() const MIJIN_NOEXCEPT_IF(!exceptions)
|
constexpr decltype(auto) get() const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(ready(), "Attempting to get from future that is not ready.");
|
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
|
||||||
if constexpr(std::is_same_v<TValue, void>)
|
if constexpr(std::is_same_v<TValue, void>) {
|
||||||
{
|
|
||||||
value_.getValue(); // in case of exceptions
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -179,22 +104,22 @@ public: // access
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr bool ready() const MIJIN_NOEXCEPT
|
constexpr bool ready() const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return value_.hasValue();
|
return isSet_;
|
||||||
}
|
}
|
||||||
public: // modification
|
public: // modification
|
||||||
template<typename TArg> requires (!std::is_same_v<TValue, void>)
|
template<typename TArg> requires (!std::is_same_v<TValue, void>)
|
||||||
constexpr void set(TArg&& value) MIJIN_NOEXCEPT
|
constexpr void set(TArg&& value) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
|
||||||
value_.setValue(std::move(value));
|
value_.setValue(std::move(value));
|
||||||
|
isSet_ = true;
|
||||||
sigSet.emit();
|
sigSet.emit();
|
||||||
}
|
}
|
||||||
constexpr void set() MIJIN_NOEXCEPT requires (std::is_same_v<TValue, void>)
|
constexpr void set() MIJIN_NOEXCEPT requires (std::is_same_v<TValue, void>)
|
||||||
{
|
{
|
||||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
|
||||||
if constexpr (std::is_same_v<TValue, void>)
|
isSet_ = true;
|
||||||
{
|
if constexpr (std::is_same_v<TValue, void>) {
|
||||||
value_.setValue();
|
|
||||||
sigSet.emit();
|
sigSet.emit();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -202,38 +127,12 @@ public: // modification
|
|||||||
MIJIN_ERROR("Attempting to call set(void) on future with value.");
|
MIJIN_ERROR("Attempting to call set(void) on future with value.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
constexpr void setException(std::exception_ptr exception) requires (exceptions)
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
|
||||||
if constexpr (exceptions)
|
|
||||||
{
|
|
||||||
value_.setException(std::move(exception));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public: // signals
|
public: // signals
|
||||||
BaseSignal<TAllocator> sigSet;
|
BaseSignal<TAllocator> sigSet;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, bool exceptions = false>
|
|
||||||
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator, exceptions>>;
|
|
||||||
|
|
||||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||||
using ExceptFuture = Future<TValue, TAllocator, true>;
|
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator>>;
|
||||||
|
|
||||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
|
||||||
using ExceptFuturePtr = std::shared_ptr<Future<TValue, TAllocator, true>>;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct is_future : std::false_type {};
|
|
||||||
|
|
||||||
template<typename TValue, template<typename> typename TAllocator, bool exceptions>
|
|
||||||
struct is_future<Future<TValue, TAllocator, exceptions>> : std::true_type {};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
inline constexpr bool is_future_t = is_future<T>::value;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
concept FutureType = is_future<T>::value;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
|
|||||||
@@ -32,10 +32,6 @@ inline constexpr signal_token_t INVALID_SIGNAL_TOKEN = std::numeric_limits<signa
|
|||||||
//
|
//
|
||||||
|
|
||||||
MIJIN_DEFINE_FLAG(Oneshot);
|
MIJIN_DEFINE_FLAG(Oneshot);
|
||||||
MIJIN_DEFINE_FLAG(DisconnectSignal);
|
|
||||||
|
|
||||||
template<template<typename> typename TAllocator, typename... TArgs>
|
|
||||||
class SignalAutoToken;
|
|
||||||
|
|
||||||
template<template<typename> typename TAllocator, typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
class BaseSignal
|
class BaseSignal
|
||||||
@@ -43,7 +39,6 @@ class BaseSignal
|
|||||||
public:
|
public:
|
||||||
using handler_t = std::function<void(TArgs...)>; // TODO: write a custom function wrapper with allocator support
|
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;
|
||||||
using auto_token_t = SignalAutoToken<TAllocator, TArgs...>;
|
|
||||||
private:
|
private:
|
||||||
struct RegisteredHandler
|
struct RegisteredHandler
|
||||||
{
|
{
|
||||||
@@ -80,71 +75,10 @@ public:
|
|||||||
template<typename... TArgs>
|
template<typename... TArgs>
|
||||||
using Signal = BaseSignal<MIJIN_DEFAULT_ALLOCATOR, TArgs...>;
|
using Signal = BaseSignal<MIJIN_DEFAULT_ALLOCATOR, TArgs...>;
|
||||||
|
|
||||||
template<template<typename> typename TAllocator, typename... TArgs>
|
|
||||||
class SignalAutoToken
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using signal_t = BaseSignal<TAllocator, TArgs...>;
|
|
||||||
private:
|
|
||||||
signal_t* signal_ = nullptr;
|
|
||||||
signal_token_t token_ = INVALID_SIGNAL_TOKEN;
|
|
||||||
public:
|
|
||||||
SignalAutoToken() = default;
|
|
||||||
SignalAutoToken(const SignalAutoToken&) = delete;
|
|
||||||
SignalAutoToken(SignalAutoToken&& other) MIJIN_NOEXCEPT
|
|
||||||
: signal_(std::exchange(other.signal_, nullptr)), token_(std::exchange(other.token_, INVALID_SIGNAL_TOKEN)) {}
|
|
||||||
|
|
||||||
template<typename THandler>
|
|
||||||
SignalAutoToken(signal_t& signal, THandler&& handler, Oneshot oneshot = Oneshot::NO) MIJIN_NOEXCEPT
|
|
||||||
: signal_(&signal), token_(signal.connect(std::forward<THandler>(handler), oneshot))
|
|
||||||
{}
|
|
||||||
|
|
||||||
template<typename TObject>
|
|
||||||
SignalAutoToken(signal_t& signal, TObject& object, void (TObject::* handler)(TArgs...), Oneshot oneshot = Oneshot::NO) MIJIN_NOEXCEPT
|
|
||||||
: signal_(&signal), token_(signal.connect(object, handler, oneshot)) {}
|
|
||||||
|
|
||||||
template<typename TObject>
|
|
||||||
SignalAutoToken(signal_t& signal, TObject& object, void (TObject::* handler)(TArgs...) const, Oneshot oneshot = Oneshot::NO) MIJIN_NOEXCEPT
|
|
||||||
: signal_(&signal), token_(signal.connect(object, handler, oneshot)) {}
|
|
||||||
|
|
||||||
~SignalAutoToken() noexcept
|
|
||||||
{
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
SignalAutoToken& operator=(const SignalAutoToken&) = delete;
|
|
||||||
SignalAutoToken& operator=(SignalAutoToken&& other) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
reset();
|
|
||||||
signal_ = std::exchange(other.signal_, nullptr);
|
|
||||||
token_ = std::exchange(other.token_, INVALID_SIGNAL_TOKEN);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
void reset(DisconnectSignal disconnect = DisconnectSignal::YES) MIJIN_NOEXCEPT;
|
|
||||||
|
|
||||||
friend signal_t;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
template<template<typename> typename TAllocator, typename... TArgs>
|
|
||||||
void SignalAutoToken<TAllocator, TArgs...>::reset(DisconnectSignal disconnect) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
if (signal_ != nullptr && token_ != INVALID_SIGNAL_TOKEN)
|
|
||||||
{
|
|
||||||
if (disconnect) {
|
|
||||||
signal_->disconnect(token_);
|
|
||||||
}
|
|
||||||
signal_ = nullptr;
|
|
||||||
token_ = INVALID_SIGNAL_TOKEN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<template<typename> typename TAllocator, typename... TArgs>
|
template<template<typename> typename TAllocator, typename... TArgs>
|
||||||
template<typename THandler, typename TWeak>
|
template<typename THandler, typename TWeak>
|
||||||
inline auto BaseSignal<TAllocator, 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
|
||||||
|
|||||||
@@ -41,9 +41,6 @@ concept RWMemoryViewable = MemoryViewable<T> && requires(T& object)
|
|||||||
template<typename TConcrete>
|
template<typename TConcrete>
|
||||||
class MixinMemoryView
|
class MixinMemoryView
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
MixinMemoryView() = default;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr bool WRITABLE = requires(TConcrete& object) { { object.data() } -> std::convertible_to<void*>; };
|
static constexpr bool WRITABLE = requires(TConcrete& object) { { object.data() } -> std::convertible_to<void*>; };
|
||||||
|
|
||||||
@@ -85,8 +82,6 @@ private:
|
|||||||
{
|
{
|
||||||
return static_cast<const TConcrete*>(this)->byteSize();
|
return static_cast<const TConcrete*>(this)->byteSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend TConcrete;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class MemoryView : public MixinMemoryView<MemoryView>
|
class MemoryView : public MixinMemoryView<MemoryView>
|
||||||
|
|||||||
@@ -133,8 +133,7 @@ public:
|
|||||||
VectorMap& operator=(VectorMap&&) = default;
|
VectorMap& operator=(VectorMap&&) = default;
|
||||||
auto operator<=>(const VectorMap& other) const noexcept = default;
|
auto operator<=>(const VectorMap& other) const noexcept = default;
|
||||||
|
|
||||||
template<typename TIndex>
|
TValue& operator[](const TKey& key)
|
||||||
TValue& operator[](const TIndex& key)
|
|
||||||
{
|
{
|
||||||
auto it = find(key);
|
auto it = find(key);
|
||||||
if (it != end())
|
if (it != end())
|
||||||
@@ -144,8 +143,7 @@ public:
|
|||||||
return emplace(key, TValue()).first->second;
|
return emplace(key, TValue()).first->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TIndex>
|
const TValue& operator[](const TKey& key) const
|
||||||
const TValue& operator[](const TIndex& key) const
|
|
||||||
{
|
{
|
||||||
return at(key);
|
return at(key);
|
||||||
}
|
}
|
||||||
@@ -253,13 +251,13 @@ public:
|
|||||||
return eraseImpl(idx, count);
|
return eraseImpl(idx, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TSearch>
|
template<std::equality_comparable_with<TKey> T>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
iterator find(const TSearch& key) MIJIN_NOEXCEPT
|
iterator find(const T& keyValue) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
||||||
{
|
{
|
||||||
if (keys_[idx] == key)
|
if (keys_[idx] == keyValue)
|
||||||
{
|
{
|
||||||
return iterator(&keys_[idx], &values_[idx]);
|
return iterator(&keys_[idx], &values_[idx]);
|
||||||
}
|
}
|
||||||
@@ -267,13 +265,13 @@ public:
|
|||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TSearch>
|
template<std::equality_comparable_with<TKey> T>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
const_iterator find(const TSearch& key) const MIJIN_NOEXCEPT
|
const_iterator find(const T& keyValue) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
||||||
{
|
{
|
||||||
if (keys_[idx] == key)
|
if (keys_[idx] == keyValue)
|
||||||
{
|
{
|
||||||
return const_iterator(&keys_[idx], &values_[idx]);
|
return const_iterator(&keys_[idx], &values_[idx]);
|
||||||
}
|
}
|
||||||
@@ -281,10 +279,11 @@ public:
|
|||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<std::equality_comparable_with<TKey> T>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
bool contains(const TKey& key) const MIJIN_NOEXCEPT
|
bool contains(const T& keyValue) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return std::ranges::contains(keys_, key);
|
return std::ranges::contains(keys_, keyValue);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
iterator eraseImpl(std::ptrdiff_t idx, std::ptrdiff_t count = 1) MIJIN_NOEXCEPT
|
iterator eraseImpl(std::ptrdiff_t idx, std::ptrdiff_t count = 1) MIJIN_NOEXCEPT
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
#pragma comment(lib, "kernel32")
|
#pragma comment(lib, "kernel32")
|
||||||
extern "C" __declspec(dllimport) void __stdcall DebugBreak();
|
extern "C" __declspec(dllimport) void __stdcall DebugBreak();
|
||||||
#define MIJIN_TRAP() DebugBreak()
|
#define MIJIN_TRAP() DebugBreak()
|
||||||
#define MIJIN_FUNC() __FUNCSIG__
|
|
||||||
#else // _WIN32
|
#else // _WIN32
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define MIJIN_TRAP() \
|
#define MIJIN_TRAP() \
|
||||||
@@ -34,7 +33,6 @@ extern "C" __declspec(dllimport) void __stdcall DebugBreak();
|
|||||||
: "rcx", "r11", "memory" \
|
: "rcx", "r11", "memory" \
|
||||||
); \
|
); \
|
||||||
}
|
}
|
||||||
#define MIJIN_FUNC() "" // TODO: __PRETTY_FUNCTION__ is not working for some reason -.-
|
|
||||||
#endif // !_WIN32
|
#endif // !_WIN32
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ bool initDbgHelp() MIJIN_NOEXCEPT
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[maybe_unused]] const int result = std::atexit(&cleanupDbgHelp);
|
const int result = std::atexit(&cleanupDbgHelp);
|
||||||
MIJIN_ASSERT(result == 0, "Error registering DbgHelp cleanup handler.");
|
MIJIN_ASSERT(result == 0, "Error registering DbgHelp cleanup handler.");
|
||||||
|
|
||||||
// only copy in the end so we can still figure out if initialization was successful
|
// only copy in the end so we can still figure out if initialization was successful
|
||||||
|
|||||||
@@ -61,26 +61,6 @@ namespace mijin
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(MIJIN_RTTI)
|
|
||||||
#if MIJIN_COMPILER == MIJIN_COMPILER_GCC
|
|
||||||
#if defined(__GXX_RTTI)
|
|
||||||
#define MIJIN_RTTI 1
|
|
||||||
#else
|
|
||||||
#define MIJIN_RTTI 0
|
|
||||||
#endif
|
|
||||||
#elif MIJIN_COMPILER == MIJIN_COMPILER_CLANG
|
|
||||||
#define MIJIN_RTTI (__has_feature(cxx_rtti))
|
|
||||||
#elif MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
|
||||||
#if defined(_CPPRTTI)
|
|
||||||
#define MIJIN_RTTI 1
|
|
||||||
#else
|
|
||||||
#define MIJIN_RTTI 0
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define MIJIN_RTTI 0
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public constants
|
// public constants
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -290,29 +290,6 @@ mijin::Task<StreamError> Stream::c_readLine(std::string& outString)
|
|||||||
co_return StreamError::SUCCESS;
|
co_return StreamError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamError Stream::copyTo(Stream& other)
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(getFeatures().read, "Stream must support reading.");
|
|
||||||
MIJIN_ASSERT(other.getFeatures().write, "Other stream must support writing.");
|
|
||||||
|
|
||||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
|
||||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
|
||||||
|
|
||||||
while (!isAtEnd())
|
|
||||||
{
|
|
||||||
std::size_t bytesRead = 0;
|
|
||||||
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
if (const StreamError error = other.writeRaw(chunk.data(), bytesRead); error != StreamError::SUCCESS)
|
|
||||||
{
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StreamError::SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileStream::~FileStream()
|
FileStream::~FileStream()
|
||||||
{
|
{
|
||||||
if (handle) {
|
if (handle) {
|
||||||
@@ -464,7 +441,7 @@ StreamFeatures FileStream::getFeatures()
|
|||||||
if (handle)
|
if (handle)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
.read = (mode == FileOpenMode::READ || mode == FileOpenMode::READ_WRITE),
|
.read = (mode == FileOpenMode::READ),
|
||||||
.write = (mode == FileOpenMode::WRITE || mode == FileOpenMode::APPEND || mode == FileOpenMode::READ_WRITE),
|
.write = (mode == FileOpenMode::WRITE || mode == FileOpenMode::APPEND || mode == FileOpenMode::READ_WRITE),
|
||||||
.tell = true,
|
.tell = true,
|
||||||
.seek = true,
|
.seek = true,
|
||||||
|
|||||||
@@ -275,8 +275,6 @@ public:
|
|||||||
|
|
||||||
StreamError getTotalLength(std::size_t& outLength);
|
StreamError getTotalLength(std::size_t& outLength);
|
||||||
|
|
||||||
StreamError copyTo(Stream& otherStream);
|
|
||||||
|
|
||||||
template<template<typename> typename TAllocator>
|
template<template<typename> typename TAllocator>
|
||||||
StreamError readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
StreamError readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||||
|
|
||||||
@@ -546,7 +544,7 @@ inline void throwOnError(mijin::StreamError error)
|
|||||||
if (error == mijin::StreamError::SUCCESS) {
|
if (error == mijin::StreamError::SUCCESS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw Exception(errorName(error));
|
throw std::runtime_error(errorName(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void throwOnError(mijin::StreamError error, std::string message)
|
inline void throwOnError(mijin::StreamError error, std::string message)
|
||||||
@@ -554,7 +552,7 @@ inline void throwOnError(mijin::StreamError error, std::string message)
|
|||||||
if (error == mijin::StreamError::SUCCESS) {
|
if (error == mijin::StreamError::SUCCESS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw Exception(message + ": " + errorName(error));
|
throw std::runtime_error(message + ": " + errorName(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TSuccess>
|
template<typename TSuccess>
|
||||||
|
|||||||
@@ -1,376 +0,0 @@
|
|||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#if !defined(MIJIN_LOGGING_BUFFER_SINK_HPP_INCLUDED)
|
|
||||||
#define MIJIN_LOGGING_BUFFER_SINK_HPP_INCLUDED 1
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <cstring>
|
|
||||||
#include <mutex>
|
|
||||||
#include <span>
|
|
||||||
#include "./logger.hpp"
|
|
||||||
|
|
||||||
namespace mijin
|
|
||||||
{
|
|
||||||
inline constexpr std::size_t BUFFER_SINK_DEFAULT_SIZE = 10 * 1024 * 1024; // default 10 MiB buffer
|
|
||||||
|
|
||||||
template<std::size_t VBufferSize = BUFFER_SINK_DEFAULT_SIZE, typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>>
|
|
||||||
class BaseBufferSink;
|
|
||||||
|
|
||||||
namespace impl
|
|
||||||
{
|
|
||||||
static constexpr std::uint32_t INVALID_BUFFER_INDEX = std::numeric_limits<std::uint32_t>::max();
|
|
||||||
|
|
||||||
template<typename TChar>
|
|
||||||
struct BufferedMessageHeader
|
|
||||||
{
|
|
||||||
const BaseLogChannel<TChar>* channel;
|
|
||||||
const BaseLogLevel<TChar>* level;
|
|
||||||
std::source_location sourceLocation;
|
|
||||||
std::uint32_t nextIdx = INVALID_BUFFER_INDEX;
|
|
||||||
std::uint32_t prevIdx = INVALID_BUFFER_INDEX;
|
|
||||||
std::uint32_t numChars = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, std::size_t VBufferSize>
|
|
||||||
struct MessageBuffer
|
|
||||||
{
|
|
||||||
using char_t = TChar;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
using message_t = BaseLogMessage<char_t, traits_t>;
|
|
||||||
using header_t = BufferedMessageHeader<TChar>;
|
|
||||||
static constexpr std::size_t BUFFER_SIZE = VBufferSize;
|
|
||||||
|
|
||||||
std::array<std::uint8_t, BUFFER_SIZE> bytes;
|
|
||||||
mutable std::mutex mutex;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
header_t& messageAt(std::uint32_t idx) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return *reinterpret_cast<header_t*>(bytes.data() + idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
std::span<char_t> messageText(header_t& header) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return {reinterpret_cast<char_t*>(reinterpret_cast<std::uint8_t*>(&header)) + sizeof(header_t), header.numChars};
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
const header_t& messageAt(std::uint32_t idx) const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return *reinterpret_cast<const header_t*>(bytes.data() + idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
std::span<const char_t> messageText(const header_t& header) const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return {reinterpret_cast<const char_t*>(reinterpret_cast<const std::uint8_t*>(&header)) + sizeof(header_t), header.numChars};
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::uint32_t messageBytes(std::uint32_t numChars) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return static_cast<std::uint32_t>(sizeof(header_t)) + (numChars * static_cast<std::uint32_t>(sizeof(char_t)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, std::size_t VBufferSize>
|
|
||||||
class BufferSinkRange;
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, std::size_t VBufferSize>
|
|
||||||
class LockedMessageBuffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
|
|
||||||
static constexpr std::size_t BUFFER_SIZE = VBufferSize;
|
|
||||||
|
|
||||||
using range_t = impl::BufferSinkRange<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
private:
|
|
||||||
using buffer_t = MessageBuffer<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
|
|
||||||
const buffer_t* buffer_ = nullptr;
|
|
||||||
std::uint32_t firstIdx_ = INVALID_BUFFER_INDEX;
|
|
||||||
std::uint32_t lastIdx_ = INVALID_BUFFER_INDEX;
|
|
||||||
|
|
||||||
LockedMessageBuffer(const buffer_t& buffer, std::uint32_t firstIdx, std::uint32_t lastIdx) MIJIN_NOEXCEPT
|
|
||||||
: buffer_(&buffer), firstIdx_(firstIdx), lastIdx_(lastIdx) {
|
|
||||||
buffer_->mutex.lock();
|
|
||||||
}
|
|
||||||
public:
|
|
||||||
LockedMessageBuffer() noexcept = default;
|
|
||||||
LockedMessageBuffer(const LockedMessageBuffer&) = delete;
|
|
||||||
LockedMessageBuffer(LockedMessageBuffer&& other) noexcept
|
|
||||||
: buffer_(std::exchange(other.buffer_, nullptr)), firstIdx_(other.firstIdx_), lastIdx_(other.lastIdx_) {}
|
|
||||||
|
|
||||||
~LockedMessageBuffer() noexcept
|
|
||||||
{
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
LockedMessageBuffer& operator=(const LockedMessageBuffer&) = delete;
|
|
||||||
LockedMessageBuffer& operator=(LockedMessageBuffer&& other) noexcept
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
reset();
|
|
||||||
buffer_ = std::exchange(other.buffer_, nullptr);
|
|
||||||
firstIdx_ = other.firstIdx_;
|
|
||||||
lastIdx_ = other.lastIdx_;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
range_t getMessages() const MIJIN_NOEXCEPT;
|
|
||||||
private:
|
|
||||||
void reset()
|
|
||||||
{
|
|
||||||
if (buffer_ != nullptr)
|
|
||||||
{
|
|
||||||
buffer_->mutex.unlock();
|
|
||||||
buffer_ = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<std::size_t, typename, typename>
|
|
||||||
friend class mijin::BaseBufferSink;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, std::size_t VBufferSize>
|
|
||||||
class BufferSinkIterator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
using message_t = BaseLogMessage<char_t, traits_t>;
|
|
||||||
static constexpr std::size_t BUFFER_SIZE = VBufferSize;
|
|
||||||
|
|
||||||
using difference_type = std::ptrdiff_t;
|
|
||||||
using value_type = const message_t;
|
|
||||||
using pointer = void;
|
|
||||||
using reference = value_type;
|
|
||||||
using iterator_category = std::bidirectional_iterator_tag;
|
|
||||||
private:
|
|
||||||
using buffer_t = MessageBuffer<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
using header_t = BufferedMessageHeader<char_t>;
|
|
||||||
using string_view_t = message_t::string_view_t;
|
|
||||||
|
|
||||||
const buffer_t* buffer_ = nullptr;
|
|
||||||
std::uint32_t idx_ = INVALID_BUFFER_INDEX;
|
|
||||||
std::uint32_t prevIdx_ = INVALID_BUFFER_INDEX;
|
|
||||||
|
|
||||||
BufferSinkIterator(const buffer_t& buffer, std::uint32_t idx, std::uint32_t prevIdx) MIJIN_NOEXCEPT : buffer_(&buffer), idx_(idx), prevIdx_(prevIdx) {}
|
|
||||||
public:
|
|
||||||
BufferSinkIterator() MIJIN_NOEXCEPT = default;
|
|
||||||
BufferSinkIterator(const BufferSinkIterator&) MIJIN_NOEXCEPT = default;
|
|
||||||
|
|
||||||
BufferSinkIterator& operator=(const BufferSinkIterator&) MIJIN_NOEXCEPT = default;
|
|
||||||
|
|
||||||
auto operator<=>(const BufferSinkIterator&) const noexcept = default;
|
|
||||||
|
|
||||||
reference operator*() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(idx_ != INVALID_BUFFER_INDEX, "Attempting to dereference an invalid iterator.");
|
|
||||||
const header_t& header = buffer_->messageAt(idx_);
|
|
||||||
const string_view_t text(buffer_->messageText(header));
|
|
||||||
return {
|
|
||||||
.text = text,
|
|
||||||
.channel = header.channel,
|
|
||||||
.level = header.level,
|
|
||||||
.sourceLocation = header.sourceLocation
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferSinkIterator& operator++() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(idx_ != INVALID_BUFFER_INDEX, "Attempting to increment an invalid iterator.");
|
|
||||||
prevIdx_ = idx_;
|
|
||||||
idx_ = buffer_->messageAt(idx_).nextIdx;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferSinkIterator operator++(int) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
BufferSinkIterator copy(*this);
|
|
||||||
operator++();
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferSinkIterator& operator--() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(prevIdx_ != INVALID_BUFFER_INDEX, "Attempting to decrement an invalid iterator.");
|
|
||||||
idx_ = prevIdx_;
|
|
||||||
prevIdx_ = buffer_->messageAt(idx_).prevIdx;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferSinkIterator operator--(int) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
BufferSinkIterator copy(*this);
|
|
||||||
operator--();
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename, typename, std::size_t>
|
|
||||||
friend class BufferSinkRange;
|
|
||||||
};
|
|
||||||
static_assert(std::forward_iterator<BufferSinkIterator<char, std::char_traits<char>, BUFFER_SINK_DEFAULT_SIZE>>);
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, std::size_t VBufferSize>
|
|
||||||
class BufferSinkRange
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
static constexpr std::size_t BUFFER_SIZE = VBufferSize;
|
|
||||||
|
|
||||||
using iterator = BufferSinkIterator<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
using const_iterator = iterator;
|
|
||||||
private:
|
|
||||||
using buffer_t = MessageBuffer<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
|
|
||||||
const buffer_t* buffer_ = nullptr;
|
|
||||||
std::uint32_t firstIdx_ = INVALID_BUFFER_INDEX;
|
|
||||||
std::uint32_t lastIdx_ = INVALID_BUFFER_INDEX;
|
|
||||||
|
|
||||||
BufferSinkRange(const buffer_t& buffer, std::uint32_t firstIdx, std::uint32_t lastIdx) MIJIN_NOEXCEPT
|
|
||||||
: buffer_(&buffer), firstIdx_(firstIdx), lastIdx_(lastIdx) {
|
|
||||||
}
|
|
||||||
public:
|
|
||||||
BufferSinkRange() noexcept = default;
|
|
||||||
BufferSinkRange(const BufferSinkRange&) MIJIN_NOEXCEPT = default;
|
|
||||||
BufferSinkRange& operator=(const BufferSinkRange&) MIJIN_NOEXCEPT = default;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
iterator begin() const MIJIN_NOEXCEPT { return {*buffer_, firstIdx_, INVALID_BUFFER_INDEX}; }
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
iterator end() const MIJIN_NOEXCEPT { return {*buffer_, INVALID_BUFFER_INDEX, lastIdx_}; }
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
const_iterator cbegin() const MIJIN_NOEXCEPT { return {*buffer_, firstIdx_, INVALID_BUFFER_INDEX}; }
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
const_iterator cend() const MIJIN_NOEXCEPT { return {*buffer_, INVALID_BUFFER_INDEX, lastIdx_}; }
|
|
||||||
|
|
||||||
friend class LockedMessageBuffer<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
};
|
|
||||||
|
|
||||||
static_assert(std::bidirectional_iterator<BufferSinkIterator<char, std::char_traits<char>, 100>>);
|
|
||||||
static_assert(std::ranges::bidirectional_range<BufferSinkRange<char, std::char_traits<char>, 100>>);
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, std::size_t VBufferSize>
|
|
||||||
auto LockedMessageBuffer<TChar, TTraits, VBufferSize>::getMessages() const MIJIN_NOEXCEPT -> range_t
|
|
||||||
{
|
|
||||||
return range_t(*buffer_, firstIdx_, lastIdx_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
using impl::LockedMessageBuffer;
|
|
||||||
|
|
||||||
template<std::size_t VBufferSize, typename TChar, typename TTraits>
|
|
||||||
class BaseBufferSink : public BaseLogSink<TChar, TTraits>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using base_t = BaseLogSink<TChar>;
|
|
||||||
using typename base_t::char_t;
|
|
||||||
using typename base_t::traits_t;
|
|
||||||
using typename base_t::message_t;
|
|
||||||
|
|
||||||
static constexpr std::size_t BUFFER_SIZE = VBufferSize;
|
|
||||||
|
|
||||||
using locked_buffer_t = LockedMessageBuffer<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
private:
|
|
||||||
using buffer_t = impl::MessageBuffer<char_t, traits_t, BUFFER_SIZE>;
|
|
||||||
using header_t = impl::BufferedMessageHeader<char_t>;
|
|
||||||
|
|
||||||
buffer_t buffer_;
|
|
||||||
std::uint32_t firstIdx_ = impl::INVALID_BUFFER_INDEX;
|
|
||||||
std::uint32_t lastIdx_ = impl::INVALID_BUFFER_INDEX;
|
|
||||||
public:
|
|
||||||
void handleMessage(const message_t& message) MIJIN_NOEXCEPT override
|
|
||||||
{
|
|
||||||
const std::uint32_t numChars = static_cast<std::uint32_t>(message.text.size());
|
|
||||||
const std::uint32_t totalBytes = buffer_t::messageBytes(numChars);
|
|
||||||
|
|
||||||
std::scoped_lock _(buffer_.mutex); // TODO: use a message queue and try_lock here
|
|
||||||
if (lastIdx_ == impl::INVALID_BUFFER_INDEX)
|
|
||||||
{
|
|
||||||
// no message yet
|
|
||||||
insertMessageAt(0, message);
|
|
||||||
firstIdx_ = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
header_t& lastHeader = buffer_.messageAt(lastIdx_);
|
|
||||||
const std::uint32_t newIdx = lastIdx_ + buffer_t::messageBytes(lastHeader.numChars);
|
|
||||||
if (newIdx + totalBytes < BUFFER_SIZE)
|
|
||||||
{
|
|
||||||
// enough space in the buffer, can append
|
|
||||||
insertMessageAt(newIdx, message);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// not enough space, put at front
|
|
||||||
insertMessageAt(0, message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
locked_buffer_t lockBuffer() const MIJIN_NOEXCEPT { return locked_buffer_t(buffer_, firstIdx_, lastIdx_); }
|
|
||||||
private:
|
|
||||||
void insertMessageAt(std::uint32_t idx, const message_t& message)
|
|
||||||
{
|
|
||||||
const std::uint32_t numChars = static_cast<std::uint32_t>(message.text.size());
|
|
||||||
freeSpace(idx, idx + buffer_t::messageBytes(numChars));
|
|
||||||
|
|
||||||
if (lastIdx_ != impl::INVALID_BUFFER_INDEX) {
|
|
||||||
buffer_.messageAt(lastIdx_).nextIdx = idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
header_t& newHeader = buffer_.messageAt(idx);
|
|
||||||
::new (&newHeader) header_t({
|
|
||||||
.channel = message.channel,
|
|
||||||
.level = message.level,
|
|
||||||
.sourceLocation = message.sourceLocation,
|
|
||||||
.nextIdx = impl::INVALID_BUFFER_INDEX,
|
|
||||||
.prevIdx = lastIdx_,
|
|
||||||
.numChars = numChars
|
|
||||||
});
|
|
||||||
lastIdx_ = idx;
|
|
||||||
std::ranges::copy(message.text, buffer_.messageText(newHeader).begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
void freeSpace(std::uint32_t startIdx, std::uint32_t endIdx)
|
|
||||||
{
|
|
||||||
while (firstIdx_ != impl::INVALID_BUFFER_INDEX && firstIdx_ >= startIdx && firstIdx_ < endIdx)
|
|
||||||
{
|
|
||||||
header_t& message = buffer_.messageAt(firstIdx_);
|
|
||||||
firstIdx_ = message.nextIdx;
|
|
||||||
message.~header_t();
|
|
||||||
}
|
|
||||||
|
|
||||||
// cleared everything?
|
|
||||||
if (firstIdx_ == impl::INVALID_BUFFER_INDEX) {
|
|
||||||
lastIdx_ = impl::INVALID_BUFFER_INDEX;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
buffer_.messageAt(firstIdx_).prevIdx = impl::INVALID_BUFFER_INDEX;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#define SINK_COMMON_ARGS(chr_type) std::size_t BUFFER_SIZE
|
|
||||||
#define SINK_SET_ARGS(chr_type) BUFFER_SIZE, chr_type
|
|
||||||
|
|
||||||
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(BufferSink, SINK_COMMON_ARGS, SINK_SET_ARGS)
|
|
||||||
|
|
||||||
#undef SINK_COMMON_ARGS
|
|
||||||
#undef SINK_SET_ARGS
|
|
||||||
} // namespace mijin
|
|
||||||
|
|
||||||
|
|
||||||
#endif // !defined(MIJIN_LOGGING_STDIO_SINK_HPP_INCLUDED)
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#if !defined(MIJIN_LOGGING_DEBUG_OUTPUT_SINK_HPP_INCLUDED)
|
|
||||||
#define MIJIN_LOGGING_DEBUG_OUTPUT_SINK_HPP_INCLUDED 1
|
|
||||||
|
|
||||||
#include "./formatting.hpp"
|
|
||||||
#include "../detect.hpp"
|
|
||||||
#include "../util/traits.hpp"
|
|
||||||
|
|
||||||
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
|
||||||
#pragma comment(lib, "Kernel32.lib")
|
|
||||||
|
|
||||||
extern "C" void OutputDebugStringA(const char* lpOutputString);
|
|
||||||
extern "C" void OutputDebugStringW(const wchar_t* lpOutputString);
|
|
||||||
|
|
||||||
namespace mijin
|
|
||||||
{
|
|
||||||
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
|
||||||
requires(allocator_type<TAllocator<TChar>>)
|
|
||||||
class BaseDebugOutputSink : 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;
|
|
||||||
using typename base_t::string_t;
|
|
||||||
public:
|
|
||||||
explicit BaseDebugOutputSink(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)) {}
|
|
||||||
|
|
||||||
void handleMessageFormatted(const message_t&, const string_t& formatted) MIJIN_NOEXCEPT override
|
|
||||||
{
|
|
||||||
if constexpr (std::is_same_v<char_t, char>)
|
|
||||||
{
|
|
||||||
OutputDebugStringA(formatted.c_str());
|
|
||||||
OutputDebugStringA("\n");
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_same_v<char_t, wchar_t>)
|
|
||||||
{
|
|
||||||
OutputDebugStringW(formatted.c_str());
|
|
||||||
OutputDebugStringW(L"\n");
|
|
||||||
}
|
|
||||||
else if constexpr (sizeof(char_t) == sizeof(char))
|
|
||||||
{
|
|
||||||
// char8_t etc.
|
|
||||||
OutputDebugStringA(std::bit_cast<const char*>(formatted.c_str()));
|
|
||||||
OutputDebugStringA("\n");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
static_assert(always_false_v<char_t>, "Character type not supported.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#define SINK_SET_ARGS(chr_type) chr_type, std::char_traits<chr_type>, TAllocator, TDeleter
|
|
||||||
|
|
||||||
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(DebugOutputSink, MIJIN_FORMATTING_SINK_COMMON_ARGS, SINK_SET_ARGS)
|
|
||||||
|
|
||||||
#undef SINK_SET_ARGS
|
|
||||||
} // namespace mijin
|
|
||||||
|
|
||||||
#endif // MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
|
||||||
#endif // !defined(MIJIN_LOGGING_DEBUG_OUTPUT_SINK_HPP_INCLUDED)
|
|
||||||
@@ -8,11 +8,11 @@
|
|||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>>
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
class BaseLevelFilter : public BaseLogFilter<TChar, TTraits>
|
class BaseLevelFilter : public BaseLogFilter<TChar>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using base_t = BaseLogFilter<TChar, TTraits>;
|
using base_t = BaseLogFilter<TChar>;
|
||||||
using typename base_t::char_t;
|
using typename base_t::char_t;
|
||||||
using typename base_t::message_t;
|
using typename base_t::message_t;
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -64,15 +64,6 @@ MIJIN_DEFINE_CHAR_VERSIONS_TMPL(SimpleLogFormatter, FORMATTER_COMMON_ARGS, FORMA
|
|||||||
#undef FORMATTER_COMMON_ARGS
|
#undef FORMATTER_COMMON_ARGS
|
||||||
#undef FORMATTER_SET_ARGS
|
#undef FORMATTER_SET_ARGS
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, typename TAllocator, typename TFormatter = BaseSimpleLogFormatter<TChar, TTraits, TAllocator>>
|
|
||||||
DynamicPointer<TFormatter> makeLogFormatter(typename TFormatter::string_t&& format)
|
|
||||||
{
|
|
||||||
return makeDynamic<TFormatter>(std::move(format));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TChar, typename TTraits, typename TAllocator, typename TFormatter = BaseSimpleLogFormatter<TChar, TTraits, TAllocator>>
|
|
||||||
inline TFormatter DEFAULT_FORMATTER("[{level}] {text}");
|
|
||||||
|
|
||||||
#define MIJIN_FORMATTING_SINK_COMMON_ARGS(chr_type) \
|
#define MIJIN_FORMATTING_SINK_COMMON_ARGS(chr_type) \
|
||||||
typename TTraits = std::char_traits<chr_type>, \
|
typename TTraits = std::char_traits<chr_type>, \
|
||||||
template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, \
|
template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, \
|
||||||
@@ -87,36 +78,35 @@ inline TFormatter DEFAULT_FORMATTER("[{level}] {text}");
|
|||||||
|
|
||||||
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
||||||
requires(allocator_type<TAllocator<TChar>>)
|
requires(allocator_type<TAllocator<TChar>>)
|
||||||
class BaseFormattingLogSink : public BaseLogSink<TChar, TTraits>
|
class BaseFormattingLogSink : public BaseLogSink<TChar>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using base_t = BaseLogSink<TChar, TTraits>;
|
using base_t = BaseLogSink<TChar>;
|
||||||
|
|
||||||
using typename base_t::char_t;
|
using char_t = TChar;
|
||||||
using typename base_t::message_t;
|
using traits_t = TTraits;
|
||||||
using typename base_t::traits_t;
|
|
||||||
using allocator_t = TAllocator<TChar>;
|
using allocator_t = TAllocator<TChar>;
|
||||||
using formatter_t = BaseLogFormatter<char_t, traits_t, allocator_t>;
|
using formatter_t = BaseLogFormatter<char_t, traits_t, allocator_t>;
|
||||||
using formatter_deleter_t = TDeleter;
|
using formatter_deleter_t = TDeleter;
|
||||||
using formatter_ptr_t = DynamicPointer<formatter_t, formatter_deleter_t>;
|
using formatter_ptr_t = DynamicPointer<formatter_t, formatter_deleter_t>;
|
||||||
using string_t = formatter_t::string_t;
|
using string_t = formatter_t::string_t;
|
||||||
|
using typename base_t::message_t;
|
||||||
private:
|
private:
|
||||||
formatter_ptr_t mFormatter;
|
not_null_t<formatter_ptr_t> mFormatter;
|
||||||
string_t mBuffer;
|
string_t mBuffer;
|
||||||
public:
|
public:
|
||||||
explicit BaseFormattingLogSink(formatter_ptr_t formatter = wrapDynamic(&DEFAULT_FORMATTER<char_t, traits_t, allocator_t>), allocator_t allocator = {})
|
explicit BaseFormattingLogSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||||
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||||
: mFormatter(std::move(formatter)), mBuffer(std::move(allocator))
|
: mFormatter(std::move(formatter)), mBuffer(std::move(allocator))
|
||||||
{
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
virtual void handleMessageFormatted(const message_t& message, const string_t& formatted) MIJIN_NOEXCEPT = 0;
|
virtual void handleMessageFormatted(const message_t& message, const char_t* formatted) MIJIN_NOEXCEPT = 0;
|
||||||
|
|
||||||
void handleMessage(const message_t& message) noexcept override
|
void handleMessage(const message_t& message) noexcept override
|
||||||
{
|
{
|
||||||
mBuffer.clear();
|
mBuffer.clear();
|
||||||
mFormatter->format(message, mBuffer);
|
mFormatter->format(message, mBuffer);
|
||||||
handleMessageFormatted(message, mBuffer);
|
handleMessageFormatted(message, mBuffer.c_str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -172,7 +162,7 @@ void BaseSimpleLogFormatter<TChar, TTraits, TAllocator>::format(const LogMessage
|
|||||||
{
|
{
|
||||||
std::format_to(std::back_inserter(outFormatted), MIJIN_SMART_QUOTE(char_t, "{}"), value);
|
std::format_to(std::back_inserter(outFormatted), MIJIN_SMART_QUOTE(char_t, "{}"), value);
|
||||||
}
|
}
|
||||||
else if constexpr (is_string_v<type_t> || is_string_view_v<type_t> || is_cstring_v<type_t>)
|
else if constexpr (is_string_v<type_t> || is_cstring_v<type_t>)
|
||||||
{
|
{
|
||||||
convertStringType(value, outFormatted);
|
convertStringType(value, outFormatted);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,10 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <source_location>
|
#include <source_location>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "../internal/common.hpp"
|
#include "../internal/common.hpp"
|
||||||
#include "../util/annot.hpp"
|
#include "../util/annot.hpp"
|
||||||
|
#include "../util/iterators.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@@ -79,14 +79,12 @@ struct BaseLogChannel
|
|||||||
|
|
||||||
MIJIN_DEFINE_CHAR_VERSIONS(LogChannel)
|
MIJIN_DEFINE_CHAR_VERSIONS(LogChannel)
|
||||||
|
|
||||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>>
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
struct BaseLogMessage
|
struct BaseLogMessage
|
||||||
{
|
{
|
||||||
using char_t = TChar;
|
using char_t = TChar;
|
||||||
using traits_t = TTraits;
|
|
||||||
using string_view_t = std::basic_string_view<char_t, traits_t>;
|
|
||||||
|
|
||||||
string_view_t text;
|
const char_t* text;
|
||||||
const BaseLogChannel<char_t>* channel;
|
const BaseLogChannel<char_t>* channel;
|
||||||
const BaseLogLevel<char_t>* level;
|
const BaseLogLevel<char_t>* level;
|
||||||
std::source_location sourceLocation;
|
std::source_location sourceLocation;
|
||||||
@@ -94,13 +92,12 @@ struct BaseLogMessage
|
|||||||
|
|
||||||
MIJIN_DEFINE_CHAR_VERSIONS(LogMessage)
|
MIJIN_DEFINE_CHAR_VERSIONS(LogMessage)
|
||||||
|
|
||||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>>
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
class BaseLogSink
|
class BaseLogSink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using char_t = TChar;
|
using char_t = TChar;
|
||||||
using traits_t = TTraits;
|
using message_t = BaseLogMessage<char_t>;
|
||||||
using message_t = BaseLogMessage<char_t, traits_t>;
|
|
||||||
|
|
||||||
virtual ~BaseLogSink() noexcept = default;
|
virtual ~BaseLogSink() noexcept = default;
|
||||||
|
|
||||||
@@ -109,13 +106,12 @@ public:
|
|||||||
|
|
||||||
MIJIN_DEFINE_CHAR_VERSIONS(LogSink)
|
MIJIN_DEFINE_CHAR_VERSIONS(LogSink)
|
||||||
|
|
||||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>>
|
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||||
class BaseLogFilter
|
class BaseLogFilter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using char_t = TChar;
|
using char_t = TChar;
|
||||||
using traits_t = TTraits;
|
using message_t = BaseLogMessage<char_t>;
|
||||||
using message_t = BaseLogMessage<char_t, traits_t>;
|
|
||||||
|
|
||||||
virtual ~BaseLogFilter() noexcept = default;
|
virtual ~BaseLogFilter() noexcept = default;
|
||||||
|
|
||||||
@@ -133,13 +129,12 @@ public:
|
|||||||
using traits_t = TTraits;
|
using traits_t = TTraits;
|
||||||
using allocator_t = TAllocator<char_t>;
|
using allocator_t = TAllocator<char_t>;
|
||||||
|
|
||||||
using sink_t = BaseLogSink<char_t, traits_t>;
|
using sink_t = BaseLogSink<char_t>;
|
||||||
using filter_t = BaseLogFilter<char_t, traits_t>;
|
using filter_t = BaseLogFilter<char_t>;
|
||||||
using level_t = BaseLogLevel<char_t>;
|
using level_t = BaseLogLevel<char_t>;
|
||||||
using channel_t = BaseLogChannel<char_t>;
|
using channel_t = BaseLogChannel<char_t>;
|
||||||
using message_t = BaseLogMessage<char_t, traits_t>;
|
using message_t = BaseLogMessage<char_t>;
|
||||||
using string_t = std::basic_string<char_t, traits_t, allocator_t>;
|
using string_t = std::basic_string<char_t, traits_t, allocator_t>;
|
||||||
using string_view_t = std::basic_string_view<char_t, traits_t>;
|
|
||||||
private:
|
private:
|
||||||
struct SinkEntry
|
struct SinkEntry
|
||||||
{
|
{
|
||||||
@@ -152,9 +147,9 @@ public:
|
|||||||
explicit BaseLogger(TAllocator<void> allocator = {}) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TAllocator<SinkEntry>, TAllocator<void>&&>))
|
explicit BaseLogger(TAllocator<void> allocator = {}) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TAllocator<SinkEntry>, TAllocator<void>&&>))
|
||||||
: mSinks(TAllocator<SinkEntry>(std::move(allocator)))
|
: mSinks(TAllocator<SinkEntry>(std::move(allocator)))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
BaseLogger(const BaseLogger&) = default;
|
BaseLogger(const BaseLogger&) = default;
|
||||||
BaseLogger(BaseLogger&&) = default;
|
BaseLogger(BaseLogger&&) = default;
|
||||||
|
|
||||||
BaseLogger& operator=(const BaseLogger&) = default;
|
BaseLogger& operator=(const BaseLogger&) = default;
|
||||||
BaseLogger& operator=(BaseLogger&&) = default;
|
BaseLogger& operator=(BaseLogger&&) = default;
|
||||||
|
|
||||||
@@ -170,19 +165,6 @@ public:
|
|||||||
mSinks.push_back({&sink, &filter});
|
mSinks.push_back({&sink, &filter});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool removeSink(sink_t& sink)
|
|
||||||
{
|
|
||||||
std::unique_lock _(mMutex);
|
|
||||||
auto it = std::ranges::find_if(mSinks, [&](const SinkEntry& entry) {
|
|
||||||
return entry.sink == &sink;
|
|
||||||
});
|
|
||||||
if (it == mSinks.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
mSinks.erase(it);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void postMessage(const message_t& message) const MIJIN_NOEXCEPT
|
void postMessage(const message_t& message) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::unique_lock _(mMutex);
|
std::unique_lock _(mMutex);
|
||||||
@@ -195,7 +177,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void logUnformatted(const level_t& level, const channel_t& channel, std::source_location sourceLocation, string_view_t msg) const MIJIN_NOEXCEPT
|
void log(const level_t& level, const channel_t& channel, std::source_location sourceLocation, const char_t* msg) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
postMessage({
|
postMessage({
|
||||||
.text = msg,
|
.text = msg,
|
||||||
@@ -205,19 +187,30 @@ public:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(const level_t& level, const channel_t& channel, std::source_location sourceLocation, std::basic_format_string<char_t> msg) const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
logUnformatted(level, channel, sourceLocation, msg.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<typename... TArgs>
|
||||||
void log(const level_t& level, const channel_t& channel, std::source_location sourceLocation,
|
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
|
std::basic_format_string<char_t, std::type_identity_t<TArgs>...> fmt, TArgs&& ... args) const MIJIN_NOEXCEPT
|
||||||
MIJIN_NOEXCEPT_IF(noexcept(std::declval<allocator_t>().allocate(1)))
|
|
||||||
{
|
{
|
||||||
string_t buffer(allocator_t(mSinks.get_allocator()));
|
// TODO: make the logger use a traits struct to make this adjustable
|
||||||
std::format_to(std::back_inserter(buffer), fmt, std::forward<TArgs>(args)...);
|
static constexpr std::size_t BUFFER_SIZE = 256;
|
||||||
logUnformatted(level, channel, std::move(sourceLocation), buffer);
|
std::array<char_t, BUFFER_SIZE> buffer;
|
||||||
|
|
||||||
|
// first try to write into a buffer on the stack
|
||||||
|
FixedArrayOutputIterator itAfter = std::format_to(FixedArrayOutputIterator(buffer), fmt, std::forward<TArgs>(args)...);
|
||||||
|
*itAfter = '\0';
|
||||||
|
++itAfter;
|
||||||
|
if (!itAfter.didOverflow())
|
||||||
|
{
|
||||||
|
log(level, channel, std::move(sourceLocation), buffer.data());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if that didn't work, allocate more space
|
||||||
|
const std::size_t newBufferSize = itAfter.getCounter();
|
||||||
|
char_t* newBuffer = static_cast<char_t*>(alloca(newBufferSize * sizeof(char_t)));
|
||||||
|
const std::format_to_n_result result = std::format_to_n(newBuffer, newBufferSize - 1, fmt, std::forward<TArgs>(args)...);
|
||||||
|
*result.out = '\0';
|
||||||
|
log(level, channel, std::move(sourceLocation), newBuffer);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -275,25 +268,10 @@ MIJIN_DEFINE_LOG_LEVEL(ERROR, MIJIN_LOG_LEVEL_VALUE_ERROR)
|
|||||||
#define MIJIN_LOG_ALWAYS(level, channel, ...) MIJIN_FUNCNAME_GET_LOGGER().log( \
|
#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__ \
|
MIJIN_LOG_LEVEL_OBJECT(level), MIJIN_LOG_CHANNEL_OBJECT(channel), std::source_location::current(), __VA_ARGS__ \
|
||||||
)
|
)
|
||||||
#define MIJIN_LOG_ALWAYS_RAW(level, channel, ...) MIJIN_FUNCNAME_GET_LOGGER().logUnformatted( \
|
#define MIJIN_LOG(level, channel, ...) \
|
||||||
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()) {} \
|
if constexpr (MIJIN_LOG_LEVEL_OBJECT(level).value < MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE()) {} \
|
||||||
else MIJIN_LOG_ALWAYS(level, channel, __VA_ARGS__)
|
else MIJIN_LOG_ALWAYS(level, channel, __VA_ARGS__)
|
||||||
|
|
||||||
#define MIJIN_LOG_IF(cond, level, channel, ...) \
|
|
||||||
if constexpr (MIJIN_LOG_LEVEL_OBJECT(level).value < MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE()) {} \
|
|
||||||
else if (cond) MIJIN_LOG_ALWAYS(level, channel, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define MIJIN_LOG_RAW(level, channel, ...) \
|
|
||||||
if constexpr (MIJIN_LOG_LEVEL_OBJECT(level).value < MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE()) {} \
|
|
||||||
else MIJIN_LOG_ALWAYS_RAW(level, channel, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define MIJIN_LOG_IF_RAW(cond, level, channel, ...) \
|
|
||||||
if constexpr (MIJIN_LOG_LEVEL_OBJECT(level).value < MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE()) {} \
|
|
||||||
else if (cond) MIJIN_LOG_ALWAYS_RAW(level, channel, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define MIJIN_SET_CLASS_LOGGER(loggerExpr) \
|
#define MIJIN_SET_CLASS_LOGGER(loggerExpr) \
|
||||||
const auto& MIJIN_FUNCNAME_GET_LOGGER() const noexcept \
|
const auto& MIJIN_FUNCNAME_GET_LOGGER() const noexcept \
|
||||||
{ \
|
{ \
|
||||||
@@ -304,11 +282,6 @@ auto MIJIN_FUNCNAME_GET_LOGGER = [&]() -> const auto& \
|
|||||||
{ \
|
{ \
|
||||||
return loggerExpr; \
|
return loggerExpr; \
|
||||||
};
|
};
|
||||||
#define MIJIN_SET_NS_LOGGER(loggerExpr) \
|
|
||||||
inline const mijin::Logger& MIJIN_FUNCNAME_GET_LOGGER() \
|
|
||||||
{ \
|
|
||||||
return loggerExpr; \
|
|
||||||
}
|
|
||||||
#define MIJIN_SET_CLASS_MIN_LOG_LEVEL_COMPILE(level) \
|
#define MIJIN_SET_CLASS_MIN_LOG_LEVEL_COMPILE(level) \
|
||||||
int MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE() MIJIN_NOEXCEPT \
|
int MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE() MIJIN_NOEXCEPT \
|
||||||
{ \
|
{ \
|
||||||
|
|||||||
@@ -9,9 +9,6 @@
|
|||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
template<typename TChar, typename TTraits, typename TAllocator, typename TFormatter = BaseSimpleLogFormatter<TChar, TTraits, TAllocator>>
|
|
||||||
inline TFormatter DEFAULT_STDIO_FORMATTER("[{ansi:level_color}{level}{ansi:reset}] {text} <{file}:{line}>");
|
|
||||||
|
|
||||||
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
||||||
requires(allocator_type<TAllocator<TChar>>)
|
requires(allocator_type<TAllocator<TChar>>)
|
||||||
class BaseStdioSink : public BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG_NAMES>
|
class BaseStdioSink : public BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG_NAMES>
|
||||||
@@ -19,15 +16,13 @@ class BaseStdioSink : public BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG
|
|||||||
public:
|
public:
|
||||||
using base_t = BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG_NAMES>;
|
using base_t = BaseFormattingLogSink<MIJIN_FORMATTING_SINK_TMP_ARG_NAMES>;
|
||||||
using typename base_t::char_t;
|
using typename base_t::char_t;
|
||||||
using typename base_t::traits_t;
|
|
||||||
using typename base_t::allocator_t;
|
using typename base_t::allocator_t;
|
||||||
using typename base_t::formatter_ptr_t;
|
using typename base_t::formatter_ptr_t;
|
||||||
using typename base_t::message_t;
|
using typename base_t::message_t;
|
||||||
using typename base_t::string_t;
|
|
||||||
private:
|
private:
|
||||||
int mMinStderrLevel = MIJIN_LOG_LEVEL_VALUE_WARNING;
|
int mMinStderrLevel = MIJIN_LOG_LEVEL_VALUE_WARNING;
|
||||||
public:
|
public:
|
||||||
explicit BaseStdioSink(formatter_ptr_t formatter = wrapDynamic(&DEFAULT_STDIO_FORMATTER<char_t, traits_t, allocator_t>), allocator_t allocator = {})
|
explicit BaseStdioSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||||
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||||
: base_t(std::move(formatter), std::move(allocator)) {}
|
: base_t(std::move(formatter), std::move(allocator)) {}
|
||||||
|
|
||||||
@@ -38,30 +33,30 @@ public:
|
|||||||
|
|
||||||
void setMinStderrLevel(const BaseLogLevel<char_t>& level) MIJIN_NOEXCEPT { mMinStderrLevel = level.value; }
|
void setMinStderrLevel(const BaseLogLevel<char_t>& level) MIJIN_NOEXCEPT { mMinStderrLevel = level.value; }
|
||||||
|
|
||||||
void handleMessageFormatted(const message_t& message, const string_t& formatted) MIJIN_NOEXCEPT override
|
void handleMessageFormatted(const message_t& message, const char_t* formatted) MIJIN_NOEXCEPT override
|
||||||
{
|
{
|
||||||
FILE* stream = (message.level->value >= mMinStderrLevel) ? stderr : stdout;
|
FILE* stream = (message.level->value >= mMinStderrLevel) ? stderr : stdout;
|
||||||
if constexpr (std::is_same_v<char_t, char>)
|
if constexpr (std::is_same_v<char_t, char>)
|
||||||
{
|
{
|
||||||
(void) std::fputs(formatted.c_str(), stream);
|
std::fputs(formatted, stream);
|
||||||
(void) std::fputc('\n', stream);
|
std::fputc('\n', stream);
|
||||||
}
|
}
|
||||||
else if constexpr (std::is_same_v<char_t, wchar_t>)
|
else if constexpr (std::is_same_v<char_t, wchar_t>)
|
||||||
{
|
{
|
||||||
(void) std::fputws(formatted.c_str(), stream);
|
std::fputws(formatted, stream);
|
||||||
(void) std::fputwc(L'\n', stream);
|
std::fputwc(L'\n', stream);
|
||||||
}
|
}
|
||||||
else if constexpr (sizeof(char_t) == sizeof(char))
|
else if constexpr (sizeof(char_t) == sizeof(char))
|
||||||
{
|
{
|
||||||
// char8_t etc.
|
// char8_t etc.
|
||||||
(void) std::fputs(std::bit_cast<const char*>(formatted.c_str()), stream);
|
std::fputs(std::bit_cast<const char*>(formatted), stream);
|
||||||
(void) std::fputc('\n', stream);
|
std::fputc('\n', stream);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
static_assert(always_false_v<char_t>, "Character type not supported.");
|
static_assert(always_false_v<char_t>, "Character type not supported.");
|
||||||
}
|
}
|
||||||
(void) std::fflush(stream);
|
std::fflush(stream);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "./formatting.hpp"
|
#include "./formatting.hpp"
|
||||||
#include "../io/stream.hpp"
|
#include "../io/stream.hpp"
|
||||||
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@@ -19,7 +20,6 @@ public:
|
|||||||
using typename base_t::allocator_t;
|
using typename base_t::allocator_t;
|
||||||
using typename base_t::formatter_ptr_t;
|
using typename base_t::formatter_ptr_t;
|
||||||
using typename base_t::message_t;
|
using typename base_t::message_t;
|
||||||
using typename base_t::string_t;
|
|
||||||
using stream_ptr_t = DynamicPointer<Stream>;
|
using stream_ptr_t = DynamicPointer<Stream>;
|
||||||
private:
|
private:
|
||||||
stream_ptr_t mStream;
|
stream_ptr_t mStream;
|
||||||
@@ -36,12 +36,12 @@ public:
|
|||||||
mStream = std::move(stream).release();
|
mStream = std::move(stream).release();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleMessageFormatted(const message_t& /* message */, const string_t& formatted) MIJIN_NOEXCEPT override
|
void handleMessageFormatted(const message_t& /* message */, const char_t* formatted) MIJIN_NOEXCEPT override
|
||||||
{
|
{
|
||||||
if (!mStream) {
|
if (!mStream) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(void) mStream->writeSpan(formatted);
|
(void) mStream->writeSpan(std::basic_string_view(formatted));
|
||||||
(void) mStream->write('\n');
|
(void) mStream->write('\n');
|
||||||
mStream->flush();
|
mStream->flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,11 +35,10 @@ public:
|
|||||||
{
|
{
|
||||||
MIJIN_ASSERT((std::bit_cast<std::uintptr_t>(ptr) & 1) == 0, "Invalid address, DynamicPointer requires addresses to be divisible by two.");
|
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_assignable_v<T*&, TOther*> && std::is_constructible_v<TDeleter, TOtherDeleter&&>)
|
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>))
|
constexpr DynamicPointer(DynamicPointer<TOther, TOtherDeleter>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_convertible_v<TOtherDeleter, TDeleter>))
|
||||||
: DynamicPointer(other.get(), other.isOwning() ? Owning::YES : Owning::NO, TDeleter(std::move(other.mDeleter)))
|
: mData(std::exchange(other.mData, 0)), mDeleter(std::move(other.mDeleter)) {
|
||||||
{
|
MIJIN_ASSERT(other.mData == 0, "");
|
||||||
other.mData = 0;
|
|
||||||
}
|
}
|
||||||
constexpr ~DynamicPointer() noexcept
|
constexpr ~DynamicPointer() noexcept
|
||||||
{
|
{
|
||||||
@@ -65,15 +64,6 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// template<typename TOther, typename TOtherDeleter> requires (std::is_base_of_v<TOther, T>)
|
|
||||||
// constexpr operator DynamicPointer<TOther, TOtherDeleter>() && // MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TDeleter>>)
|
|
||||||
// {
|
|
||||||
// const Owning owning = isOwning() ? Owning::YES : Owning::NO;
|
|
||||||
// T* const ptr = release();
|
|
||||||
//
|
|
||||||
// return DynamicPointer<TOther, TOtherDeleter>(static_cast<TOther*>(ptr), owning, TOtherDeleter(std::move(mDeleter)));
|
|
||||||
// }
|
|
||||||
|
|
||||||
template<typename TOther, typename TOtherDeleter> requires(std::equality_comparable_with<T, TOther>)
|
template<typename TOther, typename TOtherDeleter> requires(std::equality_comparable_with<T, TOther>)
|
||||||
auto operator<=>(const DynamicPointer<TOther, TOtherDeleter>& other) MIJIN_NOEXCEPT
|
auto operator<=>(const DynamicPointer<TOther, TOtherDeleter>& other) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
@@ -165,9 +155,9 @@ bool operator!=(std::nullptr_t, const DynamicPointer<T, TDeleter>& pointer) MIJI
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename... TArgs>
|
template<typename T, typename... TArgs>
|
||||||
DynamicPointer<T> makeDynamic(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
DynamicPointer<T, std::default_delete<T>> makeDynamic(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
||||||
{
|
{
|
||||||
return DynamicPointer<T>(new T(std::forward<TArgs>(args)...), Owning::YES);
|
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>
|
template<typename T, allocator_type_for<T> TAllocator, typename... TArgs>
|
||||||
@@ -181,12 +171,6 @@ DynamicPointer<T, AllocatorDeleter<TAllocator>> makeDynamicWithAllocator(TAlloca
|
|||||||
}
|
}
|
||||||
return DynamicPointer<T, AllocatorDeleter<TAllocator>>(obj, Owning::YES, AllocatorDeleter<TAllocator>(std::move(allocator)));
|
return DynamicPointer<T, AllocatorDeleter<TAllocator>>(obj, Owning::YES, AllocatorDeleter<TAllocator>(std::move(allocator)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
DynamicPointer<T> wrapDynamic(T* raw)
|
|
||||||
{
|
|
||||||
return DynamicPointer<T>(raw, Owning::NO);
|
|
||||||
}
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_MEMORY_DYNAMIC_POINTER_HPP_INCLUDED)
|
#endif // !defined(MIJIN_MEMORY_DYNAMIC_POINTER_HPP_INCLUDED)
|
||||||
|
|||||||
40
source/mijin/types/path.cpp
Normal file
40
source/mijin/types/path.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
#include "./path.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
|
||||||
|
//
|
||||||
|
// internal defines
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// internal constants
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// internal types
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// internal variables
|
||||||
|
//
|
||||||
|
|
||||||
|
//
|
||||||
|
// internal functions
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// public functions
|
||||||
|
//
|
||||||
|
|
||||||
|
NativePath getWorkingDirectory()
|
||||||
|
{
|
||||||
|
return fs::current_path().generic_string();
|
||||||
|
}
|
||||||
|
} // namespace mijin
|
||||||
511
source/mijin/types/path.hpp
Normal file
511
source/mijin/types/path.hpp
Normal file
@@ -0,0 +1,511 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_TYPES_PATH_HPP_INCLUDED)
|
||||||
|
#define MIJIN_TYPES_PATH_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "../debug/assert.hpp"
|
||||||
|
#include "../internal/common.hpp"
|
||||||
|
#include "../util/traits.hpp"
|
||||||
|
|
||||||
|
#include <ranges>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept BasePathType = requires(const T& path)
|
||||||
|
{
|
||||||
|
typename T::traits_t;
|
||||||
|
typename T::char_t;
|
||||||
|
typename T::char_traits_t;
|
||||||
|
typename T::allocator_t;
|
||||||
|
typename T::string_t;
|
||||||
|
typename T::string_view_t;
|
||||||
|
typename T::size_type;
|
||||||
|
typename T::difference_type;
|
||||||
|
typename T::path_view_t;
|
||||||
|
{ T::SEPARATOR } -> std::convertible_to<typename T::char_t>;
|
||||||
|
{ path.stringView() } -> std::convertible_to<typename T::string_view_t>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept PathType = BasePathType<T> && std::is_same_v<typename T::char_t, char>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept WPathType = BasePathType<T> && std::is_same_v<typename T::char_t, wchar_t>;
|
||||||
|
|
||||||
|
template<typename TChar, TChar separator = TChar('/')>
|
||||||
|
struct DefaultPathTraits
|
||||||
|
{
|
||||||
|
using char_t = TChar;
|
||||||
|
using char_traits_t = std::char_traits<TChar>;
|
||||||
|
using allocator_t = std::allocator<TChar>;
|
||||||
|
using string_t = std::basic_string<char_t, char_traits_t, allocator_t>;
|
||||||
|
using string_view_t = std::basic_string_view<char_t, char_traits_t>;
|
||||||
|
using size_type = string_view_t::size_type;
|
||||||
|
using difference_type = string_view_t::difference_type;
|
||||||
|
static constexpr char_t SEPARATOR = separator;
|
||||||
|
|
||||||
|
static constexpr bool isAbsolute(string_view_t path) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !path.empty() && path[0] == SEPARATOR;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
|
||||||
|
class BasePathView;
|
||||||
|
|
||||||
|
template<typename TConcrete, typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
|
||||||
|
class MixinPath;
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template<typename TChar, typename TTraits, TChar separator>
|
||||||
|
class BasePathIterator
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::string_view full_;
|
||||||
|
std::string_view::iterator pos_;
|
||||||
|
std::string_view::iterator next_;
|
||||||
|
|
||||||
|
BasePathIterator(std::string_view full, std::string_view::iterator pos) MIJIN_NOEXCEPT : full_(full), pos_(pos) {
|
||||||
|
if (pos != full_.end() && *pos == separator) {
|
||||||
|
++pos;
|
||||||
|
}
|
||||||
|
findNext();
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
BasePathIterator() MIJIN_NOEXCEPT : pos_(full_.end()), next_(full_.end()) {}
|
||||||
|
BasePathIterator(const BasePathIterator&) noexcept = default;
|
||||||
|
|
||||||
|
BasePathIterator& operator=(const BasePathIterator&) noexcept = default;
|
||||||
|
|
||||||
|
bool operator==(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ == other.pos_; }
|
||||||
|
bool operator!=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ != other.pos_; }
|
||||||
|
bool operator<(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ < other.pos_; }
|
||||||
|
bool operator<=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ <= other.pos_; }
|
||||||
|
bool operator>(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ > other.pos_; }
|
||||||
|
bool operator>=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ >= other.pos_; }
|
||||||
|
|
||||||
|
std::string_view operator*() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(pos_ != full_.end(), "Dereferencing an invalid iterator.");
|
||||||
|
return {pos_, next_};
|
||||||
|
}
|
||||||
|
|
||||||
|
BasePathIterator& operator++() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(pos_ != full_.end(), "Iterating past end.");
|
||||||
|
if (next_ == full_.end()) {
|
||||||
|
pos_ = full_.end();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pos_ = std::next(next_);
|
||||||
|
findNext();
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
BasePathIterator operator++(int) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
BasePathIterator copy(*this);
|
||||||
|
++copy;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// BasePathIterator& operator--() MIJIN_NOEXCEPT
|
||||||
|
// {
|
||||||
|
// MIJIN_ASSERT(pos_ != full_.begin(), "Iterating past begin.");
|
||||||
|
// next_ = std::prev(pos_);
|
||||||
|
// pos_ = std::find(std::reverse_iterator(next_), std::reverse_iterator(full_.begin()), separator).base();
|
||||||
|
// }
|
||||||
|
private:
|
||||||
|
void findNext()
|
||||||
|
{
|
||||||
|
next_ = std::find(pos_, full_.end(), separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TConcrete, typename TCharOther, typename TTraitsOther>
|
||||||
|
friend class ::mijin::MixinPath;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TConcrete, typename TChar, typename TTraits>
|
||||||
|
class MixinPath
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using traits_t = TTraits;
|
||||||
|
using char_t = typename traits_t::char_t;
|
||||||
|
using char_traits_t = typename traits_t::char_traits_t;
|
||||||
|
using allocator_t = typename traits_t::allocator_t;
|
||||||
|
using string_t = typename traits_t::string_t;
|
||||||
|
using string_view_t = typename traits_t::string_view_t;
|
||||||
|
using size_type = typename traits_t::size_type;
|
||||||
|
using difference_type = typename traits_t::difference_type;
|
||||||
|
using path_view_t = BasePathView<TChar, traits_t>;
|
||||||
|
using iterator = impl::BasePathIterator<char_t, char_traits_t, traits_t::SEPARATOR>;
|
||||||
|
using const_iterator = iterator;
|
||||||
|
static constexpr char_t SEPARATOR = traits_t::SEPARATOR;
|
||||||
|
|
||||||
|
constexpr bool operator==(string_view_t stringView) const MIJIN_NOEXCEPT { return stringView() == stringView; }
|
||||||
|
constexpr bool operator==(const TChar* cString) const MIJIN_NOEXCEPT { return stringView() == cString; }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr string_view_t getName() const MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr path_view_t getParent() const MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr string_view_t stringView() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return static_cast<const TConcrete*>(this)->stringViewImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr bool empty() const MIJIN_NOEXCEPT { return stringView().empty(); }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr size_type size() const MIJIN_NOEXCEPT { return stringView().size(); }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr bool isAbsolute() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return traits_t::isAbsolute(stringView());
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
iterator begin() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().begin()); }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
iterator end() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().end()); }
|
||||||
|
private:
|
||||||
|
|
||||||
|
template<typename TConcreteOther, typename TCharOther, typename TTraitsOther>
|
||||||
|
friend class MixinPath;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TConcreteA, typename TConcreteB, typename TChar, typename TTraits>
|
||||||
|
constexpr bool operator==(const MixinPath<TConcreteA, TChar, TTraits>& pathA, const MixinPath<TConcreteB, TChar, TTraits>& pathB) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return pathA.stringView() == pathB.stringView();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
|
||||||
|
class BasePath : public MixinPath<BasePath<TChar, TTraits>, TChar, TTraits>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using mixin_t = MixinPath<BasePath<TChar, TTraits>, TChar, TTraits>;
|
||||||
|
using typename mixin_t::traits_t;
|
||||||
|
using typename mixin_t::char_t;
|
||||||
|
using typename mixin_t::char_traits_t;
|
||||||
|
using typename mixin_t::allocator_t;
|
||||||
|
using typename mixin_t::string_t;
|
||||||
|
using typename mixin_t::string_view_t;
|
||||||
|
using typename mixin_t::difference_type;
|
||||||
|
using typename mixin_t::path_view_t;
|
||||||
|
static constexpr char_t SEPARATOR = mixin_t::SEPARATOR;
|
||||||
|
private:
|
||||||
|
struct NoSimplify {};
|
||||||
|
|
||||||
|
string_t storage_;
|
||||||
|
|
||||||
|
constexpr BasePath(string_t storage, NoSimplify) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(storage)) {}
|
||||||
|
public:
|
||||||
|
constexpr BasePath() = default;
|
||||||
|
constexpr BasePath(const BasePath&) = default;
|
||||||
|
constexpr BasePath(BasePath&&) = default;
|
||||||
|
constexpr BasePath(string_t string) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(string)) {
|
||||||
|
simplify();
|
||||||
|
}
|
||||||
|
constexpr BasePath(const char_t* cString, allocator_t allocator = {}) : BasePath(string_t(cString, std::move(allocator))) {}
|
||||||
|
constexpr BasePath(string_view_t stringView, allocator_t allocator = {}) : BasePath(string_t(stringView, std::move(allocator))) {}
|
||||||
|
template<typename TConcreteOther> requires (!std::is_same_v<BasePath<TChar, TTraits>, TConcreteOther>)
|
||||||
|
explicit constexpr BasePath(const MixinPath<TConcreteOther, TChar, TTraits> other, allocator_t allocator = {}) : BasePath(string_t(other.stringView(), std::move(allocator)), NoSimplify()) {}
|
||||||
|
|
||||||
|
constexpr BasePath& operator=(const BasePath&) = default;
|
||||||
|
constexpr BasePath& operator=(BasePath&&) = default;
|
||||||
|
|
||||||
|
constexpr auto operator<=>(const BasePath&) const noexcept = default;
|
||||||
|
using mixin_t::operator==;
|
||||||
|
|
||||||
|
template<typename TConcreteOther>
|
||||||
|
BasePath& operator/=(const MixinPath<TConcreteOther, char_t, traits_t>& other);
|
||||||
|
BasePath& operator/=(string_view_t more);
|
||||||
|
|
||||||
|
template<typename TConcreteOther>
|
||||||
|
BasePath operator/(const MixinPath<TConcreteOther, char_t, traits_t>& other) const;
|
||||||
|
BasePath operator/(string_view_t more) const;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr const string_t& string() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return storage_;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return storage_;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
constexpr void simplify() MIJIN_NOEXCEPT;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Path = BasePath<char>;
|
||||||
|
using WPath = BasePath<wchar_t>;
|
||||||
|
|
||||||
|
using NativePath = Path; // TODO
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
class BasePathView : public MixinPath<BasePathView<TChar, TTraits>, TChar, TTraits>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using mixin_t = MixinPath<BasePathView<TChar, TTraits>, TChar, TTraits>;
|
||||||
|
using typename mixin_t::char_t;
|
||||||
|
using typename mixin_t::string_view_t;
|
||||||
|
private:
|
||||||
|
string_view_t view_;
|
||||||
|
public:
|
||||||
|
constexpr BasePathView() noexcept = default;
|
||||||
|
constexpr BasePathView(const BasePathView&) noexcept = default;
|
||||||
|
explicit constexpr BasePathView(string_view_t view) MIJIN_NOEXCEPT : view_(view) {}
|
||||||
|
template<typename TIterator>
|
||||||
|
constexpr BasePathView(const TIterator& begin, const TIterator& end) MIJIN_NOEXCEPT : view_(begin, end) {}
|
||||||
|
template<typename TConcreteOther> requires (!std::is_same_v<BasePathView<TChar, TTraits>, TConcreteOther>)
|
||||||
|
constexpr BasePathView(const MixinPath<TConcreteOther>& other) MIJIN_NOEXCEPT : view_(other.stringView()) {}
|
||||||
|
|
||||||
|
constexpr BasePathView& operator=(const BasePathView&) noexcept = default;
|
||||||
|
|
||||||
|
constexpr auto operator<=>(const BasePathView&) const noexcept = default;
|
||||||
|
using mixin_t::operator==;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return view_;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using PathView = BasePathView<char>;
|
||||||
|
using WPathView = BasePathView<wchar_t>;
|
||||||
|
|
||||||
|
using NativePathView = PathView; // TODO
|
||||||
|
|
||||||
|
template<typename TConcrete, typename TChar, typename TTraits>
|
||||||
|
constexpr auto MixinPath<TConcrete, TChar, TTraits>::getName() const MIJIN_NOEXCEPT -> string_view_t
|
||||||
|
{
|
||||||
|
const string_view_t strView = stringView();
|
||||||
|
auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base();
|
||||||
|
return {it, strView.end()};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TConcrete, typename TChar, typename TTraits>
|
||||||
|
constexpr auto MixinPath<TConcrete, TChar, TTraits>::getParent() const MIJIN_NOEXCEPT -> path_view_t
|
||||||
|
{
|
||||||
|
const string_view_t strView = stringView();
|
||||||
|
auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base();
|
||||||
|
if (it == strView.begin()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (std::prev(it) == strView.begin()) {
|
||||||
|
return {strView.begin(), it}; // edge case, return "/" instead of nothing
|
||||||
|
}
|
||||||
|
return {strView.begin(), std::prev(it)};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
template<typename TConcreteOther>
|
||||||
|
auto BasePath<TChar, TTraits>::operator/=(const MixinPath<TConcreteOther, char_t, traits_t>& other) -> BasePath&
|
||||||
|
{
|
||||||
|
if (other.isAbsolute())
|
||||||
|
{
|
||||||
|
storage_ = other.stringView();
|
||||||
|
}
|
||||||
|
else if (!other.empty())
|
||||||
|
{
|
||||||
|
if (other.stringView()[0] != SEPARATOR)
|
||||||
|
{
|
||||||
|
storage_.reserve(storage_.size() + other.size() + 1);
|
||||||
|
storage_.push_back(SEPARATOR);
|
||||||
|
}
|
||||||
|
storage_.append_range(other.stringView());
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
auto BasePath<TChar, TTraits>::operator/=(string_view_t more) -> BasePath&
|
||||||
|
{
|
||||||
|
operator/=(path_view_t(more));
|
||||||
|
simplify();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
template<typename TConcreteOther>
|
||||||
|
auto BasePath<TChar, TTraits>::operator/(const MixinPath<TConcreteOther, char_t, traits_t>& other) const -> BasePath
|
||||||
|
{
|
||||||
|
if (other.isAbsolute() || other.empty())
|
||||||
|
{
|
||||||
|
return BasePath(string_t(other.stringView(), storage_.get_allocator()), NoSimplify());
|
||||||
|
}
|
||||||
|
const bool addSeparator = other.stringView()[0] != SEPARATOR;
|
||||||
|
string_t combined(storage_.get_allocator());
|
||||||
|
combined.reserve(storage_.size() + other.stringView().size() + (addSeparator ? 1 : 0));
|
||||||
|
combined.append_range(storage_);
|
||||||
|
if (addSeparator) {
|
||||||
|
combined.push_back(SEPARATOR);
|
||||||
|
}
|
||||||
|
combined.append_range(other.stringView());
|
||||||
|
return BasePath(std::move(combined), NoSimplify());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
auto BasePath<TChar, TTraits>::operator/(string_view_t other) const -> BasePath
|
||||||
|
{
|
||||||
|
BasePath combined = (*this / path_view_t(other));
|
||||||
|
combined.simplify();
|
||||||
|
return combined;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
constexpr void BasePath<TChar, TTraits>::simplify() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
// step 1: remove double slashes
|
||||||
|
difference_type moveBy = 0;
|
||||||
|
for (auto it = std::next(storage_.begin()); it < storage_.end(); ++it)
|
||||||
|
{
|
||||||
|
const bool doubleSlash = (*it == SEPARATOR && *std::prev(it) == SEPARATOR); // check this before moving the current character, as that might create a double slash itself
|
||||||
|
if (moveBy > 0) {
|
||||||
|
*std::prev(it, moveBy) = *it;
|
||||||
|
}
|
||||||
|
if (doubleSlash) {
|
||||||
|
++moveBy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// step 1.5: remove trailing slash (but only if it's not the only remaining char)
|
||||||
|
if (moveBy < static_cast<difference_type>(storage_.size() - 1) && storage_[storage_.size() - moveBy - 1] == SEPARATOR) {
|
||||||
|
++moveBy;
|
||||||
|
}
|
||||||
|
storage_.resize(storage_.size() - moveBy);
|
||||||
|
|
||||||
|
// step 2: get rid of any "/.." together with the preceeding segment
|
||||||
|
moveBy = 0;
|
||||||
|
for (auto it = std::next(storage_.begin(), 2); it < storage_.end(); ++it)
|
||||||
|
{
|
||||||
|
if (moveBy > 0)
|
||||||
|
{
|
||||||
|
*std::prev(it, moveBy) = *it;
|
||||||
|
}
|
||||||
|
if (*std::prev(it, moveBy) == TChar('.') && *std::prev(it, moveBy + 1) == TChar('.') && *std::prev(it, moveBy + 2) == SEPARATOR
|
||||||
|
&& (std::next(it) == storage_.end() || *std::next(it) == SEPARATOR))
|
||||||
|
{
|
||||||
|
if (std::prev(it, moveBy + 2) == storage_.begin())
|
||||||
|
{
|
||||||
|
// leading "/.." -> just remove it
|
||||||
|
moveBy += 3;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// find the start of the preceeding segment
|
||||||
|
for (auto itStart = std::prev(it, moveBy + 3);; --itStart)
|
||||||
|
{
|
||||||
|
if (*std::prev(itStart) == SEPARATOR || std::prev(itStart) == storage_.begin())
|
||||||
|
{
|
||||||
|
// /path/with/../double/dot
|
||||||
|
// itStart --A A
|
||||||
|
// it -------------|
|
||||||
|
// remove everything from itStart to it + two slashes
|
||||||
|
moveBy += std::distance(itStart, std::prev(it, moveBy)) + 2; // skip it all
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
storage_.resize(storage_.size() - moveBy);
|
||||||
|
|
||||||
|
// step 3: eliminate any segments that are just "."
|
||||||
|
moveBy = 0;
|
||||||
|
if (storage_.size() == 1) {
|
||||||
|
return; // just stop it here
|
||||||
|
}
|
||||||
|
for (auto it = storage_.begin(); it < storage_.end(); ++it)
|
||||||
|
{
|
||||||
|
const bool atStart = (it == storage_.begin());
|
||||||
|
const bool atEnd = (std::next(it) == storage_.end());
|
||||||
|
const bool emptyEle = (*it == TChar('.') // char is a dot
|
||||||
|
&& (atStart || *std::prev(it, moveBy + 1) == SEPARATOR) // previous is a slash or doesn't exist
|
||||||
|
&& (atEnd || *std::next(it) == SEPARATOR)); // next is a slash or doesn't exist
|
||||||
|
if (moveBy > 0) {
|
||||||
|
*std::prev(it, moveBy) = *it;
|
||||||
|
}
|
||||||
|
if (emptyEle) {
|
||||||
|
moveBy += 2;
|
||||||
|
if (!atEnd) {
|
||||||
|
++it; // skip the next one
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
storage_.resize(storage_.size() - moveBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, TChar separator = TChar('/'), typename TTraits = std::char_traits<TChar>>
|
||||||
|
constexpr bool verifyPathString(std::basic_string_view<TChar, TTraits> stringView) MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
for (auto it = std::next(stringView.begin()); it < stringView.end(); ++it)
|
||||||
|
{
|
||||||
|
if(*it == separator && *std::prev(it) == separator) {
|
||||||
|
return false; // double slash
|
||||||
|
}
|
||||||
|
if (it != std::next(stringView.begin())
|
||||||
|
&& *it == TChar('.') && *std::prev(it) == TChar('.') && *std::prev(it, 2) == separator
|
||||||
|
&& (std::next(it) == stringView.end() || *std::next(it) == separator)) {
|
||||||
|
return false; // "/.."
|
||||||
|
}
|
||||||
|
const bool atStart = (it == stringView.begin());
|
||||||
|
const bool atEnd = (std::next(it) == stringView.end());
|
||||||
|
if(*it == TChar('.') // char is a dot
|
||||||
|
&& (atStart || *std::prev(it) == separator) // previous is a slash or doesn't exist
|
||||||
|
&& (atEnd || *std::next(it) == separator)) // next is a slash or doesn't exist
|
||||||
|
{
|
||||||
|
return false; // "/."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
consteval PathView operator""_pv(const char* cString, std::size_t len)
|
||||||
|
{
|
||||||
|
if (!verifyPathString(std::string_view(cString, len))) {
|
||||||
|
throw "Invalid path string.";
|
||||||
|
}
|
||||||
|
return PathView(std::string_view(cString, len));
|
||||||
|
}
|
||||||
|
|
||||||
|
consteval WPathView operator""_pv(const wchar_t* cString, std::size_t len)
|
||||||
|
{
|
||||||
|
if (!verifyPathString(std::wstring_view(cString, len))) {
|
||||||
|
throw "Invalid path string.";
|
||||||
|
}
|
||||||
|
return WPathView(std::wstring_view(cString, len));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
NativePath getWorkingDirectory();
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
template<mijin::BasePathType TPath>
|
||||||
|
struct std::hash<TPath>
|
||||||
|
{
|
||||||
|
std::size_t operator()(const TPath& path) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return std::hash<std::string_view>()(path.stringView());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_TYPES_PATH_HPP_INCLUDED)
|
||||||
@@ -60,32 +60,6 @@ public:
|
|||||||
[[nodiscard]] const TError& getError() const MIJIN_NOEXCEPT { return std::get<TError>(state_); }
|
[[nodiscard]] const TError& getError() const MIJIN_NOEXCEPT { return std::get<TError>(state_); }
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace impl
|
|
||||||
{
|
|
||||||
struct ResultSuccess {};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TError>
|
|
||||||
class ResultBase<void, TError> : public ResultBase<impl::ResultSuccess, TError>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ResultBase() MIJIN_NOEXCEPT : ResultBase<impl::ResultSuccess, TError>(impl::ResultSuccess{}) {}
|
|
||||||
ResultBase(const ResultBase&) MIJIN_NOEXCEPT = default;
|
|
||||||
ResultBase(ResultBase&&) MIJIN_NOEXCEPT = default;
|
|
||||||
ResultBase(TError errorValue) MIJIN_NOEXCEPT : ResultBase<impl::ResultSuccess, TError>(std::move(errorValue)) {}
|
|
||||||
|
|
||||||
ResultBase& operator=(const ResultBase&) = default;
|
|
||||||
ResultBase& operator=(ResultBase&&) = default;
|
|
||||||
|
|
||||||
impl::ResultSuccess& operator*() MIJIN_NOEXCEPT = delete;
|
|
||||||
const impl::ResultSuccess& operator*() const MIJIN_NOEXCEPT = delete;
|
|
||||||
impl::ResultSuccess* operator->() MIJIN_NOEXCEPT = delete;
|
|
||||||
const impl::ResultSuccess* operator->() const MIJIN_NOEXCEPT = delete;
|
|
||||||
|
|
||||||
[[nodiscard]] impl::ResultSuccess& getValue() MIJIN_NOEXCEPT = delete;
|
|
||||||
[[nodiscard]] const impl::ResultSuccess& getValue() const MIJIN_NOEXCEPT = delete;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ResultError
|
struct ResultError
|
||||||
{
|
{
|
||||||
std::string message;
|
std::string message;
|
||||||
@@ -99,7 +73,7 @@ struct ResultError
|
|||||||
ResultError& operator=(ResultError&&) MIJIN_NOEXCEPT = default;
|
ResultError& operator=(ResultError&&) MIJIN_NOEXCEPT = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TSuccess = void>
|
template<typename TSuccess>
|
||||||
using Result = ResultBase<TSuccess, ResultError>;
|
using Result = ResultBase<TSuccess, ResultError>;
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ concept nullable_type = !std::is_same_v<T, std::nullptr_t> && requires(T t) { t
|
|||||||
template<nullable_type T>
|
template<nullable_type T>
|
||||||
class NotNullable
|
class NotNullable
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
using wrapped_t = T;
|
||||||
|
using element_type = std::remove_reference_t<decltype(*std::declval<const wrapped_t>())>;
|
||||||
|
using pointer = element_type*;
|
||||||
private:
|
private:
|
||||||
T base_;
|
T base_;
|
||||||
public:
|
public:
|
||||||
@@ -55,6 +59,12 @@ public:
|
|||||||
{
|
{
|
||||||
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
}
|
}
|
||||||
|
template<typename TOther> requires(std::is_base_of_v<typename NotNullable<TOther>::element_type, element_type> && std::is_constructible_v<T, pointer>)
|
||||||
|
explicit constexpr NotNullable(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, pointer>))
|
||||||
|
: base_(static_cast<pointer>(&*other))
|
||||||
|
{
|
||||||
|
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
|
||||||
|
}
|
||||||
template<typename TOther> requires(std::is_constructible_v<T, TOther&&>)
|
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&&>))
|
constexpr NotNullable(NotNullable<TOther>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TOther&&>))
|
||||||
: base_(std::exchange(other.base_, nullptr))
|
: base_(std::exchange(other.base_, nullptr))
|
||||||
|
|||||||
@@ -4,8 +4,18 @@
|
|||||||
#if !defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)
|
#if !defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)
|
||||||
#define MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED 1
|
#define MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include "../detect.hpp"
|
||||||
|
|
||||||
#define MIJIN_CONCAT_DETAIL(a, b) a ## b
|
#define MIJIN_CONCAT_DETAIL(a, b) a ## b
|
||||||
#define MIJIN_CONCAT(a, b) MIJIN_CONCAT_DETAIL(a, b)
|
#define MIJIN_CONCAT(a, b) MIJIN_CONCAT_DETAIL(a, b)
|
||||||
#define MIJIN_CONCAT3(a, b, c) MIJIN_CONCAT(a, MIJIN_CONCAT(b, c))
|
#define MIJIN_CONCAT3(a, b, c) MIJIN_CONCAT(a, MIJIN_CONCAT(b, c))
|
||||||
|
|
||||||
|
#if MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
|
||||||
|
#define MIJIN_FUNCNAME() __PRETTY_FUNCTION__
|
||||||
|
#elif MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
||||||
|
#define MIJIN_FUNCNAME() __FUNCSIG__
|
||||||
|
#else
|
||||||
|
#define MIJIN_FUNCNAME() __func__
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)
|
#endif // defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)
|
||||||
@@ -4,11 +4,8 @@
|
|||||||
#ifndef MIJIN_UTIL_EXCEPTION_HPP_INCLUDED
|
#ifndef MIJIN_UTIL_EXCEPTION_HPP_INCLUDED
|
||||||
#define MIJIN_UTIL_EXCEPTION_HPP_INCLUDED 1
|
#define MIJIN_UTIL_EXCEPTION_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <format>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <typeinfo>
|
|
||||||
|
|
||||||
#include "../detect.hpp"
|
|
||||||
#include "../debug/stacktrace.hpp"
|
#include "../debug/stacktrace.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
@@ -76,76 +73,5 @@ void walkExceptionCause(const std::exception_ptr& cause, TFunc func)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TFunc>
|
|
||||||
void walkException(const std::exception& exc, TFunc func)
|
|
||||||
{
|
|
||||||
func(exc);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TFunc>
|
|
||||||
void walkException(const mijin::Exception& exc, TFunc func)
|
|
||||||
{
|
|
||||||
func(exc);
|
|
||||||
walkExceptionCause(exc.getCause(), func);
|
|
||||||
}
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
template<typename TChar>
|
|
||||||
struct std::formatter<mijin::Exception, 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::Exception& exception, TContext& ctx) const
|
|
||||||
{
|
|
||||||
using namespace std::literals;
|
|
||||||
|
|
||||||
auto it = ctx.out();
|
|
||||||
bool first = true;
|
|
||||||
mijin::walkException(exception, [&]<typename T>(const T& exc)
|
|
||||||
{
|
|
||||||
if constexpr (!std::is_same_v<T, std::nullptr_t>)
|
|
||||||
{
|
|
||||||
if (!first) {
|
|
||||||
it = std::ranges::copy(MIJIN_SMART_QUOTE(char_t, "\nCaused by:\n"sv), it).out;
|
|
||||||
}
|
|
||||||
first = false;
|
|
||||||
#if MIJIN_RTTI
|
|
||||||
it = std::ranges::copy(std::basic_string_view(typeid(exc).name()), it).out;
|
|
||||||
it = std::ranges::copy(MIJIN_SMART_QUOTE(char_t, ": "sv), it).out;
|
|
||||||
#endif
|
|
||||||
it = std::ranges::copy(std::basic_string_view(exc.what()), it).out;
|
|
||||||
if constexpr (std::is_same_v<T, mijin::Exception>)
|
|
||||||
{
|
|
||||||
if (const mijin::Result<mijin::Stacktrace>& trace = exc.getStacktrace(); trace.isSuccess())
|
|
||||||
{
|
|
||||||
*it = MIJIN_SMART_QUOTE(char_t, '\n');
|
|
||||||
++it;
|
|
||||||
it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "{}"), trace.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
it = std::ranges::copy(MIJIN_SMART_QUOTE(char_t, "<unknown exception>"sv), it).out;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return it;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif // MIJIN_UTIL_EXCEPTION_HPP_INCLUDED
|
#endif // MIJIN_UTIL_EXCEPTION_HPP_INCLUDED
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <variant>
|
#include <variant>
|
||||||
#include "../container/optional.hpp"
|
#include "../container/optional.hpp"
|
||||||
#include "../internal/common.hpp"
|
#include "../internal/common.hpp"
|
||||||
|
#include "../util/annot.hpp"
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
@@ -855,6 +856,64 @@ TAs collect(TIterable&& iterable)
|
|||||||
return TAs(iterable.begin(), iterable.end());
|
return TAs(iterable.begin(), iterable.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TEle, std::size_t numEles>
|
||||||
|
class FixedArrayOutputIterator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using array_t = std::array<TEle, numEles>;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
using value_type = TEle;
|
||||||
|
using pointer = value_type*;
|
||||||
|
using reference = value_type&;
|
||||||
|
|
||||||
|
static constexpr std::size_t NUM_ELES = numEles;
|
||||||
|
private:
|
||||||
|
not_null_t<array_t*> array_;
|
||||||
|
std::size_t counter_ = 0;
|
||||||
|
public:
|
||||||
|
constexpr explicit FixedArrayOutputIterator(array_t& array) MIJIN_NOEXCEPT : array_(&array) {}
|
||||||
|
constexpr explicit FixedArrayOutputIterator(array_t& array, array_t::iterator pos) MIJIN_NOEXCEPT
|
||||||
|
: array_(&array), counter_(static_cast<std::size_t>(pos - array.begin())) {}
|
||||||
|
constexpr FixedArrayOutputIterator(const FixedArrayOutputIterator&) noexcept = default;
|
||||||
|
|
||||||
|
constexpr FixedArrayOutputIterator& operator=(const FixedArrayOutputIterator&) noexcept = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr std::size_t getCounter() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return counter_;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr bool didOverflow() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
return counter_ >= NUM_ELES;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr reference operator*() const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
static TEle dummy_; // returned when we're at the end of the array
|
||||||
|
if (counter_ < NUM_ELES) {
|
||||||
|
return array_->at(counter_);
|
||||||
|
}
|
||||||
|
return dummy_;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr FixedArrayOutputIterator& operator++() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
++counter_;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr FixedArrayOutputIterator operator++(int) const MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
FixedArrayOutputIterator copy(*this);
|
||||||
|
++copy;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
static_assert(std::output_iterator<FixedArrayOutputIterator<int, 1>, int>);
|
||||||
|
|
||||||
namespace pipe
|
namespace pipe
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -973,7 +1032,7 @@ auto operator|(TIterable&& iterable, Xth<idx>)
|
|||||||
{
|
{
|
||||||
return map(std::forward<TIterable>(iterable), [](auto&& element) { return std::get<idx>(element); } );
|
return map(std::forward<TIterable>(iterable), [](auto&& element) { return std::get<idx>(element); } );
|
||||||
}
|
}
|
||||||
}
|
} // namespace pipe
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // MIJIN_UTIL_ITERATORS_HPP_INCLUDED
|
#endif // MIJIN_UTIL_ITERATORS_HPP_INCLUDED
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <time.h>
|
||||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
@@ -42,7 +43,16 @@ namespace mijin
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
std::mutex gDlErrorMutex; // dlerror may not be thread-safe
|
std::mutex gDlErrorMutex; // dlerror may not be thread-safe
|
||||||
}
|
|
||||||
|
const std::uint64_t gCPUClockResolution = []()
|
||||||
|
{
|
||||||
|
struct timespec time;
|
||||||
|
[[maybe_unused]] const int result = clock_getres(CLOCK_PROCESS_CPUTIME_ID, &time);
|
||||||
|
MIJIN_ASSERT(result == 0, "Error getting cputime resolution.");
|
||||||
|
MIJIN_ASSERT(time.tv_sec == 0, "What kind of cpu clock is this?");
|
||||||
|
return static_cast<std::uint64_t>(time.tv_nsec);
|
||||||
|
}();
|
||||||
|
};
|
||||||
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -178,4 +188,26 @@ void alignedFree(void* ptr)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::uint64_t getCPUTicks() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||||
|
struct timespec time;
|
||||||
|
[[maybe_unused]] const int result = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
|
||||||
|
MIJIN_ASSERT(result == 0, "Error getting cpu time.");
|
||||||
|
return (static_cast<std::uint64_t>(time.tv_sec) * 1e9 + time.tv_nsec) / gCPUClockResolution;
|
||||||
|
#else
|
||||||
|
MIJIN_ERROR("TODO");
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint64_t getCPUTicksPerSecond() MIJIN_NOEXCEPT
|
||||||
|
{
|
||||||
|
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||||
|
return 1e9 / gCPUClockResolution;
|
||||||
|
#else
|
||||||
|
MIJIN_ERROR("TODO");
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#define MIJIN_UTIL_OS_HPP_INCLUDED 1
|
#define MIJIN_UTIL_OS_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -85,6 +86,9 @@ void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT;
|
|||||||
[[nodiscard]] void* alignedRealloc(void* ptr, 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);
|
void alignedFree(void* ptr);
|
||||||
|
|
||||||
|
[[nodiscard]] std::uint64_t getCPUTicks() MIJIN_NOEXCEPT;
|
||||||
|
[[nodiscard]] std::uint64_t getCPUTicksPerSecond() MIJIN_NOEXCEPT;
|
||||||
|
|
||||||
SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT
|
SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
|
|||||||
@@ -33,16 +33,6 @@ namespace mijin
|
|||||||
// public constants
|
// public constants
|
||||||
//
|
//
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
template<typename TChar>
|
|
||||||
static constexpr std::array DEFAULT_TRIM_CHARS_DATA = {TChar(' '), TChar('\t'), TChar('\r'), TChar('\n')};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TChar>
|
|
||||||
static const std::basic_string_view<TChar, std::char_traits<TChar>> DEFAULT_TRIM_CHARS
|
|
||||||
= {detail::DEFAULT_TRIM_CHARS_DATA<TChar>.begin(), detail::DEFAULT_TRIM_CHARS_DATA<TChar>.end()};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public traits
|
// public traits
|
||||||
//
|
//
|
||||||
@@ -108,361 +98,6 @@ struct SplitOptions
|
|||||||
bool ignoreEmpty = true;
|
bool ignoreEmpty = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SplitViewOptions
|
|
||||||
{
|
|
||||||
bool ignoreEmpty = true;
|
|
||||||
bool trim = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, TChar splitAt, SplitViewOptions options = SplitViewOptions(), typename TCharTraits = std::char_traits<TChar>>
|
|
||||||
struct SplitStringTraitsCT
|
|
||||||
{
|
|
||||||
using char_t = TChar;
|
|
||||||
using string_view_t = std::basic_string_view<TChar, TCharTraits>;
|
|
||||||
|
|
||||||
static constexpr char_t getSplitAt() MIJIN_NOEXCEPT { return splitAt; }
|
|
||||||
static constexpr bool getIgnoreEmpty() MIJIN_NOEXCEPT { return options.ignoreEmpty; }
|
|
||||||
static constexpr bool getTrim() MIJIN_NOEXCEPT { return options.trim; }
|
|
||||||
static constexpr auto getTrimChars() MIJIN_NOEXCEPT { return DEFAULT_TRIM_CHARS<char_t>; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TCharTraits = std::char_traits<TChar>>
|
|
||||||
struct SplitStringTraitsRT
|
|
||||||
{
|
|
||||||
using char_t = TChar;
|
|
||||||
using string_view_t = std::basic_string_view<TChar, TCharTraits>;
|
|
||||||
|
|
||||||
char_t splitAt;
|
|
||||||
bool ignoreEmpty;
|
|
||||||
string_view_t trimChars = {};
|
|
||||||
|
|
||||||
constexpr char_t getSplitAt() const MIJIN_NOEXCEPT { return splitAt; }
|
|
||||||
constexpr bool getIgnoreEmpty() const MIJIN_NOEXCEPT { return ignoreEmpty; }
|
|
||||||
constexpr bool getTrim() const MIJIN_NOEXCEPT { return !trimChars.empty(); }
|
|
||||||
constexpr string_view_t getTrimChars() const MIJIN_NOEXCEPT { return trimChars; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T, typename TChar>
|
|
||||||
concept SplitStringTraitsType = std::is_copy_constructible_v<T> && requires(const T& object)
|
|
||||||
{
|
|
||||||
typename T::char_t;
|
|
||||||
typename T::string_view_t;
|
|
||||||
{ object.getSplitAt() } -> std::convertible_to<TChar>;
|
|
||||||
{ object.getIgnoreEmpty() } -> std::convertible_to<bool>;
|
|
||||||
{ object.getTrim() } -> std::convertible_to<bool>;
|
|
||||||
{ object.getTrimChars() } -> std::convertible_to<typename T::string_view_t>;
|
|
||||||
};
|
|
||||||
static_assert(SplitStringTraitsType<SplitStringTraitsCT<char, ' '>, char>);
|
|
||||||
static_assert(SplitStringTraitsType<SplitStringTraitsRT<char>, char>);
|
|
||||||
|
|
||||||
template<typename TChar, typename TLine, SplitViewOptions options = SplitViewOptions(), typename TCharTraits = std::char_traits<TChar>>
|
|
||||||
struct SplitLineTraitsCT : SplitStringTraitsCT<TChar, '\n', options, TCharTraits>
|
|
||||||
{
|
|
||||||
using base_t = SplitStringTraitsCT<TChar, '\n', options, TCharTraits>;
|
|
||||||
using char_t = TChar;
|
|
||||||
using line_t = TLine;
|
|
||||||
|
|
||||||
line_t line = 1;
|
|
||||||
|
|
||||||
using base_t::getSplitAt;
|
|
||||||
using base_t::getIgnoreEmpty;
|
|
||||||
using base_t::getTrim;
|
|
||||||
using base_t::getTrimChars;
|
|
||||||
constexpr void onNext() MIJIN_NOEXCEPT {
|
|
||||||
++line;
|
|
||||||
}
|
|
||||||
constexpr line_t getLine() const MIJIN_NOEXCEPT { return line; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TLine, typename TCharTraits = std::char_traits<TChar>>
|
|
||||||
struct SplitLineTraitsRT
|
|
||||||
{
|
|
||||||
using char_t = TChar;
|
|
||||||
using line_t = TLine;
|
|
||||||
using string_view_t = std::basic_string_view<TChar, TCharTraits>;
|
|
||||||
|
|
||||||
line_t line = 1;
|
|
||||||
bool ignoreEmpty;
|
|
||||||
string_view_t trimChars = {};
|
|
||||||
|
|
||||||
constexpr char_t getSplitAt() const MIJIN_NOEXCEPT { return '\n'; }
|
|
||||||
constexpr bool getIgnoreEmpty() const MIJIN_NOEXCEPT { return ignoreEmpty; }
|
|
||||||
constexpr bool getTrim() const MIJIN_NOEXCEPT { return !trimChars.empty(); }
|
|
||||||
constexpr string_view_t getTrimChars() const MIJIN_NOEXCEPT { return trimChars; }
|
|
||||||
constexpr line_t getLine() const MIJIN_NOEXCEPT { return line; }
|
|
||||||
constexpr void onNext() MIJIN_NOEXCEPT {
|
|
||||||
++line;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T, typename TChar, typename TLine>
|
|
||||||
concept SplitLineTraitsType = SplitStringTraitsType<T, TChar> && requires (const T& object)
|
|
||||||
{
|
|
||||||
{ object.getLine() } -> std::convertible_to<TLine>;
|
|
||||||
};
|
|
||||||
static_assert(SplitLineTraitsType<SplitLineTraitsCT<char, unsigned>, char, unsigned>);
|
|
||||||
static_assert(SplitLineTraitsType<SplitLineTraitsRT<char, unsigned>, char, unsigned>);
|
|
||||||
|
|
||||||
template<typename TString, typename TChars>
|
|
||||||
[[nodiscard]]
|
|
||||||
auto trim(TString&& string, TChars&& chars);
|
|
||||||
|
|
||||||
template<typename TChar, SplitStringTraitsType<TChar> TTraits>
|
|
||||||
class SplitStringIterator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
using string_view_t = traits_t::string_view_t;
|
|
||||||
using base_t = string_view_t::iterator;
|
|
||||||
using value_type = string_view_t;
|
|
||||||
private:
|
|
||||||
[[no_unique_address]] traits_t traits_;
|
|
||||||
|
|
||||||
string_view_t full_;
|
|
||||||
string_view_t::iterator pos_;
|
|
||||||
string_view_t::iterator next_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
constexpr SplitStringIterator(string_view_t full, base_t pos, traits_t traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<traits_t>)
|
|
||||||
: full_(full), pos_(pos), traits_(std::move(traits))
|
|
||||||
{
|
|
||||||
findNext();
|
|
||||||
}
|
|
||||||
constexpr explicit SplitStringIterator(traits_t traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<traits_t>)
|
|
||||||
: traits_(std::move(traits)) {}
|
|
||||||
constexpr SplitStringIterator(const SplitStringIterator&) noexcept(std::is_nothrow_copy_constructible_v<traits_t>) = default;
|
|
||||||
constexpr SplitStringIterator(SplitStringIterator&&) noexcept(std::is_nothrow_move_constructible_v<traits_t>) = default;
|
|
||||||
constexpr SplitStringIterator& operator=(const SplitStringIterator&) noexcept(std::is_nothrow_copy_assignable_v<traits_t>) = default;
|
|
||||||
constexpr SplitStringIterator& operator=(SplitStringIterator&&) noexcept(std::is_nothrow_move_assignable_v<traits_t>) = default;
|
|
||||||
|
|
||||||
constexpr bool operator==(const SplitStringIterator& other) const MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ == other.pos_; }
|
|
||||||
constexpr bool operator!=(const SplitStringIterator& other) const MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ != other.pos_; }
|
|
||||||
constexpr bool operator<(const SplitStringIterator& other) const MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ < other.pos_; }
|
|
||||||
constexpr bool operator<=(const SplitStringIterator& other) const MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ <= other.pos_; }
|
|
||||||
constexpr bool operator>(const SplitStringIterator& other) const MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ > other.pos_; }
|
|
||||||
constexpr bool operator>=(const SplitStringIterator& other) const MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ >= other.pos_; }
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
traits_t& getTraits() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return traits_;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
const traits_t& getTraits() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return traits_;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr value_type operator*() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(pos_ != full_.end(), "Dereferencing an invalid iterator.");
|
|
||||||
string_view_t result{pos_, next_};
|
|
||||||
if (traits_.getTrim())
|
|
||||||
{
|
|
||||||
result = trim(result, traits_.getTrimChars());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr SplitStringIterator& operator++() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
MIJIN_ASSERT(pos_ != full_.end(), "Iterating past end.");
|
|
||||||
if (next_ == full_.end()) {
|
|
||||||
pos_ = full_.end();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
pos_ = std::next(next_);
|
|
||||||
findNext();
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr SplitStringIterator operator++(int) const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
SplitStringIterator copy(*this);
|
|
||||||
++copy;
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
// SplitStringIterator& operator--() MIJIN_NOEXCEPT
|
|
||||||
// {
|
|
||||||
// MIJIN_ASSERT(pos_ != full_.begin(), "Iterating past begin.");
|
|
||||||
// next_ = std::prev(pos_);
|
|
||||||
// pos_ = std::find(std::reverse_iterator(next_), std::reverse_iterator(full_.begin()), separator).base();
|
|
||||||
// }
|
|
||||||
private:
|
|
||||||
constexpr void findNext()
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if constexpr (requires{{ traits_.onNext() };}) {
|
|
||||||
traits_.onNext();
|
|
||||||
}
|
|
||||||
next_ = std::find(pos_, full_.end(), traits_.getSplitAt());
|
|
||||||
|
|
||||||
if (!traits_.getIgnoreEmpty() || pos_ == full_.end()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (traits_.getTrim())
|
|
||||||
{
|
|
||||||
const string_view_t trimChars = traits_.getTrimChars();
|
|
||||||
typename string_view_t::iterator trimmedPos = std::find_if(pos_, next_, [&](char_t chr)
|
|
||||||
{
|
|
||||||
return !trimChars.contains(chr);
|
|
||||||
});
|
|
||||||
if (trimmedPos == next_)
|
|
||||||
{
|
|
||||||
pos_ = next_; // skip this part
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pos_ != next_) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
pos_ = std::next(pos_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, SplitStringTraitsType<TChar> TTraits>
|
|
||||||
class SplitStringRange
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
using string_view_t = traits_t::string_view_t;
|
|
||||||
using iterator = SplitStringIterator<char_t, traits_t>;
|
|
||||||
private:
|
|
||||||
[[no_unique_address]] traits_t traits_;
|
|
||||||
string_view_t stringView_;
|
|
||||||
public:
|
|
||||||
constexpr explicit SplitStringRange(string_view_t stringView, traits_t traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<traits_t>)
|
|
||||||
: stringView_(stringView), traits_(std::move(traits)) {}
|
|
||||||
constexpr SplitStringRange(const SplitStringRange&) noexcept(std::is_nothrow_copy_constructible_v<traits_t>) = default;
|
|
||||||
constexpr SplitStringRange(SplitStringRange&&) noexcept(std::is_nothrow_move_constructible_v<traits_t>) = default;
|
|
||||||
constexpr SplitStringRange& operator=(const SplitStringRange&) noexcept(std::is_nothrow_copy_assignable_v<traits_t>) = default;
|
|
||||||
constexpr SplitStringRange& operator=(SplitStringRange&&) noexcept(std::is_nothrow_move_assignable_v<traits_t>) = default;
|
|
||||||
constexpr auto operator<=>(const SplitStringRange&) const noexcept = default;
|
|
||||||
|
|
||||||
constexpr iterator begin() const MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<traits_t>)
|
|
||||||
{
|
|
||||||
return iterator(stringView_, stringView_.begin(), traits_);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr iterator end() const MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<traits_t>)
|
|
||||||
{
|
|
||||||
return iterator(stringView_, stringView_.end(), traits_);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TLine = unsigned, SplitLineTraitsType<TChar, TLine> TTraits = SplitLineTraitsCT<TChar, TLine>>
|
|
||||||
class LineIterator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using line_t = TLine;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
using base_t = SplitStringIterator<TChar, traits_t>;
|
|
||||||
using string_view_t = base_t::string_view_t;
|
|
||||||
using value_type = std::pair<string_view_t, line_t>;
|
|
||||||
private:
|
|
||||||
base_t base_ = {};
|
|
||||||
public:
|
|
||||||
constexpr LineIterator(string_view_t full, string_view_t::iterator pos, traits_t traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<traits_t>)
|
|
||||||
: base_(full, pos, std::move(traits)) {}
|
|
||||||
constexpr explicit LineIterator(traits_t traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<traits_t>)
|
|
||||||
: base_(std::move(traits)) {}
|
|
||||||
LineIterator(const LineIterator&) noexcept = default;
|
|
||||||
LineIterator(LineIterator&&) noexcept = default;
|
|
||||||
|
|
||||||
LineIterator& operator=(const LineIterator&) noexcept = default;
|
|
||||||
LineIterator& operator=(LineIterator&&) noexcept = default;
|
|
||||||
|
|
||||||
bool operator==(const LineIterator& other) const MIJIN_NOEXCEPT { return base_ == other.base_; }
|
|
||||||
bool operator!=(const LineIterator& other) const MIJIN_NOEXCEPT { return base_ != other.base_; }
|
|
||||||
bool operator<(const LineIterator& other) const MIJIN_NOEXCEPT { return base_ < other.base_; }
|
|
||||||
bool operator>(const LineIterator& other) const MIJIN_NOEXCEPT { return base_ > other.base_; }
|
|
||||||
bool operator<=(const LineIterator& other) const MIJIN_NOEXCEPT { return base_ <= other.base_; }
|
|
||||||
bool operator>=(const LineIterator& other) const MIJIN_NOEXCEPT { return base_ >= other.base_; }
|
|
||||||
|
|
||||||
constexpr value_type operator*() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
string_view_t stringView = *base_;
|
|
||||||
if (!base_.getTraits().getTrim())
|
|
||||||
{
|
|
||||||
// always split \r, even if not trimming other whitespace
|
|
||||||
if (stringView.ends_with('\r')) {
|
|
||||||
stringView = stringView.substr(0, stringView.size() - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {stringView, base_.getTraits().line};
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr LineIterator& operator++() MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
++base_;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr LineIterator operator++(int) const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
SplitStringIterator copy(*this);
|
|
||||||
++copy;
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename TChar, typename TLine = unsigned, SplitLineTraitsType<TChar, TLine> TTraits = SplitLineTraitsCT<TChar, TLine>>
|
|
||||||
class LineRange
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using char_t = TChar;
|
|
||||||
using line_t = TLine;
|
|
||||||
using traits_t = TTraits;
|
|
||||||
using iterator = LineIterator<char_t, line_t, traits_t>;
|
|
||||||
using string_view_t = iterator::string_view_t;
|
|
||||||
private:
|
|
||||||
[[no_unique_address]] traits_t traits_;
|
|
||||||
string_view_t stringView_;
|
|
||||||
public:
|
|
||||||
constexpr explicit LineRange(string_view_t stringView, traits_t traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<traits_t>)
|
|
||||||
: traits_(std::move(traits)), stringView_(stringView) {}
|
|
||||||
constexpr LineRange(const LineRange&) noexcept(std::is_nothrow_copy_constructible_v<traits_t>) = default;
|
|
||||||
constexpr LineRange(LineRange&&) noexcept(std::is_nothrow_move_constructible_v<traits_t>) = default;
|
|
||||||
constexpr LineRange& operator=(const LineRange&) noexcept(std::is_nothrow_copy_assignable_v<traits_t>) = default;
|
|
||||||
constexpr LineRange& operator=(LineRange&&) noexcept(std::is_nothrow_move_assignable_v<traits_t>) = default;
|
|
||||||
constexpr auto operator<=>(const LineRange&) const noexcept = default;
|
|
||||||
|
|
||||||
constexpr iterator begin() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return iterator(stringView_, stringView_.begin(), traits_);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr iterator end() const MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return iterator(stringView_, stringView_.end(), traits_);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
@@ -638,6 +273,13 @@ std::basic_string_view<TChar, TTraits> trimImpl(std::basic_string_view<TChar, TT
|
|||||||
{
|
{
|
||||||
return trimPrefixImpl(trimSuffixImpl(stringView, charsToTrim), charsToTrim);
|
return trimPrefixImpl(trimSuffixImpl(stringView, charsToTrim), charsToTrim);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TChar>
|
||||||
|
static const std::array DEFAULT_TRIM_CHARS_DATA = {TChar(' '), TChar('\t'), TChar('\r'), TChar('\n')};
|
||||||
|
|
||||||
|
template<typename TChar>
|
||||||
|
static const std::basic_string_view<TChar, std::char_traits<TChar>> DEFAULT_TRIM_CHARS
|
||||||
|
= {DEFAULT_TRIM_CHARS_DATA<TChar>.begin(), DEFAULT_TRIM_CHARS_DATA<TChar>.end()};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TLeft, typename TRight>
|
template<typename TLeft, typename TRight>
|
||||||
@@ -654,61 +296,6 @@ template<std::size_t count, typename TLeft, typename TRight>
|
|||||||
std::basic_string_view(std::forward<TRight>(separator)), options, outNumResults);
|
std::basic_string_view(std::forward<TRight>(separator)), options, outNumResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TTraits, typename TChar> requires (SplitStringTraitsType<TTraits, TChar>)
|
|
||||||
[[nodiscard]] SplitStringRange<TChar, TTraits> splitView(typename TTraits::string_view_t stringView, TTraits traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TTraits>)
|
|
||||||
{
|
|
||||||
return SplitStringRange<TChar, TTraits>(stringView, std::move(traits));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<auto splitAt, SplitViewOptions options = SplitViewOptions(), typename TStringView, typename TChar = decltype(splitAt), typename TCharTraits = std::char_traits<TChar>>
|
|
||||||
[[nodiscard]] SplitStringRange<TChar, SplitStringTraitsCT<TChar, splitAt, options, TCharTraits>> splitView(TStringView&& stringView) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return splitView<SplitStringTraitsCT<TChar, splitAt, options, TCharTraits>>(std::basic_string_view<TChar, TCharTraits>(std::forward<TStringView>(stringView)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TStringView, typename TChar, typename TCharTraits = std::char_traits<TChar>, typename TTrimChars = std::basic_string_view<TChar, TCharTraits>>
|
|
||||||
[[nodiscard]] auto splitView(TStringView&& stringView, TChar splitAt, bool ignoreEmpty = true, TTrimChars trimChars = {}) MIJIN_NOEXCEPT
|
|
||||||
{
|
|
||||||
return splitView(std::basic_string_view<TChar, TCharTraits>(std::forward<TStringView>(stringView)), SplitStringTraitsRT<TChar, TCharTraits>{
|
|
||||||
.splitAt = splitAt,
|
|
||||||
.ignoreEmpty = ignoreEmpty,
|
|
||||||
.trimChars = std::basic_string_view<TChar, TCharTraits>(std::forward<TTrimChars>(trimChars))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TTraits, typename TChar, typename TLine = unsigned> requires(SplitLineTraitsType<TTraits, TChar, TLine>)
|
|
||||||
[[nodiscard]] LineRange<TChar, TLine, TTraits> splitLines(typename TTraits::string_view_t stringView, TTraits traits = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TTraits>)
|
|
||||||
{
|
|
||||||
return LineRange<TChar, TLine, TTraits>(stringView, std::move(traits));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TLine = unsigned, SplitViewOptions options = SplitViewOptions{.ignoreEmpty=false},
|
|
||||||
typename TParam,
|
|
||||||
typename TStringView = decltype(std::basic_string_view(std::declval<TParam&&>())),
|
|
||||||
typename TChar = typename TStringView::value_type,
|
|
||||||
typename TCharTraits = typename TStringView::traits_type,
|
|
||||||
typename TTraits = SplitLineTraitsCT<TChar, TLine, options, TCharTraits>>
|
|
||||||
[[nodiscard]]
|
|
||||||
auto splitLines(TParam&& stringView) MIJIN_NOEXCEPT -> LineRange<TChar, TLine, TTraits>
|
|
||||||
{
|
|
||||||
return LineRange<TChar, TLine, TTraits>(std::basic_string_view(std::forward<TParam>(stringView)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TParam, typename TLine = unsigned,
|
|
||||||
typename TStringView = decltype(std::basic_string_view(std::declval<TParam&&>())),
|
|
||||||
typename TChar = typename TStringView::value_type,
|
|
||||||
typename TCharTraits = typename TStringView::traits_type,
|
|
||||||
typename TTraits = SplitLineTraitsRT<TChar, TLine, TCharTraits>,
|
|
||||||
typename TTrimChars = TStringView>
|
|
||||||
[[nodiscard]]
|
|
||||||
auto splitLines(TParam&& stringView, bool ignoreEmpty, TTrimChars&& trimChars = {}) MIJIN_NOEXCEPT -> LineRange<TChar, TLine, TTraits>
|
|
||||||
{
|
|
||||||
return LineRange<TChar, TLine, TTraits>(std::basic_string_view(std::forward<TParam>(stringView)), TTraits{
|
|
||||||
.ignoreEmpty = ignoreEmpty,
|
|
||||||
.trimChars = std::basic_string_view<TChar, TCharTraits>(std::forward<TTrimChars>(trimChars))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TString, typename TChars>
|
template<typename TString, typename TChars>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trimPrefix(TString&& string, TChars&& chars)
|
auto trimPrefix(TString&& string, TChars&& chars)
|
||||||
@@ -720,7 +307,7 @@ template<typename TString>
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trimPrefix(TString&& string)
|
auto trimPrefix(TString&& string)
|
||||||
{
|
{
|
||||||
return trimPrefix(string, DEFAULT_TRIM_CHARS<str_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>
|
||||||
@@ -734,7 +321,7 @@ template<typename TString>
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trimSuffix(TString&& string)
|
auto trimSuffix(TString&& string)
|
||||||
{
|
{
|
||||||
return trimSuffix(string, DEFAULT_TRIM_CHARS<str_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>
|
||||||
@@ -748,7 +335,7 @@ template<typename TString>
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
auto trim(TString&& string)
|
auto trim(TString&& string)
|
||||||
{
|
{
|
||||||
return trim(string, DEFAULT_TRIM_CHARS<str_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>
|
||||||
@@ -864,12 +451,6 @@ inline auto findIgnoreCase(std::string_view haystack, std::string_view needle)
|
|||||||
return std::ranges::search(haystack, needle, &compareIgnoreCase<char>);
|
return std::ranges::search(haystack, needle, &compareIgnoreCase<char>);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
inline bool containsIgnoreCase(std::string_view haystack, std::string_view needle)
|
|
||||||
{
|
|
||||||
return findIgnoreCase(haystack, needle).begin() != haystack.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
inline bool startsWithIgnoreCase(std::string_view string, std::string_view part)
|
inline bool startsWithIgnoreCase(std::string_view string, std::string_view part)
|
||||||
{
|
{
|
||||||
@@ -906,6 +487,21 @@ auto operator|(TIterable&& iterable, const Join& joiner)
|
|||||||
}
|
}
|
||||||
} // namespace pipe
|
} // 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>
|
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
|
ConvertCharTypeResult convertCharType(const TFrom* chrFrom, std::size_t numFrom, TTo* outTo, std::size_t numTo, std::mbstate_t& mbstate) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
@@ -1026,54 +622,6 @@ bool convertStringType(const TFrom* strFrom, std::basic_string<TTo, TToTraits, T
|
|||||||
{
|
{
|
||||||
return convertStringType(std::basic_string_view<TFrom>(strFrom), outString);
|
return convertStringType(std::basic_string_view<TFrom>(strFrom), outString);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StringQuoteOptions
|
|
||||||
{
|
|
||||||
bool replaceNewlines = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<StringQuoteOptions options = {}, typename TChar, typename TTraits, typename TAlloc = MIJIN_DEFAULT_ALLOCATOR<TChar>>
|
|
||||||
std::basic_string<TChar, TTraits, TAlloc> quoted(std::basic_string_view<TChar, TTraits> input)
|
|
||||||
{
|
|
||||||
std::basic_string<TChar, TTraits> result;
|
|
||||||
result.reserve(input.size() + 2);
|
|
||||||
result.push_back(TChar('"'));
|
|
||||||
for (const TChar chr : input)
|
|
||||||
{
|
|
||||||
switch (chr)
|
|
||||||
{
|
|
||||||
case TChar('"'):
|
|
||||||
case TChar('\\'):
|
|
||||||
result.push_back(TChar('\\'));
|
|
||||||
break;
|
|
||||||
case TChar('\n'):
|
|
||||||
if constexpr (options.replaceNewlines)
|
|
||||||
{
|
|
||||||
result.push_back(TChar('\\'));
|
|
||||||
result.push_back(TChar('n'));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TChar('\r'):
|
|
||||||
if constexpr (options.replaceNewlines)
|
|
||||||
{
|
|
||||||
result.push_back(TChar('\\'));
|
|
||||||
result.push_back(TChar('r'));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
result.push_back(chr);
|
|
||||||
}
|
|
||||||
result.push_back(TChar('"'));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<StringQuoteOptions options = {}, typename TChar, typename TTraits, typename TAlloc>
|
|
||||||
std::basic_string<TChar, TTraits, TAlloc> quoted(const std::basic_string<TChar, TTraits, TAlloc>& input)
|
|
||||||
{
|
|
||||||
return quoted<options, TChar, TTraits, TAlloc>(std::basic_string_view(input));
|
|
||||||
}
|
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
#endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)
|
#endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)
|
||||||
|
|||||||
@@ -192,18 +192,6 @@ struct is_type_member<TElement, TCollection<Ts...>>
|
|||||||
template<typename TElement, typename TCollection>
|
template<typename TElement, typename TCollection>
|
||||||
constexpr bool is_type_member_v = is_type_member<TElement, TCollection>::value;
|
constexpr bool is_type_member_v = is_type_member<TElement, TCollection>::value;
|
||||||
|
|
||||||
template<typename T, typename TObject>
|
|
||||||
struct is_member_object_pointer_of : std::false_type {};
|
|
||||||
|
|
||||||
template<typename TMember, typename TObject>
|
|
||||||
struct is_member_object_pointer_of<TMember (TObject::*), TObject> : std::true_type {};
|
|
||||||
|
|
||||||
template<typename T, typename TObject>
|
|
||||||
inline constexpr bool is_member_object_pointer_of_v = is_member_object_pointer_of<T, TObject>::value;
|
|
||||||
|
|
||||||
template<typename T, typename TObject>
|
|
||||||
concept member_object_pointer_of = is_member_object_pointer_of_v<T, TObject>;
|
|
||||||
|
|
||||||
template<typename TFrom, typename TTo>
|
template<typename TFrom, typename TTo>
|
||||||
using copy_const_t = std::conditional_t<std::is_const_v<TFrom>, std::add_const_t<TTo>, std::remove_const_t<TTo>>;
|
using copy_const_t = std::conditional_t<std::is_const_v<TFrom>, std::add_const_t<TTo>, std::remove_const_t<TTo>>;
|
||||||
|
|
||||||
@@ -265,6 +253,42 @@ struct optional_base<T, false>
|
|||||||
template<typename T, bool enable>
|
template<typename T, bool enable>
|
||||||
using optional_base_t = optional_base<T, enable>::type;
|
using optional_base_t = optional_base<T, enable>::type;
|
||||||
|
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template<typename TFunc>
|
||||||
|
struct function_traits_base {};
|
||||||
|
|
||||||
|
template<typename TResult, typename... TParams>
|
||||||
|
struct function_traits_base<TResult (*)(TParams...)>
|
||||||
|
{
|
||||||
|
using result_t = TResult;
|
||||||
|
using params_t = std::tuple<TParams...>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TResult, typename TType, typename... TParams>
|
||||||
|
struct function_traits_base<TResult (TType::*)(TParams...)>
|
||||||
|
{
|
||||||
|
using result_t = TResult;
|
||||||
|
using params_t = std::tuple<TType*, TParams...>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TResult, typename TType, typename... TParams>
|
||||||
|
struct function_traits_base<TResult (TType::*)(TParams...) const>
|
||||||
|
{
|
||||||
|
using result_t = TResult;
|
||||||
|
using params_t = std::tuple<const TType*, TParams...>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TFunc>
|
||||||
|
struct function_traits : impl::function_traits_base<TFunc>
|
||||||
|
{
|
||||||
|
static constexpr std::size_t NUM_PARAMS = std::tuple_size_v<typename impl::function_traits_base<TFunc>::params_t>;
|
||||||
|
|
||||||
|
template<std::size_t pos>
|
||||||
|
using param_t = std::tuple_element_t<pos, typename impl::function_traits_base<TFunc>::params_t>;
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
@@ -300,19 +324,6 @@ static_assert(union_type<MyTemplate<int, int>, MyTemplate<double, double>, MyTem
|
|||||||
static_assert(!union_type<int*, int>);
|
static_assert(!union_type<int*, int>);
|
||||||
static_assert(union_type<int*, std::is_pointer<Type_>>);
|
static_assert(union_type<int*, std::is_pointer<Type_>>);
|
||||||
static_assert(!union_type<int, std::is_pointer<Type_>>);
|
static_assert(!union_type<int, std::is_pointer<Type_>>);
|
||||||
|
|
||||||
struct DetectNo {};
|
|
||||||
struct DetectYes {
|
|
||||||
using some_type = double;
|
|
||||||
};
|
|
||||||
template<typename T>
|
|
||||||
using detect_some_type = typename T::some_type;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using some_type = detect_or_t<int, detect_some_type, T>;
|
|
||||||
|
|
||||||
static_assert(std::is_same_v<some_type<DetectNo>, int>);
|
|
||||||
static_assert(std::is_same_v<some_type<DetectYes>, double>);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#if !defined(MIJIN_UTIL_VARIANT_HPP_INCLUDED)
|
#if !defined(MIJIN_UTIL_VARIANT_HPP_INCLUDED)
|
||||||
#define MIJIN_UTIL_VARIANT_HPP_INCLUDED 1
|
#define MIJIN_UTIL_VARIANT_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include "./traits.hpp"
|
#include "./traits.hpp"
|
||||||
|
|
||||||
@@ -23,10 +24,21 @@ inline constexpr bool variant_contains_v<TSearch, std::variant<TVariantTypes...>
|
|||||||
//
|
//
|
||||||
// public types
|
// public types
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename... TCallable>
|
template<typename... TCallable>
|
||||||
struct Visitor : TCallable ...
|
struct Visitor : TCallable ...
|
||||||
{
|
{
|
||||||
using TCallable::operator()...;
|
using TCallable::operator()...;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename TRet>
|
||||||
|
struct CastTo
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
TRet operator()(T&& arg) const
|
||||||
|
{
|
||||||
|
return static_cast<TRet>(std::forward<T>(arg));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
#endif // !defined(MIJIN_UTIL_VARIANT_HPP_INCLUDED)
|
#endif // !defined(MIJIN_UTIL_VARIANT_HPP_INCLUDED)
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#include "../platform/folders.hpp"
|
#include "../platform/folders.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -26,100 +30,91 @@ namespace mijin
|
|||||||
// internal functions
|
// internal functions
|
||||||
//
|
//
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
void doGetFileInfo(const fs::path& stlPath, FileInfo& outInfo)
|
||||||
|
{
|
||||||
|
std::error_code err;
|
||||||
|
|
||||||
|
outInfo.isFolder = fs::is_directory(stlPath, err);
|
||||||
|
outInfo.isSymlink = fs::is_symlink(stlPath, err);
|
||||||
|
outInfo.isSpecial = !outInfo.isFolder && !fs::is_regular_file(stlPath, err);
|
||||||
|
outInfo.isHidden = stlPath.c_str()[0] == '.'; // at least for Linux
|
||||||
|
if (outInfo.isFolder)
|
||||||
|
{
|
||||||
|
const fs::directory_iterator dirIt(stlPath, err);
|
||||||
|
if (err != std::error_code{})
|
||||||
|
{
|
||||||
|
outInfo.size = std::distance(dirIt, fs::directory_iterator());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outInfo.size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!outInfo.isSpecial)
|
||||||
|
{
|
||||||
|
outInfo.size = fs::file_size(stlPath, err);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
outInfo.size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
fs::path OSFileSystemAdapter::getHomeFolder()
|
std::vector<FolderEntry> OSFileSystemAdapter::listFiles(PathView folder)
|
||||||
{
|
{
|
||||||
return getKnownFolder(KnownFolder::USER_HOME);
|
std::vector<FolderEntry> entries;
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<FileInfo> OSFileSystemAdapter::listFiles(const fs::path& folder)
|
|
||||||
{
|
|
||||||
std::vector<FileInfo> entries;
|
|
||||||
std::error_code err;
|
std::error_code err;
|
||||||
const fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err);
|
|
||||||
|
const fs::path stlFolder(folder.stringView());
|
||||||
|
const fs::directory_iterator iterator(stlFolder, fs::directory_options::skip_permission_denied, err);
|
||||||
if (err) {
|
if (err) {
|
||||||
return {}; // TODO: propagate?
|
return {}; // TODO: propagate?
|
||||||
}
|
}
|
||||||
for (const fs::directory_entry& entry : iterator)
|
for (const fs::directory_entry& stlEntry : iterator)
|
||||||
{
|
{
|
||||||
FileInfo& info = entries.emplace_back();
|
FolderEntry& entry = entries.emplace_back();
|
||||||
info.path = entry.path();
|
entry.path = stlEntry.path().generic_string();
|
||||||
info.exists = true;
|
entry.info.exists = true;
|
||||||
info.isFolder = entry.is_directory(err);
|
doGetFileInfo(stlEntry.path(), entry.info);
|
||||||
info.isSymlink = entry.is_symlink(err);
|
|
||||||
info.isSpecial = !info.isFolder && !entry.is_regular_file(err);
|
|
||||||
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
|
|
||||||
if (info.isFolder)
|
|
||||||
{
|
|
||||||
std::error_code errorCode;
|
|
||||||
fs::directory_iterator dirIt(info.path, errorCode);
|
|
||||||
if (errorCode != std::error_code{})
|
|
||||||
{
|
|
||||||
info.size = std::distance(dirIt, fs::directory_iterator());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
info.size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!info.isSpecial)
|
|
||||||
{
|
|
||||||
info.size = entry.file_size(err);
|
|
||||||
if (err)
|
|
||||||
{
|
|
||||||
info.size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file)
|
FileInfo OSFileSystemAdapter::getFileInfo(PathView file)
|
||||||
{
|
{
|
||||||
|
const fs::path stlFile(file.stringView());
|
||||||
|
|
||||||
FileInfo info = {};
|
FileInfo info = {};
|
||||||
std::error_code err;
|
std::error_code err;
|
||||||
info.path = file;
|
info.exists = fs::exists(stlFile, err);
|
||||||
info.exists = fs::exists(file, err);
|
|
||||||
if (info.exists)
|
if (info.exists)
|
||||||
{
|
{
|
||||||
info.isFolder = fs::is_directory(file, err);
|
doGetFileInfo(fs::path(file.stringView()), info);
|
||||||
info.isSymlink = fs::is_symlink(file, err);
|
|
||||||
info.isSpecial = !info.isFolder && !fs::is_regular_file(file, err);
|
|
||||||
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
|
|
||||||
if (info.isFolder) {
|
|
||||||
MIJIN_TRY
|
|
||||||
{
|
|
||||||
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
|
|
||||||
}
|
|
||||||
MIJIN_CATCH(std::runtime_error&)
|
|
||||||
{
|
|
||||||
info.size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!info.isSpecial)
|
|
||||||
{
|
|
||||||
info.size = fs::file_size(file, err);
|
|
||||||
if (err) {
|
|
||||||
info.size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<fs::path> OSFileSystemAdapter::getNativePath(const fs::path& file)
|
Optional<NativePath> OSFileSystemAdapter::getNativePath(PathView file)
|
||||||
{
|
{
|
||||||
return file;
|
return NativePath(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
StreamError OSFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||||
{
|
{
|
||||||
const std::string pathStr = path.string();
|
const PathView::string_view_t pathSv = path.stringView();
|
||||||
|
char* pathStr = static_cast<char*>(alloca(pathSv.size() + 1));
|
||||||
|
std::memcpy(pathStr, pathSv.data(), pathSv.size());
|
||||||
|
pathStr[pathSv.size()] = '\0';
|
||||||
|
|
||||||
auto stream = std::make_unique<FileStream>();
|
auto stream = std::make_unique<FileStream>();
|
||||||
const StreamError error = stream->open(pathStr.c_str(), mode);
|
const StreamError error = stream->open(pathStr, mode);
|
||||||
if (error != StreamError::SUCCESS) {
|
if (error != StreamError::SUCCESS) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,17 +6,15 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <filesystem>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "../container/optional.hpp"
|
#include "../container/optional.hpp"
|
||||||
#include "../io/stream.hpp"
|
#include "../io/stream.hpp"
|
||||||
#include "../internal/common.hpp"
|
#include "../internal/common.hpp"
|
||||||
|
#include "../types/path.hpp"
|
||||||
#include "../util/hash.hpp"
|
#include "../util/hash.hpp"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -34,7 +32,6 @@ namespace mijin
|
|||||||
|
|
||||||
struct FileInfo
|
struct FileInfo
|
||||||
{
|
{
|
||||||
fs::path path;
|
|
||||||
/// either file size in bytes, or number of entries if folder
|
/// either file size in bytes, or number of entries if folder
|
||||||
std::size_t size = 0;
|
std::size_t size = 0;
|
||||||
bool exists : 1 = false;
|
bool exists : 1 = false;
|
||||||
@@ -44,17 +41,23 @@ struct FileInfo
|
|||||||
bool isHidden : 1 = false;
|
bool isHidden : 1 = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FolderEntry
|
||||||
|
{
|
||||||
|
Path path;
|
||||||
|
FileInfo info;
|
||||||
|
};
|
||||||
|
|
||||||
// basically just a thin wrapper around adapter + path, for utility
|
// basically just a thin wrapper around adapter + path, for utility
|
||||||
class PathReference
|
class PathReference
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
class FileSystemAdapter* adapter_ = nullptr;
|
class FileSystemAdapter* adapter_ = nullptr;
|
||||||
fs::path path_ = {};
|
Path path_ = {};
|
||||||
public:
|
public:
|
||||||
PathReference() = default;
|
PathReference() = default;
|
||||||
PathReference(const PathReference&) = default;
|
PathReference(const PathReference&) = default;
|
||||||
PathReference(PathReference&&) = default;
|
PathReference(PathReference&&) = default;
|
||||||
PathReference(class FileSystemAdapter* adapter, fs::path path) MIJIN_NOEXCEPT : adapter_(adapter), path_(std::move(path)) {}
|
PathReference(class FileSystemAdapter* adapter, Path path) MIJIN_NOEXCEPT : adapter_(adapter), path_(std::move(path)) {}
|
||||||
|
|
||||||
PathReference& operator=(const PathReference&) = default;
|
PathReference& operator=(const PathReference&) = default;
|
||||||
PathReference& operator=(PathReference&&) MIJIN_NOEXCEPT = default;
|
PathReference& operator=(PathReference&&) MIJIN_NOEXCEPT = default;
|
||||||
@@ -63,13 +66,13 @@ public:
|
|||||||
|
|
||||||
[[nodiscard]] bool empty() const MIJIN_NOEXCEPT { return adapter_ == nullptr; }
|
[[nodiscard]] bool empty() const MIJIN_NOEXCEPT { return adapter_ == nullptr; }
|
||||||
[[nodiscard]] class FileSystemAdapter* getAdapter() const noexcept { return adapter_; }
|
[[nodiscard]] class FileSystemAdapter* getAdapter() const noexcept { return adapter_; }
|
||||||
[[nodiscard]] const fs::path& getPath() const MIJIN_NOEXCEPT { return path_; }
|
[[nodiscard]] const Path& getPath() const MIJIN_NOEXCEPT { return path_; }
|
||||||
[[nodiscard]] inline FileInfo getInfo() const;
|
[[nodiscard]] inline FileInfo getInfo() const;
|
||||||
[[nodiscard]] inline Optional<fs::path> getNativePath() const;
|
[[nodiscard]] inline Optional<NativePath> getNativePath() const;
|
||||||
[[nodiscard]] inline StreamError open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const;
|
[[nodiscard]] inline StreamError open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
PathReference operator/(const fs::path& more) const
|
PathReference operator/(const Path& more) const
|
||||||
{
|
{
|
||||||
return PathReference(adapter_, path_ / more);
|
return PathReference(adapter_, path_ / more);
|
||||||
}
|
}
|
||||||
@@ -80,16 +83,14 @@ class FileSystemAdapter
|
|||||||
public:
|
public:
|
||||||
virtual ~FileSystemAdapter() = default;
|
virtual ~FileSystemAdapter() = default;
|
||||||
|
|
||||||
[[deprecated("Will be removed ASAP, use getKnownFolder(KnownFolder::USER_HOME) from platform/folders.hpp instead.")]]
|
[[nodiscard]] virtual std::vector<FolderEntry> listFiles(PathView folder) = 0;
|
||||||
[[nodiscard]] virtual fs::path getHomeFolder() { return {}; } // TODO: get rid of this ...
|
[[nodiscard]] virtual FileInfo getFileInfo(PathView file) = 0;
|
||||||
[[nodiscard]] virtual std::vector<FileInfo> listFiles(const fs::path& folder) = 0;
|
[[nodiscard]] virtual Optional<NativePath> getNativePath(PathView /* file */) { return NULL_OPTIONAL; }
|
||||||
[[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0;
|
[[nodiscard]] virtual StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) = 0;
|
||||||
[[nodiscard]] virtual Optional<fs::path> getNativePath(const fs::path& /* file */) { return NULL_OPTIONAL; }
|
virtual void getAllPaths(PathView path, std::vector<PathReference>& outPaths) { outPaths.push_back(getPath(Path(path))); }
|
||||||
[[nodiscard]] virtual StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) = 0;
|
|
||||||
virtual void getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths) { outPaths.push_back(getPath(path)); }
|
|
||||||
|
|
||||||
[[nodiscard]] PathReference getPath(fs::path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); }
|
[[nodiscard]] PathReference getPath(Path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); }
|
||||||
[[nodiscard]] std::vector<PathReference> getAllPaths(const fs::path& path)
|
[[nodiscard]] std::vector<PathReference> getAllPaths(PathView path)
|
||||||
{
|
{
|
||||||
std::vector<PathReference> paths;
|
std::vector<PathReference> paths;
|
||||||
getAllPaths(path, paths);
|
getAllPaths(path, paths);
|
||||||
@@ -100,11 +101,10 @@ public:
|
|||||||
class OSFileSystemAdapter : public FileSystemAdapter
|
class OSFileSystemAdapter : public FileSystemAdapter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
fs::path getHomeFolder() override;
|
std::vector<FolderEntry> listFiles(PathView folder) override;
|
||||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
FileInfo getFileInfo(PathView file) override;
|
||||||
FileInfo getFileInfo(const fs::path& file) override;
|
Optional<NativePath> getNativePath(PathView file) override;
|
||||||
Optional<fs::path> getNativePath(const fs::path& file) override;
|
StreamError open(PathView, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
||||||
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
|
||||||
|
|
||||||
static OSFileSystemAdapter& getInstance();
|
static OSFileSystemAdapter& getInstance();
|
||||||
};
|
};
|
||||||
@@ -118,7 +118,7 @@ inline FileInfo PathReference::getInfo() const
|
|||||||
return adapter_->getFileInfo(path_);
|
return adapter_->getFileInfo(path_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<fs::path> PathReference::getNativePath() const
|
Optional<NativePath> PathReference::getNativePath() const
|
||||||
{
|
{
|
||||||
return adapter_->getNativePath(path_);
|
return adapter_->getNativePath(path_);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,31 @@
|
|||||||
|
|
||||||
namespace mijin
|
namespace mijin
|
||||||
{
|
{
|
||||||
std::vector<FileInfo> MemoryFileSystemAdapter::listFiles(const fs::path& folder)
|
std::vector<FolderEntry> MemoryFileSystemAdapter::listFiles(PathView folder)
|
||||||
{
|
{
|
||||||
const detail::MemoryFolder* folderObj = findFolder(folder);
|
const detail::MemoryFolder* folderObj = findFolder(folder);
|
||||||
if (folderObj == nullptr)
|
if (folderObj == nullptr)
|
||||||
{
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
std::vector<FileInfo> result;
|
std::vector<FolderEntry> result;
|
||||||
result.reserve(folderObj->folders.size() + folderObj->files.size());
|
result.reserve(folderObj->folders.size() + folderObj->files.size());
|
||||||
|
|
||||||
|
const Path folderPath(folder);
|
||||||
for (const auto& [name, subFolder] : folderObj->folders)
|
for (const auto& [name, subFolder] : folderObj->folders)
|
||||||
{
|
{
|
||||||
result.push_back(folderInfo(folder / name, subFolder));
|
result.emplace_back(folderPath / name, folderInfo(subFolder));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& [name, file] : folderObj->files)
|
for (const auto& [name, file] : folderObj->files)
|
||||||
{
|
{
|
||||||
result.push_back(fileInfo(folder / name, file));
|
result.emplace_back(folderPath / name, fileInfo(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file)
|
FileInfo MemoryFileSystemAdapter::getFileInfo(PathView file)
|
||||||
{
|
{
|
||||||
#if 0 // shouldn't be necessary
|
#if 0 // shouldn't be necessary
|
||||||
// empty means root
|
// empty means root
|
||||||
@@ -43,38 +44,38 @@ FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const detail::MemoryFolder* folderObj = findFolder(file.parent_path());
|
const detail::MemoryFolder* folderObj = findFolder(file.getParent());
|
||||||
if (folderObj == nullptr)
|
if (folderObj == nullptr)
|
||||||
{
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
const std::string filename = file.filename().generic_string();
|
const std::string_view filename = file.getName();
|
||||||
|
|
||||||
if (auto itFolder = folderObj->folders.find(filename); itFolder != folderObj->folders.end())
|
if (auto itFolder = folderObj->folders.find(filename); itFolder != folderObj->folders.end())
|
||||||
{
|
{
|
||||||
return folderInfo(file, itFolder->second);
|
return folderInfo(itFolder->second);
|
||||||
}
|
}
|
||||||
if (auto itFile = folderObj->files.find(filename); itFile != folderObj->files.end())
|
if (auto itFile = folderObj->files.find(filename); itFile != folderObj->files.end())
|
||||||
{
|
{
|
||||||
return fileInfo(file, itFile->second);
|
return fileInfo(itFile->second);
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
StreamError MemoryFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||||
{
|
{
|
||||||
if (mode != FileOpenMode::READ)
|
if (mode != FileOpenMode::READ)
|
||||||
{
|
{
|
||||||
return StreamError::IO_ERROR;
|
return StreamError::IO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::MemoryFolder* folderObj = findFolder(path.parent_path());
|
const detail::MemoryFolder* folderObj = findFolder(path.getParent());
|
||||||
if (folderObj == nullptr)
|
if (folderObj == nullptr)
|
||||||
{
|
{
|
||||||
return StreamError::IO_ERROR;
|
return StreamError::IO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto itFile = folderObj->files.find(path.filename().generic_string());
|
auto itFile = folderObj->files.find(path.getName());
|
||||||
if (itFile == folderObj->files.end())
|
if (itFile == folderObj->files.end())
|
||||||
{
|
{
|
||||||
return StreamError::IO_ERROR;
|
return StreamError::IO_ERROR;
|
||||||
@@ -87,10 +88,10 @@ StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mod
|
|||||||
return StreamError::SUCCESS;
|
return StreamError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span<const std::uint8_t> data, Overwrite overwrite, CopyData copyData)
|
bool MemoryFileSystemAdapter::addFile(PathView path, std::span<const std::uint8_t> data, Overwrite overwrite, CopyData copyData)
|
||||||
{
|
{
|
||||||
detail::MemoryFolder& folder = *findFolder(path.parent_path(), true);
|
detail::MemoryFolder& folder = *findFolder(path.getParent(), true);
|
||||||
std::string filename = path.filename().generic_string();
|
const std::string_view filename = path.getName();
|
||||||
|
|
||||||
if (folder.folders.contains(filename))
|
if (folder.folders.contains(filename))
|
||||||
{
|
{
|
||||||
@@ -107,29 +108,28 @@ bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span<const std:
|
|||||||
data = fileData_.emplace_back(data.begin(), data.end());
|
data = fileData_.emplace_back(data.begin(), data.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
folder.files.emplace(std::move(filename), detail::MemoryFile{.data = data});
|
folder.files.emplace(filename, detail::MemoryFile{.data = data});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemoryFileSystemAdapter::addFolder(const fs::path& path)
|
void MemoryFileSystemAdapter::addFolder(PathView path)
|
||||||
{
|
{
|
||||||
(void) findFolder(path, true);
|
(void) findFolder(path, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path, bool create) MIJIN_NOEXCEPT
|
detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(PathView path, bool create) MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
detail::MemoryFolder* folder = &root_;
|
detail::MemoryFolder* folder = &root_;
|
||||||
for (const fs::path& part : path)
|
for (const std::string_view part : path)
|
||||||
{
|
{
|
||||||
std::string partname = part.generic_string();
|
auto it = folder->folders.find(part);
|
||||||
auto it = folder->folders.find(partname);
|
|
||||||
if (it == folder->folders.end())
|
if (it == folder->folders.end())
|
||||||
{
|
{
|
||||||
if (!create)
|
if (!create)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
folder = &folder->folders[std::move(partname)];
|
folder = &folder->folders[std::string(part)];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -139,20 +139,18 @@ detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path,
|
|||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
|
FileInfo MemoryFileSystemAdapter::folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
.path = path,
|
|
||||||
.size = folder.folders.size() + folder.files.size(),
|
.size = folder.folders.size() + folder.files.size(),
|
||||||
.exists = true,
|
.exists = true,
|
||||||
.isFolder = true
|
.isFolder = true
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo MemoryFileSystemAdapter::fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT
|
FileInfo MemoryFileSystemAdapter::fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
.path = path,
|
|
||||||
.size = file.data.size(),
|
.size = file.data.size(),
|
||||||
.exists = true
|
.exists = true
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -35,20 +35,20 @@ private:
|
|||||||
detail::MemoryFolder root_;
|
detail::MemoryFolder root_;
|
||||||
std::vector<std::vector<std::uint8_t>> fileData_;
|
std::vector<std::vector<std::uint8_t>> fileData_;
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
[[nodiscard]] std::vector<FolderEntry> listFiles(PathView folder) override;
|
||||||
[[nodiscard]] FileInfo getFileInfo(const fs::path& file) override;
|
[[nodiscard]] FileInfo getFileInfo(PathView file) override;
|
||||||
[[nodiscard]] StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
[[nodiscard]] StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
||||||
|
|
||||||
bool addFile(const fs::path& path, std::span<const std::uint8_t> data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO);
|
bool addFile(PathView path, std::span<const std::uint8_t> data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO);
|
||||||
bool addFile(const fs::path& path, std::span<const std::uint8_t> data, CopyData copyData)
|
bool addFile(PathView path, std::span<const std::uint8_t> data, CopyData copyData)
|
||||||
{
|
{
|
||||||
return addFile(path, data, Overwrite::NO, copyData);
|
return addFile(path, data, Overwrite::NO, copyData);
|
||||||
}
|
}
|
||||||
void addFolder(const fs::path& path);
|
void addFolder(PathView path);
|
||||||
private:
|
private:
|
||||||
detail::MemoryFolder* findFolder(const fs::path& path, bool create = false) MIJIN_NOEXCEPT;
|
detail::MemoryFolder* findFolder(PathView path, bool create = false) MIJIN_NOEXCEPT;
|
||||||
FileInfo folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT;
|
FileInfo folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT;
|
||||||
FileInfo fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT;
|
FileInfo fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT;
|
||||||
};
|
};
|
||||||
} // namespace mijin
|
} // namespace mijin
|
||||||
|
|
||||||
|
|||||||
@@ -27,10 +27,10 @@ class RelativeFileSystemAdapter : public FileSystemAdapter
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
TWrapped wrapped_;
|
TWrapped wrapped_;
|
||||||
fs::path root_;
|
Path root_;
|
||||||
public:
|
public:
|
||||||
template<typename... TArgs>
|
template<typename... TArgs>
|
||||||
explicit RelativeFileSystemAdapter(fs::path root, TArgs&&... args)
|
explicit RelativeFileSystemAdapter(Path root, TArgs&&... args)
|
||||||
: wrapped_(std::forward<TArgs>(args)...), root_(std::move(root)) {}
|
: wrapped_(std::forward<TArgs>(args)...), root_(std::move(root)) {}
|
||||||
RelativeFileSystemAdapter(const RelativeFileSystemAdapter&) = default;
|
RelativeFileSystemAdapter(const RelativeFileSystemAdapter&) = default;
|
||||||
RelativeFileSystemAdapter(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
RelativeFileSystemAdapter(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
||||||
@@ -38,13 +38,12 @@ public:
|
|||||||
RelativeFileSystemAdapter& operator=(const RelativeFileSystemAdapter&) = default;
|
RelativeFileSystemAdapter& operator=(const RelativeFileSystemAdapter&) = default;
|
||||||
RelativeFileSystemAdapter& operator=(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
RelativeFileSystemAdapter& operator=(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
||||||
|
|
||||||
fs::path getHomeFolder() override;
|
std::vector<FolderEntry> listFiles(PathView folder) override;
|
||||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
FileInfo getFileInfo(PathView file) override;
|
||||||
FileInfo getFileInfo(const fs::path& file) override;
|
Optional<NativePath> getNativePath(PathView file) override;
|
||||||
Optional<fs::path> getNativePath(const fs::path& file) override;
|
StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
||||||
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
|
||||||
private:
|
private:
|
||||||
fs::path appendPath(const fs::path& other) const MIJIN_NOEXCEPT;
|
Path appendPath(PathView other) const MIJIN_NOEXCEPT;
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -52,52 +51,45 @@ private:
|
|||||||
//
|
//
|
||||||
|
|
||||||
template<typename TWrapped>
|
template<typename TWrapped>
|
||||||
fs::path RelativeFileSystemAdapter<TWrapped>::getHomeFolder()
|
std::vector<FolderEntry> RelativeFileSystemAdapter<TWrapped>::listFiles(PathView folder)
|
||||||
{
|
{
|
||||||
return root_;
|
std::vector<FolderEntry> result;
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TWrapped>
|
|
||||||
std::vector<FileInfo> RelativeFileSystemAdapter<TWrapped>::listFiles(const fs::path& folder)
|
|
||||||
{
|
|
||||||
std::vector<FileInfo> result;
|
|
||||||
|
|
||||||
result = wrapped_.listFiles(appendPath(folder));
|
result = wrapped_.listFiles(appendPath(folder));
|
||||||
for (FileInfo& fileInfo : result) {
|
for (FolderEntry& fileInfo : result) {
|
||||||
fileInfo.path = "/" / fileInfo.path.lexically_relative(root_);
|
fileInfo.path = Path("/") / fileInfo.path.stringView().substr(root_.size());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TWrapped>
|
template<typename TWrapped>
|
||||||
FileInfo RelativeFileSystemAdapter<TWrapped>::getFileInfo(const fs::path& file)
|
FileInfo RelativeFileSystemAdapter<TWrapped>::getFileInfo(PathView file)
|
||||||
{
|
{
|
||||||
return wrapped_.getFileInfo(appendPath(file));
|
return wrapped_.getFileInfo(appendPath(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TWrapped>
|
template<typename TWrapped>
|
||||||
Optional<fs::path> RelativeFileSystemAdapter<TWrapped>::getNativePath(const fs::path& file)
|
Optional<NativePath> RelativeFileSystemAdapter<TWrapped>::getNativePath(PathView file)
|
||||||
{
|
{
|
||||||
return wrapped_.getNativePath(appendPath(file));
|
return wrapped_.getNativePath(appendPath(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TWrapped>
|
template<typename TWrapped>
|
||||||
StreamError RelativeFileSystemAdapter<TWrapped>::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
StreamError RelativeFileSystemAdapter<TWrapped>::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||||
{
|
{
|
||||||
return wrapped_.open(appendPath(path), mode, outStream);
|
return wrapped_.open(appendPath(path), mode, outStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TWrapped>
|
template<typename TWrapped>
|
||||||
fs::path RelativeFileSystemAdapter<TWrapped>::appendPath(const fs::path& other) const MIJIN_NOEXCEPT
|
Path RelativeFileSystemAdapter<TWrapped>::appendPath(PathView other) const MIJIN_NOEXCEPT
|
||||||
{
|
{
|
||||||
fs::path combinedPath = root_;
|
Path combinedPath = root_;
|
||||||
if (other.is_absolute()) {
|
std::string_view otherSv(other.stringView());
|
||||||
combinedPath += other;
|
if (!otherSv.empty() && otherSv[0] == Path::SEPARATOR) {
|
||||||
|
otherSv = otherSv.substr(1);
|
||||||
}
|
}
|
||||||
else {
|
combinedPath /= otherSv;
|
||||||
combinedPath /= other;
|
return combinedPath;
|
||||||
}
|
|
||||||
return combinedPath.lexically_normal();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace vfs_pipe
|
namespace vfs_pipe
|
||||||
@@ -105,7 +97,7 @@ namespace vfs_pipe
|
|||||||
template<typename TBase>
|
template<typename TBase>
|
||||||
struct RelativeBuilder : Builder<RelativeBuilder<TBase>, RelativeFileSystemAdapter<typename TBase::adapter_t>>
|
struct RelativeBuilder : Builder<RelativeBuilder<TBase>, RelativeFileSystemAdapter<typename TBase::adapter_t>>
|
||||||
{
|
{
|
||||||
fs::path root;
|
Path root;
|
||||||
TBase base;
|
TBase base;
|
||||||
|
|
||||||
std::unique_ptr<RelativeFileSystemAdapter<typename TBase::adapter_t>> build()
|
std::unique_ptr<RelativeFileSystemAdapter<typename TBase::adapter_t>> build()
|
||||||
@@ -116,11 +108,11 @@ struct RelativeBuilder : Builder<RelativeBuilder<TBase>, RelativeFileSystemAdapt
|
|||||||
|
|
||||||
struct RelativeOptions
|
struct RelativeOptions
|
||||||
{
|
{
|
||||||
fs::path root;
|
Path root;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
inline RelativeOptions relative_to(fs::path root) noexcept
|
inline RelativeOptions relative_to(Path root) noexcept
|
||||||
{
|
{
|
||||||
return {.root = std::move(root) };
|
return {.root = std::move(root) };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,40 +31,20 @@ namespace mijin
|
|||||||
// public functions
|
// public functions
|
||||||
//
|
//
|
||||||
|
|
||||||
fs::path StackedFileSystemAdapter::getHomeFolder()
|
std::vector<FolderEntry> StackedFileSystemAdapter::listFiles(PathView folder)
|
||||||
{
|
{
|
||||||
if (adapters_.empty()) {
|
std::vector<FolderEntry> files;
|
||||||
return fs::path();
|
|
||||||
}
|
|
||||||
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
|
||||||
#pragma warning(push)
|
|
||||||
#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
|
|
||||||
return adapters_.front()->getHomeFolder();
|
|
||||||
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
|
|
||||||
#pragma warning(pop)
|
|
||||||
#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<FileInfo> StackedFileSystemAdapter::listFiles(const fs::path& folder)
|
|
||||||
{
|
|
||||||
std::vector<FileInfo> files;
|
|
||||||
|
|
||||||
for (auto& adapter : adapters_)
|
for (auto& adapter : adapters_)
|
||||||
{
|
{
|
||||||
for (const FileInfo& file : adapter->listFiles(folder))
|
for (const FolderEntry& entry : adapter->listFiles(folder))
|
||||||
{
|
{
|
||||||
auto it = std::find_if(files.begin(), files.end(), [&](const FileInfo& existing)
|
auto it = std::ranges::find_if(files, [&](const FolderEntry& existing)
|
||||||
{
|
{
|
||||||
return existing.path == file.path;
|
return existing.path == entry.path;
|
||||||
});
|
});
|
||||||
if (it == files.end()) {
|
if (it == files.end()) {
|
||||||
files.push_back(file);
|
files.push_back(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,7 +52,7 @@ std::vector<FileInfo> StackedFileSystemAdapter::listFiles(const fs::path& folder
|
|||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file)
|
FileInfo StackedFileSystemAdapter::getFileInfo(PathView file)
|
||||||
{
|
{
|
||||||
for (auto& adapter : adapters_)
|
for (auto& adapter : adapters_)
|
||||||
{
|
{
|
||||||
@@ -85,11 +65,11 @@ FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file)
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
|
Optional<NativePath> StackedFileSystemAdapter::getNativePath(PathView file)
|
||||||
{
|
{
|
||||||
for (auto& adapter : adapters_)
|
for (auto& adapter : adapters_)
|
||||||
{
|
{
|
||||||
Optional<fs::path> result = adapter->getNativePath(file);
|
Optional<NativePath> result = adapter->getNativePath(file);
|
||||||
if (!result.empty())
|
if (!result.empty())
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
@@ -98,7 +78,7 @@ Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
|
|||||||
return NULL_OPTIONAL;
|
return NULL_OPTIONAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
StreamError StackedFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||||
{
|
{
|
||||||
// try to open existing files first
|
// try to open existing files first
|
||||||
for (auto& adapter : adapters_)
|
for (auto& adapter : adapters_)
|
||||||
@@ -125,7 +105,7 @@ StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mo
|
|||||||
return StreamError::IO_ERROR;
|
return StreamError::IO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StackedFileSystemAdapter::getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths)
|
void StackedFileSystemAdapter::getAllPaths(PathView path, std::vector<PathReference>& outPaths)
|
||||||
{
|
{
|
||||||
for (auto& adapter : adapters_)
|
for (auto& adapter : adapters_)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,12 +28,11 @@ class StackedFileSystemAdapter : public FileSystemAdapter
|
|||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<FileSystemAdapter>> adapters_;
|
std::vector<std::unique_ptr<FileSystemAdapter>> adapters_;
|
||||||
public:
|
public:
|
||||||
fs::path getHomeFolder() override;
|
std::vector<FolderEntry> listFiles(PathView folder) override;
|
||||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
FileInfo getFileInfo(PathView file) override;
|
||||||
FileInfo getFileInfo(const fs::path& file) override;
|
Optional<NativePath> getNativePath(PathView file) override;
|
||||||
Optional<fs::path> getNativePath(const fs::path& file) override;
|
StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
||||||
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
void getAllPaths(PathView path, std::vector<PathReference> &outPaths) override;
|
||||||
void getAllPaths(const fs::path &path, std::vector<PathReference> &outPaths) override;
|
|
||||||
using FileSystemAdapter::getAllPaths;
|
using FileSystemAdapter::getAllPaths;
|
||||||
|
|
||||||
inline FileSystemAdapter* addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) {
|
inline FileSystemAdapter* addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) {
|
||||||
|
|||||||
Reference in New Issue
Block a user