|
1 | 1 | #ifndef YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP |
2 | 2 | #define YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP |
3 | | -// NOLINTBEGIN |
| 3 | + |
| 4 | +#include <sys/types.h> |
4 | 5 |
|
5 | 6 | #include <cstddef> |
6 | 7 | #include <string> |
|
10 | 11 | namespace ystdlib::io_interface { |
11 | 12 | class ReaderInterface { |
12 | 13 | public: |
13 | | - // Types |
14 | | - class OperationFailed : public std::exception { |
15 | | - public: |
16 | | - OperationFailed(ErrorCode error_code) {} |
17 | | - }; |
| 14 | + // Constructor |
| 15 | + ReaderInterface() = default; |
18 | 16 |
|
19 | | - // Destructor |
20 | | - virtual ~ReaderInterface() = default; |
| 17 | + // Delete copy constructor and assignment operator |
| 18 | + ReaderInterface(ReaderInterface const&) = delete; |
| 19 | + auto operator=(ReaderInterface const&) -> ReaderInterface& = delete; |
21 | 20 |
|
22 | | - // Methods |
23 | | - virtual ErrorCode try_read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) = 0; |
24 | | - virtual ErrorCode try_seek_from_begin(size_t pos) = 0; |
25 | | - virtual ErrorCode try_get_pos(size_t& pos) = 0; |
| 21 | + // Default move constructor and assignment operator |
| 22 | + ReaderInterface(ReaderInterface&&) noexcept = default; |
| 23 | + auto operator=(ReaderInterface&&) noexcept -> ReaderInterface& = default; |
26 | 24 |
|
27 | | - /** |
28 | | - * Tries to read up to the next delimiter and stores it in the given string. |
29 | | - * NOTE: Implementations should override this if they can achieve better performance. |
30 | | - * @param delim The delimiter to stop at |
31 | | - * @param keep_delimiter Whether to include the delimiter in the output string or not |
32 | | - * @param append Whether to append to the given string or replace its contents |
33 | | - * @param str The string read |
34 | | - * @return ErrorCode_Success on success |
35 | | - * @return Same as ReaderInterface::try_read otherwise |
36 | | - */ |
37 | | - virtual ErrorCode |
38 | | - try_read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str); |
| 25 | + // Destructor |
| 26 | + virtual ~ReaderInterface() = default; |
39 | 27 |
|
40 | | - /** |
41 | | - * Reads up to a given number of bytes |
| 28 | + // Methods implementing ReaderInterface |
| 29 | + /* |
| 30 | + * Reads up to the given number of bytes from the underlying medium into the given buffer. |
42 | 31 | * @param buf |
43 | | - * @param num_bytes_to_read The number of bytes to try and read |
44 | | - * @param num_bytes_read The actual number of bytes read |
45 | | - * @return false on EOF |
46 | | - * @return true otherwise |
| 32 | + * @param num_bytes_to_read |
| 33 | + * @param num_bytes_read Returns the actual number of bytes read. |
47 | 34 | */ |
48 | | - bool read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read); |
| 35 | + [[nodiscard]] virtual auto read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) |
| 36 | + -> ErrorCode |
| 37 | + = 0; |
49 | 38 |
|
50 | 39 | /** |
51 | | - * Reads up to the next delimiter and stores it in the given string |
52 | | - * @param delim The delimiter to stop at |
53 | | - * @param keep_delimiter Whether to include the delimiter in the output string or not |
54 | | - * @param append Whether to append to the given string or replace its contents |
55 | | - * @param str The string read |
56 | | - * @return false on EOF |
57 | | - * @return true on success |
| 40 | + * Reads up to the next delimiter from the underlying medium into the given string. |
| 41 | + * @param delim The delimiter to stop at. |
| 42 | + * @param keep_delimiter Whether to include the delimiter in the output string or not. |
| 43 | + * @param append Whether to append to the given string or replace its contents. |
| 44 | + * @param str Returns the string read. |
58 | 45 | */ |
59 | | - bool read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str); |
| 46 | + [[nodiscard]] virtual auto |
| 47 | + read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str) -> ErrorCode; |
60 | 48 |
|
61 | 49 | /** |
62 | | - * Tries to read a number of bytes |
63 | | - * @param buf |
64 | | - * @param num_bytes Number of bytes to read |
65 | | - * @return Same as the underlying medium's try_read method |
66 | | - * @return ErrorCode_Truncated if 0 < # bytes read < num_bytes |
67 | | - */ |
68 | | - ErrorCode try_read_exact_length(char* buf, size_t num_bytes); |
69 | | - /** |
70 | | - * Reads a number of bytes |
| 50 | + * Reads the given number of bytes from the underlying medium into the given buffer. |
71 | 51 | * @param buf |
72 | | - * @param num_bytes Number of bytes to read |
73 | | - * @param eof_possible If EOF should be possible (without reading any bytes) |
74 | | - * @return false if EOF is possible and EOF was hit |
75 | | - * @return true on success |
| 52 | + * @param num_bytes Number of bytes to read. |
76 | 53 | */ |
77 | | - bool read_exact_length(char* buf, size_t num_bytes, bool eof_possible); |
| 54 | + [[nodiscard]] virtual auto read_exact_length(char* buf, size_t num_bytes) -> ErrorCode; |
78 | 55 |
|
79 | 56 | /** |
80 | | - * Tries to read a numeric value from a file |
81 | | - * @param value The read value |
82 | | - * @return Same as FileReader::try_read_exact_length's return values |
| 57 | + * @param value Returns the read numeric value. |
83 | 58 | */ |
84 | 59 | template <typename ValueType> |
85 | | - ErrorCode try_read_numeric_value(ValueType& value); |
86 | | - /** |
87 | | - * Reads a numeric value |
88 | | - * @param value The read value |
89 | | - * @param eof_possible If EOF should be possible (without reading any bytes) |
90 | | - * @return false if EOF is possible and EOF was hit |
91 | | - * @return true on success |
92 | | - */ |
93 | | - template <typename ValueType> |
94 | | - bool read_numeric_value(ValueType& value, bool eof_possible); |
| 60 | + [[nodiscard]] auto read_numeric_value(ValueType& value) -> ErrorCode; |
95 | 61 |
|
96 | 62 | /** |
97 | | - * Tries to read a string |
98 | 63 | * @param str_length |
99 | | - * @param str The string read |
100 | | - * @return Same as ReaderInterface::try_read_exact_length |
| 64 | + * @param str Returns the string read. |
101 | 65 | */ |
102 | | - ErrorCode try_read_string(size_t str_length, std::string& str); |
| 66 | + [[nodiscard]] virtual auto read_string(size_t str_length, std::string& str) -> ErrorCode; |
| 67 | + |
| 68 | + // Methods implementing general stream interface |
103 | 69 | /** |
104 | | - * Reads a string |
105 | | - * @param str_length |
106 | | - * @param str The string read |
107 | | - * @param eof_possible If EOF should be possible (without reading any bytes) |
108 | | - * @return false if EOF is possible and EOF was hit |
109 | | - * @return true on success |
| 70 | + * Seeks from the beginning to the given position. |
| 71 | + * @param pos |
110 | 72 | */ |
111 | | - bool read_string(size_t str_length, std::string& str, bool eof_possible); |
| 73 | + [[nodiscard]] virtual auto seek_from_begin(size_t pos) -> ErrorCode = 0; |
112 | 74 |
|
113 | 75 | /** |
114 | | - * Seeks from the beginning to the given position |
115 | | - * @param pos |
| 76 | + * Seeks from the current position to the next position by the given offset amount. |
| 77 | + * @param offset |
116 | 78 | */ |
117 | | - void seek_from_begin(size_t pos); |
| 79 | + [[nodiscard]] virtual auto seek_from_current(off_t offset) -> ErrorCode = 0; |
118 | 80 |
|
119 | 81 | /** |
120 | | - * Gets the current position of the read head |
121 | | - * @return Position of the read head |
| 82 | + * @param pos Returns the current position of the stream pointer. |
122 | 83 | */ |
123 | | - size_t get_pos(); |
| 84 | + [[nodiscard]] virtual auto get_pos(size_t& pos) -> ErrorCode = 0; |
124 | 85 | }; |
125 | 86 |
|
126 | 87 | template <typename ValueType> |
127 | | -ErrorCode ReaderInterface::try_read_numeric_value(ValueType& value) { |
128 | | - ErrorCode error_code = try_read_exact_length(reinterpret_cast<char*>(&value), sizeof(value)); |
129 | | - if (ErrorCode_Success != error_code) { |
130 | | - return error_code; |
131 | | - } |
132 | | - return ErrorCode_Success; |
133 | | -} |
134 | | - |
135 | | -template <typename ValueType> |
136 | | -bool ReaderInterface::read_numeric_value(ValueType& value, bool eof_possible) { |
137 | | - ErrorCode error_code = try_read_numeric_value(value); |
138 | | - if (ErrorCode_EndOfFile == error_code && eof_possible) { |
139 | | - return false; |
140 | | - } |
141 | | - if (ErrorCode_Success != error_code) { |
142 | | - throw OperationFailed(error_code); |
143 | | - } |
144 | | - return true; |
| 88 | +auto ReaderInterface::read_numeric_value(ValueType& value) -> ErrorCode { |
| 89 | + return read_exact_length(static_cast<char*>(&value), sizeof(ValueType)); |
145 | 90 | } |
146 | 91 | } // namespace ystdlib::io_interface |
147 | 92 |
|
148 | | -// NOLINTEND |
149 | 93 | #endif // YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP |
0 commit comments