cubos::core::data::StandardArchive class

Archive implementation which reads and writes from/into the OS file system using the standard library.

Can represent both regular files and directories.

Base classes

class Archive
Interface for a bridge between the Cubos virtual file system and the real world.

Constructors, destructors, conversion operators

StandardArchive(const std::filesystem::path& osPath, bool isDirectory, bool readOnly)
Constructs pointing to the regular file or directory with the given osPath.

Public functions

auto create(std::size_t parent, std::string_view name, bool directory = false) -> std::size_t override
Creates a new file in the archive.
auto destroy(std::size_t id) -> bool override
Destroys a regular file or empty directory in the archive.
auto name(std::size_t id) const -> std::string override
Gets the name of the file with the given id.
auto directory(std::size_t id) const -> bool override
Checks whether the file with the given id is a directory.
auto readOnly() const -> bool override
Checks whether the archive is read-only.
auto parent(std::size_t id) const -> std::size_t override
Gets the identifier of the parent file of the file with the given id.
auto sibling(std::size_t id) const -> std::size_t override
Gets the identifier of the sibling of the file with the given id.
auto child(std::size_t id) const -> std::size_t override
Gets the identifier of the first child of the file with the given id.
auto open(std::size_t id, File::Handle handle, File::OpenMode mode) -> std::unique_ptr<memory::Stream> override
Opens a file in the archive.

Function documentation

cubos::core::data::StandardArchive::StandardArchive(const std::filesystem::path& osPath, bool isDirectory, bool readOnly)

Constructs pointing to the regular file or directory with the given osPath.

Parameters
osPath The path to the file/directory in the real file system.
isDirectory Whether the path is a directory or a file.
readOnly True if the archive is read-only, false otherwise.

If readOnly is false and there's no file or directory at osPath, then it is created.

std::size_t cubos::core::data::StandardArchive::create(std::size_t parent, std::string_view name, bool directory = false) override

Creates a new file in the archive.

Parameters
parent Parent directory of the new file.
name Name of the new file.
directory Whether the new file is a directory or not.
Returns Identifier of the file, or 0 if the file could not be created.

The first child of the parent will have its sibling set to the new file and then be replaced by the new file.

bool cubos::core::data::StandardArchive::destroy(std::size_t id) override

Destroys a regular file or empty directory in the archive.

Parameters
id Identifier of the file.
Returns Whether the file was destroyed successfully.

Will be replaced in the tree by its sibling.

std::string cubos::core::data::StandardArchive::name(std::size_t id) const override

Gets the name of the file with the given id.

Parameters
id Identifier of the file.
Returns Name of the file.

bool cubos::core::data::StandardArchive::directory(std::size_t id) const override

Checks whether the file with the given id is a directory.

Parameters
id Identifier of the file.
Returns Whether the file is a directory or not.

bool cubos::core::data::StandardArchive::readOnly() const override

Checks whether the archive is read-only.

Returns Whether the archive is read-only.

std::size_t cubos::core::data::StandardArchive::parent(std::size_t id) const override

Gets the identifier of the parent file of the file with the given id.

Parameters
id Identifier of the file.
Returns Identifier of the parent file, or 0 if the file is the root file.

std::size_t cubos::core::data::StandardArchive::sibling(std::size_t id) const override

Gets the identifier of the sibling of the file with the given id.

Parameters
id Identifier of the file.
Returns Identifier of the sibling, or 0 if there are no more files in the parent.

std::size_t cubos::core::data::StandardArchive::child(std::size_t id) const override

Gets the identifier of the first child of the file with the given id.

Parameters
id Identifier of the file.
Returns Identifier of the first child, or 0 if the file is not a directory or is empty.

std::unique_ptr<memory::Stream> cubos::core::data::StandardArchive::open(std::size_t id, File::Handle handle, File::OpenMode mode) override

Opens a file in the archive.

Parameters
id Identifier of the file.
handle Handle to the file.
mode Mode to open the file in.
Returns File stream, or nullptr if the file could not be opened.

Although a bit hacky, the handle parameter is used to keep a reference to the respective File alive, preventing the file from being destroyed while the stream is open.