GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
NativeOutputFile.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_SYSTEM_NATIVE_OUTPUT_FILE_HPP
5#define GREM_CORE_SYSTEM_NATIVE_OUTPUT_FILE_HPP
6
8
17
18#include <fstream> // std::ofstream, std::streamoff, std::streamsize
19#include <utility> // std::move
20
21namespace grem {
22
23class NativeFilesystem; // Forward declaration, to avoid a circular include of NativeFilesystem.hpp.
24
29 : public OutputFile
30 , public std::ofstream {
31public:
35 NativeOutputFile() noexcept = default;
36
38 ~NativeOutputFile() override = default;
39
42
44 NativeOutputFile(NativeOutputFile&& other) // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
45 : OutputFile(std::move(static_cast<OutputFile&>(other)))
46 , std::ofstream(std::move(static_cast<std::ofstream&>(other))) {}
47
50
52 NativeOutputFile& operator=(NativeOutputFile&&) = default; // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
53
54 void close() override {
55 std::ofstream::close();
56 }
57
58 [[nodiscard]] bool isOpen() const noexcept override {
59 return std::ofstream::is_open();
60 }
61
62 [[nodiscard]] size_t tellp() const noexcept override {
63 return static_cast<size_t>(const_cast<std::ofstream*>(static_cast<const std::ofstream*>(this))->tellp());
64 }
65
66 void seekp(size_t position) override {
67 if (std::ofstream::seekp(static_cast<std::ofstream::pos_type>(static_cast<std::streamoff>(position)), std::ofstream::beg).fail()) {
68 throw File::Error{"Failed to seek in file."};
69 }
70 }
71
72 void skipp(ptrdiff_t offset) override {
73 if (std::ofstream::seekp(static_cast<std::ofstream::pos_type>(static_cast<std::streamoff>(offset)), std::ofstream::cur).fail()) {
74 throw File::Error{"Failed to seek in file."};
75 }
76 }
77
78 size_t writeSome(Span<const byte> data) override {
79 const size_t size = min(data.size(), size_t{Limits<std::streamsize>::MAX});
80 if (std::ofstream::write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(size)).fail()) {
81 throw File::Error{"Failed to write to file."};
82 }
83 return size;
84 }
85
86 void flush() override {
87 if (std::ofstream::flush().fail()) {
88 throw File::Error{"Failed to flush file."};
89 }
90 }
91
92private:
93 friend NativeFilesystem;
94
95 explicit NativeOutputFile(std::ofstream&& stream) noexcept
96 : std::ofstream(std::move(stream)) {}
97};
98
99} // namespace grem
100
101#endif
Wrapper over the native host filesystem.
Definition NativeFilesystem.hpp:21
NativeOutputFile & operator=(const NativeOutputFile &)=delete
Copying a file handle is not allowed.
size_t writeSome(Span< const byte > data) override
Write data to the end of an open file from a buffer.
Definition NativeOutputFile.hpp:78
NativeOutputFile() noexcept=default
Construct a closed file handle without an associated file.
bool isOpen() const noexcept override
Check if the file handle has an open file associated with it.
Definition NativeOutputFile.hpp:58
void flush() override
Synchronize with the underlying file to make sure that all buffered data that has been written so far...
Definition NativeOutputFile.hpp:86
NativeOutputFile & operator=(NativeOutputFile &&)=default
Move assignment.
size_t tellp() const noexcept override
Get the current writing position of the file.
Definition NativeOutputFile.hpp:62
void seekp(size_t position) override
Set the file writing position to an absolute offset from the beginning of the file.
Definition NativeOutputFile.hpp:66
void close() override
Close the associated file so that it can no longer be accessed through this handle,...
Definition NativeOutputFile.hpp:54
void skipp(ptrdiff_t offset) override
Advance the file writing position by a relative offset, which may be negative in order to go backward...
Definition NativeOutputFile.hpp:72
OutputFile() noexcept=default
Default constructor.
Definition Span.hpp:146
constexpr size_type size() const noexcept
Definition Span.hpp:207
constexpr pointer data() const noexcept
Definition Span.hpp:203
Definition aliases.hpp:9
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
Exception type for errors that may arise when attempting to access files through the File API.
Definition File.hpp:37