Compare commits

...

16 Commits
master ... dev

Author SHA1 Message Date
680acdb332 Fixed compilation of boxed_object.hpp in non-debug builds. 2025-07-25 00:51:53 +02:00
3d507ddef7 Finally implemented object pools. 2025-07-25 00:51:32 +02:00
d35140d278 Added missing template parameter. 2025-07-17 23:47:00 +02:00
933a36d992 Added formattable utility. 2025-07-17 10:56:52 +02:00
d3659f11f7 Implemented getCPUTicks() and getCPUTicksPerSecond() for Windows. 2025-07-17 10:56:39 +02:00
d207774868 Added missing include. 2025-07-17 10:56:15 +02:00
54d9cd327a Added undef for VOID. 2025-07-17 07:51:19 +02:00
93ef90aeb8 Added function_traits. 2025-07-16 21:07:50 +02:00
ba6ffa6c42 Added getCPUTicks() and getCPUTicksPerSeconds() functions. 2025-07-16 21:07:50 +02:00
2cf270df84 Added MIJIN_FUNCNAME() macro. 2025-07-16 21:07:50 +02:00
9808fcf50e Removed unused MIJIN_FUNC() macro. 2025-07-16 21:07:50 +02:00
e91924e049 Added FixedArrayOutputIterator and use it for logging without heap allocations. 2025-07-16 21:07:50 +02:00
79f49829d3 Added utility type to convert variant members via std::visit. 2025-07-16 21:07:50 +02:00
042e0d2465 Added explicit constructor for casting between related not-nullable pointers. 2025-07-16 21:07:50 +02:00
018c75a5ed First implementation of custom path type. 2025-07-16 21:07:50 +02:00
33bc48dd58 Added Path type (WIP). 2025-07-16 21:07:50 +02:00
24 changed files with 1340 additions and 249 deletions

View File

@ -16,6 +16,7 @@ mijin_sources = Split("""
source/mijin/platform/folders.cpp
source/mijin/util/os.cpp
source/mijin/types/name.cpp
source/mijin/types/path.cpp
source/mijin/virtual_filesystem/filesystem.cpp
source/mijin/virtual_filesystem/memory.cpp
source/mijin/virtual_filesystem/stacked.cpp

View File

@ -68,12 +68,12 @@ public:
BoxedObject(const BoxedObject&) = delete;
BoxedObject(BoxedObject&&) = delete;
#if MIJIN_BOXED_OBJECT_DEBUG
constexpr ~BoxedObject() noexcept
{
#if MIJIN_BOXED_OBJECT_DEBUG
MIJIN_ASSERT(!constructed, "BoxedObject::~BoxedObject(): Object has not been destroyed prior to destructor!");
}
#endif
}
BoxedObject& operator=(const BoxedObject&) = delete;
BoxedObject& operator=(BoxedObject&&) = delete;

View File

@ -251,12 +251,13 @@ public:
return eraseImpl(idx, count);
}
template<std::equality_comparable_with<TKey> T>
[[nodiscard]]
iterator find(const TKey& key) MIJIN_NOEXCEPT
iterator find(const T& keyValue) MIJIN_NOEXCEPT
{
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
{
if (keys_[idx] == key)
if (keys_[idx] == keyValue)
{
return iterator(&keys_[idx], &values_[idx]);
}
@ -264,12 +265,13 @@ public:
return end();
}
template<std::equality_comparable_with<TKey> T>
[[nodiscard]]
const_iterator find(const TKey& key) const MIJIN_NOEXCEPT
const_iterator find(const T& keyValue) const MIJIN_NOEXCEPT
{
for (std::size_t idx = 0; idx < keys_.size(); ++idx)
{
if (keys_[idx] == key)
if (keys_[idx] == keyValue)
{
return const_iterator(&keys_[idx], &values_[idx]);
}
@ -277,10 +279,11 @@ public:
return end();
}
template<std::equality_comparable_with<TKey> T>
[[nodiscard]]
bool contains(const TKey& key) const MIJIN_NOEXCEPT
bool contains(const T& keyValue) const MIJIN_NOEXCEPT
{
return std::ranges::contains(keys_, key);
return std::ranges::contains(keys_, keyValue);
}
private:
iterator eraseImpl(std::ptrdiff_t idx, std::ptrdiff_t count = 1) MIJIN_NOEXCEPT

View File

@ -19,7 +19,6 @@
#pragma comment(lib, "kernel32")
extern "C" __declspec(dllimport) void __stdcall DebugBreak();
#define MIJIN_TRAP() DebugBreak()
#define MIJIN_FUNC() __FUNCSIG__
#else // _WIN32
#include <unistd.h>
#define MIJIN_TRAP() \
@ -34,7 +33,6 @@ extern "C" __declspec(dllimport) void __stdcall DebugBreak();
: "rcx", "r11", "memory" \
); \
}
#define MIJIN_FUNC() "" // TODO: __PRETTY_FUNCTION__ is not working for some reason -.-
#endif // !_WIN32
namespace mijin

View File

@ -12,6 +12,7 @@
#include <vector>
#include "../internal/common.hpp"
#include "../util/annot.hpp"
#include "../util/iterators.hpp"
namespace mijin
{
@ -148,11 +149,8 @@ public:
{}
BaseLogger(const BaseLogger&) = default;
BaseLogger(BaseLogger&&) = default;
BaseLogger& operator=(const BaseLogger&) = default;
BaseLogger& operator=(BaseLogger&&) = default;
void addSink(sink_t& sink)
@ -191,12 +189,28 @@ public:
template<typename... TArgs>
void log(const level_t& level, const channel_t& channel, std::source_location sourceLocation,
std::basic_format_string<char_t, std::type_identity_t<TArgs>...> fmt, TArgs&& ... args) const
MIJIN_NOEXCEPT_IF(noexcept(std::declval<allocator_t>().allocate(1)))
std::basic_format_string<char_t, std::type_identity_t<TArgs>...> fmt, TArgs&& ... args) const MIJIN_NOEXCEPT
{
string_t buffer(allocator_t(mSinks.get_allocator()));
std::format_to(std::back_inserter(buffer), fmt, std::forward<TArgs>(args)...);
log(level, channel, std::move(sourceLocation), buffer.c_str());
// TODO: make the logger use a traits struct to make this adjustable
static constexpr std::size_t BUFFER_SIZE = 256;
std::array<char_t, BUFFER_SIZE> buffer;
// first try to write into a buffer on the stack
FixedArrayOutputIterator itAfter = std::format_to(FixedArrayOutputIterator(buffer), fmt, std::forward<TArgs>(args)...);
*itAfter = '\0';
++itAfter;
if (!itAfter.didOverflow())
{
log(level, channel, std::move(sourceLocation), buffer.data());
return;
}
// if that didn't work, allocate more space
const std::size_t newBufferSize = itAfter.getCounter();
char_t* newBuffer = static_cast<char_t*>(alloca(newBufferSize * sizeof(char_t)));
const std::format_to_n_result result = std::format_to_n(newBuffer, newBufferSize - 1, fmt, std::forward<TArgs>(args)...);
*result.out = '\0';
log(level, channel, std::move(sourceLocation), newBuffer);
}
};

View File

@ -7,56 +7,317 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <vector>
#include "../internal/common.hpp"
#include "../debug/assert.hpp"
#include "../util/align.hpp"
namespace mijin
{
template<typename TObject, std::size_t OBJECTS_PER_PAGE = 1024>
class ObjectPool
namespace impl
{
private:
struct GapInfo
struct ObjectPoolGap
{
std::uint32_t objectCount;
std::uint32_t nextGap;
};
static constexpr std::size_t ALIGN = std::max(alignof(TObject), alignof(GapInfo));
std::uint16_t nextGapOffset;
std::uint16_t remainingObjects;
struct alignas(ALIGN) ObjectData
{
std::array<std::byte, sizeof(TObject)> bytes;
};
struct Page
{
std::uint32_t freeOffset = 0;
std::array<ObjectData, OBJECTS_PER_PAGE> data;
};
std::vector<std::unique_ptr<Page>> pages;
public:
[[nodiscard]] TObject* allocate(std::size_t count = 1) noexcept;
void free(TObject* object, std::size_t count = 1) noexcept;
ObjectPoolGap* nextGap() noexcept {
return nextGapOffset == 0 ? nullptr : reinterpret_cast<ObjectPoolGap*>(reinterpret_cast<std::byte*>(this) + nextGapOffset);
}
};
template<typename TObject, std::size_t OBJECTS_PER_PAGE>
TObject* ObjectPool<TObject, OBJECTS_PER_PAGE>::allocate(std::size_t count) noexcept
struct ObjectPoolPage
{
MIJIN_ASSERT(count <= OBJECTS_PER_PAGE, "Cannot allocate more than OBJECTS_PER_PAGE elements at once!");
ObjectPoolGap* firstGap;
ObjectPoolPage* nextPage = nullptr;
std::array<std::byte, OBJECTS_PER_PAGE * MIJIN_STRIDEOF(TObject)> data;
ObjectPoolPage() noexcept
{
firstGap = reinterpret_cast<ObjectPoolGap*>(data.data());
firstGap->nextGapOffset = 0;
firstGap->remainingObjects = OBJECTS_PER_PAGE;
}
};
}
template<typename TObject, std::size_t OBJECTS_PER_PAGE>
class ObjectPoolIterator
{
public:
using value_type = TObject;
using difference_type = std::ptrdiff_t;
private:
using gap_t = impl::ObjectPoolGap;
using page_t = impl::ObjectPoolPage<TObject, OBJECTS_PER_PAGE>;
page_t* currentPage_ = nullptr;
gap_t* currentGap_ = nullptr;
std::uint16_t currentObject_ = 0;
explicit ObjectPoolIterator(page_t& firstPage) noexcept
{
seekNextOnPage(firstPage);
}
public:
ObjectPoolIterator() noexcept = default;
ObjectPoolIterator(const ObjectPoolIterator&) noexcept = default;
ObjectPoolIterator& operator=(const ObjectPoolIterator&) noexcept = default;
auto operator<=>(const ObjectPoolIterator&) const noexcept = default;
TObject& operator*() const noexcept { return getObject(); }
TObject* operator->() const noexcept { return &getObject(); }
ObjectPoolIterator& operator++() noexcept
{
seekNext();
return *this;
}
ObjectPoolIterator operator++(int) const noexcept
{
ObjectPoolIterator copy(*this);
++(*this);
return copy;
}
private:
TObject& getObject() const noexcept
{
MIJIN_ASSERT(currentGap_ != nullptr, "Attempting to dereference an invalid iterator.");
return *(reinterpret_cast<TObject*>(currentGap_) + currentGap_->remainingObjects + currentObject_);
}
bool seekNextOnPage(page_t& firstPage) noexcept
{
for (page_t* page = &firstPage; page != nullptr; page = page->nextPage)
{
if (page->firstGap != nullptr && seekNextInGap(*page->firstGap))
{
currentPage_ = page;
return true;
}
}
return false;
}
bool seekNextInGap(gap_t& firstGap) noexcept
{
for (gap_t* gap = &firstGap; gap != nullptr; gap = gap->nextGap())
{
if (gap->remainingObjects < OBJECTS_PER_PAGE)
{
currentGap_ = gap;
currentObject_ = 0;
return true;
}
}
return false;
}
void seekNext() noexcept
{
if (++currentObject_ < OBJECTS_PER_PAGE - currentGap_->remainingObjects)
{
return;
}
if (currentGap_->nextGapOffset != 0 && seekNextInGap(*currentGap_->nextGap())) {
return;
}
if (currentPage_->nextPage != nullptr && seekNextOnPage(*currentPage_->nextPage)) {
return;
}
currentPage_ = nullptr;
currentGap_ = nullptr;
currentObject_ = 0;
}
template<typename TObject2, std::size_t OBJECTS_PER_PAGE2, template<typename> typename TAllocator>
friend class ObjectPool;
};
template<typename TObject, std::size_t OBJECTS_PER_PAGE = 1024, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
class ObjectPool
{
public:
using iterator = ObjectPoolIterator<TObject, OBJECTS_PER_PAGE>;
using const_iterator = iterator;
private:
using gap_t = impl::ObjectPoolGap;
using page_t = impl::ObjectPoolPage<TObject, OBJECTS_PER_PAGE>;
static_assert(sizeof(gap_t) <= sizeof(TObject));
[[no_unique_address]] TAllocator<page_t> allocator_;
page_t* firstPage = nullptr;
public:
explicit ObjectPool(TAllocator<void> allocator = {}) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<TAllocator<page_t>, TAllocator<void>&&>))
: allocator_(std::move(allocator))
{
}
ObjectPool(const ObjectPool&) = delete;
ObjectPool(ObjectPool&&) = default;
ObjectPool& operator=(const ObjectPool&) = delete;
ObjectPool& operator=(ObjectPool&&) = default;
[[nodiscard]]
TObject* allocate(std::size_t count = 1) noexcept
{
MIJIN_ASSERT(count <= OBJECTS_PER_PAGE, "Cannot allocate that many objects at once.");
// first try to find a free spot in the existing pages
for (std::unique_ptr<Page>& page : pages)
for (page_t* page = firstPage; page != nullptr; page = page->nextPage)
{
std::uint32_t offset = page->freeOffset;
while (offset < OBJECTS_PER_PAGE)
{
GapInfo& gapInfo = *std::bit_cast<GapInfo*>(&page->data[offset]);
if (TObject* result = allocateFromPage(*page, count); result != nullptr) {
return result;
}
}
// nothing found in the existing pages, allocate a new one and return memory from there
page_t* newPage = ::new (allocator_.allocate(1)) page_t;
if (firstPage == nullptr)
{
firstPage = newPage;
}
else
{
page_t* lastPage = firstPage;
for (; lastPage->nextPage != nullptr; lastPage = lastPage->nextPage);
lastPage->nextPage = newPage;
}
return allocateFromPage(*newPage, count);
}
void deallocate(TObject* object, std::size_t count = 1) noexcept
{
std::byte* const objectPtr = reinterpret_cast<std::byte*>(object); // for easier comparison
for (page_t* page = firstPage; page != nullptr; page = page->nextPage)
{
// first find the page it's in
std::byte* pageStart = page->data.data();
std::byte* pageEnd = pageStart + (OBJECTS_PER_PAGE * sizeof(TObject));
if (objectPtr >= pageStart && objectPtr <= pageEnd)
{
// then the corresponding gap
if (page->firstGap == nullptr)
{
// everything is used, create a new gap
gap_t* newGap = reinterpret_cast<gap_t*>(objectPtr);
newGap->remainingObjects = count;
newGap->nextGapOffset = 0;
page->firstGap = newGap;
return;
}
for (gap_t* gap = page->firstGap; gap != nullptr; gap = gap->nextGap())
{
std::byte* gapStart = reinterpret_cast<std::byte*>(gap);
std::byte* gapEnd = gap->nextGapOffset == 0 ? pageEnd : gapStart + gap->nextGapOffset;
if (objectPtr > gapStart && objectPtr < gapEnd)
{
if (objectPtr + (count * sizeof(TObject)) == gapEnd)
{
// deallocating right from the end -> just increase the gap remainingObjects
gap->remainingObjects += count;
mergeGaps(gap);
}
else
{
// deallocating from the middle -> create a new gap
gap_t* newGap = reinterpret_cast<gap_t*>(objectPtr);
newGap->remainingObjects = count;
newGap->nextGapOffset = gap->nextGapOffset == 0 ? 0 : (gapEnd - reinterpret_cast<std::byte*>(newGap));
gap->nextGapOffset -= objectPtr - gapStart;
mergeGaps(newGap);
}
return;
}
}
}
}
}
template<typename... TArgs>
[[nodiscard]]
TObject* create(TArgs&&... args) noexcept
{
TObject* result = allocate();
if (result == nullptr) {
return nullptr;
}
return ::new (result) TObject(std::forward<TArgs>(args)...);
}
[[nodiscard]]
TObject* createMultiple(std::size_t count = 1) noexcept
{
TObject* result = allocate(count);
if (result == nullptr) {
return nullptr;
}
return ::new (result) TObject[count];
}
void destroy(TObject* ptr, std::size_t count = 1) noexcept
{
for (std::size_t idx = 0; idx < count; ++idx) {
ptr[idx].~TObject();
}
deallocate(ptr, count);
}
[[nodiscard]]
iterator begin() const noexcept { return firstPage != nullptr ? iterator(*firstPage) : iterator(); }
[[nodiscard]]
iterator end() const noexcept { return {}; }
private:
TObject* allocateFromPage(page_t& page, std::size_t count)
{
gap_t* previousGap = nullptr;
for (gap_t* gap = page.firstGap; gap != nullptr; previousGap = gap, gap = gap->nextGap())
{
if (gap->remainingObjects == count)
{
// exactly the correct size
if (previousGap != nullptr) {
previousGap->nextGapOffset += gap->nextGapOffset;
}
else {
page.firstGap = gap->nextGap();
}
return reinterpret_cast<TObject*>(gap);
}
else if (gap->remainingObjects > count)
{
// still enough space
TObject* memory = reinterpret_cast<TObject*>(gap) + gap->remainingObjects - count;
gap->remainingObjects -= count;
return reinterpret_cast<TObject*>(memory);
}
}
return nullptr;
}
void mergeGaps(gap_t* start) noexcept
{
while (start->nextGapOffset != 0 && start->remainingObjects == start->nextGapOffset * sizeof(TObject))
{
gap_t& next = *start->nextGap();
start->remainingObjects += start->nextGap()->remainingObjects;
if (next.nextGapOffset == 0) {
start->nextGapOffset = 0;
}
else {
start->nextGapOffset += next.nextGapOffset;
}
}
}
};
} // namespace mijin
#endif // !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED)

View File

@ -0,0 +1,40 @@
#include "./path.hpp"
#include <filesystem>
namespace fs = std::filesystem;
namespace mijin
{
//
// internal defines
//
//
// internal constants
//
//
// internal types
//
//
// internal variables
//
//
// internal functions
//
//
// public functions
//
NativePath getWorkingDirectory()
{
return fs::current_path().generic_string();
}
} // namespace mijin

511
source/mijin/types/path.hpp Normal file
View File

@ -0,0 +1,511 @@
#pragma once
#if !defined(MIJIN_TYPES_PATH_HPP_INCLUDED)
#define MIJIN_TYPES_PATH_HPP_INCLUDED 1
#include "../debug/assert.hpp"
#include "../internal/common.hpp"
#include "../util/traits.hpp"
#include <ranges>
#include <string>
#include <type_traits>
namespace mijin
{
template<typename T>
concept BasePathType = requires(const T& path)
{
typename T::traits_t;
typename T::char_t;
typename T::char_traits_t;
typename T::allocator_t;
typename T::string_t;
typename T::string_view_t;
typename T::size_type;
typename T::difference_type;
typename T::path_view_t;
{ T::SEPARATOR } -> std::convertible_to<typename T::char_t>;
{ path.stringView() } -> std::convertible_to<typename T::string_view_t>;
};
template<typename T>
concept PathType = BasePathType<T> && std::is_same_v<typename T::char_t, char>;
template<typename T>
concept WPathType = BasePathType<T> && std::is_same_v<typename T::char_t, wchar_t>;
template<typename TChar, TChar separator = TChar('/')>
struct DefaultPathTraits
{
using char_t = TChar;
using char_traits_t = std::char_traits<TChar>;
using allocator_t = std::allocator<TChar>;
using string_t = std::basic_string<char_t, char_traits_t, allocator_t>;
using string_view_t = std::basic_string_view<char_t, char_traits_t>;
using size_type = string_view_t::size_type;
using difference_type = string_view_t::difference_type;
static constexpr char_t SEPARATOR = separator;
static constexpr bool isAbsolute(string_view_t path) MIJIN_NOEXCEPT
{
return !path.empty() && path[0] == SEPARATOR;
}
};
template<typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
class BasePathView;
template<typename TConcrete, typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
class MixinPath;
namespace impl
{
template<typename TChar, typename TTraits, TChar separator>
class BasePathIterator
{
private:
std::string_view full_;
std::string_view::iterator pos_;
std::string_view::iterator next_;
BasePathIterator(std::string_view full, std::string_view::iterator pos) MIJIN_NOEXCEPT : full_(full), pos_(pos) {
if (pos != full_.end() && *pos == separator) {
++pos;
}
findNext();
}
public:
BasePathIterator() MIJIN_NOEXCEPT : pos_(full_.end()), next_(full_.end()) {}
BasePathIterator(const BasePathIterator&) noexcept = default;
BasePathIterator& operator=(const BasePathIterator&) noexcept = default;
bool operator==(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ == other.pos_; }
bool operator!=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ != other.pos_; }
bool operator<(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ < other.pos_; }
bool operator<=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ <= other.pos_; }
bool operator>(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ > other.pos_; }
bool operator>=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ >= other.pos_; }
std::string_view operator*() const MIJIN_NOEXCEPT
{
MIJIN_ASSERT(pos_ != full_.end(), "Dereferencing an invalid iterator.");
return {pos_, next_};
}
BasePathIterator& operator++() MIJIN_NOEXCEPT
{
MIJIN_ASSERT(pos_ != full_.end(), "Iterating past end.");
if (next_ == full_.end()) {
pos_ = full_.end();
}
else
{
pos_ = std::next(next_);
findNext();
}
return *this;
}
BasePathIterator operator++(int) const MIJIN_NOEXCEPT
{
BasePathIterator copy(*this);
++copy;
return copy;
}
// TODO
// BasePathIterator& operator--() MIJIN_NOEXCEPT
// {
// MIJIN_ASSERT(pos_ != full_.begin(), "Iterating past begin.");
// next_ = std::prev(pos_);
// pos_ = std::find(std::reverse_iterator(next_), std::reverse_iterator(full_.begin()), separator).base();
// }
private:
void findNext()
{
next_ = std::find(pos_, full_.end(), separator);
}
template<typename TConcrete, typename TCharOther, typename TTraitsOther>
friend class ::mijin::MixinPath;
};
}
template<typename TConcrete, typename TChar, typename TTraits>
class MixinPath
{
public:
using traits_t = TTraits;
using char_t = typename traits_t::char_t;
using char_traits_t = typename traits_t::char_traits_t;
using allocator_t = typename traits_t::allocator_t;
using string_t = typename traits_t::string_t;
using string_view_t = typename traits_t::string_view_t;
using size_type = typename traits_t::size_type;
using difference_type = typename traits_t::difference_type;
using path_view_t = BasePathView<TChar, traits_t>;
using iterator = impl::BasePathIterator<char_t, char_traits_t, traits_t::SEPARATOR>;
using const_iterator = iterator;
static constexpr char_t SEPARATOR = traits_t::SEPARATOR;
constexpr bool operator==(string_view_t stringView) const MIJIN_NOEXCEPT { return stringView() == stringView; }
constexpr bool operator==(const TChar* cString) const MIJIN_NOEXCEPT { return stringView() == cString; }
[[nodiscard]]
constexpr string_view_t getName() const MIJIN_NOEXCEPT;
[[nodiscard]]
constexpr path_view_t getParent() const MIJIN_NOEXCEPT;
[[nodiscard]]
constexpr string_view_t stringView() const MIJIN_NOEXCEPT
{
return static_cast<const TConcrete*>(this)->stringViewImpl();
}
[[nodiscard]]
constexpr bool empty() const MIJIN_NOEXCEPT { return stringView().empty(); }
[[nodiscard]]
constexpr size_type size() const MIJIN_NOEXCEPT { return stringView().size(); }
[[nodiscard]]
constexpr bool isAbsolute() const MIJIN_NOEXCEPT
{
return traits_t::isAbsolute(stringView());
}
[[nodiscard]]
iterator begin() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().begin()); }
[[nodiscard]]
iterator end() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().end()); }
private:
template<typename TConcreteOther, typename TCharOther, typename TTraitsOther>
friend class MixinPath;
};
template<typename TConcreteA, typename TConcreteB, typename TChar, typename TTraits>
constexpr bool operator==(const MixinPath<TConcreteA, TChar, TTraits>& pathA, const MixinPath<TConcreteB, TChar, TTraits>& pathB) MIJIN_NOEXCEPT
{
return pathA.stringView() == pathB.stringView();
}
template<typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
class BasePath : public MixinPath<BasePath<TChar, TTraits>, TChar, TTraits>
{
public:
using mixin_t = MixinPath<BasePath<TChar, TTraits>, TChar, TTraits>;
using typename mixin_t::traits_t;
using typename mixin_t::char_t;
using typename mixin_t::char_traits_t;
using typename mixin_t::allocator_t;
using typename mixin_t::string_t;
using typename mixin_t::string_view_t;
using typename mixin_t::difference_type;
using typename mixin_t::path_view_t;
static constexpr char_t SEPARATOR = mixin_t::SEPARATOR;
private:
struct NoSimplify {};
string_t storage_;
constexpr BasePath(string_t storage, NoSimplify) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(storage)) {}
public:
constexpr BasePath() = default;
constexpr BasePath(const BasePath&) = default;
constexpr BasePath(BasePath&&) = default;
constexpr BasePath(string_t string) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(string)) {
simplify();
}
constexpr BasePath(const char_t* cString, allocator_t allocator = {}) : BasePath(string_t(cString, std::move(allocator))) {}
constexpr BasePath(string_view_t stringView, allocator_t allocator = {}) : BasePath(string_t(stringView, std::move(allocator))) {}
template<typename TConcreteOther> requires (!std::is_same_v<BasePath<TChar, TTraits>, TConcreteOther>)
explicit constexpr BasePath(const MixinPath<TConcreteOther, TChar, TTraits> other, allocator_t allocator = {}) : BasePath(string_t(other.stringView(), std::move(allocator)), NoSimplify()) {}
constexpr BasePath& operator=(const BasePath&) = default;
constexpr BasePath& operator=(BasePath&&) = default;
constexpr auto operator<=>(const BasePath&) const noexcept = default;
using mixin_t::operator==;
template<typename TConcreteOther>
BasePath& operator/=(const MixinPath<TConcreteOther, char_t, traits_t>& other);
BasePath& operator/=(string_view_t more);
template<typename TConcreteOther>
BasePath operator/(const MixinPath<TConcreteOther, char_t, traits_t>& other) const;
BasePath operator/(string_view_t more) const;
[[nodiscard]]
constexpr const string_t& string() const MIJIN_NOEXCEPT
{
return storage_;
}
[[nodiscard]]
constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT
{
return storage_;
}
private:
constexpr void simplify() MIJIN_NOEXCEPT;
};
using Path = BasePath<char>;
using WPath = BasePath<wchar_t>;
using NativePath = Path; // TODO
template<typename TChar, typename TTraits>
class BasePathView : public MixinPath<BasePathView<TChar, TTraits>, TChar, TTraits>
{
public:
using mixin_t = MixinPath<BasePathView<TChar, TTraits>, TChar, TTraits>;
using typename mixin_t::char_t;
using typename mixin_t::string_view_t;
private:
string_view_t view_;
public:
constexpr BasePathView() noexcept = default;
constexpr BasePathView(const BasePathView&) noexcept = default;
explicit constexpr BasePathView(string_view_t view) MIJIN_NOEXCEPT : view_(view) {}
template<typename TIterator>
constexpr BasePathView(const TIterator& begin, const TIterator& end) MIJIN_NOEXCEPT : view_(begin, end) {}
template<typename TConcreteOther> requires (!std::is_same_v<BasePathView<TChar, TTraits>, TConcreteOther>)
constexpr BasePathView(const MixinPath<TConcreteOther>& other) MIJIN_NOEXCEPT : view_(other.stringView()) {}
constexpr BasePathView& operator=(const BasePathView&) noexcept = default;
constexpr auto operator<=>(const BasePathView&) const noexcept = default;
using mixin_t::operator==;
[[nodiscard]]
constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT
{
return view_;
}
};
using PathView = BasePathView<char>;
using WPathView = BasePathView<wchar_t>;
using NativePathView = PathView; // TODO
template<typename TConcrete, typename TChar, typename TTraits>
constexpr auto MixinPath<TConcrete, TChar, TTraits>::getName() const MIJIN_NOEXCEPT -> string_view_t
{
const string_view_t strView = stringView();
auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base();
return {it, strView.end()};
}
template<typename TConcrete, typename TChar, typename TTraits>
constexpr auto MixinPath<TConcrete, TChar, TTraits>::getParent() const MIJIN_NOEXCEPT -> path_view_t
{
const string_view_t strView = stringView();
auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base();
if (it == strView.begin()) {
return {};
}
if (std::prev(it) == strView.begin()) {
return {strView.begin(), it}; // edge case, return "/" instead of nothing
}
return {strView.begin(), std::prev(it)};
}
template<typename TChar, typename TTraits>
template<typename TConcreteOther>
auto BasePath<TChar, TTraits>::operator/=(const MixinPath<TConcreteOther, char_t, traits_t>& other) -> BasePath&
{
if (other.isAbsolute())
{
storage_ = other.stringView();
}
else if (!other.empty())
{
if (other.stringView()[0] != SEPARATOR)
{
storage_.reserve(storage_.size() + other.size() + 1);
storage_.push_back(SEPARATOR);
}
storage_.append_range(other.stringView());
}
return *this;
}
template<typename TChar, typename TTraits>
auto BasePath<TChar, TTraits>::operator/=(string_view_t more) -> BasePath&
{
operator/=(path_view_t(more));
simplify();
return *this;
}
template<typename TChar, typename TTraits>
template<typename TConcreteOther>
auto BasePath<TChar, TTraits>::operator/(const MixinPath<TConcreteOther, char_t, traits_t>& other) const -> BasePath
{
if (other.isAbsolute() || other.empty())
{
return BasePath(string_t(other.stringView(), storage_.get_allocator()), NoSimplify());
}
const bool addSeparator = other.stringView()[0] != SEPARATOR;
string_t combined(storage_.get_allocator());
combined.reserve(storage_.size() + other.stringView().size() + (addSeparator ? 1 : 0));
combined.append_range(storage_);
if (addSeparator) {
combined.push_back(SEPARATOR);
}
combined.append_range(other.stringView());
return BasePath(std::move(combined), NoSimplify());
}
template<typename TChar, typename TTraits>
auto BasePath<TChar, TTraits>::operator/(string_view_t other) const -> BasePath
{
BasePath combined = (*this / path_view_t(other));
combined.simplify();
return combined;
}
template<typename TChar, typename TTraits>
constexpr void BasePath<TChar, TTraits>::simplify() MIJIN_NOEXCEPT
{
// step 1: remove double slashes
difference_type moveBy = 0;
for (auto it = std::next(storage_.begin()); it < storage_.end(); ++it)
{
const bool doubleSlash = (*it == SEPARATOR && *std::prev(it) == SEPARATOR); // check this before moving the current character, as that might create a double slash itself
if (moveBy > 0) {
*std::prev(it, moveBy) = *it;
}
if (doubleSlash) {
++moveBy;
}
}
// step 1.5: remove trailing slash (but only if it's not the only remaining char)
if (moveBy < static_cast<difference_type>(storage_.size() - 1) && storage_[storage_.size() - moveBy - 1] == SEPARATOR) {
++moveBy;
}
storage_.resize(storage_.size() - moveBy);
// step 2: get rid of any "/.." together with the preceeding segment
moveBy = 0;
for (auto it = std::next(storage_.begin(), 2); it < storage_.end(); ++it)
{
if (moveBy > 0)
{
*std::prev(it, moveBy) = *it;
}
if (*std::prev(it, moveBy) == TChar('.') && *std::prev(it, moveBy + 1) == TChar('.') && *std::prev(it, moveBy + 2) == SEPARATOR
&& (std::next(it) == storage_.end() || *std::next(it) == SEPARATOR))
{
if (std::prev(it, moveBy + 2) == storage_.begin())
{
// leading "/.." -> just remove it
moveBy += 3;
continue;
}
// find the start of the preceeding segment
for (auto itStart = std::prev(it, moveBy + 3);; --itStart)
{
if (*std::prev(itStart) == SEPARATOR || std::prev(itStart) == storage_.begin())
{
// /path/with/../double/dot
// itStart --A A
// it -------------|
// remove everything from itStart to it + two slashes
moveBy += std::distance(itStart, std::prev(it, moveBy)) + 2; // skip it all
break;
}
}
}
}
storage_.resize(storage_.size() - moveBy);
// step 3: eliminate any segments that are just "."
moveBy = 0;
if (storage_.size() == 1) {
return; // just stop it here
}
for (auto it = storage_.begin(); it < storage_.end(); ++it)
{
const bool atStart = (it == storage_.begin());
const bool atEnd = (std::next(it) == storage_.end());
const bool emptyEle = (*it == TChar('.') // char is a dot
&& (atStart || *std::prev(it, moveBy + 1) == SEPARATOR) // previous is a slash or doesn't exist
&& (atEnd || *std::next(it) == SEPARATOR)); // next is a slash or doesn't exist
if (moveBy > 0) {
*std::prev(it, moveBy) = *it;
}
if (emptyEle) {
moveBy += 2;
if (!atEnd) {
++it; // skip the next one
}
}
}
storage_.resize(storage_.size() - moveBy);
}
template<typename TChar, TChar separator = TChar('/'), typename TTraits = std::char_traits<TChar>>
constexpr bool verifyPathString(std::basic_string_view<TChar, TTraits> stringView) MIJIN_NOEXCEPT
{
for (auto it = std::next(stringView.begin()); it < stringView.end(); ++it)
{
if(*it == separator && *std::prev(it) == separator) {
return false; // double slash
}
if (it != std::next(stringView.begin())
&& *it == TChar('.') && *std::prev(it) == TChar('.') && *std::prev(it, 2) == separator
&& (std::next(it) == stringView.end() || *std::next(it) == separator)) {
return false; // "/.."
}
const bool atStart = (it == stringView.begin());
const bool atEnd = (std::next(it) == stringView.end());
if(*it == TChar('.') // char is a dot
&& (atStart || *std::prev(it) == separator) // previous is a slash or doesn't exist
&& (atEnd || *std::next(it) == separator)) // next is a slash or doesn't exist
{
return false; // "/."
}
}
return true;
}
consteval PathView operator""_pv(const char* cString, std::size_t len)
{
if (!verifyPathString(std::string_view(cString, len))) {
throw "Invalid path string.";
}
return PathView(std::string_view(cString, len));
}
consteval WPathView operator""_pv(const wchar_t* cString, std::size_t len)
{
if (!verifyPathString(std::wstring_view(cString, len))) {
throw "Invalid path string.";
}
return WPathView(std::wstring_view(cString, len));
}
[[nodiscard]]
NativePath getWorkingDirectory();
} // namespace mijin
template<mijin::BasePathType TPath>
struct std::hash<TPath>
{
std::size_t operator()(const TPath& path) const MIJIN_NOEXCEPT
{
return std::hash<std::string_view>()(path.stringView());
}
};
#endif // !defined(MIJIN_TYPES_PATH_HPP_INCLUDED)

View File

@ -35,6 +35,10 @@ concept nullable_type = !std::is_same_v<T, std::nullptr_t> && requires(T t) { t
template<nullable_type T>
class NotNullable
{
public:
using wrapped_t = T;
using element_type = std::remove_reference_t<decltype(*std::declval<const wrapped_t>())>;
using pointer = element_type*;
private:
T base_;
public:
@ -55,6 +59,12 @@ public:
{
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
}
template<typename TOther> requires(std::is_base_of_v<typename NotNullable<TOther>::element_type, element_type> && std::is_constructible_v<T, pointer>)
explicit constexpr NotNullable(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, pointer>))
: base_(static_cast<pointer>(&*other))
{
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
}
template<typename TOther> requires(std::is_constructible_v<T, TOther&&>)
constexpr NotNullable(NotNullable<TOther>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TOther&&>))
: base_(std::exchange(other.base_, nullptr))

View File

@ -4,8 +4,18 @@
#if !defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)
#define MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED 1
#include "../detect.hpp"
#define MIJIN_CONCAT_DETAIL(a, b) a ## b
#define MIJIN_CONCAT(a, b) MIJIN_CONCAT_DETAIL(a, b)
#define MIJIN_CONCAT3(a, b, c) MIJIN_CONCAT(a, MIJIN_CONCAT(b, c))
#if MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#define MIJIN_FUNCNAME() __PRETTY_FUNCTION__
#elif MIJIN_COMPILER == MIJIN_COMPILER_MSVC
#define MIJIN_FUNCNAME() __FUNCSIG__
#else
#define MIJIN_FUNCNAME() __func__
#endif
#endif // defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)

View File

@ -0,0 +1,122 @@
#pragma once
#ifndef MIJIN_UTIL_FORMATTABLE_HPP_INCLUDED
#define MIJIN_UTIL_FORMATTABLE_HPP_INCLUDED 1
#include <format>
#include "../container/boxed_object.hpp"
namespace mijin
{
//
// public concepts
//
template<typename T>
concept parse_result_type = requires
{
typename T::parse_result_t;
};
template<typename T, typename TParseContext>
concept parseable_by_type = parse_result_type<T> &&
requires(const T& object, TParseContext& parseContext)
{
{ T::parseFormat(parseContext) } -> std::convertible_to<std::pair<typename TParseContext::iterator, typename T::parse_result_t>>;
};
template<typename T, typename TFmtContext>
concept simple_formattable_to_type = requires(const T& object, TFmtContext& formatContext)
{
{ object.format(formatContext) } -> std::convertible_to<typename TFmtContext::iterator>;
};
template<typename T, typename TFmtContext, typename TParseContext>
concept complex_formattable_to_type = parseable_by_type<T, TParseContext> &&
requires(const T& object, TParseContext& parseContext, TFmtContext& formatContext, T::parse_result_t& parseResult)
{
{ object.format(formatContext, parseResult) } -> std::convertible_to<typename TFmtContext::iterator>;
};
template<typename T, typename TFmtContext, typename TParseContext>
concept formattable_to_type = simple_formattable_to_type<T, TFmtContext> || complex_formattable_to_type<T, TFmtContext, TParseContext>;
template<typename T>
concept cformattable_type = formattable_to_type<T, std::format_context, std::format_parse_context>;
template<typename T>
concept wformattable_type = formattable_to_type<T, std::wformat_context, std::wformat_parse_context>;
template<typename T>
concept formattable_type = cformattable_type<T> && wformattable_type<T>;
template<typename T>
concept any_formattable_type = cformattable_type<T> || wformattable_type<T>;
//
// internal types
//
namespace impl
{
template<typename T>
struct ParseResult
{
};
template<parse_result_type T>
struct ParseResult<T>
{
BoxedObject<typename T::parse_result_t> value;
};
}
} // namespace mijin
template<mijin::any_formattable_type T>
struct std::formatter<T>
{
[[no_unique_address]] [[maybe_unused]] mijin::impl::ParseResult<T> parseResult;
template<typename TContext>
constexpr TContext::iterator parse(TContext& ctx)
{
if constexpr (mijin::parse_result_type<T>)
{
static_assert(mijin::parseable_by_type<T, TContext>, "Type does not support parsing by this context.");
auto [it, result] = T::parseFormat(ctx);
parseResult.value.construct(std::move(result));
return it;
}
else
{
auto it = ctx.begin();
auto end = ctx.end();
if (it != end && *it != '}')
{
throw std::format_error("invalid format");
}
return it;
}
}
template<typename TContext>
TContext::iterator format(const T& object, TContext& ctx) const
{
if constexpr (mijin::parse_result_type<T>)
{
auto it = object.format(ctx, std::move(*parseResult.value));
parseResult.destroy();
return it;
}
else
{
static_assert(mijin::simple_formattable_to_type<T, TContext>, "Type does not support formatting to this context.");
return object.format(ctx);
}
}
};
#endif // MIJIN_UTIL_FORMATTABLE_HPP_INCLUDED

View File

@ -3,6 +3,7 @@
#if !defined(MIJIN_UTIL_ITERATORS_HPP_INCLUDED)
#define MIJIN_UTIL_ITERATORS_HPP_INCLUDED 1
#include <array>
#include <cstddef>
#include <functional>
#include <optional>
@ -13,6 +14,7 @@
#include <variant>
#include "../container/optional.hpp"
#include "../internal/common.hpp"
#include "../util/annot.hpp"
namespace mijin
{
@ -855,6 +857,64 @@ TAs collect(TIterable&& iterable)
return TAs(iterable.begin(), iterable.end());
}
template<typename TEle, std::size_t numEles>
class FixedArrayOutputIterator
{
public:
using array_t = std::array<TEle, numEles>;
using difference_type = std::ptrdiff_t;
using value_type = TEle;
using pointer = value_type*;
using reference = value_type&;
static constexpr std::size_t NUM_ELES = numEles;
private:
not_null_t<array_t*> array_;
std::size_t counter_ = 0;
public:
constexpr explicit FixedArrayOutputIterator(array_t& array) MIJIN_NOEXCEPT : array_(&array) {}
constexpr explicit FixedArrayOutputIterator(array_t& array, array_t::iterator pos) MIJIN_NOEXCEPT
: array_(&array), counter_(static_cast<std::size_t>(pos - array.begin())) {}
constexpr FixedArrayOutputIterator(const FixedArrayOutputIterator&) noexcept = default;
constexpr FixedArrayOutputIterator& operator=(const FixedArrayOutputIterator&) noexcept = default;
[[nodiscard]]
constexpr std::size_t getCounter() const MIJIN_NOEXCEPT
{
return counter_;
}
[[nodiscard]]
constexpr bool didOverflow() const MIJIN_NOEXCEPT
{
return counter_ >= NUM_ELES;
}
constexpr reference operator*() const MIJIN_NOEXCEPT
{
static TEle dummy_; // returned when we're at the end of the array
if (counter_ < NUM_ELES) {
return array_->at(counter_);
}
return dummy_;
}
constexpr FixedArrayOutputIterator& operator++() MIJIN_NOEXCEPT
{
++counter_;
return *this;
}
constexpr FixedArrayOutputIterator operator++(int) const MIJIN_NOEXCEPT
{
FixedArrayOutputIterator copy(*this);
++copy;
return copy;
}
};
static_assert(std::output_iterator<FixedArrayOutputIterator<int, 1>, int>);
namespace pipe
{
template<typename T>
@ -973,7 +1033,7 @@ auto operator|(TIterable&& iterable, Xth<idx>)
{
return map(std::forward<TIterable>(iterable), [](auto&& element) { return std::get<idx>(element); } );
}
}
} // namespace pipe
} // namespace mijin
#endif // MIJIN_UTIL_ITERATORS_HPP_INCLUDED

View File

@ -10,6 +10,7 @@
#include <mutex>
#include <dlfcn.h>
#include <pthread.h>
#include <time.h>
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
#include <array>
#include <malloc.h>
@ -38,12 +39,29 @@ namespace mijin
// internal variables
//
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
namespace
{
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
std::mutex gDlErrorMutex; // dlerror may not be thread-safe
const std::uint64_t gCPUClockResolution = []()
{
struct timespec time;
[[maybe_unused]] const int result = clock_getres(CLOCK_PROCESS_CPUTIME_ID, &time);
MIJIN_ASSERT(result == 0, "Error getting cputime resolution.");
MIJIN_ASSERT(time.tv_sec == 0, "What kind of cpu clock is this?");
return static_cast<std::uint64_t>(time.tv_nsec);
}();
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
const std::uint64_t gCPUTickFrequency = []()
{
LARGE_INTEGER ticks;
[[maybe_unused]] const BOOL result = QueryPerformanceFrequency(&ticks);
MIJIN_ASSERT(result, "Error getting cpu frequency.");
return static_cast<std::uint64_t>(ticks.QuadPart);
}();
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX || MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
}
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX
//
// internal functions
@ -178,4 +196,27 @@ void alignedFree(void* ptr)
#endif
}
std::uint64_t getCPUTicks() MIJIN_NOEXCEPT
{
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
struct timespec time;
[[maybe_unused]] const int result = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
MIJIN_ASSERT(result == 0, "Error getting cpu time.");
return (static_cast<std::uint64_t>(time.tv_sec) * 1e9 + time.tv_nsec) / gCPUClockResolution;
#else
LARGE_INTEGER ticks;
[[maybe_unused]] const BOOL result = QueryPerformanceCounter(&ticks);
MIJIN_ASSERT(result, "Error getting cpu time.");
return static_cast<std::uint64_t>(ticks.QuadPart);
#endif
}
std::uint64_t getCPUTicksPerSecond() MIJIN_NOEXCEPT
{
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
return 1e9 / gCPUClockResolution;
#else
return gCPUTickFrequency;
#endif
}
} // namespace mijin

View File

@ -5,6 +5,7 @@
#define MIJIN_UTIL_OS_HPP_INCLUDED 1
#include <csignal>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
@ -85,6 +86,9 @@ void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT;
[[nodiscard]] void* alignedRealloc(void* ptr, std::size_t alignment, std::size_t size) MIJIN_NOEXCEPT;
void alignedFree(void* ptr);
[[nodiscard]] std::uint64_t getCPUTicks() MIJIN_NOEXCEPT;
[[nodiscard]] std::uint64_t getCPUTicksPerSecond() MIJIN_NOEXCEPT;
SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT
{
close();

View File

@ -253,6 +253,42 @@ struct optional_base<T, false>
template<typename T, bool enable>
using optional_base_t = optional_base<T, enable>::type;
namespace impl
{
template<typename TFunc>
struct function_traits_base {};
template<typename TResult, typename... TParams>
struct function_traits_base<TResult (*)(TParams...)>
{
using result_t = TResult;
using params_t = std::tuple<TParams...>;
};
template<typename TResult, typename TType, typename... TParams>
struct function_traits_base<TResult (TType::*)(TParams...)>
{
using result_t = TResult;
using params_t = std::tuple<TType*, TParams...>;
};
template<typename TResult, typename TType, typename... TParams>
struct function_traits_base<TResult (TType::*)(TParams...) const>
{
using result_t = TResult;
using params_t = std::tuple<const TType*, TParams...>;
};
}
template<typename TFunc>
struct function_traits : impl::function_traits_base<TFunc>
{
static constexpr std::size_t NUM_PARAMS = std::tuple_size_v<typename impl::function_traits_base<TFunc>::params_t>;
template<std::size_t pos>
using param_t = std::tuple_element_t<pos, typename impl::function_traits_base<TFunc>::params_t>;
};
//
// public functions
//

View File

@ -4,6 +4,7 @@
#if !defined(MIJIN_UTIL_VARIANT_HPP_INCLUDED)
#define MIJIN_UTIL_VARIANT_HPP_INCLUDED 1
#include <utility>
#include <variant>
#include "./traits.hpp"
@ -23,10 +24,21 @@ inline constexpr bool variant_contains_v<TSearch, std::variant<TVariantTypes...>
//
// public types
//
template<typename... TCallable>
struct Visitor : TCallable ...
{
using TCallable::operator()...;
};
template<typename TRet>
struct CastTo
{
template<typename T>
TRet operator()(T&& arg) const
{
return static_cast<TRet>(std::forward<T>(arg));
}
};
}
#endif // !defined(MIJIN_UTIL_VARIANT_HPP_INCLUDED)

View File

@ -26,3 +26,7 @@
#if defined(DEBUG)
#undef DEBUG
#endif
#if defined(VOID)
#undef VOID
#endif

View File

@ -3,6 +3,10 @@
#include "../platform/folders.hpp"
#include <filesystem>
namespace fs = std::filesystem;
namespace mijin
{
@ -26,100 +30,91 @@ namespace mijin
// internal functions
//
namespace
{
void doGetFileInfo(const fs::path& stlPath, FileInfo& outInfo)
{
std::error_code err;
outInfo.isFolder = fs::is_directory(stlPath, err);
outInfo.isSymlink = fs::is_symlink(stlPath, err);
outInfo.isSpecial = !outInfo.isFolder && !fs::is_regular_file(stlPath, err);
outInfo.isHidden = stlPath.c_str()[0] == '.'; // at least for Linux
if (outInfo.isFolder)
{
const fs::directory_iterator dirIt(stlPath, err);
if (err != std::error_code{})
{
outInfo.size = std::distance(dirIt, fs::directory_iterator());
}
else
{
outInfo.size = 0;
}
}
else if (!outInfo.isSpecial)
{
outInfo.size = fs::file_size(stlPath, err);
if (err)
{
outInfo.size = 0;
}
}
}
}
//
// public functions
//
fs::path OSFileSystemAdapter::getHomeFolder()
std::vector<FolderEntry> OSFileSystemAdapter::listFiles(PathView folder)
{
return getKnownFolder(KnownFolder::USER_HOME);
}
std::vector<FileInfo> OSFileSystemAdapter::listFiles(const fs::path& folder)
{
std::vector<FileInfo> entries;
std::vector<FolderEntry> entries;
std::error_code err;
const fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err);
const fs::path stlFolder(folder.stringView());
const fs::directory_iterator iterator(stlFolder, fs::directory_options::skip_permission_denied, err);
if (err) {
return {}; // TODO: propagate?
}
for (const fs::directory_entry& entry : iterator)
for (const fs::directory_entry& stlEntry : iterator)
{
FileInfo& info = entries.emplace_back();
info.path = entry.path();
info.exists = true;
info.isFolder = entry.is_directory(err);
info.isSymlink = entry.is_symlink(err);
info.isSpecial = !info.isFolder && !entry.is_regular_file(err);
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
if (info.isFolder)
{
std::error_code errorCode;
fs::directory_iterator dirIt(info.path, errorCode);
if (errorCode != std::error_code{})
{
info.size = std::distance(dirIt, fs::directory_iterator());
}
else
{
info.size = 0;
}
}
else if (!info.isSpecial)
{
info.size = entry.file_size(err);
if (err)
{
info.size = 0;
}
}
FolderEntry& entry = entries.emplace_back();
entry.path = stlEntry.path().generic_string();
entry.info.exists = true;
doGetFileInfo(stlEntry.path(), entry.info);
}
return entries;
}
FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file)
FileInfo OSFileSystemAdapter::getFileInfo(PathView file)
{
const fs::path stlFile(file.stringView());
FileInfo info = {};
std::error_code err;
info.path = file;
info.exists = fs::exists(file, err);
info.exists = fs::exists(stlFile, err);
if (info.exists)
{
info.isFolder = fs::is_directory(file, err);
info.isSymlink = fs::is_symlink(file, err);
info.isSpecial = !info.isFolder && !fs::is_regular_file(file, err);
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
if (info.isFolder) {
MIJIN_TRY
{
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
}
MIJIN_CATCH(std::runtime_error&)
{
info.size = 0;
}
}
else if (!info.isSpecial)
{
info.size = fs::file_size(file, err);
if (err) {
info.size = 0;
}
}
doGetFileInfo(fs::path(file.stringView()), info);
}
return info;
}
Optional<fs::path> OSFileSystemAdapter::getNativePath(const fs::path& file)
Optional<NativePath> OSFileSystemAdapter::getNativePath(PathView file)
{
return file;
return NativePath(file);
}
StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
StreamError OSFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{
const std::string pathStr = path.string();
const PathView::string_view_t pathSv = path.stringView();
char* pathStr = static_cast<char*>(alloca(pathSv.size() + 1));
std::memcpy(pathStr, pathSv.data(), pathSv.size());
pathStr[pathSv.size()] = '\0';
auto stream = std::make_unique<FileStream>();
const StreamError error = stream->open(pathStr.c_str(), mode);
const StreamError error = stream->open(pathStr, mode);
if (error != StreamError::SUCCESS) {
return error;
}

View File

@ -6,17 +6,15 @@
#include <array>
#include <cmath>
#include <filesystem>
#include <sstream>
#include <string>
#include <vector>
#include "../container/optional.hpp"
#include "../io/stream.hpp"
#include "../internal/common.hpp"
#include "../types/path.hpp"
#include "../util/hash.hpp"
namespace fs = std::filesystem;
namespace mijin
{
@ -34,7 +32,6 @@ namespace mijin
struct FileInfo
{
fs::path path;
/// either file size in bytes, or number of entries if folder
std::size_t size = 0;
bool exists : 1 = false;
@ -44,17 +41,23 @@ struct FileInfo
bool isHidden : 1 = false;
};
struct FolderEntry
{
Path path;
FileInfo info;
};
// basically just a thin wrapper around adapter + path, for utility
class PathReference
{
private:
class FileSystemAdapter* adapter_ = nullptr;
fs::path path_ = {};
Path path_ = {};
public:
PathReference() = default;
PathReference(const PathReference&) = default;
PathReference(PathReference&&) = default;
PathReference(class FileSystemAdapter* adapter, fs::path path) MIJIN_NOEXCEPT : adapter_(adapter), path_(std::move(path)) {}
PathReference(class FileSystemAdapter* adapter, Path path) MIJIN_NOEXCEPT : adapter_(adapter), path_(std::move(path)) {}
PathReference& operator=(const PathReference&) = default;
PathReference& operator=(PathReference&&) MIJIN_NOEXCEPT = default;
@ -63,13 +66,13 @@ public:
[[nodiscard]] bool empty() const MIJIN_NOEXCEPT { return adapter_ == nullptr; }
[[nodiscard]] class FileSystemAdapter* getAdapter() const noexcept { return adapter_; }
[[nodiscard]] const fs::path& getPath() const MIJIN_NOEXCEPT { return path_; }
[[nodiscard]] const Path& getPath() const MIJIN_NOEXCEPT { return path_; }
[[nodiscard]] inline FileInfo getInfo() const;
[[nodiscard]] inline Optional<fs::path> getNativePath() const;
[[nodiscard]] inline Optional<NativePath> getNativePath() const;
[[nodiscard]] inline StreamError open(FileOpenMode mode, std::unique_ptr<Stream>& outStream) const;
[[nodiscard]]
PathReference operator/(const fs::path& more) const
PathReference operator/(const Path& more) const
{
return PathReference(adapter_, path_ / more);
}
@ -80,16 +83,14 @@ class FileSystemAdapter
public:
virtual ~FileSystemAdapter() = default;
[[deprecated("Will be removed ASAP")]]
[[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;
[[nodiscard]] virtual Optional<fs::path> getNativePath(const fs::path& /* file */) { return NULL_OPTIONAL; }
[[nodiscard]] virtual StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) = 0;
virtual void getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths) { outPaths.push_back(getPath(path)); }
[[nodiscard]] virtual std::vector<FolderEntry> listFiles(PathView folder) = 0;
[[nodiscard]] virtual FileInfo getFileInfo(PathView file) = 0;
[[nodiscard]] virtual Optional<NativePath> getNativePath(PathView /* file */) { return NULL_OPTIONAL; }
[[nodiscard]] virtual StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) = 0;
virtual void getAllPaths(PathView path, std::vector<PathReference>& outPaths) { outPaths.push_back(getPath(Path(path))); }
[[nodiscard]] PathReference getPath(fs::path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); }
[[nodiscard]] std::vector<PathReference> getAllPaths(const fs::path& path)
[[nodiscard]] PathReference getPath(Path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); }
[[nodiscard]] std::vector<PathReference> getAllPaths(PathView path)
{
std::vector<PathReference> paths;
getAllPaths(path, paths);
@ -100,11 +101,10 @@ public:
class OSFileSystemAdapter : public FileSystemAdapter
{
public:
fs::path getHomeFolder() override;
std::vector<FileInfo> listFiles(const fs::path& folder) override;
FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(const fs::path& file) override;
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
std::vector<FolderEntry> listFiles(PathView folder) override;
FileInfo getFileInfo(PathView file) override;
Optional<NativePath> getNativePath(PathView file) override;
StreamError open(PathView, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
static OSFileSystemAdapter& getInstance();
};
@ -118,7 +118,7 @@ inline FileInfo PathReference::getInfo() const
return adapter_->getFileInfo(path_);
}
Optional<fs::path> PathReference::getNativePath() const
Optional<NativePath> PathReference::getNativePath() const
{
return adapter_->getNativePath(path_);
}

View File

@ -5,30 +5,31 @@
namespace mijin
{
std::vector<FileInfo> MemoryFileSystemAdapter::listFiles(const fs::path& folder)
std::vector<FolderEntry> MemoryFileSystemAdapter::listFiles(PathView folder)
{
const detail::MemoryFolder* folderObj = findFolder(folder);
if (folderObj == nullptr)
{
return {};
}
std::vector<FileInfo> result;
std::vector<FolderEntry> result;
result.reserve(folderObj->folders.size() + folderObj->files.size());
const Path folderPath(folder);
for (const auto& [name, subFolder] : folderObj->folders)
{
result.push_back(folderInfo(folder / name, subFolder));
result.emplace_back(folderPath / name, folderInfo(subFolder));
}
for (const auto& [name, file] : folderObj->files)
{
result.push_back(fileInfo(folder / name, file));
result.emplace_back(folderPath / name, fileInfo(file));
}
return result;
}
FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file)
FileInfo MemoryFileSystemAdapter::getFileInfo(PathView file)
{
#if 0 // shouldn't be necessary
// empty means root
@ -43,38 +44,38 @@ FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file)
}
#endif
const detail::MemoryFolder* folderObj = findFolder(file.parent_path());
const detail::MemoryFolder* folderObj = findFolder(file.getParent());
if (folderObj == nullptr)
{
return {};
}
const std::string filename = file.filename().generic_string();
const std::string_view filename = file.getName();
if (auto itFolder = folderObj->folders.find(filename); itFolder != folderObj->folders.end())
{
return folderInfo(file, itFolder->second);
return folderInfo(itFolder->second);
}
if (auto itFile = folderObj->files.find(filename); itFile != folderObj->files.end())
{
return fileInfo(file, itFile->second);
return fileInfo(itFile->second);
}
return {};
}
StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
StreamError MemoryFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{
if (mode != FileOpenMode::READ)
{
return StreamError::IO_ERROR;
}
const detail::MemoryFolder* folderObj = findFolder(path.parent_path());
const detail::MemoryFolder* folderObj = findFolder(path.getParent());
if (folderObj == nullptr)
{
return StreamError::IO_ERROR;
}
auto itFile = folderObj->files.find(path.filename().generic_string());
auto itFile = folderObj->files.find(path.getName());
if (itFile == folderObj->files.end())
{
return StreamError::IO_ERROR;
@ -87,10 +88,10 @@ StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mod
return StreamError::SUCCESS;
}
bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span<const std::uint8_t> data, Overwrite overwrite, CopyData copyData)
bool MemoryFileSystemAdapter::addFile(PathView path, std::span<const std::uint8_t> data, Overwrite overwrite, CopyData copyData)
{
detail::MemoryFolder& folder = *findFolder(path.parent_path(), true);
std::string filename = path.filename().generic_string();
detail::MemoryFolder& folder = *findFolder(path.getParent(), true);
const std::string_view filename = path.getName();
if (folder.folders.contains(filename))
{
@ -107,29 +108,28 @@ bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span<const std:
data = fileData_.emplace_back(data.begin(), data.end());
}
folder.files.emplace(std::move(filename), detail::MemoryFile{.data = data});
folder.files.emplace(filename, detail::MemoryFile{.data = data});
return true;
}
void MemoryFileSystemAdapter::addFolder(const fs::path& path)
void MemoryFileSystemAdapter::addFolder(PathView path)
{
(void) findFolder(path, true);
}
detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path, bool create) MIJIN_NOEXCEPT
detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(PathView path, bool create) MIJIN_NOEXCEPT
{
detail::MemoryFolder* folder = &root_;
for (const fs::path& part : path)
for (const std::string_view part : path)
{
std::string partname = part.generic_string();
auto it = folder->folders.find(partname);
auto it = folder->folders.find(part);
if (it == folder->folders.end())
{
if (!create)
{
return nullptr;
}
folder = &folder->folders[std::move(partname)];
folder = &folder->folders[std::string(part)];
}
else
{
@ -139,20 +139,18 @@ detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path,
return folder;
}
FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
FileInfo MemoryFileSystemAdapter::folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
{
return {
.path = path,
.size = folder.folders.size() + folder.files.size(),
.exists = true,
.isFolder = true
};
}
FileInfo MemoryFileSystemAdapter::fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT
FileInfo MemoryFileSystemAdapter::fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT
{
return {
.path = path,
.size = file.data.size(),
.exists = true
};

View File

@ -35,20 +35,20 @@ private:
detail::MemoryFolder root_;
std::vector<std::vector<std::uint8_t>> fileData_;
public:
[[nodiscard]] std::vector<FileInfo> listFiles(const fs::path& folder) override;
[[nodiscard]] FileInfo getFileInfo(const fs::path& file) override;
[[nodiscard]] StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
[[nodiscard]] std::vector<FolderEntry> listFiles(PathView folder) override;
[[nodiscard]] FileInfo getFileInfo(PathView file) override;
[[nodiscard]] StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
bool addFile(const fs::path& path, std::span<const std::uint8_t> data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO);
bool addFile(const fs::path& path, std::span<const std::uint8_t> data, CopyData copyData)
bool addFile(PathView path, std::span<const std::uint8_t> data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO);
bool addFile(PathView path, std::span<const std::uint8_t> data, CopyData copyData)
{
return addFile(path, data, Overwrite::NO, copyData);
}
void addFolder(const fs::path& path);
void addFolder(PathView path);
private:
detail::MemoryFolder* findFolder(const fs::path& path, bool create = false) MIJIN_NOEXCEPT;
FileInfo folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT;
FileInfo fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT;
detail::MemoryFolder* findFolder(PathView path, bool create = false) MIJIN_NOEXCEPT;
FileInfo folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT;
FileInfo fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT;
};
} // namespace mijin

View File

@ -27,10 +27,10 @@ class RelativeFileSystemAdapter : public FileSystemAdapter
{
private:
TWrapped wrapped_;
fs::path root_;
Path root_;
public:
template<typename... TArgs>
explicit RelativeFileSystemAdapter(fs::path root, TArgs&&... args)
explicit RelativeFileSystemAdapter(Path root, TArgs&&... args)
: wrapped_(std::forward<TArgs>(args)...), root_(std::move(root)) {}
RelativeFileSystemAdapter(const RelativeFileSystemAdapter&) = default;
RelativeFileSystemAdapter(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
@ -38,13 +38,12 @@ public:
RelativeFileSystemAdapter& operator=(const RelativeFileSystemAdapter&) = default;
RelativeFileSystemAdapter& operator=(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
fs::path getHomeFolder() override;
std::vector<FileInfo> listFiles(const fs::path& folder) override;
FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(const fs::path& file) override;
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
std::vector<FolderEntry> listFiles(PathView folder) override;
FileInfo getFileInfo(PathView file) override;
Optional<NativePath> getNativePath(PathView file) override;
StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
private:
fs::path appendPath(const fs::path& other) const MIJIN_NOEXCEPT;
Path appendPath(PathView other) const MIJIN_NOEXCEPT;
};
//
@ -52,51 +51,44 @@ private:
//
template<typename TWrapped>
fs::path RelativeFileSystemAdapter<TWrapped>::getHomeFolder()
std::vector<FolderEntry> RelativeFileSystemAdapter<TWrapped>::listFiles(PathView folder)
{
return root_;
}
template<typename TWrapped>
std::vector<FileInfo> RelativeFileSystemAdapter<TWrapped>::listFiles(const fs::path& folder)
{
std::vector<FileInfo> result;
std::vector<FolderEntry> result;
result = wrapped_.listFiles(appendPath(folder));
for (FileInfo& fileInfo : result) {
fileInfo.path = "/" / fileInfo.path.lexically_relative(root_);
for (FolderEntry& fileInfo : result) {
fileInfo.path = Path("/") / fileInfo.path.stringView().substr(root_.size());
}
return result;
}
template<typename TWrapped>
FileInfo RelativeFileSystemAdapter<TWrapped>::getFileInfo(const fs::path& file)
FileInfo RelativeFileSystemAdapter<TWrapped>::getFileInfo(PathView file)
{
return wrapped_.getFileInfo(appendPath(file));
}
template<typename TWrapped>
Optional<fs::path> RelativeFileSystemAdapter<TWrapped>::getNativePath(const fs::path& file)
Optional<NativePath> RelativeFileSystemAdapter<TWrapped>::getNativePath(PathView file)
{
return wrapped_.getNativePath(appendPath(file));
}
template<typename TWrapped>
StreamError RelativeFileSystemAdapter<TWrapped>::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
StreamError RelativeFileSystemAdapter<TWrapped>::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{
return wrapped_.open(appendPath(path), mode, outStream);
}
template<typename TWrapped>
fs::path RelativeFileSystemAdapter<TWrapped>::appendPath(const fs::path& other) const MIJIN_NOEXCEPT
Path RelativeFileSystemAdapter<TWrapped>::appendPath(PathView other) const MIJIN_NOEXCEPT
{
fs::path combinedPath = root_;
if (other.is_absolute()) {
combinedPath += other;
}
else {
combinedPath /= other;
Path combinedPath = root_;
std::string_view otherSv(other.stringView());
if (!otherSv.empty() && otherSv[0] == Path::SEPARATOR) {
otherSv = otherSv.substr(1);
}
combinedPath /= otherSv;
return combinedPath;
}
@ -105,7 +97,7 @@ namespace vfs_pipe
template<typename TBase>
struct RelativeBuilder : Builder<RelativeBuilder<TBase>, RelativeFileSystemAdapter<typename TBase::adapter_t>>
{
fs::path root;
Path root;
TBase base;
std::unique_ptr<RelativeFileSystemAdapter<typename TBase::adapter_t>> build()
@ -116,11 +108,11 @@ struct RelativeBuilder : Builder<RelativeBuilder<TBase>, RelativeFileSystemAdapt
struct RelativeOptions
{
fs::path root;
Path root;
};
[[nodiscard]]
inline RelativeOptions relative_to(fs::path root) noexcept
inline RelativeOptions relative_to(Path root) noexcept
{
return {.root = std::move(root) };
}

View File

@ -31,40 +31,20 @@ namespace mijin
// public functions
//
fs::path StackedFileSystemAdapter::getHomeFolder()
std::vector<FolderEntry> StackedFileSystemAdapter::listFiles(PathView folder)
{
if (adapters_.empty()) {
return fs::path();
}
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
#pragma warning(push)
#pragma warning(disable : 4996) // yeah, we're using a deprecated function here, in order to implement another deprecated function ¯\_(ツ)_/¯
#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return adapters_.front()->getHomeFolder();
#if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
#pragma warning(pop)
#elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#pragma GCC diagnostic pop
#endif
}
std::vector<FileInfo> StackedFileSystemAdapter::listFiles(const fs::path& folder)
{
std::vector<FileInfo> files;
std::vector<FolderEntry> files;
for (auto& adapter : adapters_)
{
for (const FileInfo& file : adapter->listFiles(folder))
for (const FolderEntry& entry : adapter->listFiles(folder))
{
auto it = std::find_if(files.begin(), files.end(), [&](const FileInfo& existing)
auto it = std::ranges::find_if(files, [&](const FolderEntry& existing)
{
return existing.path == file.path;
return existing.path == entry.path;
});
if (it == files.end()) {
files.push_back(file);
files.push_back(entry);
}
}
}
@ -72,7 +52,7 @@ std::vector<FileInfo> StackedFileSystemAdapter::listFiles(const fs::path& folder
return files;
}
FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file)
FileInfo StackedFileSystemAdapter::getFileInfo(PathView file)
{
for (auto& adapter : adapters_)
{
@ -85,11 +65,11 @@ FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file)
return {};
}
Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
Optional<NativePath> StackedFileSystemAdapter::getNativePath(PathView file)
{
for (auto& adapter : adapters_)
{
Optional<fs::path> result = adapter->getNativePath(file);
Optional<NativePath> result = adapter->getNativePath(file);
if (!result.empty())
{
return result;
@ -98,7 +78,7 @@ Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
return NULL_OPTIONAL;
}
StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
StreamError StackedFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{
// try to open existing files first
for (auto& adapter : adapters_)
@ -125,7 +105,7 @@ StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mo
return StreamError::IO_ERROR;
}
void StackedFileSystemAdapter::getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths)
void StackedFileSystemAdapter::getAllPaths(PathView path, std::vector<PathReference>& outPaths)
{
for (auto& adapter : adapters_)
{

View File

@ -28,12 +28,11 @@ class StackedFileSystemAdapter : public FileSystemAdapter
private:
std::vector<std::unique_ptr<FileSystemAdapter>> adapters_;
public:
fs::path getHomeFolder() override;
std::vector<FileInfo> listFiles(const fs::path& folder) override;
FileInfo getFileInfo(const fs::path& file) override;
Optional<fs::path> getNativePath(const fs::path& file) override;
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
void getAllPaths(const fs::path &path, std::vector<PathReference> &outPaths) override;
std::vector<FolderEntry> listFiles(PathView folder) override;
FileInfo getFileInfo(PathView file) override;
Optional<NativePath> getNativePath(PathView file) override;
StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
void getAllPaths(PathView path, std::vector<PathReference> &outPaths) override;
using FileSystemAdapter::getAllPaths;
inline FileSystemAdapter* addAdapter(std::unique_ptr<FileSystemAdapter>&& adapter) {