Skip to content

Commit 3ac60d1

Browse files
authored
refactor(io_interface): Change WriterInterface to return ErrorCodes instead of throwing exceptions; address clang-tidy violations in WriterInterface. (#51)
1 parent c293831 commit 3ac60d1

File tree

3 files changed

+41
-68
lines changed

3 files changed

+41
-68
lines changed

src/ystdlib/io_interface/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ cpp_library(
77
WriterInterface.hpp
88
PRIVATE_SOURCES
99
ReaderInterface.cpp
10-
WriterInterface.cpp
1110
TESTS_SOURCES
1211
test/test_ReaderInterface.cpp
1312
test/test_WriterInterface.cpp

src/ystdlib/io_interface/WriterInterface.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP
22
#define YSTDLIB_IO_INTERFACE_WRITERINTERFACE_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,69 +13,78 @@
1013
namespace ystdlib::io_interface {
1114
class WriterInterface {
1215
public:
13-
// Types
14-
class OperationFailed : public std::exception {
15-
public:
16-
OperationFailed(ErrorCode error_code) {}
17-
};
16+
// Constructor
17+
WriterInterface() = default;
18+
19+
// Delete copy constructor and assignment operator
20+
WriterInterface(WriterInterface const&) = delete;
21+
auto operator=(WriterInterface const&) -> WriterInterface& = delete;
22+
23+
// Default move constructor and assignment operator
24+
WriterInterface(WriterInterface&&) noexcept = default;
25+
auto operator=(WriterInterface&&) noexcept -> WriterInterface& = default;
1826

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

2230
// Methods
2331
/**
24-
* Writes the given data to the underlying medium
32+
* Writes the given data to the underlying medium.
2533
* @param data
2634
* @param data_length
2735
*/
28-
virtual void write(char const* data, size_t data_length) = 0;
29-
virtual void flush() = 0;
30-
virtual ErrorCode try_seek_from_begin(size_t pos) = 0;
31-
virtual ErrorCode try_seek_from_current(off_t offset) = 0;
32-
virtual ErrorCode try_get_pos(size_t& pos) const = 0;
36+
[[nodiscard]] virtual auto write(char const* data, size_t data_length) -> ErrorCode = 0;
3337

3438
/**
35-
* Writes a numeric value
36-
* @param val Value to write
39+
* Forces any buffered output data to be available at the underlying medium.
40+
*/
41+
[[nodiscard]] virtual auto flush() -> ErrorCode = 0;
42+
43+
/**
44+
* Writes a numeric value to the underlying medium.
45+
* @param val
3746
*/
3847
template <typename ValueType>
39-
void write_numeric_value(ValueType value);
48+
[[nodiscard]] auto write_numeric_value(ValueType value) -> ErrorCode;
4049

4150
/**
42-
* Writes a character to the underlying medium
51+
* Writes a character to the underlying medium.
4352
* @param c
4453
*/
45-
void write_char(char c);
54+
[[nodiscard]] virtual auto write_char(char c) -> ErrorCode { return write(&c, 1); }
55+
4656
/**
47-
* Writes a string to the underlying medium
57+
* Writes a string to the underlying medium.
4858
* @param str
4959
*/
50-
void write_string(std::string const& str);
60+
[[nodiscard]] virtual auto write_string(std::string const& str) -> ErrorCode {
61+
return write(str.c_str(), str.length());
62+
}
5163

5264
/**
53-
* Seeks from the beginning to the given position
65+
* Seeks from the beginning to the given position.
5466
* @param pos
5567
*/
56-
void seek_from_begin(size_t pos);
68+
[[nodiscard]] virtual auto seek_from_begin(size_t pos) -> ErrorCode = 0;
5769

5870
/**
59-
* Offsets from the current position by the given amount
71+
* Seeks from the current position to the next position by the given offset amount.
6072
* @param offset
6173
*/
62-
void seek_from_current(off_t offset);
74+
// TODO: https://github.com/y-scope/ystdlib-cpp/issues/50
75+
// NOLINTNEXTLINE(misc-include-cleaner)
76+
[[nodiscard]] virtual auto seek_from_current(off_t offset) -> ErrorCode = 0;
6377

6478
/**
65-
* Gets the current position of the write head
66-
* @return Position of the write head
79+
* @param pos Returns the current position of the read pointer.
6780
*/
68-
size_t get_pos() const;
81+
[[nodiscard]] virtual auto get_pos(size_t& pos) -> ErrorCode = 0;
6982
};
7083

7184
template <typename ValueType>
72-
void WriterInterface::write_numeric_value(ValueType val) {
73-
write(reinterpret_cast<char*>(&val), sizeof(val));
85+
auto WriterInterface::write_numeric_value(ValueType val) -> ErrorCode {
86+
return write(static_cast<char const*>(&val), sizeof(ValueType));
7487
}
7588
} // namespace ystdlib::io_interface
7689

77-
// NOLINTEND
7890
#endif // YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP

0 commit comments

Comments
 (0)