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