Skip to content

Commit c293831

Browse files
refactor(io_interface): Change ReaderInterface to return ErrorCodes instead of throwing exceptions; address clang-tidy violations in ReaderInterface. (#49)
Co-authored-by: davidlion <davidlion2@protonmail.com>
1 parent 2bfe6d7 commit c293831

File tree

2 files changed

+64
-182
lines changed

2 files changed

+64
-182
lines changed
Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
// NOLINTBEGIN
21
#include "ReaderInterface.hpp"
32

4-
using std::string;
3+
#include <cstddef>
4+
#include <string>
5+
6+
#include "ErrorCode.hpp"
57

68
namespace ystdlib::io_interface {
7-
ErrorCode ReaderInterface::try_read_to_delimiter(
8-
char delim,
9-
bool keep_delimiter,
10-
bool append,
11-
std::string& str
12-
) {
9+
auto
10+
ReaderInterface::read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str)
11+
-> ErrorCode {
1312
if (false == append) {
1413
str.clear();
1514
}
1615

17-
size_t original_str_length = str.length();
16+
auto const original_str_length{str.length()};
1817

1918
// Read character by character into str, until we find a delimiter
20-
char c;
21-
size_t num_bytes_read;
19+
char c{0};
20+
size_t num_bytes_read{0};
2221
while (true) {
23-
auto error_code = try_read(&c, 1, num_bytes_read);
22+
auto const error_code{read(&c, 1, num_bytes_read)};
2423
if (ErrorCode_Success != error_code) {
2524
if (ErrorCode_EndOfFile == error_code && str.length() > original_str_length) {
2625
return ErrorCode_Success;
@@ -43,32 +42,9 @@ ErrorCode ReaderInterface::try_read_to_delimiter(
4342
return ErrorCode_Success;
4443
}
4544

46-
bool ReaderInterface::read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) {
47-
ErrorCode error_code = try_read(buf, num_bytes_to_read, num_bytes_read);
48-
if (ErrorCode_EndOfFile == error_code) {
49-
return false;
50-
}
51-
if (ErrorCode_Success != error_code) {
52-
throw OperationFailed(error_code);
53-
}
54-
return true;
55-
}
56-
57-
bool ReaderInterface::read_to_delimiter(char delim, bool keep_delimiter, bool append, string& str) {
58-
ErrorCode error_code = try_read_to_delimiter(delim, keep_delimiter, append, str);
59-
if (ErrorCode_EndOfFile == error_code) {
60-
return false;
61-
}
62-
if (ErrorCode_Success != error_code) {
63-
throw OperationFailed(error_code);
64-
}
65-
66-
return true;
67-
}
68-
69-
ErrorCode ReaderInterface::try_read_exact_length(char* buf, size_t num_bytes) {
70-
size_t num_bytes_read;
71-
auto error_code = try_read(buf, num_bytes, num_bytes_read);
45+
auto ReaderInterface::read_exact_length(char* buf, size_t num_bytes) -> ErrorCode {
46+
size_t num_bytes_read{0};
47+
auto const error_code{read(buf, num_bytes, num_bytes_read)};
7248
if (ErrorCode_Success != error_code) {
7349
return error_code;
7450
}
@@ -79,51 +55,10 @@ ErrorCode ReaderInterface::try_read_exact_length(char* buf, size_t num_bytes) {
7955
return ErrorCode_Success;
8056
}
8157

82-
bool ReaderInterface::read_exact_length(char* buf, size_t num_bytes, bool eof_possible) {
83-
ErrorCode error_code = try_read_exact_length(buf, num_bytes);
84-
if (eof_possible && ErrorCode_EndOfFile == error_code) {
85-
return false;
86-
}
87-
if (ErrorCode_Success != error_code) {
88-
throw OperationFailed(error_code);
89-
}
90-
return true;
91-
}
92-
93-
ErrorCode ReaderInterface::try_read_string(size_t const str_length, string& str) {
58+
auto ReaderInterface::read_string(size_t const str_length, std::string& str) -> ErrorCode {
9459
// Resize string to fit str_length
9560
str.resize(str_length);
9661

97-
return try_read_exact_length(&str[0], str_length);
98-
}
99-
100-
bool ReaderInterface::read_string(size_t const str_length, string& str, bool eof_possible) {
101-
ErrorCode error_code = try_read_string(str_length, str);
102-
if (eof_possible && ErrorCode_EndOfFile == error_code) {
103-
return false;
104-
}
105-
if (ErrorCode_Success != error_code) {
106-
throw OperationFailed(error_code);
107-
}
108-
return true;
109-
}
110-
111-
void ReaderInterface::seek_from_begin(size_t pos) {
112-
ErrorCode error_code = try_seek_from_begin(pos);
113-
if (ErrorCode_Success != error_code) {
114-
throw OperationFailed(error_code);
115-
}
116-
}
117-
118-
size_t ReaderInterface::get_pos() {
119-
size_t pos;
120-
ErrorCode error_code = try_get_pos(pos);
121-
if (ErrorCode_Success != error_code) {
122-
throw OperationFailed(error_code);
123-
}
124-
125-
return pos;
62+
return read_exact_length(str.data(), str_length);
12663
}
12764
} // namespace ystdlib::io_interface
128-
129-
// NOLINTEND
Lines changed: 48 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP
22
#define YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP
3-
// NOLINTBEGIN
3+
4+
// TODO: https://github.com/y-scope/ystdlib-cpp/issues/50
5+
// NOLINTNEXTLINE(misc-include-cleaner)
6+
#include <sys/types.h>
47

58
#include <cstddef>
69
#include <string>
@@ -10,140 +13,84 @@
1013
namespace ystdlib::io_interface {
1114
class ReaderInterface {
1215
public:
13-
// Types
14-
class OperationFailed : public std::exception {
15-
public:
16-
OperationFailed(ErrorCode error_code) {}
17-
};
16+
// Constructor
17+
ReaderInterface() = default;
18+
19+
// Delete copy constructor and assignment operator
20+
ReaderInterface(ReaderInterface const&) = delete;
21+
auto operator=(ReaderInterface const&) -> ReaderInterface& = delete;
22+
23+
// Default move constructor and assignment operator
24+
ReaderInterface(ReaderInterface&&) noexcept = default;
25+
auto operator=(ReaderInterface&&) noexcept -> ReaderInterface& = default;
1826

1927
// Destructor
2028
virtual ~ReaderInterface() = default;
2129

2230
// 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;
26-
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);
39-
40-
/**
41-
* Reads up to a given number of bytes
31+
/*
32+
* Reads up to the given number of bytes from the underlying medium into the given buffer.
4233
* @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
34+
* @param num_bytes_to_read
35+
* @param num_bytes_read Returns the actual number of bytes read.
4736
*/
48-
bool read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read);
37+
[[nodiscard]] virtual auto read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read)
38+
-> ErrorCode
39+
= 0;
4940

5041
/**
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
42+
* Reads up to the next delimiter from the underlying medium into the given string.
43+
* @param delim The delimiter to stop at.
44+
* @param keep_delimiter Whether to include the delimiter in the output string or not.
45+
* @param append Whether to append to the given string or replace its contents.
46+
* @param str Returns the string read.
5847
*/
59-
bool read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str);
48+
[[nodiscard]] virtual auto
49+
read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str) -> ErrorCode;
6050

6151
/**
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
52+
* Reads the given number of bytes from the underlying medium into the given buffer.
7153
* @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
54+
* @param num_bytes Number of bytes to read.
7655
*/
77-
bool read_exact_length(char* buf, size_t num_bytes, bool eof_possible);
56+
[[nodiscard]] virtual auto read_exact_length(char* buf, size_t num_bytes) -> ErrorCode;
7857

7958
/**
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
59+
* @param value Returns the read numeric value.
8360
*/
8461
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);
62+
[[nodiscard]] auto read_numeric_value(ValueType& value) -> ErrorCode;
9563

9664
/**
97-
* Tries to read a string
9865
* @param str_length
99-
* @param str The string read
100-
* @return Same as ReaderInterface::try_read_exact_length
66+
* @param str Returns the string read.
10167
*/
102-
ErrorCode try_read_string(size_t str_length, std::string& str);
68+
[[nodiscard]] virtual auto read_string(size_t str_length, std::string& str) -> ErrorCode;
69+
10370
/**
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
71+
* Seeks from the beginning to the given position.
72+
* @param pos
11073
*/
111-
bool read_string(size_t str_length, std::string& str, bool eof_possible);
74+
[[nodiscard]] virtual auto seek_from_begin(size_t pos) -> ErrorCode = 0;
11275

11376
/**
114-
* Seeks from the beginning to the given position
115-
* @param pos
77+
* Seeks from the current position to the next position by the given offset amount.
78+
* @param offset
11679
*/
117-
void seek_from_begin(size_t pos);
80+
// TODO: https://github.com/y-scope/ystdlib-cpp/issues/50
81+
// NOLINTNEXTLINE(misc-include-cleaner)
82+
[[nodiscard]] virtual auto seek_from_current(off_t offset) -> ErrorCode = 0;
11883

11984
/**
120-
* Gets the current position of the read head
121-
* @return Position of the read head
85+
* @param pos Returns the current position of the read pointer.
12286
*/
123-
size_t get_pos();
87+
[[nodiscard]] virtual auto get_pos(size_t& pos) -> ErrorCode = 0;
12488
};
12589

12690
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;
91+
auto ReaderInterface::read_numeric_value(ValueType& value) -> ErrorCode {
92+
return read_exact_length(static_cast<char*>(&value), sizeof(ValueType));
14593
}
14694
} // namespace ystdlib::io_interface
14795

148-
// NOLINTEND
14996
#endif // YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP

0 commit comments

Comments
 (0)