4#ifndef GREM_APPLICATION_VIRTUAL_FILESYSTEM_HPP
5#define GREM_APPLICATION_VIRTUAL_FILESYSTEM_HPP
17#ifdef GREM_USE_MULTITHREADING
23#include <system_error>
298 friend VirtualInputFile;
299 friend VirtualOutputFile;
302#ifdef GREM_USE_MULTITHREADING
303 struct SharedFileMutex {
305 explicit Value(
String canonicalFilepath)
306 : canonicalFilepath(std::
move(canonicalFilepath)) {}
313 using is_transparent = void;
315 [[nodiscard]]
size_t operator()(
const SharedFileMutex& a)
const {
316 return stringHasher(a.value->canonicalFilepath);
319 [[nodiscard]]
size_t operator()(
const String& a)
const {
320 return stringHasher(a);
324 [[no_unique_address]] std::hash<String> stringHasher;
328 using is_transparent = void;
330 [[nodiscard]]
bool operator()(
const SharedFileMutex& a,
const SharedFileMutex& b)
const {
331 return a.value->canonicalFilepath == b.value->canonicalFilepath;
334 [[nodiscard]]
bool operator()(
const SharedFileMutex& a,
const String& b)
const {
335 return a.value->canonicalFilepath == b;
338 [[nodiscard]]
bool operator()(
const String& a,
const SharedFileMutex& b)
const {
339 return a == b.value->canonicalFilepath;
342 [[nodiscard]]
bool operator()(
const String& a,
const String& b)
const {
347 SharedPointer<Value> value{};
349 SharedFileMutex() noexcept = default;
351 explicit SharedFileMutex(String canonicalFilepath)
352 : value(SharedPointer<Value>::create(std::move(canonicalFilepath))) {}
355 mutable HashSet<SharedFileMutex, SharedFileMutex::Hash, SharedFileMutex::Equal> fileMutexes{};
356 mutable Mutex fileMutexesSetMutex{};
359 const VirtualFilesystem* filesystem =
nullptr;
360 SharedFileMutex fileMutex{};
361 UniqueLock<RecursiveMutex>
lock{};
363 FileLock() noexcept = default;
365 FileLock(const VirtualFilesystem& filesystem, SharedFileMutex fileMutex)
366 : filesystem(&filesystem)
367 , fileMutex(std::move(fileMutex))
368 , lock(this->fileMutex.value->mutex) {}
370 GREM_API(application) ~FileLock();
372 FileLock(
const FileLock&) =
delete;
373 FileLock(FileLock&&) noexcept = default;
374 FileLock& operator=(const FileLock&) = delete;
375 FileLock& operator=(FileLock&&) noexcept = default;
378 [[nodiscard]] GREM_API(application) FileLock lockInputFile(CStringView filepath, std::error_code& errorCode) const;
379 [[nodiscard]] GREM_API(application) FileLock lockOutputFile(CStringView filepath, std::error_code& errorCode) const;
constexpr Filesystem() noexcept=default
Default constructor.
Definition FunctionView.hpp:29
Generic boxed unique handle to a writable file in any filesystem.
Definition File.hpp:686
Definition synchronization.hpp:986
VirtualFilesystem(VirtualFilesystem &&)=delete
Moving a filesystem is not allowed, since it manages global state.
OutputFileHandle openOutputFileForAppending(CStringView filepath) override
Open a file relative to the current output directory for appended writing, or create an empty file an...
VirtualFilesystem & operator=(VirtualFilesystem &&)=delete
Moving a filesystem is not allowed, since it manages global state.
InputFileHandle openInputFile(CStringView filepath) const override
Open an input file in the virtual filesystem for reading.
CStringView getOutputDirectory() const noexcept override
Get the current output directory of the virtual filesystem.
OutputFileHandle openEmptyOutputFile(CStringView filepath) override
Create a file relative to the current output directory and open it for writing, overwriting any exist...
File::Metadata getFileMetadata(CStringView filepath) const override
Get the metadata of a file that is mounted at a given virtual filepath.
void forEachInputFilenameInDirectory(CStringView filepath, FunctionView< bool(CStringView filename)> callback) const override
Iterate the filenames of all readable virtual filepaths that are direct children of a given directory...
void createOutputDirectory(CStringView filepath) override
Create a new host directory relative to the current output directory, while also creating any missing...
bool inputFileExists(CStringView filepath) const override
Check if a given virtual filepath has a corresponding host file mounted.
void unmountInputArchive(CStringView path)
Unmount a previously mounted input directory or archive on the host filesystem from the virtual files...
void mountInputArchivesInMountedDirectory(CStringView filepath, CStringView archiveFileExtension, MountPriority priority=MountPriority::HIGHER, FunctionView< void(CStringView path)> callback=[](CStringView) -> void {})
Mount all archives in a given directory on the host filesystem to the root input directory of the vir...
InputFileHandle tryOpenInputFile(CStringView filepath) const override
Try to open an input file in the virtual filesystem for reading.
~VirtualFilesystem() override
Destructor.
VirtualFilesystem(const VirtualFilesystem &)=delete
Copying a filesystem is not allowed, since it manages global state.
VirtualFilesystem & operator=(const VirtualFilesystem &)=delete
Copying a filesystem is not allowed, since it manages global state.
void mountInputArchive(CStringView path, MountPriority priority=MountPriority::HIGHER)
Mount a directory or archive on the host filesystem to the root input directory of the virtual filesy...
void createEmptyOutputFile(CStringView filepath) override
Create an empty file relative to the current output directory, overwriting any existing file at the s...
CStringView findInputArchiveOfFile(CStringView filepath) const noexcept override
Get the host filepath of the mounted input archive on the host filesystem that contains a given virtu...
void createParentOutputDirectories(CStringView filepath) override
Create any missing parent directories above a host directory relative to the current output directory...
void setOutputDirectory(CStringView path)
Set the output directory of the virtual filesystem, where all output files will be written.
String createStandardOutputDirectory(const StandardOutputDirectoryOptions &standardOutputDirectoryOptions)
Get a suitable output directory for configuration files and other save data on the host platform,...
VirtualFilesystem(CStringView programFilepath)
Initialize the virtual filesystem.
void deleteOutputFile(CStringView filepath) override
Delete a host file or directory relative to the current output directory.
Unique handle to a writable file in the virtual Filesystem.
Definition VirtualOutputFile.hpp:35
Definition Application.hpp:18
MountPriority
Mount priority for a newly mounted archive to a VirtualFilesystem, relative to all previously mounted...
Definition VirtualFilesystem.hpp:36
@ HIGHER
Mount the archive at a higher priority than any previously mounted archive, meaning files in the new ...
Definition VirtualFilesystem.hpp:49
@ LOWER
Mount the archive at a lower priority than any previously mounted archive, meaning files in already m...
Definition VirtualFilesystem.hpp:42
GREM_ALWAYS_INLINE void lock(Lockable1 &lock1, Lockable2 &lock2, LockableN &... lockN)
Definition synchronization.hpp:1268
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Definition VirtualFilesystem.hpp:93
CStringView applicationName
String that uniquely identifies the application among all other applications released by the same org...
Definition VirtualFilesystem.hpp:113
CStringView organizationName
String that commonly identifies the publisher of the application, such as an organization name,...
Definition VirtualFilesystem.hpp:102