|
1 | 1 | #ifndef YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP |
2 | 2 | #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> |
4 | 7 |
|
5 | 8 | #include <cstddef> |
6 | 9 | #include <string> |
|
10 | 13 | namespace ystdlib::io_interface { |
11 | 14 | class WriterInterface { |
12 | 15 | 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; |
18 | 26 |
|
19 | 27 | // Destructor |
20 | 28 | virtual ~WriterInterface() = default; |
21 | 29 |
|
22 | 30 | // Methods |
23 | 31 | /** |
24 | | - * Writes the given data to the underlying medium |
| 32 | + * Writes the given data to the underlying medium. |
25 | 33 | * @param data |
26 | 34 | * @param data_length |
27 | 35 | */ |
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; |
33 | 37 |
|
34 | 38 | /** |
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 |
37 | 46 | */ |
38 | 47 | template <typename ValueType> |
39 | | - void write_numeric_value(ValueType value); |
| 48 | + [[nodiscard]] auto write_numeric_value(ValueType value) -> ErrorCode; |
40 | 49 |
|
41 | 50 | /** |
42 | | - * Writes a character to the underlying medium |
| 51 | + * Writes a character to the underlying medium. |
43 | 52 | * @param c |
44 | 53 | */ |
45 | | - void write_char(char c); |
| 54 | + [[nodiscard]] virtual auto write_char(char c) -> ErrorCode { return write(&c, 1); } |
| 55 | + |
46 | 56 | /** |
47 | | - * Writes a string to the underlying medium |
| 57 | + * Writes a string to the underlying medium. |
48 | 58 | * @param str |
49 | 59 | */ |
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 | + } |
51 | 63 |
|
52 | 64 | /** |
53 | | - * Seeks from the beginning to the given position |
| 65 | + * Seeks from the beginning to the given position. |
54 | 66 | * @param pos |
55 | 67 | */ |
56 | | - void seek_from_begin(size_t pos); |
| 68 | + [[nodiscard]] virtual auto seek_from_begin(size_t pos) -> ErrorCode = 0; |
57 | 69 |
|
58 | 70 | /** |
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. |
60 | 72 | * @param offset |
61 | 73 | */ |
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; |
63 | 77 |
|
64 | 78 | /** |
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. |
67 | 80 | */ |
68 | | - size_t get_pos() const; |
| 81 | + [[nodiscard]] virtual auto get_pos(size_t& pos) -> ErrorCode = 0; |
69 | 82 | }; |
70 | 83 |
|
71 | 84 | 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)); |
74 | 87 | } |
75 | 88 | } // namespace ystdlib::io_interface |
76 | 89 |
|
77 | | -// NOLINTEND |
78 | 90 | #endif // YSTDLIB_IO_INTERFACE_WRITERINTERFACE_HPP |
0 commit comments