Cogs.Core
MemoryBufferBackedFileContents.h
1#pragma once
2
3#include "FileContents.h"
4
5namespace Cogs {
6 struct COGSFOUNDATION_API MemoryBufferBackedFileContents : public FileContents {
8 std::string path;
9
12 : FileContents(static_cast<const uint8_t*>(buffer.data()), buffer.size(), hints),
13 bytes(std::move(buffer)),
14 path(path)
15 {}
16
17 MemoryBufferBackedFileContents(size_t size, const std::string& path, FileContentsHints hints) : FileContents(nullptr, size, hints), bytes(size), path(path) {
18 ptr = static_cast<const uint8_t*>(bytes.data());
19 }
20
21 MemoryBufferBackedFileContents(void* data, size_t size, FileContentsHints hints) : FileContents(nullptr, size, hints) {
22 bytes.write(data, size);
23 ptr = static_cast<const uint8_t*>(bytes.data());
24 }
25
26 [[nodiscard]] Memory::MemoryBuffer take() override { return (ptr == bytes.data()) ? std::move(bytes) : takeCopy(); }
27 [[nodiscard]] StringView origin() override { return path; }
28 };
29}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
FileContentsHints
Definition: FileContents.h:11
STL namespace.
Abstract base class storing data read from a file.
Definition: FileContents.h:20
MemoryBufferBackedFileContents(Memory::MemoryBuffer &&buffer, const std::string &path, FileContentsHints hints)
Create a file contents object that takes ownership of an existing memorybuffer.
Memory::MemoryBuffer take() override
Take ownership of underlying memorybuffer if exists.