-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathReaderInterface.hpp
More file actions
95 lines (81 loc) · 3.55 KB
/
ReaderInterface.hpp
File metadata and controls
95 lines (81 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP
#define YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP
#include <cstddef>
#include <span>
#include <string>
#include <ystdlib/error_handling/Result.hpp>
#include "StreamInterface.hpp"
namespace ystdlib::io_interface {
class ReaderInterface : public StreamInterface {
public:
// Constructor
ReaderInterface() = default;
// Delete copy constructor and assignment operator
ReaderInterface(ReaderInterface const&) = delete;
auto operator=(ReaderInterface const&) -> ReaderInterface& = delete;
// Default move constructor and assignment operator
ReaderInterface(ReaderInterface&&) noexcept = default;
auto operator=(ReaderInterface&&) noexcept -> ReaderInterface& = default;
// Destructor
~ReaderInterface() override = default;
// Methods
/**
* Reads up to the given number of bytes from the underlying medium into the given buffer.
* @param output_buffer
* @param num_bytes
* @param exact If the number of bytes read needs to be equal to the amount requested.
* @return The actual number of bytes read.
* @return ystdlib::io_interface::ErrorCode::BadParam
* @return ystdlib::io_interface::ErrorCode::BufferFull
* @return ystdlib::io_interface::ErrorCode::Corrupt
* @return ystdlib::io_interface::ErrorCode::EndOfFile
* @return ystdlib::io_interface::ErrorCode::EndOfStream
*/
[[nodiscard]] virtual auto
read(std::span<char> output_buffer, size_t num_bytes, bool exact = false)
-> ystdlib::error_handling::Result<size_t>
= 0;
/**
* @return The read character from the underlying medium.
* @return ystdlib::io_interface::ErrorCode::Corrupt
* @return ystdlib::io_interface::ErrorCode::EndOfFile
* @return ystdlib::io_interface::ErrorCode::EndOfStream
*/
[[nodiscard]] virtual auto read_char() -> ystdlib::error_handling::Result<char>;
/**
* @param num_bytes
* @return The read sring from the underlying medium.
* @return ystdlib::io_interface::ErrorCode::Corrupt
* @return ystdlib::io_interface::ErrorCode::EndOfFile
* @return ystdlib::io_interface::ErrorCode::EndOfStream
* @return ystdlib::io_interface::ErrorCode::OutOfMemory if the requested read is too large.
*/
[[nodiscard]] virtual auto read_string(size_t num_bytes)
-> ystdlib::error_handling::Result<std::string>;
/**
* @return The read numeric value from the underlying medium.
* @return ystdlib::io_interface::ErrorCode::Corrupt
* @return ystdlib::io_interface::ErrorCode::EndOfFile
* @return ystdlib::io_interface::ErrorCode::EndOfStream
*/
template <NumericType T>
[[nodiscard]] auto read_numeric_value() -> ystdlib::error_handling::Result<T>;
/**
* Reads up to the next delimiter from the underlying medium into the given buffer.
* @param output_buffer
* @param delim The delimiter to stop at.
* @param keep_delim Whether to include the delimiter in the output.
* @return The actual number of bytes read.
* @return Same as ReaderInterface::read.
*/
[[nodiscard]] virtual auto
read_to_delimiter(std::span<char> output_buffer, char delim, bool keep_delim)
-> ystdlib::error_handling::Result<size_t>;
};
template <NumericType T>
auto ReaderInterface::read_numeric_value() -> ystdlib::error_handling::Result<T> {
T value{};
return YSTDLIB_TRY(read({static_cast<char*>(&value), sizeof(T)}, sizeof(T), true));
}
} // namespace ystdlib::io_interface
#endif // YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP