| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_BASE_HPP |
| 4 |
#define MPT_IO_BASE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/integer.hpp" |
| 9 |
#include "mpt/base/memory.hpp" |
| 10 |
#include "mpt/base/namespace.hpp" |
| 11 |
|
| 12 |
#include <cstddef> |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
namespace mpt { |
| 17 |
inline namespace MPT_INLINE_NS { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace IO { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
using Offset = int64; |
| 26 |
|
| 27 |
inline constexpr std::size_t BUFFERSIZE_MINUSCULE = 1 * 256; // on stack usage, tuned for single word/line buffers |
| 28 |
inline constexpr std::size_t BUFFERSIZE_TINY = 1 * 1024; // on stack usage |
| 29 |
inline constexpr std::size_t BUFFERSIZE_SMALL = 4 * 1024; // on heap |
| 30 |
inline constexpr std::size_t BUFFERSIZE_NORMAL = 64 * 1024; // FILE I/O |
| 31 |
inline constexpr std::size_t BUFFERSIZE_LARGE = 1024 * 1024; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
template <typename Tfile, typename Enable = void> |
| 36 |
struct FileOperations { |
| 37 |
}; |
| 38 |
|
| 39 |
template <typename Tfile> |
| 40 |
inline FileOperations<Tfile> FileOps(Tfile & f) { |
| 41 |
return FileOperations<Tfile>{f}; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
template <typename Tfile> |
| 47 |
inline bool IsValid(Tfile & f) { |
| 48 |
return FileOps(f).IsValid(); |
| 49 |
} |
| 50 |
|
| 51 |
template <typename Tfile> |
| 52 |
inline bool IsReadSeekable(Tfile & f) { |
| 53 |
return FileOps(f).IsReadSeekable(); |
| 54 |
} |
| 55 |
|
| 56 |
template <typename Tfile> |
| 57 |
inline bool IsWriteSeekable(Tfile & f) { |
| 58 |
return FileOps(f).IsWriteSeekable(); |
| 59 |
} |
| 60 |
|
| 61 |
template <typename Tfile> |
| 62 |
inline IO::Offset TellRead(Tfile & f) { |
| 63 |
return FileOps(f).TellRead(); |
| 64 |
} |
| 65 |
|
| 66 |
template <typename Tfile> |
| 67 |
inline IO::Offset TellWrite(Tfile & f) { |
| 68 |
return FileOps(f).TellWrite(); |
| 69 |
} |
| 70 |
|
| 71 |
template <typename Tfile> |
| 72 |
inline bool SeekBegin(Tfile & f) { |
| 73 |
return FileOps(f).SeekBegin(); |
| 74 |
} |
| 75 |
|
| 76 |
template <typename Tfile> |
| 77 |
inline bool SeekEnd(Tfile & f) { |
| 78 |
return FileOps(f).SeekEnd(); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename Tfile> |
| 82 |
inline bool SeekAbsolute(Tfile & f, IO::Offset pos) { |
| 83 |
return FileOps(f).SeekAbsolute(pos); |
| 84 |
} |
| 85 |
|
| 86 |
template <typename Tfile> |
| 87 |
inline bool SeekRelative(Tfile & f, IO::Offset off) { |
| 88 |
return FileOps(f).SeekRelative(off); |
| 89 |
} |
| 90 |
|
| 91 |
template <typename Tfile> |
| 92 |
inline mpt::byte_span ReadRawImpl(Tfile & f, mpt::byte_span data) { |
| 93 |
return FileOps(f).ReadRawImpl(data); |
| 94 |
} |
| 95 |
|
| 96 |
template <typename Tfile> |
| 97 |
inline bool WriteRawImpl(Tfile & f, mpt::const_byte_span data) { |
| 98 |
return FileOps(f).WriteRawImpl(data); |
| 99 |
} |
| 100 |
|
| 101 |
template <typename Tfile> |
| 102 |
inline bool IsEof(Tfile & f) { |
| 103 |
return FileOps(f).IsEof(); |
| 104 |
} |
| 105 |
|
| 106 |
template <typename Tfile> |
| 107 |
inline bool Flush(Tfile & f) { |
| 108 |
return FileOps(f).Flush(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
} // namespace IO |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
} // namespace MPT_INLINE_NS |
| 118 |
} // namespace mpt |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#endif // MPT_IO_BASE_HPP |