| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_WRITE_BUFFER_HPP |
| 4 |
#define MPT_IO_WRITE_BUFFER_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/memory.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/integer.hpp" |
| 11 |
#include "mpt/base/span.hpp" |
| 12 |
#include "mpt/io/base.hpp" |
| 13 |
|
| 14 |
#include <cstddef> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
namespace mpt { |
| 19 |
inline namespace MPT_INLINE_NS { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace IO { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
// WriteBuffer class that avoids calling to the underlying file writing |
| 28 |
// functions for every operation, which would involve rather slow un-inlinable |
| 29 |
// virtual calls in the iostream and FILE* cases. It is the users responabiliy |
| 30 |
// to call HasWriteError() to check for writeback errors at this buffering |
| 31 |
// level. |
| 32 |
|
| 33 |
template <typename Tfile> |
| 34 |
class WriteBuffer { |
| 35 |
private: |
| 36 |
mpt::byte_span buffer; |
| 37 |
std::size_t size = 0; |
| 38 |
Tfile & f; |
| 39 |
bool writeError = false; |
| 40 |
|
| 41 |
public: |
| 42 |
WriteBuffer(const WriteBuffer &) = delete; |
| 43 |
WriteBuffer & operator=(const WriteBuffer &) = delete; |
| 44 |
|
| 45 |
public: |
| 46 |
inline WriteBuffer(Tfile & f_, mpt::byte_span buffer_) |
| 47 |
: buffer(buffer_) |
| 48 |
, f(f_) { |
| 49 |
return; |
| 50 |
} |
| 51 |
inline ~WriteBuffer() noexcept(false) { |
| 52 |
if (!writeError) { |
| 53 |
FlushLocal(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public: |
| 58 |
inline Tfile & file() const { |
| 59 |
if (IsDirty()) { |
| 60 |
FlushLocal(); |
| 61 |
} |
| 62 |
return f; |
| 63 |
} |
| 64 |
|
| 65 |
public: |
| 66 |
inline bool HasWriteError() const { |
| 67 |
return writeError; |
| 68 |
} |
| 69 |
inline void ClearError() { |
| 70 |
writeError = false; |
| 71 |
} |
| 72 |
inline bool IsDirty() const { |
| 73 |
return size > 0; |
| 74 |
} |
| 75 |
inline bool IsClean() const { |
| 76 |
return size == 0; |
| 77 |
} |
| 78 |
inline bool IsFull() const { |
| 79 |
return size == buffer.size(); |
| 80 |
} |
| 81 |
inline std::size_t GetCurrentSize() const { |
| 82 |
return size; |
| 83 |
} |
| 84 |
inline bool Write(mpt::const_byte_span data) { |
| 85 |
bool result = true; |
| 86 |
for (std::size_t i = 0; i < data.size(); ++i) { |
| 87 |
buffer[size] = data[i]; |
| 88 |
size++; |
| 89 |
if (IsFull()) { |
| 90 |
FlushLocal(); |
| 91 |
} |
| 92 |
} |
| 93 |
return result; |
| 94 |
} |
| 95 |
inline void FlushLocal() { |
| 96 |
if (IsClean()) { |
| 97 |
return; |
| 98 |
} |
| 99 |
try { |
| 100 |
if (!mpt::IO::WriteRaw(f, mpt::as_span(buffer.data(), size))) { |
| 101 |
writeError = true; |
| 102 |
} |
| 103 |
} catch (const std::exception &) { |
| 104 |
writeError = true; |
| 105 |
throw; |
| 106 |
} |
| 107 |
size = 0; |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
template <typename Tfile> |
| 112 |
struct FileOperations<WriteBuffer<Tfile>> { |
| 113 |
|
| 114 |
private: |
| 115 |
WriteBuffer<Tfile> & f; |
| 116 |
|
| 117 |
public: |
| 118 |
FileOperations(WriteBuffer<Tfile> & f_) |
| 119 |
: f(f_) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
public: |
| 124 |
// cppcheck false-positive |
| 125 |
// cppcheck-suppress duplInheritedMember |
| 126 |
inline bool IsValid() { |
| 127 |
return IsValid(f.file()); |
| 128 |
} |
| 129 |
|
| 130 |
// cppcheck false-positive |
| 131 |
// cppcheck-suppress duplInheritedMember |
| 132 |
inline bool IsReadSeekable() { |
| 133 |
return IsReadSeekable(f.file()); |
| 134 |
} |
| 135 |
|
| 136 |
// cppcheck false-positive |
| 137 |
// cppcheck-suppress duplInheritedMember |
| 138 |
inline bool IsWriteSeekable() { |
| 139 |
return IsWriteSeekable(f.file()); |
| 140 |
} |
| 141 |
|
| 142 |
// cppcheck false-positive |
| 143 |
// cppcheck-suppress duplInheritedMember |
| 144 |
inline IO::Offset TellRead() { |
| 145 |
f.FlushLocal(); |
| 146 |
return TellRead(f.file()); |
| 147 |
} |
| 148 |
|
| 149 |
// cppcheck false-positive |
| 150 |
// cppcheck-suppress duplInheritedMember |
| 151 |
inline IO::Offset TellWrite() { |
| 152 |
return TellWrite(f.file()) + f.GetCurrentSize(); |
| 153 |
} |
| 154 |
|
| 155 |
// cppcheck false-positive |
| 156 |
// cppcheck-suppress duplInheritedMember |
| 157 |
inline bool SeekBegin() { |
| 158 |
f.FlushLocal(); |
| 159 |
return SeekBegin(f.file()); |
| 160 |
} |
| 161 |
|
| 162 |
// cppcheck false-positive |
| 163 |
// cppcheck-suppress duplInheritedMember |
| 164 |
inline bool SeekEnd() { |
| 165 |
f.FlushLocal(); |
| 166 |
return SeekEnd(f.file()); |
| 167 |
} |
| 168 |
|
| 169 |
// cppcheck false-positive |
| 170 |
// cppcheck-suppress duplInheritedMember |
| 171 |
inline bool SeekAbsolute(IO::Offset pos) { |
| 172 |
f.FlushLocal(); |
| 173 |
return SeekAbsolute(f.file(), pos); |
| 174 |
} |
| 175 |
|
| 176 |
// cppcheck false-positive |
| 177 |
// cppcheck-suppress duplInheritedMember |
| 178 |
inline bool SeekRelative(IO::Offset off) { |
| 179 |
f.FlushLocal(); |
| 180 |
return SeekRelative(f.file(), off); |
| 181 |
} |
| 182 |
|
| 183 |
// cppcheck false-positive |
| 184 |
// cppcheck-suppress duplInheritedMember |
| 185 |
inline mpt::byte_span ReadRawImpl(mpt::byte_span data) { |
| 186 |
f.FlushLocal(); |
| 187 |
return ReadRawImpl(f.file(), data); |
| 188 |
} |
| 189 |
|
| 190 |
// cppcheck false-positive |
| 191 |
// cppcheck-suppress duplInheritedMember |
| 192 |
inline bool WriteRawImpl(mpt::const_byte_span data) { |
| 193 |
return f.Write(data); |
| 194 |
} |
| 195 |
|
| 196 |
// cppcheck false-positive |
| 197 |
// cppcheck-suppress duplInheritedMember |
| 198 |
inline bool IsEof() { |
| 199 |
f.FlushLocal(); |
| 200 |
return IsEof(f.file()); |
| 201 |
} |
| 202 |
|
| 203 |
// cppcheck false-positive |
| 204 |
// cppcheck-suppress duplInheritedMember |
| 205 |
inline bool Flush() { |
| 206 |
f.FlushLocal(); |
| 207 |
return Flush(f.file()); |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
} // namespace IO |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
} // namespace MPT_INLINE_NS |
| 218 |
} // namespace mpt |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#endif // MPT_IO_WRITE_BUFFER_HPP |