Compare commits
38 Commits
1d32530be7
...
feature/pr
| Author | SHA1 | Date | |
|---|---|---|---|
| eda08e509e | |||
| 1c7f043e6f | |||
|
|
9ae424e968 | ||
|
|
5a111df9ea | ||
|
|
32ccaad00a | ||
|
|
10d9b4c98f | ||
|
|
cc20702249 | ||
|
|
7d6fcc60fc | ||
| 3891c0f8ce | |||
| 7da2f7b7f4 | |||
| bd06118b29 | |||
|
|
4d19752964 | ||
|
|
0e988a4d9e | ||
|
|
a95885880f | ||
|
|
d76e64c062 | ||
|
|
e704c082b7 | ||
|
|
b44d6feb97 | ||
|
|
e91184ec82 | ||
|
|
51092bb4cb | ||
|
|
02e99bbc82 | ||
|
|
ad627b7c70 | ||
|
|
4a9a60c7f5 | ||
|
|
d8b03893b3 | ||
|
|
7939d458b3 | ||
|
|
b5546067a8 | ||
| 9e572061da | |||
| d0be5f7739 | |||
| e6556c6f90 | |||
| addae96c91 | |||
| cf860392a5 | |||
| 8ad34427f3 | |||
| 59780ed6de | |||
| 888b0a16f7 | |||
| d29a025ec5 | |||
| 40c0d888e6 | |||
| bf53622b19 | |||
| 6090d5fc74 | |||
| a1d7a63aba |
@@ -5,10 +5,6 @@
|
||||
#define MIJIN_ASYNC_COROUTINE_HPP_INCLUDED 1
|
||||
|
||||
|
||||
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
|
||||
# define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0 // Capture stack each time a coroutine is started. Warning, expensive! // TODO: maybe implement a lighter version only storing the return address?
|
||||
#endif
|
||||
|
||||
#include <any>
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
@@ -25,7 +21,17 @@
|
||||
#include "../util/flag.hpp"
|
||||
#include "../util/iterators.hpp"
|
||||
#include "../util/misc.hpp"
|
||||
#include "../util/scope_guard.hpp"
|
||||
#include "../util/traits.hpp"
|
||||
|
||||
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
|
||||
# define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0 // Capture stack each time a coroutine is started. Warning, expensive! // TODO: maybe implement a lighter version only storing the return address?
|
||||
#endif
|
||||
|
||||
#if !defined(MIJIN_COROUTINE_ENABLE_EXCEPTIONS)
|
||||
# define MIJIN_COROUTINE_ENABLE_EXCEPTIONS 0
|
||||
#endif
|
||||
|
||||
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
|
||||
#include "../debug/stacktrace.hpp"
|
||||
#endif
|
||||
@@ -185,10 +191,16 @@ struct TaskReturn<void, TPromise>
|
||||
};
|
||||
}
|
||||
|
||||
template<typename TValue>
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using TaskFuture = Future<TValue, TAllocator, MIJIN_COROUTINE_ENABLE_EXCEPTIONS>;
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using TaskFuturePtr = FuturePtr<TValue, TAllocator, MIJIN_COROUTINE_ENABLE_EXCEPTIONS>;
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator>
|
||||
struct TaskAwaitableFuture
|
||||
{
|
||||
FuturePtr<TValue> future;
|
||||
TaskFuturePtr<TValue, TAllocator> future;
|
||||
|
||||
[[nodiscard]] constexpr bool await_ready() const MIJIN_NOEXCEPT { return future->ready(); }
|
||||
constexpr void await_suspend(std::coroutine_handle<>) const MIJIN_NOEXCEPT {}
|
||||
@@ -328,12 +340,12 @@ struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TT
|
||||
|
||||
// constexpr void unhandled_exception() MIJIN_NOEXCEPT {}
|
||||
|
||||
template<typename TValue>
|
||||
auto await_transform(FuturePtr<TValue> future, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||
template<typename TValue, template<typename> typename TAllocator2>
|
||||
auto await_transform(TaskFuturePtr<TValue, TAllocator2> future, std::source_location sourceLoc = std::source_location::current()) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(loop_ != nullptr, "Cannot await future outside of a loop!");
|
||||
sharedState_->sourceLoc = std::move(sourceLoc);
|
||||
TaskAwaitableFuture<TValue> awaitable{future};
|
||||
TaskAwaitableFuture<TValue, TAllocator> awaitable{future};
|
||||
if (!awaitable.await_ready())
|
||||
{
|
||||
state_.status = TaskStatus::WAITING;
|
||||
@@ -634,10 +646,10 @@ public:
|
||||
void setUncaughtExceptionHandler(exception_handler_t handler) MIJIN_NOEXCEPT { uncaughtExceptionHandler_ = std::move(handler); }
|
||||
|
||||
template<typename TResult>
|
||||
FuturePtr<TResult> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
||||
TaskFuturePtr<TResult, TAllocator> addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT;
|
||||
|
||||
template<typename TResult>
|
||||
FuturePtr<TResult> addTask(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle = nullptr) MIJIN_NOEXCEPT
|
||||
TaskFuturePtr<TResult, TAllocator> addTask(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle = nullptr) MIJIN_NOEXCEPT
|
||||
{
|
||||
static_assert(TaskAllocatorTraits<TAllocator>::default_is_valid_v, "Allocator is not valid when default constructed, use makeTask() instead.");
|
||||
return addTaskImpl(std::move(task), outHandle);
|
||||
@@ -663,7 +675,7 @@ protected:
|
||||
protected:
|
||||
static inline TaskLoop*& currentLoopStorage() MIJIN_NOEXCEPT;
|
||||
template<typename TResult>
|
||||
static inline void setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT;
|
||||
static inline void setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT_IF(!MIJIN_COROUTINE_ENABLE_EXCEPTIONS);
|
||||
};
|
||||
|
||||
template<typename TResult = void>
|
||||
@@ -779,7 +791,7 @@ void TaskHandle::cancel() const MIJIN_NOEXCEPT
|
||||
}
|
||||
}
|
||||
|
||||
Optional<std::source_location> TaskHandle::getLocation() const noexcept
|
||||
Optional<std::source_location> TaskHandle::getLocation() const MIJIN_NOEXCEPT
|
||||
{
|
||||
if (std::shared_ptr<TaskSharedState> state = state_.lock())
|
||||
{
|
||||
@@ -810,12 +822,12 @@ TaskBase<TResult, TAllocator>::~TaskBase() MIJIN_NOEXCEPT
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
template<typename TResult>
|
||||
FuturePtr<TResult> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT
|
||||
TaskFuturePtr<TResult, TAllocator> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocator> task, TaskHandle* outHandle) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(!task.getLoop(), "Attempting to add task that already has a loop!");
|
||||
task.setLoop(this);
|
||||
|
||||
FuturePtr<TResult> future = std::allocate_shared<Future<TResult>>(TAllocator<Future<TResult>>(allocator_), allocator_);
|
||||
TaskFuturePtr<TResult, TAllocator> future = std::allocate_shared<TaskFuture<TResult, TAllocator>>(TAllocator<Future<TResult, TAllocator>>(allocator_), allocator_);
|
||||
auto setFuture = &setFutureHelper<TResult>;
|
||||
|
||||
if (outHandle != nullptr)
|
||||
@@ -825,7 +837,7 @@ FuturePtr<TResult> TaskLoop<TAllocator>::addTaskImpl(TaskBase<TResult, TAllocato
|
||||
|
||||
// add tasks to a seperate vector first as we might be running another task right now
|
||||
TAllocator<WrappedTask<TaskBase<TResult, TAllocator>>> allocator(allocator_);
|
||||
addStoredTask(StoredTask(wrapTask(std::move(allocator), std::move(task)), std::move(setFuture), std::move(future)));
|
||||
addStoredTask(StoredTask(wrapTask(std::move(allocator), std::move(task)), std::move(setFuture), future));
|
||||
|
||||
return future;
|
||||
}
|
||||
@@ -854,7 +866,7 @@ TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
||||
while (status == TaskStatus::RUNNING);
|
||||
impl::gCurrentTaskState = nullptr;
|
||||
|
||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING && !MIJIN_COROUTINE_ENABLE_EXCEPTIONS
|
||||
if (task.task && task.task->exception())
|
||||
{
|
||||
try
|
||||
@@ -882,7 +894,22 @@ TaskStatus TaskLoop<TAllocator>::tickTask(StoredTask& task)
|
||||
#endif // MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
|
||||
if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED)
|
||||
{
|
||||
task.setFuture(task);
|
||||
try
|
||||
{
|
||||
task.setFuture(task);
|
||||
}
|
||||
catch(TaskCancelled&) {}
|
||||
catch(...)
|
||||
{
|
||||
if (uncaughtExceptionHandler_)
|
||||
{
|
||||
uncaughtExceptionHandler_(std::current_exception());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -909,10 +936,22 @@ template<template<typename> typename TAllocator>
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
template<typename TResult>
|
||||
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT
|
||||
/* static */ inline void TaskLoop<TAllocator>::setFutureHelper(StoredTask& storedTask) MIJIN_NOEXCEPT_IF(!MIJIN_COROUTINE_ENABLE_EXCEPTIONS)
|
||||
{
|
||||
TaskBase<TResult, TAllocator>& task = *static_cast<TaskBase<TResult, TAllocator>*>(storedTask.task->raw());
|
||||
auto future = std::any_cast<FuturePtr<TResult>>(storedTask.resultData);
|
||||
const auto& future = std::any_cast<TaskFuturePtr<TResult, TAllocator>&>(storedTask.resultData);
|
||||
#if MIJIN_COROUTINE_ENABLE_EXCEPTIONS
|
||||
if (task.state().exception)
|
||||
{
|
||||
if (future.use_count() < 2)
|
||||
{
|
||||
// future has been discarded, but someone must handle the exception
|
||||
std::rethrow_exception(task.state().exception);
|
||||
}
|
||||
future->setException(task.state().exception);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if constexpr (!std::is_same_v<TResult, void>)
|
||||
{
|
||||
@@ -997,6 +1036,9 @@ inline auto BaseSimpleTaskLoop<TAllocator>::tick() -> CanContinue
|
||||
// set current taskloop
|
||||
MIJIN_ASSERT(TaskLoop<TAllocator>::currentLoopStorage() == nullptr, "Trying to tick a loop from a coroutine, this is not supported.");
|
||||
TaskLoop<TAllocator>::currentLoopStorage() = this;
|
||||
MIJIN_SCOPE_EXIT {
|
||||
TaskLoop<TAllocator>::currentLoopStorage() = nullptr;
|
||||
};
|
||||
threadId_ = std::this_thread::get_id();
|
||||
|
||||
// move over all tasks from newTasks
|
||||
@@ -1046,8 +1088,6 @@ inline auto BaseSimpleTaskLoop<TAllocator>::tick() -> CanContinue
|
||||
canContinue = CanContinue::YES;
|
||||
}
|
||||
}
|
||||
// reset current loop
|
||||
TaskLoop<TAllocator>::currentLoopStorage() = nullptr;
|
||||
|
||||
// remove any tasks that have been transferred to another queue
|
||||
it = std::remove_if(tasks_.begin(), tasks_.end(), [](const StoredTask& task) {
|
||||
@@ -1247,14 +1287,14 @@ inline TaskAwaitableSuspend c_suspend() {
|
||||
return TaskAwaitableSuspend();
|
||||
}
|
||||
|
||||
template<template<typename...> typename TCollection, typename TType, typename... TTemplateArgs>
|
||||
Task<> c_allDone(const TCollection<FuturePtr<TType>, TTemplateArgs...>& futures)
|
||||
template<template<typename...> typename TCollection, FutureType TFuture, typename... TTemplateArgs>
|
||||
Task<> c_allDone(const TCollection<TFuture, TTemplateArgs...>& futures)
|
||||
{
|
||||
bool allDone = true;
|
||||
do
|
||||
{
|
||||
allDone = true;
|
||||
for (const FuturePtr<TType>& future : futures)
|
||||
for (const TFuture& future : futures)
|
||||
{
|
||||
if (future && !future->ready()) {
|
||||
allDone = false;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#if !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
|
||||
#define MIJIN_ASYNC_FUTURE_HPP_INCLUDED 1
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <tuple>
|
||||
@@ -27,43 +28,113 @@ namespace mijin
|
||||
//
|
||||
// public types
|
||||
//
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
template<typename TValue, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, bool exceptions = false>
|
||||
class Future;
|
||||
|
||||
// TODO: add support for mutexes and waiting for futures
|
||||
namespace impl
|
||||
{
|
||||
template<typename TValue>
|
||||
template<typename TValue, bool exceptions>
|
||||
struct FutureStorage
|
||||
{
|
||||
Optional<TValue> value;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return !value.empty();
|
||||
}
|
||||
|
||||
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
||||
[[nodiscard]] TValue& getValue() MIJIN_NOEXCEPT { return value.get(); }
|
||||
|
||||
};
|
||||
|
||||
// template<typename TValue>
|
||||
// struct FutureStorage<TValue&>
|
||||
// {
|
||||
// Optional<TValue*> value;
|
||||
//
|
||||
// void setValue(TValue& value_) MIJIN_NOEXCEPT { value = &value_; }
|
||||
// [[nodiscard]] TValue& getValue() const MIJIN_NOEXCEPT { return *value.get(); }
|
||||
// };
|
||||
template<>
|
||||
struct FutureStorage<void, false>
|
||||
{
|
||||
bool isSet = false;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return isSet;
|
||||
}
|
||||
void setValue() MIJIN_NOEXCEPT
|
||||
{
|
||||
isSet = true;
|
||||
}
|
||||
void getValue() MIJIN_NOEXCEPT {}
|
||||
};
|
||||
|
||||
#if MIJIN_ENABLE_EXCEPTIONS
|
||||
template<typename TValue>
|
||||
struct FutureStorage<TValue, true>
|
||||
{
|
||||
Optional<TValue> value;
|
||||
std::exception_ptr exception;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
if (exception) {
|
||||
return true;
|
||||
}
|
||||
return !value.empty();
|
||||
}
|
||||
|
||||
void setException(std::exception_ptr exc) MIJIN_NOEXCEPT
|
||||
{
|
||||
exception = std::move(exc);
|
||||
}
|
||||
void setValue(TValue value_) MIJIN_NOEXCEPT { value = std::move(value_); }
|
||||
[[nodiscard]] TValue& getValue()
|
||||
{
|
||||
if (exception) {
|
||||
std::rethrow_exception(exception);
|
||||
}
|
||||
return value.get();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<>
|
||||
struct FutureStorage<void>
|
||||
struct FutureStorage<void, true>
|
||||
{
|
||||
bool isSet = false;
|
||||
std::exception_ptr exception;
|
||||
|
||||
[[nodiscard]]
|
||||
bool hasValue() const MIJIN_NOEXCEPT
|
||||
{
|
||||
if (exception) {
|
||||
return true;
|
||||
}
|
||||
return isSet;
|
||||
}
|
||||
void setException(std::exception_ptr exc) MIJIN_NOEXCEPT
|
||||
{
|
||||
exception = std::move(exc);
|
||||
}
|
||||
void setValue() MIJIN_NOEXCEPT
|
||||
{
|
||||
isSet = true;
|
||||
}
|
||||
void getValue()
|
||||
{
|
||||
if (exception) {
|
||||
std::rethrow_exception(exception);
|
||||
}
|
||||
}
|
||||
};
|
||||
#endif
|
||||
} // namespace impl
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator>
|
||||
template<typename TValue, template<typename> typename TAllocator, bool exceptions>
|
||||
class Future
|
||||
{
|
||||
private:
|
||||
[[no_unique_address]] impl::FutureStorage<TValue> value_;
|
||||
bool isSet_ = false;
|
||||
impl::FutureStorage<TValue, exceptions> value_;
|
||||
public:
|
||||
Future() = default;
|
||||
Future(const Future&) = delete;
|
||||
@@ -80,10 +151,12 @@ public:
|
||||
constexpr bool operator!() const MIJIN_NOEXCEPT { return !ready(); }
|
||||
public: // access
|
||||
[[nodiscard]]
|
||||
constexpr decltype(auto) get() MIJIN_NOEXCEPT
|
||||
constexpr decltype(auto) get() MIJIN_NOEXCEPT_IF(!exceptions)
|
||||
{
|
||||
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>) {
|
||||
MIJIN_ASSERT(ready(), "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>)
|
||||
{
|
||||
value_.getValue(); // in case of exceptions
|
||||
return;
|
||||
}
|
||||
else {
|
||||
@@ -91,10 +164,12 @@ public: // access
|
||||
}
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr decltype(auto) get() const MIJIN_NOEXCEPT
|
||||
constexpr decltype(auto) get() const MIJIN_NOEXCEPT_IF(!exceptions)
|
||||
{
|
||||
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>) {
|
||||
MIJIN_ASSERT(ready(), "Attempting to get from future that is not ready.");
|
||||
if constexpr(std::is_same_v<TValue, void>)
|
||||
{
|
||||
value_.getValue(); // in case of exceptions
|
||||
return;
|
||||
}
|
||||
else {
|
||||
@@ -104,22 +179,22 @@ public: // access
|
||||
[[nodiscard]]
|
||||
constexpr bool ready() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return isSet_;
|
||||
return value_.hasValue();
|
||||
}
|
||||
public: // modification
|
||||
template<typename TArg> requires (!std::is_same_v<TValue, void>)
|
||||
constexpr void set(TArg&& value) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
|
||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
||||
value_.setValue(std::move(value));
|
||||
isSet_ = true;
|
||||
sigSet.emit();
|
||||
}
|
||||
constexpr void set() MIJIN_NOEXCEPT requires (std::is_same_v<TValue, void>)
|
||||
{
|
||||
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
|
||||
isSet_ = true;
|
||||
if constexpr (std::is_same_v<TValue, void>) {
|
||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
||||
if constexpr (std::is_same_v<TValue, void>)
|
||||
{
|
||||
value_.setValue();
|
||||
sigSet.emit();
|
||||
}
|
||||
else {
|
||||
@@ -127,12 +202,38 @@ public: // modification
|
||||
MIJIN_ERROR("Attempting to call set(void) on future with value.");
|
||||
}
|
||||
}
|
||||
constexpr void setException(std::exception_ptr exception) requires (exceptions)
|
||||
{
|
||||
MIJIN_ASSERT(!ready(), "Trying to set a future twice!");
|
||||
if constexpr (exceptions)
|
||||
{
|
||||
value_.setException(std::move(exception));
|
||||
}
|
||||
}
|
||||
public: // signals
|
||||
BaseSignal<TAllocator> sigSet;
|
||||
};
|
||||
|
||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, bool exceptions = false>
|
||||
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator, exceptions>>;
|
||||
|
||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using FuturePtr = std::shared_ptr<Future<TValue, TAllocator>>;
|
||||
using ExceptFuture = Future<TValue, TAllocator, true>;
|
||||
|
||||
template<typename TValue = void, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
using ExceptFuturePtr = std::shared_ptr<Future<TValue, TAllocator, true>>;
|
||||
|
||||
template<typename T>
|
||||
struct is_future : std::false_type {};
|
||||
|
||||
template<typename TValue, template<typename> typename TAllocator, bool exceptions>
|
||||
struct is_future<Future<TValue, TAllocator, exceptions>> : std::true_type {};
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_future_t = is_future<T>::value;
|
||||
|
||||
template<typename T>
|
||||
concept FutureType = is_future<T>::value;
|
||||
|
||||
//
|
||||
// public functions
|
||||
|
||||
@@ -57,8 +57,8 @@ private:
|
||||
bool constructed = false;
|
||||
#endif
|
||||
public:
|
||||
BoxedObject() noexcept : placeholder_() {};
|
||||
explicit BoxedObject(T object)
|
||||
constexpr BoxedObject() MIJIN_NOEXCEPT : placeholder_() {};
|
||||
explicit constexpr BoxedObject(T object) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
: constructed(true)
|
||||
#endif
|
||||
@@ -69,7 +69,7 @@ public:
|
||||
BoxedObject(BoxedObject&&) = delete;
|
||||
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
~BoxedObject()
|
||||
constexpr ~BoxedObject() noexcept
|
||||
{
|
||||
MIJIN_ASSERT(!constructed, "BoxedObject::~BoxedObject(): Object has not been destroyed prior to destructor!");
|
||||
}
|
||||
@@ -78,13 +78,13 @@ public:
|
||||
BoxedObject& operator=(const BoxedObject&) = delete;
|
||||
BoxedObject& operator=(BoxedObject&&) = delete;
|
||||
|
||||
T& operator*() noexcept { return get(); }
|
||||
const T& operator*() const noexcept { return get(); }
|
||||
T* operator->() noexcept { return &get(); }
|
||||
const T* operator->() const noexcept { return &get(); }
|
||||
constexpr T& operator*() noexcept { return get(); }
|
||||
constexpr const T& operator*() const noexcept { return get(); }
|
||||
constexpr T* operator->() noexcept { return &get(); }
|
||||
constexpr const T* operator->() const noexcept { return &get(); }
|
||||
|
||||
template<typename... TArgs>
|
||||
void construct(TArgs&&... args)
|
||||
constexpr void construct(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
||||
{
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
MIJIN_ASSERT(!constructed, "BoxedObject::construct(): Attempt to construct an already constructed object!");
|
||||
@@ -93,7 +93,7 @@ public:
|
||||
std::construct_at(&object_, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
void destroy()
|
||||
constexpr void destroy() MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
MIJIN_ASSERT(constructed, "BoxedObject::destroy(): Attempt to destroy a not constructed object!");
|
||||
@@ -101,8 +101,8 @@ public:
|
||||
#endif
|
||||
std::destroy_at(&object_);
|
||||
}
|
||||
|
||||
void copyTo(BoxedObject& other) const
|
||||
|
||||
constexpr void copyTo(BoxedObject& other) const MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v<T>)
|
||||
{
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!");
|
||||
@@ -110,7 +110,7 @@ public:
|
||||
other.construct(object_);
|
||||
}
|
||||
|
||||
void moveTo(BoxedObject& other)
|
||||
constexpr void moveTo(BoxedObject& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<T>)
|
||||
{
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!");
|
||||
@@ -119,7 +119,7 @@ public:
|
||||
destroy();
|
||||
}
|
||||
|
||||
[[nodiscard]] T& get()
|
||||
[[nodiscard]] constexpr T& get() MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!");
|
||||
@@ -127,7 +127,7 @@ public:
|
||||
return object_;
|
||||
}
|
||||
|
||||
[[nodiscard]] const T& get() const
|
||||
[[nodiscard]] constexpr const T& get() const MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_BOXED_OBJECT_DEBUG
|
||||
MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!");
|
||||
@@ -149,38 +149,38 @@ private:
|
||||
BoxedObject<T>* end_ = nullptr;
|
||||
#endif
|
||||
public:
|
||||
BoxedObjectIterator() = default;
|
||||
explicit constexpr BoxedObjectIterator(BoxedObject<T>* box, BoxedObject<T>* start, BoxedObject<T>* end)
|
||||
BoxedObjectIterator() noexcept = default;
|
||||
explicit constexpr BoxedObjectIterator(BoxedObject<T>* box, BoxedObject<T>* start, BoxedObject<T>* end) MIJIN_NOEXCEPT
|
||||
: box_(box)
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
, start_(start), end_(end)
|
||||
#endif
|
||||
{}
|
||||
BoxedObjectIterator(const BoxedObjectIterator&) = default;
|
||||
BoxedObjectIterator(BoxedObjectIterator&&) noexcept = default;
|
||||
constexpr BoxedObjectIterator(const BoxedObjectIterator&) noexcept = default;
|
||||
constexpr BoxedObjectIterator(BoxedObjectIterator&&) noexcept = default;
|
||||
|
||||
BoxedObjectIterator& operator=(const BoxedObjectIterator&) = default;
|
||||
BoxedObjectIterator& operator=(BoxedObjectIterator&&) noexcept = default;
|
||||
BoxedObjectIterator& operator+=(difference_type diff);
|
||||
BoxedObjectIterator& operator-=(difference_type diff) { return (*this += -diff); }
|
||||
constexpr BoxedObjectIterator& operator=(const BoxedObjectIterator&) noexcept = default;
|
||||
constexpr BoxedObjectIterator& operator=(BoxedObjectIterator&&) noexcept = default;
|
||||
constexpr BoxedObjectIterator& operator+=(difference_type diff) MIJIN_NOEXCEPT;
|
||||
constexpr BoxedObjectIterator& operator-=(difference_type diff) MIJIN_NOEXCEPT { return (*this += -diff); }
|
||||
|
||||
constexpr auto operator<=>(const BoxedObjectIterator& other) const noexcept = default;
|
||||
|
||||
[[nodiscard]] T& operator*() const;
|
||||
[[nodiscard]] T* operator->() const;
|
||||
BoxedObjectIterator& operator++();
|
||||
BoxedObjectIterator operator++(int);
|
||||
BoxedObjectIterator& operator--();
|
||||
BoxedObjectIterator operator--(int);
|
||||
[[nodiscard]] constexpr T& operator*() const MIJIN_NOEXCEPT;
|
||||
[[nodiscard]] constexpr T* operator->() const MIJIN_NOEXCEPT;
|
||||
constexpr BoxedObjectIterator& operator++() MIJIN_NOEXCEPT;
|
||||
constexpr BoxedObjectIterator operator++(int) MIJIN_NOEXCEPT;
|
||||
constexpr BoxedObjectIterator& operator--() MIJIN_NOEXCEPT;
|
||||
constexpr BoxedObjectIterator operator--(int) MIJIN_NOEXCEPT;
|
||||
|
||||
[[nodiscard]] difference_type operator-(const BoxedObjectIterator& other) const { return box_ - other.box_; }
|
||||
[[nodiscard]] BoxedObjectIterator operator+(difference_type diff) const;
|
||||
[[nodiscard]] BoxedObjectIterator operator-(difference_type diff) const { return (*this + -diff); }
|
||||
[[nodiscard]] constexpr difference_type operator-(const BoxedObjectIterator& other) const MIJIN_NOEXCEPT { return box_ - other.box_; }
|
||||
[[nodiscard]] constexpr BoxedObjectIterator operator+(difference_type diff) const MIJIN_NOEXCEPT;
|
||||
[[nodiscard]] constexpr BoxedObjectIterator operator-(difference_type diff) const MIJIN_NOEXCEPT { return (*this + -diff); }
|
||||
|
||||
[[nodiscard]] T& operator[](difference_type diff) const { return *(*this + diff); }
|
||||
[[nodiscard]] T& operator[](difference_type diff) const MIJIN_NOEXCEPT { return *(*this + diff); }
|
||||
};
|
||||
template<typename T>
|
||||
inline BoxedObjectIterator<T> operator+(std::iter_difference_t<T> diff, const BoxedObjectIterator<T>& iter) {
|
||||
constexpr BoxedObjectIterator<T> operator+(std::iter_difference_t<T> diff, const BoxedObjectIterator<T>& iter) MIJIN_NOEXCEPT {
|
||||
return iter + diff;
|
||||
}
|
||||
static_assert(std::random_access_iterator<BoxedObjectIterator<int>>);
|
||||
@@ -190,7 +190,7 @@ static_assert(std::random_access_iterator<BoxedObjectIterator<int>>);
|
||||
//
|
||||
|
||||
template<typename T>
|
||||
BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator+=(difference_type diff)
|
||||
constexpr BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator+=(difference_type diff) MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
MIJIN_ASSERT(diff <= (end_ - box_) && diff >= (box_ - start_), "BoxedObjectIterator::operator+=(): Attempt to iterate out of range.");
|
||||
@@ -200,7 +200,7 @@ BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator+=(difference_type diff)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& BoxedObjectIterator<T>::operator*() const
|
||||
constexpr T& BoxedObjectIterator<T>::operator*() const MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
MIJIN_ASSERT(box_ >= start_ && box_ < end_, "BoxedObjectIterator::operator->(): Attempt to dereference out-of-range iterator.");
|
||||
@@ -209,7 +209,7 @@ T& BoxedObjectIterator<T>::operator*() const
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator++()
|
||||
constexpr BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator++() MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(): Attempt to iterator past the end.");
|
||||
@@ -218,7 +218,7 @@ BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator++()
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
BoxedObjectIterator<T> BoxedObjectIterator<T>::operator++(int)
|
||||
constexpr BoxedObjectIterator<T> BoxedObjectIterator<T>::operator++(int) MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(int): Attempt to iterator past the end.");
|
||||
@@ -229,7 +229,7 @@ BoxedObjectIterator<T> BoxedObjectIterator<T>::operator++(int)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator--()
|
||||
constexpr BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator--() MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(): Attempt to iterator past start.");
|
||||
@@ -238,7 +238,7 @@ BoxedObjectIterator<T>& BoxedObjectIterator<T>::operator--()
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
BoxedObjectIterator<T> BoxedObjectIterator<T>::operator--(int)
|
||||
constexpr BoxedObjectIterator<T> BoxedObjectIterator<T>::operator--(int) MIJIN_NOEXCEPT
|
||||
{
|
||||
#if MIJIN_CHECKED_ITERATORS
|
||||
MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(int): Attempt to iterator past start.");
|
||||
@@ -249,7 +249,7 @@ BoxedObjectIterator<T> BoxedObjectIterator<T>::operator--(int)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
BoxedObjectIterator<T> BoxedObjectIterator<T>::operator+(difference_type diff) const
|
||||
constexpr BoxedObjectIterator<T> BoxedObjectIterator<T>::operator+(difference_type diff) const MIJIN_NOEXCEPT
|
||||
{
|
||||
BoxedObjectIterator copy(*this);
|
||||
copy += diff;
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#define MIJIN_CONTAINER_MEMORY_VIEW_HPP_INCLUDED 1
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include "../debug/assert.hpp"
|
||||
#include "../util/traits.hpp"
|
||||
#include "../internal/common.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
@@ -25,60 +25,172 @@ namespace mijin
|
||||
// public types
|
||||
//
|
||||
|
||||
template<typename TBytes>
|
||||
class MemoryViewBase
|
||||
template<typename T>
|
||||
concept MemoryViewable = requires(const T& object)
|
||||
{
|
||||
{ object.data() } -> std::convertible_to<const void*>;
|
||||
{ object.byteSize() } -> std::convertible_to<std::size_t>;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept RWMemoryViewable = MemoryViewable<T> && requires(T& object)
|
||||
{
|
||||
{ object.data() } -> std::convertible_to<void*>;
|
||||
};
|
||||
|
||||
template<typename TConcrete>
|
||||
class MixinMemoryView
|
||||
{
|
||||
public:
|
||||
static constexpr bool WRITABLE = requires(TConcrete& object) { { object.data() } -> std::convertible_to<void*>; };
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
auto makeSpan();
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
auto makeSpan() const;
|
||||
|
||||
template<typename TChar = char, typename TTraits = std::char_traits<TChar>>
|
||||
[[nodiscard]]
|
||||
std::basic_string_view<TChar, TTraits> makeStringView() const ;
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
auto& dataAt(std::size_t offset) MIJIN_NOEXCEPT;
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
auto& dataAt(std::size_t offset) const MIJIN_NOEXCEPT;
|
||||
|
||||
[[nodiscard]]
|
||||
auto bytes() MIJIN_NOEXCEPT
|
||||
{
|
||||
using return_t = mijin::copy_const_t<std::remove_pointer_t<decltype(static_cast<TConcrete*>(this)->data())>, std::byte>;
|
||||
return static_cast<return_t*>(static_cast<TConcrete*>(this)->data());
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
auto bytes() const MIJIN_NOEXCEPT
|
||||
{
|
||||
using return_t = mijin::copy_const_t<std::remove_pointer_t<decltype(static_cast<const TConcrete*>(this)->data())>, std::byte>;
|
||||
return static_cast<return_t*>(static_cast<const TConcrete*>(this)->data());
|
||||
}
|
||||
private:
|
||||
std::size_t byteSizeImpl() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return static_cast<const TConcrete*>(this)->byteSize();
|
||||
}
|
||||
};
|
||||
|
||||
class MemoryView : public MixinMemoryView<MemoryView>
|
||||
{
|
||||
public:
|
||||
using size_type = std::size_t;
|
||||
private:
|
||||
std::span<TBytes> bytes_;
|
||||
void* data_ = nullptr;
|
||||
std::size_t byteSize_ = 0;
|
||||
public:
|
||||
MemoryViewBase() noexcept = default;
|
||||
MemoryViewBase(const MemoryViewBase&) noexcept = default;
|
||||
MemoryViewBase(copy_const_t<TBytes, void>* data, std::size_t size) noexcept : bytes_(static_cast<TBytes*>(data), size) {}
|
||||
MemoryView() noexcept = default;
|
||||
MemoryView(const MemoryView&) = default;
|
||||
MemoryView(void* data, std::size_t byteSize) MIJIN_NOEXCEPT : data_(data), byteSize_(byteSize) {}
|
||||
template<typename T> requires(!std::is_const_v<T>)
|
||||
MemoryView(std::span<T> span) MIJIN_NOEXCEPT : data_(span.data()), byteSize_(span.size_bytes()) {}
|
||||
template<RWMemoryViewable T>
|
||||
MemoryView(T& memoryViewable) MIJIN_NOEXCEPT : data_(memoryViewable.data()), byteSize_(memoryViewable.byteSize()) {}
|
||||
|
||||
MemoryViewBase& operator=(const MemoryViewBase&) noexcept = default;
|
||||
|
||||
[[nodiscard]] void* data() noexcept { return bytes_.data(); }
|
||||
[[nodiscard]] const void* data() const noexcept { return bytes_.data(); }
|
||||
[[nodiscard]] size_type byteSize() const noexcept { return bytes_.size(); }
|
||||
[[nodiscard]] bool empty() const noexcept { return bytes_.empty(); }
|
||||
MemoryView& operator=(const MemoryView&) = default;
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] std::span<T> makeSpan();
|
||||
[[nodiscard]]
|
||||
void* data() const MIJIN_NOEXCEPT { return data_; }
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] std::span<const T> makeSpan() const;
|
||||
[[nodiscard]]
|
||||
size_type byteSize() const MIJIN_NOEXCEPT { return byteSize_; }
|
||||
};
|
||||
|
||||
class ConstMemoryView : public MixinMemoryView<ConstMemoryView>
|
||||
{
|
||||
public:
|
||||
using size_type = std::size_t;
|
||||
private:
|
||||
const void* data_;
|
||||
std::size_t byteSize_;
|
||||
public:
|
||||
ConstMemoryView() noexcept = default;
|
||||
ConstMemoryView(const ConstMemoryView&) = default;
|
||||
ConstMemoryView(void* data, std::size_t byteSize) MIJIN_NOEXCEPT : data_(data), byteSize_(byteSize) {}
|
||||
template<typename T>
|
||||
ConstMemoryView(std::span<T> span) MIJIN_NOEXCEPT : data_(span.data()), byteSize_(span.size_bytes()) {}
|
||||
template<MemoryViewable T>
|
||||
ConstMemoryView(const T& memoryViewable) MIJIN_NOEXCEPT : data_(memoryViewable.data()), byteSize_(memoryViewable.byteSize()) {}
|
||||
|
||||
ConstMemoryView& operator=(const ConstMemoryView& other) MIJIN_NOEXCEPT = default;
|
||||
|
||||
[[nodiscard]]
|
||||
const void* data() const MIJIN_NOEXCEPT { return data_; }
|
||||
|
||||
[[nodiscard]]
|
||||
size_type byteSize() const MIJIN_NOEXCEPT { return byteSize_; }
|
||||
};
|
||||
using MemoryView = MemoryViewBase<std::byte>;
|
||||
using ConstMemoryView = MemoryViewBase<const std::byte>;
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
template<typename TBytes>
|
||||
template<typename TConcrete>
|
||||
template<typename T>
|
||||
std::span<T> MemoryViewBase<TBytes>::makeSpan()
|
||||
auto MixinMemoryView<TConcrete>::makeSpan()
|
||||
{
|
||||
static_assert(!std::is_const_v<TBytes>, "Cannot create writable spans from const memory views.");
|
||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "MemoryView cannot be divided into elements of this type.");
|
||||
return {
|
||||
std::bit_cast<T*>(bytes_.data()),
|
||||
std::bit_cast<T*>(bytes_.data() + bytes_.size())
|
||||
MIJIN_ASSERT(byteSizeImpl() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
||||
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||
return std::span<return_t>{
|
||||
std::bit_cast<return_t*>(bytes()),
|
||||
std::bit_cast<return_t*>(bytes() + byteSizeImpl())
|
||||
};
|
||||
}
|
||||
|
||||
template<typename TBytes>
|
||||
template<typename TConcrete>
|
||||
template<typename T>
|
||||
std::span<const T> MemoryViewBase<TBytes>::makeSpan() const
|
||||
auto MixinMemoryView<TConcrete>::makeSpan() const
|
||||
{
|
||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "MemoryView cannot be divided into elements of this type.");
|
||||
return {
|
||||
std::bit_cast<const T*>(bytes_.data()),
|
||||
std::bit_cast<const T*>(bytes_.data() + bytes_.size())
|
||||
MIJIN_ASSERT(byteSizeImpl() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
||||
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||
return std::span<return_t>{
|
||||
std::bit_cast<return_t*>(bytes()),
|
||||
std::bit_cast<return_t*>(bytes() + byteSizeImpl())
|
||||
};
|
||||
}
|
||||
|
||||
template<typename TConcrete>
|
||||
template<typename TChar, typename TTraits>
|
||||
std::basic_string_view<TChar, TTraits> MixinMemoryView<TConcrete>::makeStringView() const
|
||||
{
|
||||
MIJIN_ASSERT(byteSizeImpl() % sizeof(TChar) == 0, "Buffer cannot be divided into elements of this char type.");
|
||||
return {std::bit_cast<const TChar*>(bytes()), byteSizeImpl() / sizeof(TChar)};
|
||||
}
|
||||
|
||||
template<typename TConcrete>
|
||||
template<typename T>
|
||||
auto& MixinMemoryView<TConcrete>::dataAt(std::size_t offset) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
||||
MIJIN_ASSERT(offset + sizeof(T) < byteSizeImpl(), "Buffer access out-of-range.");
|
||||
|
||||
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||
return *std::bit_cast<return_t*>(bytes().data() + offset);
|
||||
}
|
||||
|
||||
template<typename TConcrete>
|
||||
template<typename T>
|
||||
auto& MixinMemoryView<TConcrete>::dataAt(std::size_t offset) const MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
||||
MIJIN_ASSERT(offset + sizeof(T) < byteSizeImpl(), "Buffer access out-of-range.");
|
||||
|
||||
using return_t = mijin::copy_const_t<decltype(*bytes()), T>;
|
||||
return *std::bit_cast<return_t*>(bytes().data() + offset);
|
||||
}
|
||||
} // namespace mijin
|
||||
|
||||
#endif // !defined(MIJIN_CONTAINER_MEMORY_VIEW_HPP_INCLUDED)
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include "./memory_view.hpp"
|
||||
#include "../debug/assert.hpp"
|
||||
#include "../internal/common.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
@@ -27,17 +29,26 @@ namespace mijin
|
||||
// public types
|
||||
//
|
||||
|
||||
template<typename T>
|
||||
template<typename T, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
class BufferView;
|
||||
|
||||
class TypelessBuffer
|
||||
template<template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
class BaseTypelessBuffer : public MixinMemoryView<BaseTypelessBuffer<TAllocator>>
|
||||
{
|
||||
public:
|
||||
using size_type = std::size_t;
|
||||
private:
|
||||
std::vector<std::byte> bytes_;
|
||||
std::vector<std::byte, TAllocator<std::byte>> bytes_;
|
||||
public:
|
||||
auto operator<=>(const TypelessBuffer&) const noexcept = default;
|
||||
BaseTypelessBuffer() noexcept = default;
|
||||
BaseTypelessBuffer(const BaseTypelessBuffer&) = default;
|
||||
BaseTypelessBuffer(BaseTypelessBuffer&&) = default;
|
||||
explicit BaseTypelessBuffer(TAllocator<std::byte> allocator) : bytes_(std::move(allocator)) {}
|
||||
|
||||
BaseTypelessBuffer& operator=(const BaseTypelessBuffer&) = default;
|
||||
BaseTypelessBuffer& operator=(BaseTypelessBuffer&&) = default;
|
||||
|
||||
auto operator<=>(const BaseTypelessBuffer&) const noexcept = default;
|
||||
|
||||
[[nodiscard]] void* data() noexcept { return bytes_.data(); }
|
||||
[[nodiscard]] const void* data() const noexcept { return bytes_.data(); }
|
||||
@@ -49,27 +60,14 @@ public:
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] BufferView<T> makeBufferView() { return BufferView<T>(this); }
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] std::span<T> makeSpan();
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] std::span<const T> makeSpan() const;
|
||||
|
||||
template<typename TChar = char, typename TTraits = std::char_traits<TChar>>
|
||||
[[nodiscard]] std::basic_string_view<TChar, TTraits> makeStringView() const ;
|
||||
|
||||
template<typename T>
|
||||
void append(std::span<const T> data);
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] T& dataAt(size_type offset) MIJIN_NOEXCEPT;
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] const T& dataAt(size_type offset) const MIJIN_NOEXCEPT;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using TypelessBuffer = BaseTypelessBuffer<>;
|
||||
|
||||
template<typename T, template<typename> typename TAllocator>
|
||||
class BufferView
|
||||
{
|
||||
public:
|
||||
@@ -84,10 +82,10 @@ public:
|
||||
using iterator = T*;
|
||||
using const_iterator = const T*;
|
||||
private:
|
||||
class TypelessBuffer* buffer_ = nullptr;
|
||||
class BaseTypelessBuffer<TAllocator>* buffer_ = nullptr;
|
||||
public:
|
||||
BufferView() = default;
|
||||
explicit BufferView(class TypelessBuffer* buffer) : buffer_(buffer) {}
|
||||
explicit BufferView(class BaseTypelessBuffer<TAllocator>* buffer) : buffer_(buffer) {}
|
||||
BufferView(const BufferView&) = default;
|
||||
|
||||
BufferView& operator=(const BufferView&) = default;
|
||||
@@ -131,57 +129,13 @@ public:
|
||||
// public functions
|
||||
//
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
template<typename T>
|
||||
std::span<T> TypelessBuffer::makeSpan()
|
||||
{
|
||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
||||
return {
|
||||
std::bit_cast<T*>(bytes_.data()),
|
||||
std::bit_cast<T*>(bytes_.data() + bytes_.size())
|
||||
};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::span<const T> TypelessBuffer::makeSpan() const
|
||||
{
|
||||
MIJIN_ASSERT(bytes_.size() % sizeof(T) == 0, "Buffer cannot be divided into elements of this type.");
|
||||
return {
|
||||
std::bit_cast<const T*>(bytes_.data()),
|
||||
std::bit_cast<const T*>(bytes_.data() + bytes_.size())
|
||||
};
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
std::basic_string_view<TChar, TTraits> TypelessBuffer::makeStringView() const
|
||||
{
|
||||
MIJIN_ASSERT(bytes_.size() % sizeof(TChar) == 0, "Buffer cannot be divided into elements of this char type.");
|
||||
return {std::bit_cast<const TChar*>(bytes_.data()), bytes_.size() / sizeof(TChar)};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void TypelessBuffer::append(std::span<const T> data)
|
||||
void BaseTypelessBuffer<TAllocator>::append(std::span<const T> data)
|
||||
{
|
||||
bytes_.resize(bytes_.size() + data.size_bytes());
|
||||
std::memcpy(bytes_.data() + bytes_.size() - data.size_bytes(), data.data(), data.size_bytes());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& TypelessBuffer::dataAt(size_type offset) MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
||||
MIJIN_ASSERT(offset + sizeof(T) < bytes_.size(), "Buffer access out-of-range.");
|
||||
|
||||
return *std::bit_cast<T*>(bytes_.data() + offset);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const T& TypelessBuffer::dataAt(size_type offset) const MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(offset % alignof(T) == 0, "Offset must be correctly aligned.");
|
||||
MIJIN_ASSERT(offset + sizeof(T) < bytes_.size(), "Buffer access out-of-range.");
|
||||
|
||||
return *std::bit_cast<const T*>(bytes_.data() + offset);
|
||||
}
|
||||
} // namespace mijin
|
||||
|
||||
#endif // !defined(MIJIN_CONTAINER_TYPELESS_BUFFER_HPP_INCLUDED)
|
||||
|
||||
@@ -133,7 +133,8 @@ public:
|
||||
VectorMap& operator=(VectorMap&&) = default;
|
||||
auto operator<=>(const VectorMap& other) const noexcept = default;
|
||||
|
||||
TValue& operator[](const TKey& key)
|
||||
template<typename TIndex>
|
||||
TValue& operator[](const TIndex& key)
|
||||
{
|
||||
auto it = find(key);
|
||||
if (it != end())
|
||||
@@ -143,7 +144,8 @@ public:
|
||||
return emplace(key, TValue()).first->second;
|
||||
}
|
||||
|
||||
const TValue& operator[](const TKey& key) const
|
||||
template<typename TIndex>
|
||||
const TValue& operator[](const TIndex& key) const
|
||||
{
|
||||
return at(key);
|
||||
}
|
||||
@@ -251,8 +253,9 @@ public:
|
||||
return eraseImpl(idx, count);
|
||||
}
|
||||
|
||||
template<typename TSearch>
|
||||
[[nodiscard]]
|
||||
iterator find(const TKey& key) MIJIN_NOEXCEPT
|
||||
iterator find(const TSearch& key) MIJIN_NOEXCEPT
|
||||
{
|
||||
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
||||
{
|
||||
@@ -264,8 +267,9 @@ public:
|
||||
return end();
|
||||
}
|
||||
|
||||
template<typename TSearch>
|
||||
[[nodiscard]]
|
||||
const_iterator find(const TKey& key) const MIJIN_NOEXCEPT
|
||||
const_iterator find(const TSearch& key) const MIJIN_NOEXCEPT
|
||||
{
|
||||
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
|
||||
{
|
||||
|
||||
@@ -4,15 +4,24 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include "../detect.hpp"
|
||||
#include "../util/string.hpp"
|
||||
|
||||
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG || MIJIN_COMPILER == MIJIN_COMPILER_GCC
|
||||
#if MIJIN_TARGET_OS != MIJIN_OS_WINDOWS && (MIJIN_COMPILER == MIJIN_COMPILER_CLANG || MIJIN_COMPILER == MIJIN_COMPILER_GCC)
|
||||
#define MIJIN_USE_LIBBACKTRACE 1
|
||||
#else
|
||||
#define MIJIN_USE_LIBBACKTRACE 0
|
||||
#endif
|
||||
|
||||
#if MIJIN_USE_LIBBACKTRACE
|
||||
#include <backtrace.h>
|
||||
#include <backtrace.h>
|
||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
#include <Windows.h>
|
||||
#include <DbgHelp.h>
|
||||
#include "../util/winundef.hpp"
|
||||
#pragma comment(lib, "dbghelp")
|
||||
#endif
|
||||
|
||||
|
||||
@@ -32,11 +41,15 @@ namespace
|
||||
// internal types
|
||||
//
|
||||
|
||||
#if MIJIN_USE_LIBBACKTRACE
|
||||
struct BacktraceData
|
||||
{
|
||||
std::optional<std::string> error;
|
||||
std::vector<Stackframe> stackframes;
|
||||
};
|
||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
HANDLE gProcessHandle = nullptr;
|
||||
#endif
|
||||
|
||||
//
|
||||
// internal variables
|
||||
@@ -44,6 +57,11 @@ struct BacktraceData
|
||||
|
||||
thread_local Optional<Stacktrace> gCurrentExceptionStackTrace;
|
||||
|
||||
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
std::mutex gDbgHelpMutex;
|
||||
bool gDbgHelpInitCalled = false;
|
||||
#endif
|
||||
|
||||
//
|
||||
// internal functions
|
||||
//
|
||||
@@ -68,6 +86,49 @@ void backtraceErrorCallback(void* data, const char* msg, int /* errnum */)
|
||||
}
|
||||
|
||||
thread_local backtrace_state* gBacktraceState = nullptr;
|
||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
void cleanupDbgHelp() MIJIN_NOEXCEPT
|
||||
{
|
||||
if (!SymCleanup(gProcessHandle))
|
||||
{
|
||||
[[maybe_unused]] const DWORD error = GetLastError();
|
||||
MIJIN_ERROR("Error cleaning up DbgHelp.");
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool initDbgHelp() MIJIN_NOEXCEPT
|
||||
{
|
||||
if (gDbgHelpInitCalled)
|
||||
{
|
||||
return gProcessHandle != nullptr; // if init was successful, process handle is not null
|
||||
}
|
||||
gDbgHelpInitCalled = true;
|
||||
|
||||
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
|
||||
|
||||
HANDLE hCurrentProcess = GetCurrentProcess();
|
||||
HANDLE hCopy = nullptr;
|
||||
if (!DuplicateHandle(hCurrentProcess, hCurrentProcess, hCurrentProcess, &hCopy, 0, FALSE, DUPLICATE_SAME_ACCESS))
|
||||
{
|
||||
[[maybe_unused]] const DWORD error = GetLastError();
|
||||
MIJIN_ERROR("Error duplicating process handle.");
|
||||
return false;
|
||||
}
|
||||
if (!SymInitialize(hCopy, nullptr, true))
|
||||
{
|
||||
[[maybe_unused]] const DWORD error = GetLastError();
|
||||
MIJIN_ERROR("Error initializing DbHelp.");
|
||||
return false;
|
||||
}
|
||||
|
||||
[[maybe_unused]] const int result = std::atexit(&cleanupDbgHelp);
|
||||
MIJIN_ASSERT(result == 0, "Error registering DbgHelp cleanup handler.");
|
||||
|
||||
// only copy in the end so we can still figure out if initialization was successful
|
||||
gProcessHandle = hCopy;
|
||||
return true;
|
||||
}
|
||||
#endif // MIJIN_USE_LIBBACKTRACE
|
||||
} // namespace
|
||||
|
||||
@@ -98,7 +159,85 @@ Result<Stacktrace> captureStacktrace(unsigned skipFrames) MIJIN_NOEXCEPT
|
||||
}
|
||||
|
||||
return Stacktrace(std::move(btData.stackframes));
|
||||
#else // MIJIN_USE_LIBBACKTRACE
|
||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
if (!initDbgHelp())
|
||||
{
|
||||
return ResultError("error initializing DbgHelp");
|
||||
}
|
||||
|
||||
const HANDLE hThread = GetCurrentThread();
|
||||
|
||||
CONTEXT context;
|
||||
RtlCaptureContext(&context);
|
||||
|
||||
STACKFRAME64 stackFrame = {
|
||||
.AddrPC = {
|
||||
.Offset = context.Rip,
|
||||
.Mode = AddrModeFlat
|
||||
},
|
||||
.AddrFrame = {
|
||||
.Offset = context.Rbp,
|
||||
.Mode = AddrModeFlat
|
||||
},
|
||||
.AddrStack = {
|
||||
.Offset = context.Rsp,
|
||||
.Mode = AddrModeFlat
|
||||
}
|
||||
};
|
||||
|
||||
++skipFrames; // always skip the first frame (the current function)
|
||||
|
||||
// for symbol info
|
||||
DWORD64 displacement64 = 0;
|
||||
static constexpr std::size_t SYMBOL_BUFFER_SIZE = sizeof(SYMBOL_INFO) + (MAX_SYM_NAME * sizeof(char));
|
||||
std::array<std::byte, SYMBOL_BUFFER_SIZE> symbolBuffer alignas(SYMBOL_INFO);
|
||||
SYMBOL_INFO& symbolInfo = *std::bit_cast<SYMBOL_INFO*>(symbolBuffer.data());
|
||||
symbolInfo.SizeOfStruct = sizeof(SYMBOL_BUFFER_SIZE);
|
||||
symbolInfo.MaxNameLen = MAX_SYM_NAME;
|
||||
|
||||
// for file and line info
|
||||
DWORD displacement = 0;
|
||||
IMAGEHLP_LINE64 line64;
|
||||
line64.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
||||
|
||||
std::vector<Stackframe> stackframes;
|
||||
while (StackWalk64(
|
||||
/* MachineType = */ IMAGE_FILE_MACHINE_AMD64,
|
||||
/* hProcess = */ gProcessHandle,
|
||||
/* hThread = */ hThread,
|
||||
/* StackFrame = */ &stackFrame,
|
||||
/* ContextRecord = */ &context,
|
||||
/* ReadMemoryRoutine = */ nullptr,
|
||||
/* FunctionTableAccessRoutine = */ &SymFunctionTableAccess64,
|
||||
/* GetModuleBaseRoutine = */ &SymGetModuleBase64,
|
||||
/* TranslateAddress = */ nullptr
|
||||
))
|
||||
{
|
||||
if (skipFrames > 0)
|
||||
{
|
||||
--skipFrames;
|
||||
continue;
|
||||
}
|
||||
Stackframe& frame = stackframes.emplace_back();
|
||||
const DWORD64 baseAddress = SymGetModuleBase64(gProcessHandle, stackFrame.AddrPC.Offset);
|
||||
const DWORD64 relativeAddress = stackFrame.AddrPC.Offset - baseAddress;
|
||||
|
||||
frame.address = std::bit_cast<void*>(relativeAddress);
|
||||
|
||||
if (SymFromAddr(gProcessHandle, stackFrame.AddrPC.Offset, &displacement64, &symbolInfo))
|
||||
{
|
||||
frame.function = symbolInfo.Name;
|
||||
}
|
||||
|
||||
if (SymGetLineFromAddr64(gProcessHandle, stackFrame.AddrPC.Offset, &displacement, &line64))
|
||||
{
|
||||
frame.filename = line64.FileName;
|
||||
frame.lineNumber = static_cast<int>(line64.LineNumber);
|
||||
}
|
||||
}
|
||||
|
||||
return Stacktrace(std::move(stackframes));
|
||||
#else // MIJIN_USE_LIBBACKTRACE || (MIJIN_TARGET_OS == MIJIN_OS_WINDOWS)
|
||||
(void) skipFrames;
|
||||
return ResultError("not implemented");
|
||||
#endif // MIJIN_USE_LIBBACKTRACE
|
||||
|
||||
@@ -61,6 +61,26 @@ namespace mijin
|
||||
#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
|
||||
//
|
||||
|
||||
@@ -4,8 +4,11 @@
|
||||
#if !defined(MIJIN_INTERNAL_CONFIG_HPP_INCLUDED)
|
||||
#define MIJIN_INTERNAL_CONFIG_HPP_INCLUDED 1
|
||||
|
||||
#define MIJIN_QUOTED_ACTUAL(x) #x
|
||||
#define MIJIN_QUOTED(x) MIJIN_QUOTED_ACTUAL(x)
|
||||
|
||||
#if defined(MIJIN_CONFIG_HEADER)
|
||||
#include MIJIN_CONFIG_HEADER
|
||||
#include MIJIN_QUOTED(MIJIN_CONFIG_HEADER)
|
||||
#endif
|
||||
|
||||
#if !defined(MIJIN_DEFAULT_ALLOCATOR)
|
||||
|
||||
@@ -168,12 +168,17 @@ std::string shellEscape(const std::string& arg) MIJIN_NOEXCEPT
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::string makeShellCommand(const std::vector<std::string>& args) MIJIN_NOEXCEPT
|
||||
std::string makeShellCommand(std::span<std::string_view> args) MIJIN_NOEXCEPT
|
||||
{
|
||||
(void) args;
|
||||
MIJIN_ERROR("TBD");
|
||||
return {};
|
||||
#if 0
|
||||
using namespace mijin::pipe;
|
||||
return args
|
||||
| Map(&shellEscape)
|
||||
| Join(" ");
|
||||
#endif
|
||||
}
|
||||
} // namespace mijin
|
||||
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX || MIJIN_TARGET_OS == MIJIN_OS_OSX
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#if !defined(MIJIN_IO_PROCESS_HPP_INCLUDED)
|
||||
#define MIJIN_IO_PROCESS_HPP_INCLUDED 1
|
||||
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include "./stream.hpp"
|
||||
#include "../internal/common.hpp"
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
inline StreamError open(const std::string& command, FileOpenMode mode_) {
|
||||
return open(command.c_str(), mode_);
|
||||
}
|
||||
inline StreamError open(const std::vector<std::string>& args, FileOpenMode mode_);
|
||||
inline StreamError open(std::span<std::string_view> args, FileOpenMode mode_);
|
||||
int close();
|
||||
[[nodiscard]] inline bool isOpen() const { return handle != nullptr; }
|
||||
|
||||
@@ -36,13 +36,19 @@ public:
|
||||
StreamFeatures getFeatures() override;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::string shellEscape(const std::string& arg) MIJIN_NOEXCEPT;
|
||||
[[nodiscard]] std::string makeShellCommand(const std::vector<std::string>& args) MIJIN_NOEXCEPT;
|
||||
[[nodiscard]] std::string shellEscape(std::string_view arg) MIJIN_NOEXCEPT;
|
||||
[[nodiscard]] std::string makeShellCommand(std::span<std::string_view> args) MIJIN_NOEXCEPT;
|
||||
|
||||
StreamError ProcessStream::open(const std::vector<std::string>& args, FileOpenMode mode_)
|
||||
StreamError ProcessStream::open(std::span<std::string_view> args, FileOpenMode mode_)
|
||||
{
|
||||
return open(makeShellCommand(args), mode_);
|
||||
}
|
||||
|
||||
template<typename... TTypes>
|
||||
std::array<std::string_view, sizeof...(TTypes)> makeSVList(TTypes&&... args)
|
||||
{
|
||||
return std::array{std::string_view(std::forward<TTypes>(args))...};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // MIJIN_IO_PROCESS_HPP_INCLUDED
|
||||
|
||||
@@ -113,6 +113,42 @@ StreamError Stream::writeBinaryString(std::string_view str)
|
||||
return writeSpan(str.begin(), str.end());
|
||||
}
|
||||
|
||||
StreamError Stream::readZString(std::string& outString)
|
||||
{
|
||||
char chr = '\0';
|
||||
std::string result;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (isAtEnd())
|
||||
{
|
||||
return StreamError::IO_ERROR;
|
||||
}
|
||||
|
||||
if (StreamError error = read(chr); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
if (chr == '\0')
|
||||
{
|
||||
outString = std::move(result);
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
result.push_back(chr);
|
||||
}
|
||||
}
|
||||
|
||||
StreamError Stream::writeZString(std::string_view str)
|
||||
{
|
||||
static const char ZERO = '\0';
|
||||
|
||||
if (StreamError error = writeRaw(str.data(), str.size() * sizeof(char)); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
return write(ZERO);
|
||||
}
|
||||
|
||||
mijin::Task<StreamError> Stream::c_readBinaryString(std::string& outString)
|
||||
{
|
||||
std::uint32_t length; // NOLINT(cppcoreguidelines-init-variables)
|
||||
@@ -161,76 +197,6 @@ StreamError Stream::getTotalLength(std::size_t& outLength)
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
StreamError Stream::readRest(TypelessBuffer& outBuffer)
|
||||
{
|
||||
// first try to allocate everything at once
|
||||
std::size_t length = 0;
|
||||
if (const StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||
length -= tell();
|
||||
outBuffer.resize(length);
|
||||
if (const StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
// could not determine the size, read chunk-wise
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||
const std::span<std::byte> bufferBytes = outBuffer.makeSpan<std::byte>();
|
||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
mijin::Task<StreamError> Stream::c_readRest(TypelessBuffer& outBuffer)
|
||||
{
|
||||
// first try to allocate everything at once
|
||||
std::size_t length = 0;
|
||||
if (StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||
length -= tell();
|
||||
outBuffer.resize(length);
|
||||
if (StreamError error = co_await c_readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||
{
|
||||
co_return error;
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
// could not determine the size, read chunk-wise
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (StreamError error = co_await c_readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
co_return error;
|
||||
}
|
||||
|
||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||
std::span<std::byte> bufferBytes = outBuffer.makeSpan<std::byte>();
|
||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
StreamError Stream::readLine(std::string& outString)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().readOptions.peek, "Stream needs to support peeking.");
|
||||
@@ -324,6 +290,29 @@ mijin::Task<StreamError> Stream::c_readLine(std::string& outString)
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
StreamError Stream::copyTo(Stream& other)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().read, "Stream must support reading.");
|
||||
MIJIN_ASSERT(other.getFeatures().write, "Other stream must support writing.");
|
||||
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
if (const StreamError error = other.writeRaw(chunk.data(), bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
if (handle) {
|
||||
@@ -475,7 +464,7 @@ StreamFeatures FileStream::getFeatures()
|
||||
if (handle)
|
||||
{
|
||||
return {
|
||||
.read = (mode == FileOpenMode::READ),
|
||||
.read = (mode == FileOpenMode::READ || mode == FileOpenMode::READ_WRITE),
|
||||
.write = (mode == FileOpenMode::WRITE || mode == FileOpenMode::APPEND || mode == FileOpenMode::READ_WRITE),
|
||||
.tell = true,
|
||||
.seek = true,
|
||||
|
||||
@@ -132,8 +132,9 @@ public:
|
||||
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
||||
return readRaw(&*range.begin(), bytes, {.partial = partial}, outBytesRead);
|
||||
}
|
||||
|
||||
StreamError readRaw(TypelessBuffer& buffer, const ReadOptions& options = {})
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError readRaw(BaseTypelessBuffer<TAllocator>& buffer, const ReadOptions& options = {})
|
||||
{
|
||||
return readRaw(buffer.data(), buffer.byteSize(), options);
|
||||
}
|
||||
@@ -144,8 +145,9 @@ public:
|
||||
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
||||
return c_readRaw(&*range.begin(), bytes, options, outBytesRead);
|
||||
}
|
||||
|
||||
mijin::Task<StreamError> c_readRaw(TypelessBuffer& buffer, const ReadOptions& options = {})
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
mijin::Task<StreamError> c_readRaw(BaseTypelessBuffer<TAllocator>& buffer, const ReadOptions& options = {})
|
||||
{
|
||||
return c_readRaw(buffer.data(), buffer.byteSize(), options);
|
||||
}
|
||||
@@ -219,7 +221,7 @@ public:
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
StreamError write(const T& value) requires(std::is_trivial_v<T>)
|
||||
StreamError write(const T& value)
|
||||
{
|
||||
return writeRaw(&value, sizeof(T));
|
||||
}
|
||||
@@ -259,6 +261,9 @@ public:
|
||||
StreamError readBinaryString(std::string& outString);
|
||||
StreamError writeBinaryString(std::string_view str);
|
||||
|
||||
StreamError readZString(std::string& outString);
|
||||
StreamError writeZString(std::string_view str);
|
||||
|
||||
mijin::Task<StreamError> c_readBinaryString(std::string& outString);
|
||||
mijin::Task<StreamError> c_writeBinaryString(std::string_view str);
|
||||
|
||||
@@ -269,17 +274,23 @@ public:
|
||||
inline StreamError writeString(std::string_view str) { return writeBinaryString(str); }
|
||||
|
||||
StreamError getTotalLength(std::size_t& outLength);
|
||||
StreamError readRest(TypelessBuffer& outBuffer);
|
||||
mijin::Task<StreamError> c_readRest(TypelessBuffer& outBuffer);
|
||||
|
||||
StreamError copyTo(Stream& otherStream);
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
mijin::Task<StreamError> c_readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||
|
||||
StreamError readLine(std::string& outString);
|
||||
mijin::Task<StreamError> c_readLine(std::string& outString);
|
||||
|
||||
template<typename TChar = char>
|
||||
StreamError readAsString(std::basic_string<TChar>& outString);
|
||||
template<typename TChar = char, typename TTraits = std::char_traits<TChar>, typename TAllocator = MIJIN_DEFAULT_ALLOCATOR<char>>
|
||||
StreamError readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString);
|
||||
|
||||
template<typename TChar = char>
|
||||
mijin::Task<StreamError> c_readAsString(std::basic_string<TChar>& outString);
|
||||
template<typename TChar = char, typename TTraits = std::char_traits<TChar>, typename TAllocator = MIJIN_DEFAULT_ALLOCATOR<char>>
|
||||
mijin::Task<StreamError> c_readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString);
|
||||
|
||||
StreamError writeText(std::string_view str)
|
||||
{
|
||||
@@ -360,8 +371,80 @@ using StreamResult = ResultBase<TSuccess, StreamError>;
|
||||
// public functions
|
||||
//
|
||||
|
||||
template<typename TChar>
|
||||
StreamError Stream::readAsString(std::basic_string<TChar>& outString)
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError Stream::readRest(BaseTypelessBuffer<TAllocator>& outBuffer)
|
||||
{
|
||||
// first try to allocate everything at once
|
||||
std::size_t length = 0;
|
||||
if (const StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||
length -= tell();
|
||||
outBuffer.resize(length);
|
||||
if (const StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
// could not determine the size, read chunk-wise
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||
const std::span<std::byte> bufferBytes = outBuffer.template makeSpan<std::byte>();
|
||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
mijin::Task<StreamError> Stream::c_readRest(BaseTypelessBuffer<TAllocator>& outBuffer)
|
||||
{
|
||||
// first try to allocate everything at once
|
||||
std::size_t length = 0;
|
||||
if (StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||
length -= tell();
|
||||
outBuffer.resize(length);
|
||||
if (StreamError error = co_await c_readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||
{
|
||||
co_return error;
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
// could not determine the size, read chunk-wise
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (StreamError error = co_await c_readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
co_return error;
|
||||
}
|
||||
|
||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||
std::span<std::byte> bufferBytes = outBuffer.template makeSpan<std::byte>();
|
||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits, typename TAllocator>
|
||||
StreamError Stream::readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString)
|
||||
{
|
||||
static_assert(sizeof(TChar) == 1, "Can only read to 8-bit character types (char, unsigned char or char8_t");
|
||||
|
||||
@@ -396,8 +479,8 @@ StreamError Stream::readAsString(std::basic_string<TChar>& outString)
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
template<typename TChar>
|
||||
mijin::Task<StreamError> Stream::c_readAsString(std::basic_string<TChar>& outString)
|
||||
template<typename TChar, typename TTraits, typename TAllocator>
|
||||
mijin::Task<StreamError> Stream::c_readAsString(std::basic_string<TChar, TTraits, TAllocator>& outString)
|
||||
{
|
||||
static_assert(sizeof(TChar) == 1, "Can only read to 8-bit character types (char, unsigned char or char8_t");
|
||||
|
||||
@@ -463,7 +546,7 @@ inline void throwOnError(mijin::StreamError error)
|
||||
if (error == mijin::StreamError::SUCCESS) {
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error(errorName(error));
|
||||
throw Exception(errorName(error));
|
||||
}
|
||||
|
||||
inline void throwOnError(mijin::StreamError error, std::string message)
|
||||
@@ -471,7 +554,7 @@ inline void throwOnError(mijin::StreamError error, std::string message)
|
||||
if (error == mijin::StreamError::SUCCESS) {
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error(message + ": " + errorName(error));
|
||||
throw Exception(message + ": " + errorName(error));
|
||||
}
|
||||
|
||||
template<typename TSuccess>
|
||||
|
||||
67
source/mijin/logging/debug_output_sink.hpp
Normal file
67
source/mijin/logging/debug_output_sink.hpp
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
#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;
|
||||
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 char_t* formatted) MIJIN_NOEXCEPT override
|
||||
{
|
||||
if constexpr (std::is_same_v<char_t, char>)
|
||||
{
|
||||
OutputDebugStringA(formatted);
|
||||
OutputDebugStringA("\n");
|
||||
}
|
||||
else if constexpr (std::is_same_v<char_t, wchar_t>)
|
||||
{
|
||||
OutputDebugStringW(formatted);
|
||||
OutputDebugStringW(L"\n");
|
||||
}
|
||||
else if constexpr (sizeof(char_t) == sizeof(char))
|
||||
{
|
||||
// char8_t etc.
|
||||
OutputDebugStringA(std::bit_cast<const char*>(formatted));
|
||||
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)
|
||||
52
source/mijin/logging/filters.hpp
Normal file
52
source/mijin/logging/filters.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(MIJIN_LOGGING_FILTERS_HPP_INCLUDED)
|
||||
#define MIJIN_LOGGING_FILTERS_HPP_INCLUDED 1
|
||||
|
||||
#include "./logger.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||
class BaseLevelFilter : public BaseLogFilter<TChar>
|
||||
{
|
||||
public:
|
||||
using base_t = BaseLogFilter<TChar>;
|
||||
using typename base_t::char_t;
|
||||
using typename base_t::message_t;
|
||||
private:
|
||||
int mMinLevel = 0;
|
||||
int mMaxLevel = 0;
|
||||
public:
|
||||
explicit BaseLevelFilter(int minLevel, int maxLevel = std::numeric_limits<int>::max()) MIJIN_NOEXCEPT
|
||||
: mMinLevel(minLevel), mMaxLevel(maxLevel) {}
|
||||
explicit BaseLevelFilter(const BaseLogLevel<char_t>& minLevel, const BaseLogLevel<char_t>& maxLevel = {nullptr, std::numeric_limits<int>::max()}) MIJIN_NOEXCEPT
|
||||
: mMinLevel(minLevel.value), mMaxLevel(maxLevel.value) {}
|
||||
|
||||
[[nodiscard]]
|
||||
int getMinLevel() const MIJIN_NOEXCEPT { return mMinLevel; }
|
||||
|
||||
[[nodiscard]]
|
||||
int getMaxLevel() const MIJIN_NOEXCEPT { return mMaxLevel; }
|
||||
|
||||
void setMinLevel(int level) MIJIN_NOEXCEPT { mMinLevel = level; }
|
||||
|
||||
void setMinLevel(const BaseLogLevel<char_t>& level) MIJIN_NOEXCEPT { mMinLevel = level.value; }
|
||||
|
||||
void setMaxLevel(int level) MIJIN_NOEXCEPT { mMaxLevel = level; }
|
||||
|
||||
void setMaxLevel(const BaseLogLevel<char_t>& level) MIJIN_NOEXCEPT { mMaxLevel = level.value; }
|
||||
|
||||
bool shouldShow(const message_t& message) MIJIN_NOEXCEPT override
|
||||
{
|
||||
return message.level->value >= mMinLevel && message.level->value <= mMaxLevel;
|
||||
}
|
||||
};
|
||||
|
||||
MIJIN_DEFINE_CHAR_VERSIONS(LevelFilter)
|
||||
|
||||
} // namespace mijin
|
||||
|
||||
|
||||
#endif // !defined(MIJIN_LOGGING_STDIO_SINK_HPP_INCLUDED)
|
||||
@@ -92,10 +92,10 @@ public:
|
||||
using string_t = formatter_t::string_t;
|
||||
using typename base_t::message_t;
|
||||
private:
|
||||
not_null_t<formatter_ptr_t> mFormatter;
|
||||
formatter_ptr_t mFormatter;
|
||||
string_t mBuffer;
|
||||
public:
|
||||
explicit BaseFormattingLogSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||
explicit BaseFormattingLogSink(formatter_ptr_t formatter, allocator_t allocator = {})
|
||||
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||
: mFormatter(std::move(formatter)), mBuffer(std::move(allocator))
|
||||
{}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <mutex>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -104,6 +105,20 @@ public:
|
||||
|
||||
MIJIN_DEFINE_CHAR_VERSIONS(LogSink)
|
||||
|
||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE>
|
||||
class BaseLogFilter
|
||||
{
|
||||
public:
|
||||
using char_t = TChar;
|
||||
using message_t = BaseLogMessage<char_t>;
|
||||
|
||||
virtual ~BaseLogFilter() noexcept = default;
|
||||
|
||||
virtual bool shouldShow(const message_t& message) MIJIN_NOEXCEPT = 0;
|
||||
};
|
||||
|
||||
MIJIN_DEFINE_CHAR_VERSIONS(LogFilter)
|
||||
|
||||
#define LOGGER_COMMON_ARGS(chr_type) template<typename T> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR
|
||||
template<typename TChar = MIJIN_DEFAULT_CHAR_TYPE, typename TTraits = std::char_traits<TChar>, LOGGER_COMMON_ARGS(TChar)>
|
||||
class BaseLogger
|
||||
@@ -114,15 +129,22 @@ public:
|
||||
using allocator_t = TAllocator<char_t>;
|
||||
|
||||
using sink_t = BaseLogSink<char_t>;
|
||||
using filter_t = BaseLogFilter<char_t>;
|
||||
using level_t = BaseLogLevel<char_t>;
|
||||
using channel_t = BaseLogChannel<char_t>;
|
||||
using message_t = BaseLogMessage<char_t>;
|
||||
using string_t = std::basic_string<char_t, traits_t, allocator_t>;
|
||||
private:
|
||||
std::vector<sink_t*, TAllocator<sink_t*>> mSinks;
|
||||
struct SinkEntry
|
||||
{
|
||||
sink_t* sink;
|
||||
filter_t* filter;
|
||||
};
|
||||
std::vector<SinkEntry, TAllocator<SinkEntry>> mSinks;
|
||||
mutable std::mutex mMutex;
|
||||
public:
|
||||
explicit BaseLogger(TAllocator<sink_t*> allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<TAllocator<sink_t*>>)
|
||||
: mSinks(std::move(allocator))
|
||||
explicit BaseLogger(TAllocator<void> allocator = {}) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TAllocator<SinkEntry>, TAllocator<void>&&>))
|
||||
: mSinks(TAllocator<SinkEntry>(std::move(allocator)))
|
||||
{}
|
||||
|
||||
BaseLogger(const BaseLogger&) = default;
|
||||
@@ -135,14 +157,25 @@ public:
|
||||
|
||||
void addSink(sink_t& sink)
|
||||
{
|
||||
mSinks.push_back(&sink);
|
||||
std::unique_lock _(mMutex);
|
||||
mSinks.push_back({&sink, nullptr});
|
||||
}
|
||||
|
||||
void addSink(sink_t& sink, filter_t& filter)
|
||||
{
|
||||
std::unique_lock _(mMutex);
|
||||
mSinks.push_back({&sink, &filter});
|
||||
}
|
||||
|
||||
void postMessage(const message_t& message) const MIJIN_NOEXCEPT
|
||||
{
|
||||
for (sink_t* sink: mSinks)
|
||||
std::unique_lock _(mMutex);
|
||||
for (const SinkEntry& entry : mSinks)
|
||||
{
|
||||
sink->handleMessage(message);
|
||||
if (entry.filter != nullptr && !entry.filter->shouldShow(message)) {
|
||||
continue;
|
||||
}
|
||||
entry.sink->handleMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,10 +254,14 @@ MIJIN_DEFINE_LOG_LEVEL(ERROR, MIJIN_LOG_LEVEL_VALUE_ERROR)
|
||||
#define MIJIN_LOG_ALWAYS(level, channel, ...) MIJIN_FUNCNAME_GET_LOGGER().log( \
|
||||
MIJIN_LOG_LEVEL_OBJECT(level), MIJIN_LOG_CHANNEL_OBJECT(channel), std::source_location::current(), __VA_ARGS__ \
|
||||
)
|
||||
#define MIJIN_LOG(level, channel, ...) \
|
||||
#define MIJIN_LOG(level, channel, ...) \
|
||||
if constexpr (MIJIN_LOG_LEVEL_OBJECT(level).value < MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE()) {} \
|
||||
else MIJIN_LOG_ALWAYS(level, channel, __VA_ARGS__)
|
||||
|
||||
#define MIJIN_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_SET_CLASS_LOGGER(loggerExpr) \
|
||||
const auto& MIJIN_FUNCNAME_GET_LOGGER() const noexcept \
|
||||
{ \
|
||||
@@ -235,6 +272,11 @@ auto MIJIN_FUNCNAME_GET_LOGGER = [&]() -> const auto& \
|
||||
{ \
|
||||
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) \
|
||||
int MIJIN_FUNCNAME_MIN_LOG_LEVEL_COMPILE() MIJIN_NOEXCEPT \
|
||||
{ \
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
private:
|
||||
int mMinStderrLevel = MIJIN_LOG_LEVEL_VALUE_WARNING;
|
||||
public:
|
||||
explicit BaseStdioSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||
explicit BaseStdioSink(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)) {}
|
||||
|
||||
@@ -56,6 +56,7 @@ public:
|
||||
{
|
||||
static_assert(always_false_v<char_t>, "Character type not supported.");
|
||||
}
|
||||
std::fflush(stream);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
58
source/mijin/logging/stream_sink.hpp
Normal file
58
source/mijin/logging/stream_sink.hpp
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(MIJIN_LOGGING_STREAM_SINK_HPP_INCLUDED)
|
||||
#define MIJIN_LOGGING_STREAM_SINK_HPP_INCLUDED 1
|
||||
|
||||
#include "./formatting.hpp"
|
||||
#include "../io/stream.hpp"
|
||||
#include "../util/traits.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
template<MIJIN_FORMATTING_SINK_TMPL_ARGS_INIT>
|
||||
requires(allocator_type<TAllocator<TChar>>)
|
||||
class BaseStreamSink : 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 stream_ptr_t = DynamicPointer<Stream>;
|
||||
private:
|
||||
stream_ptr_t mStream;
|
||||
int mMinStderrLevel = MIJIN_LOG_LEVEL_VALUE_WARNING;
|
||||
public:
|
||||
explicit BaseStreamSink(not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||
: base_t(std::move(formatter), std::move(allocator)) {}
|
||||
explicit BaseStreamSink(not_null_t<stream_ptr_t> stream, not_null_t<formatter_ptr_t> formatter, allocator_t allocator = {})
|
||||
MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v<allocator_t>)
|
||||
: base_t(std::move(formatter), std::move(allocator)), mStream(std::move(stream)) {}
|
||||
|
||||
void setStream(not_null_t<stream_ptr_t> stream) {
|
||||
mStream = std::move(stream).release();
|
||||
}
|
||||
|
||||
void handleMessageFormatted(const message_t& /* message */, const char_t* formatted) MIJIN_NOEXCEPT override
|
||||
{
|
||||
if (!mStream) {
|
||||
return;
|
||||
}
|
||||
(void) mStream->writeSpan(std::basic_string_view(formatted));
|
||||
(void) mStream->write('\n');
|
||||
mStream->flush();
|
||||
}
|
||||
};
|
||||
|
||||
#define SINK_SET_ARGS(chr_type) chr_type, std::char_traits<chr_type>, TAllocator, TDeleter
|
||||
|
||||
MIJIN_DEFINE_CHAR_VERSIONS_TMPL(StreamSink, MIJIN_FORMATTING_SINK_COMMON_ARGS, SINK_SET_ARGS)
|
||||
|
||||
#undef SINK_SET_ARGS
|
||||
} // namespace mijin
|
||||
|
||||
|
||||
#endif // !defined(MIJIN_LOGGING_STREAM_SINK_HPP_INCLUDED)
|
||||
@@ -17,7 +17,7 @@ namespace mijin
|
||||
{
|
||||
MIJIN_DEFINE_FLAG(Owning);
|
||||
|
||||
template<typename T, deleter_type<T> TDeleter = std::default_delete<T>>
|
||||
template<typename T, deleter_type<T> TDeleter = AllocatorDeleter<MIJIN_DEFAULT_ALLOCATOR<T>>>
|
||||
class DynamicPointer
|
||||
{
|
||||
public:
|
||||
@@ -35,10 +35,11 @@ public:
|
||||
{
|
||||
MIJIN_ASSERT((std::bit_cast<std::uintptr_t>(ptr) & 1) == 0, "Invalid address, DynamicPointer requires addresses to be divisible by two.");
|
||||
}
|
||||
template<typename TOther, typename TOtherDeleter> requires (std::is_constructible_v<TDeleter, TOtherDeleter&&>)
|
||||
template<typename TOther, typename TOtherDeleter> requires (std::is_assignable_v<T*&, TOther*> && std::is_constructible_v<TDeleter, TOtherDeleter&&>)
|
||||
constexpr DynamicPointer(DynamicPointer<TOther, TOtherDeleter>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_convertible_v<TOtherDeleter, TDeleter>))
|
||||
: mData(std::exchange(other.mData, 0)), mDeleter(std::move(other.mDeleter)) {
|
||||
MIJIN_ASSERT(other.mData == 0, "");
|
||||
: DynamicPointer(other.get(), other.isOwning() ? Owning::YES : Owning::NO, TDeleter(std::move(other.mDeleter)))
|
||||
{
|
||||
other.mData = 0;
|
||||
}
|
||||
constexpr ~DynamicPointer() noexcept
|
||||
{
|
||||
@@ -64,6 +65,15 @@ public:
|
||||
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>)
|
||||
auto operator<=>(const DynamicPointer<TOther, TOtherDeleter>& other) MIJIN_NOEXCEPT
|
||||
{
|
||||
@@ -155,9 +165,9 @@ bool operator!=(std::nullptr_t, const DynamicPointer<T, TDeleter>& pointer) MIJI
|
||||
}
|
||||
|
||||
template<typename T, typename... TArgs>
|
||||
DynamicPointer<T, std::default_delete<T>> makeDynamic(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
||||
DynamicPointer<T> makeDynamic(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TArgs...>))
|
||||
{
|
||||
return DynamicPointer<T, std::default_delete<T>>(new T(std::forward<TArgs>(args)...), Owning::YES);
|
||||
return DynamicPointer<T>(new T(std::forward<TArgs>(args)...), Owning::YES);
|
||||
}
|
||||
|
||||
template<typename T, allocator_type_for<T> TAllocator, typename... TArgs>
|
||||
@@ -171,6 +181,12 @@ DynamicPointer<T, AllocatorDeleter<TAllocator>> makeDynamicWithAllocator(TAlloca
|
||||
}
|
||||
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
|
||||
|
||||
#endif // !defined(MIJIN_MEMORY_DYNAMIC_POINTER_HPP_INCLUDED)
|
||||
|
||||
@@ -326,6 +326,7 @@ public:
|
||||
// couldn't allocate the snapshot
|
||||
return {};
|
||||
}
|
||||
::new (snapshotData) StackAllocatorSnapshotData;
|
||||
StackAllocatorSnapshot snapshot;
|
||||
snapshot.data = snapshotData;
|
||||
if (firstChunk_ != prevFirst)
|
||||
@@ -442,6 +443,7 @@ public:
|
||||
Chunk* snapshotChunk = findChunk(snapshot.data);
|
||||
MIJIN_ASSERT_FATAL(snapshotChunk != nullptr, "Snapshot not in chunks?");
|
||||
snapshotChunk->allocated -= calcSnapshotSize(snapshot->numChunks); // note: this might miss the alignment bytes of the snapshot, but that should be fine
|
||||
snapshot.data->~StackAllocatorSnapshotData();
|
||||
}
|
||||
private:
|
||||
void initAndAddChunk(Chunk* newChunk) noexcept
|
||||
|
||||
@@ -44,7 +44,7 @@ struct HTTPRequestOptions
|
||||
{
|
||||
std::string method = "GET";
|
||||
std::multimap<std::string, std::string> headers;
|
||||
TypelessBuffer body;
|
||||
BaseTypelessBuffer<std::allocator> body;
|
||||
};
|
||||
|
||||
struct HTTPResponse
|
||||
@@ -53,7 +53,7 @@ struct HTTPResponse
|
||||
unsigned status;
|
||||
std::string statusMessage;
|
||||
std::multimap<std::string, std::string> headers;
|
||||
TypelessBuffer body;
|
||||
BaseTypelessBuffer<std::allocator> body;
|
||||
};
|
||||
|
||||
class HTTPStream
|
||||
|
||||
@@ -60,6 +60,32 @@ public:
|
||||
[[nodiscard]] const TError& getError() const MIJIN_NOEXCEPT { return std::get<TError>(state_); }
|
||||
};
|
||||
|
||||
namespace impl
|
||||
{
|
||||
struct ResultSuccess {};
|
||||
}
|
||||
|
||||
template<typename TError>
|
||||
class ResultBase<void, TError> : public ResultBase<impl::ResultSuccess, TError>
|
||||
{
|
||||
public:
|
||||
ResultBase() MIJIN_NOEXCEPT : ResultBase<impl::ResultSuccess, TError>(impl::ResultSuccess{}) {}
|
||||
ResultBase(const ResultBase&) MIJIN_NOEXCEPT = default;
|
||||
ResultBase(ResultBase&&) MIJIN_NOEXCEPT = default;
|
||||
ResultBase(TError errorValue) MIJIN_NOEXCEPT : ResultBase<impl::ResultSuccess, TError>(std::move(errorValue)) {}
|
||||
|
||||
ResultBase& operator=(const ResultBase&) = default;
|
||||
ResultBase& operator=(ResultBase&&) = default;
|
||||
|
||||
impl::ResultSuccess& operator*() MIJIN_NOEXCEPT = delete;
|
||||
const impl::ResultSuccess& operator*() const MIJIN_NOEXCEPT = delete;
|
||||
impl::ResultSuccess* operator->() MIJIN_NOEXCEPT = delete;
|
||||
const impl::ResultSuccess* operator->() const MIJIN_NOEXCEPT = delete;
|
||||
|
||||
[[nodiscard]] impl::ResultSuccess& getValue() MIJIN_NOEXCEPT = delete;
|
||||
[[nodiscard]] const impl::ResultSuccess& getValue() const MIJIN_NOEXCEPT = delete;
|
||||
};
|
||||
|
||||
struct ResultError
|
||||
{
|
||||
std::string message;
|
||||
@@ -73,7 +99,7 @@ struct ResultError
|
||||
ResultError& operator=(ResultError&&) MIJIN_NOEXCEPT = default;
|
||||
};
|
||||
|
||||
template<typename TSuccess>
|
||||
template<typename TSuccess = void>
|
||||
using Result = ResultBase<TSuccess, ResultError>;
|
||||
|
||||
//
|
||||
|
||||
@@ -160,6 +160,9 @@ public:
|
||||
[[nodiscard]]
|
||||
constexpr const T& get() const MIJIN_NOEXCEPT { return base_; }
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr T release() && MIJIN_NOEXCEPT { return std::exchange(base_, nullptr); }
|
||||
|
||||
template<nullable_type TOther>
|
||||
friend class NotNullable;
|
||||
};
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
|
||||
#include "../debug/assert.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
|
||||
|
||||
@@ -4,8 +4,11 @@
|
||||
#ifndef MIJIN_UTIL_EXCEPTION_HPP_INCLUDED
|
||||
#define MIJIN_UTIL_EXCEPTION_HPP_INCLUDED 1
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "../detect.hpp"
|
||||
#include "../debug/stacktrace.hpp"
|
||||
|
||||
namespace mijin
|
||||
@@ -73,5 +76,76 @@ 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
|
||||
|
||||
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
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#define MIJIN_UTIL_HASH_HPP_INCLUDED 1
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
@@ -16,4 +18,21 @@ inline void hashCombine(std::size_t& seed, const T& value, const THasher& hasher
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... T>
|
||||
struct std::hash<std::tuple<T...>>
|
||||
{
|
||||
std::size_t operator()(const std::tuple<T...>& tuple) const noexcept
|
||||
{
|
||||
return hashImpl(tuple, std::index_sequence_for<T...>());
|
||||
}
|
||||
|
||||
template<std::size_t... indices>
|
||||
std::size_t hashImpl(const std::tuple<T...>& tuple, std::index_sequence<indices...>) const noexcept
|
||||
{
|
||||
std::size_t result = 0;
|
||||
(mijin::hashCombine(result, std::get<indices>(tuple)), ...);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // MIJIN_UTIL_HASH_HPP_INCLUDED
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <climits>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
@@ -32,6 +33,16 @@ namespace mijin
|
||||
// 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
|
||||
//
|
||||
@@ -97,6 +108,361 @@ struct SplitOptions
|
||||
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
|
||||
//
|
||||
@@ -272,13 +638,6 @@ std::basic_string_view<TChar, TTraits> trimImpl(std::basic_string_view<TChar, TT
|
||||
{
|
||||
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>
|
||||
@@ -295,6 +654,61 @@ template<std::size_t count, typename TLeft, typename TRight>
|
||||
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>
|
||||
[[nodiscard]]
|
||||
auto trimPrefix(TString&& string, TChars&& chars)
|
||||
@@ -306,7 +720,7 @@ template<typename TString>
|
||||
[[nodiscard]]
|
||||
auto trimPrefix(TString&& string)
|
||||
{
|
||||
return trimPrefix(string, detail::DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||
return trimPrefix(string, DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||
}
|
||||
|
||||
template<typename TString, typename TChars>
|
||||
@@ -320,7 +734,7 @@ template<typename TString>
|
||||
[[nodiscard]]
|
||||
auto trimSuffix(TString&& string)
|
||||
{
|
||||
return trimSuffix(string, detail::DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||
return trimSuffix(string, DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||
}
|
||||
|
||||
template<typename TString, typename TChars>
|
||||
@@ -334,7 +748,7 @@ template<typename TString>
|
||||
[[nodiscard]]
|
||||
auto trim(TString&& string)
|
||||
{
|
||||
return trim(string, detail::DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||
return trim(string, DEFAULT_TRIM_CHARS<str_char_type_t<TString>>);
|
||||
}
|
||||
|
||||
template<typename TLeft, typename TRight>
|
||||
@@ -486,21 +900,6 @@ auto operator|(TIterable&& iterable, const Join& joiner)
|
||||
}
|
||||
} // namespace pipe
|
||||
|
||||
struct [[nodiscard]] ConvertCharTypeResult
|
||||
{
|
||||
unsigned numRead = 0;
|
||||
unsigned numWritten = 0;
|
||||
|
||||
constexpr operator bool() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return numRead != 0 || numWritten != 0;
|
||||
}
|
||||
constexpr bool operator !() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return !static_cast<bool>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TFrom, typename TTo>
|
||||
ConvertCharTypeResult convertCharType(const TFrom* chrFrom, std::size_t numFrom, TTo* outTo, std::size_t numTo, std::mbstate_t& mbstate) MIJIN_NOEXCEPT
|
||||
{
|
||||
@@ -530,7 +929,7 @@ ConvertCharTypeResult convertCharType(const TFrom* chrFrom, std::size_t numFrom,
|
||||
if (numTo < MB_CUR_MAX)
|
||||
{
|
||||
char tmpBuf[MB_LEN_MAX];
|
||||
const ConvertCharTypeResult result = convertCharType(chrFrom, tmpBuf, mbstate);
|
||||
const ConvertCharTypeResult result = convertCharType(chrFrom, numFrom, tmpBuf, MB_LEN_MAX, mbstate);
|
||||
if (result && result.numWritten <= numTo)
|
||||
{
|
||||
std::memcpy(outTo, tmpBuf, result.numWritten);
|
||||
@@ -621,6 +1020,54 @@ bool convertStringType(const TFrom* strFrom, std::basic_string<TTo, TToTraits, T
|
||||
{
|
||||
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
|
||||
|
||||
#endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)
|
||||
|
||||
@@ -192,6 +192,18 @@ struct is_type_member<TElement, TCollection<Ts...>>
|
||||
template<typename TElement, typename TCollection>
|
||||
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>
|
||||
using copy_const_t = std::conditional_t<std::is_const_v<TFrom>, std::add_const_t<TTo>, std::remove_const_t<TTo>>;
|
||||
|
||||
|
||||
@@ -22,3 +22,7 @@
|
||||
#if defined(RELATIVE)
|
||||
#undef RELATIVE
|
||||
#endif
|
||||
|
||||
#if defined(DEBUG)
|
||||
#undef DEBUG
|
||||
#endif
|
||||
|
||||
@@ -80,7 +80,7 @@ class FileSystemAdapter
|
||||
public:
|
||||
virtual ~FileSystemAdapter() = default;
|
||||
|
||||
[[deprecated("Will be removed ASAP")]]
|
||||
[[deprecated("Will be removed ASAP, use getKnownFolder(KnownFolder::USER_HOME) from platform/folders.hpp instead.")]]
|
||||
[[nodiscard]] virtual fs::path getHomeFolder() { return {}; } // TODO: get rid of this ...
|
||||
[[nodiscard]] virtual std::vector<FileInfo> listFiles(const fs::path& folder) = 0;
|
||||
[[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0;
|
||||
|
||||
@@ -21,8 +21,8 @@ struct MemoryFile
|
||||
};
|
||||
struct MemoryFolder
|
||||
{
|
||||
VectorMap<std::string, MemoryFile> files;
|
||||
VectorMap<std::string, MemoryFolder> folders;
|
||||
VectorMap<std::string, MemoryFile, std::allocator<std::string>, std::allocator<MemoryFile>> files; // TODO: make the FS library allocator aware
|
||||
VectorMap<std::string, MemoryFolder, std::allocator<std::string>, std::allocator<MemoryFolder>> folders;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ fs::path RelativeFileSystemAdapter<TWrapped>::appendPath(const fs::path& other)
|
||||
else {
|
||||
combinedPath /= other;
|
||||
}
|
||||
return combinedPath;
|
||||
return combinedPath.lexically_normal();
|
||||
}
|
||||
|
||||
namespace vfs_pipe
|
||||
|
||||
Reference in New Issue
Block a user