CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
FileHandle.hpp
Go to the documentation of this file.
1
2//
3// CeresEngine - A game development framework
4//
5// Created by Rogiel Sulzbach.
6// Copyright (c) 2018-2023 Rogiel Sulzbach. All rights reserved.
7//
8
9#pragma once
10
12
13#include "FileWatcher.hpp"
14
17
23
24#include <functional>
25#include <memory>
26#include <string>
27#include <vector>
28
29namespace CeresEngine {
30
47 public:
48 using ImplementationType = Poly<IFileHandle, sizeof(void*) * 4>;
49 using VisitFunc = std::function<bool(const FileHandle&)>;
50
51 private:
54
55 public:
58
61
64
67
70
73
76
80
81 public:
85
88
91
99
103
107
111
115
120
125
130
135
136#if 0
140 [[nodiscard]] Tree readTree(StringView path = "") const;
141#endif
142
146
150
154
157
160
163
169
172 [[nodiscard]] bool createDirectory() const;
173
177 FollowSymlinks = (1u << 0u),
178
180 Recursive = (1u << 1u),
181
182 None = 0,
183 Default = None,
184 };
185
188
194
206 void copyDirectory(const FileHandle& dstDir) const;
207
211 [[nodiscard]] bool copy(const FileHandle& dest) const;
212
216 [[nodiscard]] bool move(const FileHandle& dest) const;
217
221 [[nodiscard]] bool createLink(const FileHandle& dest) const;
222
227
231 [[nodiscard]] bool rename(StringView filename) const;
232
237 [[nodiscard]] bool remove() const;
238
250 [[nodiscard]] FileWatcher watch(ExecutionContext& executionContext, const FileEvents& events = FileEvent::Default, bool recursive = true) const;
251
254 [[nodiscard]] InputStream createInputStream(std::ios_base::openmode mode = std::ios_base::in) const;
255
258 [[nodiscard]] OutputStream createOutputStream(std::ios_base::openmode mode = std::ios_base::out) const;
259
263
269
270 public:
271 [[nodiscard]] explicit operator bool() const noexcept { return mImplementation != nullptr; }
272 [[nodiscard]] bool operator==(const FileHandle& other) const noexcept;
273 [[nodiscard]] bool operator!=(const FileHandle& other) const noexcept;
274
275 private:
280 [[nodiscard]] bool genericCopy(const FileHandle& dest) const;
281
286 [[nodiscard]] bool genericMove(const FileHandle& dest) const;
287 };
288
290
293 public:
294 IFileHandle() = default;
295 virtual ~IFileHandle() = default;
296
297 public:
299 [[nodiscard]] virtual IFileSystem* getFileSystem() const = 0;
300
302 virtual void updateFileInfo() = 0;
303
305 [[nodiscard]] virtual StringView getPath() const = 0;
306
308 [[nodiscard]] virtual bool exists() const = 0;
309
311 [[nodiscard]] virtual bool isFile() const = 0;
312
314 [[nodiscard]] virtual bool isDirectory() const = 0;
315
317 [[nodiscard]] virtual bool isSymbolicLink() const = 0;
318
320 [[nodiscard]] virtual FileIterator begin() const = 0;
321
323 [[nodiscard]] virtual UInt64 getSize() const = 0;
324
326 [[nodiscard]] virtual Int64 getAccessTime() const = 0;
327
329 [[nodiscard]] virtual Int64 getModificationTime() const = 0;
330
332 [[nodiscard]] virtual bool createDirectory() = 0;
333
335 [[nodiscard]] virtual bool removeDirectory() = 0;
336
338 [[nodiscard]] virtual bool copy(IFileHandle& dest) = 0;
339
341 [[nodiscard]] virtual bool move(IFileHandle& dest) = 0;
342
344 [[nodiscard]] virtual bool createLink(IFileHandle& dest) = 0;
345
348
350 [[nodiscard]] virtual bool rename(StringView filename) = 0;
351
353 [[nodiscard]] virtual bool remove() = 0;
354
356 [[nodiscard]] virtual InputStream createInputStream(std::ios_base::openmode mode) const = 0;
357
359 [[nodiscard]] virtual OutputStream createOutputStream(std::ios_base::openmode mode) = 0;
360 };
361
364 friend class FileHandle;
365
366 public:
368 virtual ~FileVisitor();
369
370 protected:
379 [[nodiscard]] virtual bool onFileEntry(const FileHandle& handle);
380
384 virtual void onFile(const FileHandle& handle);
385
389 [[nodiscard]] virtual bool onDirectory(const FileHandle& handle);
390 };
391
430
433 public:
434 using ImplementationType = Poly<IFileIterator, sizeof(void*) * 4, false>;
435
436 private:
438 ImplementationType mImplementation = nullptr;
439
440 public:
443
446
447 FileIterator(FileIterator&& other) noexcept;
449
450 FileIterator(const FileIterator& other) = delete;
451 FileIterator& operator=(const FileIterator& other) = delete;
452
455
456 public:
459
461 void operator++();
462
466
470
473 [[nodiscard]] IFileSystem* getFileSystem() const;
474 };
475
478 public:
479 IFileIterator() = default;
480 virtual ~IFileIterator() = default;
481
482 public:
483 [[nodiscard]] virtual IFileSystem* getFileSystem() const = 0;
484
487 [[nodiscard]] virtual bool isValid() const = 0;
488
490 [[nodiscard]] virtual const FileHandle& getParent() const = 0;
491
494 [[nodiscard]] virtual Int32 getIndex() const = 0;
495
497 [[nodiscard]] virtual FileHandle getFileHandle() const = 0;
498
500 virtual void next() = 0;
501 };
502
503} // namespace CeresEngine
#define CE_FLAGS_OPERATORS(Enum)
Defines global operators for a Flags<Enum, Storage> implementation.
Definition Flags.hpp:216
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
A context for function object execution.
Definition ExecutionContext.hpp:90
Handle for a file or directory.
Definition FileHandle.hpp:46
bool operator==(const FileHandle &other) const noexcept
bool isDirectory() const
Checks if item is a directory.
bool rename(StringView filename) const
Renames a file or directory.
Vector< String > listFiles() const
List files in directory.
bool genericMove(const FileHandle &dest) const
Moves a file by stream copy and delete.
FileIterator begin() const
Gets an iterator that points to the first directory entry.
bool operator!=(const FileHandle &other) const noexcept
Optional< String > readFile() const
Reads the file contents to a string.
bool remove() const
Removes a file.
void copyDirectory(const FileHandle &dstDir) const
Copies a directory recursively.
IFileSystem * getFileSystem() const
Gets the backing IFileSystem for the handle.
bool createDirectory() const
Creates a new directory.
StringView getPath() const
bool isFile() const
Checks if item is a file.
void updateFileInfo() const
Update file information.
FileIterator end() const
Gets an iterator that points after the last directory entry.
Int64 getModificationTime() const
Gets time of last modification.
std::function< bool(const FileHandle &)> VisitFunc
Definition FileHandle.hpp:49
OutputStream createOutputStream(std::ios_base::openmode mode=std::ios_base::out) const
Creates an output stream to write to the file.
bool move(const FileHandle &dest) const
Moves a file.
bool isSymbolicLink() const
Checks if item is a symbolic link.
bool removeDirectory(const RemoveDirectoryFlags &flags=RemoveDirectoryFlag::Default) const
Removes a directory.
StringView getFileName() const
bool createSymbolicLink(const FileHandle &dest) const
Creates a symbolic link.
InputStream createInputStream(std::ios_base::openmode mode=std::ios_base::in) const
Creates an input stream to read from the file.
bool copy(const FileHandle &dest) const
Copies a file.
ImplementationType mImplementation
The file handle implementation.
Definition FileHandle.hpp:53
bool writeFile(StringView content) const
Write the contents of a string to the file.
bool genericCopy(const FileHandle &dest) const
Copies the file by stream copy.
FileHandle open(StringView path) const
Opens a new file handle using a relative path from the current file/directory.
bool exists() const
Checks if the file exists on the file system.
void traverse(VisitFunc funcFileEntry) const
Traverse directory tree with callback functions.
FileHandle() noexcept
Constructs a new empty FileHandle.
Int64 getAccessTime() const
Gets the time of last access.
UInt64 getSize() const
Gets the file size.
FileHandle getParentDirectory() const
Gets a FileHandle to the parent directory.
FileWatcher watch(ExecutionContext &executionContext, const FileEvents &events=FileEvent::Default, bool recursive=true) const
Creates new file system watcher for this file handle.
bool createLink(const FileHandle &dest) const
Creates a hard link.
RemoveDirectoryFlag
A set of flags that customize removeDirectory() behavior.
Definition FileHandle.hpp:175
@ FollowSymlinks
To enter/follow symlink directories or not.
@ Recursive
Whether the file removal should be recursive or not.
An iterator that allows traversing over a file's children.
Definition FileHandle.hpp:432
FileIterator(FileIterator &&other) noexcept
FileIterator & operator=(const FileIterator &other)=delete
FileIterator(ImplementationType &&backend)
Creates a new FileIterator from a concrete iterator implementation.
FileIterator()
Creates a new empty FileIterator.
FileIterator & operator=(FileIterator &&other) noexcept
~FileIterator() noexcept
Destroys the FileIterator instance.
FileIterator(const FileIterator &other)=delete
Path to file or directory.
Definition FilePath.hpp:37
The file file visitor interface.
Definition FileHandle.hpp:363
virtual bool onFileEntry(const FileHandle &handle)
Called for each file entry visited (files and directories).
virtual bool onDirectory(const FileHandle &handle)
Called for each visited directory.
virtual void onFile(const FileHandle &handle)
Called for on each file visited.
Watcher that reports on changes of files or directories.
Definition FileWatcher.hpp:41
File visitor that calls a function or lambda.
Definition FileHandle.hpp:393
std::function< bool(const FileHandle &)> FileVistFunc
Definition FileHandle.hpp:396
std::function< bool(const FileHandle &)> DirectoryVisitFunc
Definition FileHandle.hpp:397
bool onDirectory(const FileHandle &handle) final
Called for each visited directory.
void onFile(const FileHandle &handle) final
Called for on each file visited.
FileVistFunc mFileFunc
Definition FileHandle.hpp:401
bool onFileEntry(const FileHandle &handle) final
Called for each file entry visited (files and directories).
std::function< bool(const FileHandle &)> VisitFunc
Definition FileHandle.hpp:395
DirectoryVisitFunc mDirectoryFunc
Definition FileHandle.hpp:402
VisitFunc mFileEntryFunc
Definition FileHandle.hpp:400
Base interface for file handles implementation.
Definition FileHandle.hpp:292
virtual bool removeDirectory()=0
Removes a directory.
virtual bool createLink(IFileHandle &dest)=0
Creates a hard link.
virtual bool createDirectory()=0
Creates a new directory.
virtual bool createSymbolicLink(IFileHandle &dest)=0
Creates a symbolic link.
virtual FileIterator begin() const =0
Gets an iterator that points to the first directory entry.
virtual bool copy(IFileHandle &dest)=0
Copies a file.
virtual bool isSymbolicLink() const =0
Checks if item is a symbolic link.
virtual bool isFile() const =0
Checks if item is a file.
virtual bool exists() const =0
Checks if the file exists on the file system.
virtual OutputStream createOutputStream(std::ios_base::openmode mode)=0
Creates an output stream to write to the file.
virtual bool rename(StringView filename)=0
Renames a file or directory.
virtual ~IFileHandle()=default
virtual Int64 getModificationTime() const =0
Gets time of last modification.
virtual InputStream createInputStream(std::ios_base::openmode mode) const =0
Creates an input stream to read from the file.
virtual void updateFileInfo()=0
Update file information.
virtual UInt64 getSize() const =0
Gets the file size.
virtual StringView getPath() const =0
virtual Int64 getAccessTime() const =0
Gets the time of last access.
virtual bool move(IFileHandle &dest)=0
Moves a file.
virtual IFileSystem * getFileSystem() const =0
Gets the backing IFileSystem for the handle.
virtual bool remove()=0
Removes a file.
virtual bool isDirectory() const =0
Checks if item is a directory.
Interface for iterating on directories.
Definition FileHandle.hpp:477
virtual bool isValid() const =0
Checks if iterator points to a valid item.
virtual ~IFileIterator()=default
virtual void next()=0
Advances to the next item.
virtual IFileSystem * getFileSystem() const =0
virtual const FileHandle & getParent() const =0
Gets the parent FileHandle this iterator iterates over.
virtual FileHandle getFileHandle() const =0
Gets the current entry file handle.
virtual Int32 getIndex() const =0
Gets the current index of iterator in the directory.
Interface for accessing file systems.
Definition FileSystem.hpp:28
A stream that provides read-only stream functionality.
Definition Stream.hpp:210
Definition Optional.hpp:17
A stream that provides write-only stream functionality.
Definition Stream.hpp:307
Definition Application.hpp:19
std::uint64_t UInt64
Definition DataTypes.hpp:26
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::int32_t Int32
Definition DataTypes.hpp:21
std::int64_t Int64
Definition DataTypes.hpp:24
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Wrapper around an enum that allows simple use of bitwise logic operations.
Definition Flags.hpp:19