roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
output_stream.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
5#include "roo_io/base/byte.h"
6#include "roo_io/status.h"
7
8namespace roo_io {
9
10/// Virtualizes access to writable sinks (files, memory, devices).
11///
12/// Represents an open writable resource.
13///
14/// For iterator-style writes, prefer `BufferedOutputStreamIterator` to reduce
15/// virtual-call overhead per byte.
17 public:
18 virtual ~OutputStream() { close(); }
19
20 /// Returns whether stream is considered open.
21 ///
22 /// @return `true` when `status()` is `kOk`.
23 bool isOpen() const { return status() == kOk; }
24
25 /// Flushes and closes this stream.
26 ///
27 /// On success, if status was `kOk`, status transitions to `kClosed`.
28 /// On failure, status reflects encountered error.
29 virtual void close() { flush(); }
30
31 /// Attempts to write up to `count` bytes from `buf`.
32 ///
33 /// Updates status.
34 ///
35 /// Contract:
36 /// - On success (`status() == kOk`), returns at least one byte.
37 /// - On error, may return zero or number of bytes written before failure.
38 ///
39 /// If pre-call status is not `kOk`, call should have no effect and return
40 /// zero.
41 ///
42 /// Successful return does not guarantee data reached final sink until
43 /// `flush()` succeeds.
44 ///
45 /// @return Number of bytes written.
46 virtual size_t write(const byte* buf, size_t count) = 0;
47
48 /// Attempts to write without indefinite blocking.
49 ///
50 /// Updates status.
51 ///
52 /// Similar to `write()`, but may return zero on success if forward progress
53 /// would require unacceptable blocking.
54 ///
55 /// Repeated `tryWrite()` calls make forward progress without requiring
56 /// fallback to `write()`.
57 ///
58 /// @return Number of bytes written.
59 virtual size_t tryWrite(const byte* buf, size_t count) {
60 return write(buf, count);
61 }
62
63 /// Attempts to write `count` bytes from `buf`.
64 ///
65 /// Updates status via `write()` calls.
66 ///
67 /// Unlike `write()`, this method keeps writing until either:
68 /// - all `count` bytes are written, or
69 /// - an error occurs.
70 ///
71 /// If pre-call status is not `kOk`, this method returns zero and leaves
72 /// status unchanged.
73 ///
74 /// Successful write still may be buffered; call `flush()` to force sink I/O.
75 ///
76 /// @return Total bytes written.
77 virtual size_t writeFully(const byte* buf, size_t count) {
78 size_t written_total = 0;
79 while (count > 0) {
80 size_t written_now = write(buf, count);
81 if (written_now == 0) break;
85 }
86 return written_total;
87 }
88
89 /// Flushes buffered data to the underlying sink.
90 ///
91 /// May update `status()`. Stream is also flushed on destruction.
92 virtual void flush() {}
93
94 /// Returns underlying stream status.
95 ///
96 /// Updated by write/flush operations. Status is either `kOk` or an error
97 /// (never `kEndOfStream`).
98 virtual Status status() const = 0;
99};
100
101} // namespace roo_io
Virtualizes access to writable sinks (files, memory, devices).
virtual void close()
Flushes and closes this stream.
virtual Status status() const =0
Returns underlying stream status.
virtual void flush()
Flushes buffered data to the underlying sink.
virtual size_t writeFully(const byte *buf, size_t count)
Attempts to write count bytes from buf.
virtual size_t write(const byte *buf, size_t count)=0
Attempts to write up to count bytes from buf.
bool isOpen() const
Returns whether stream is considered open.
virtual size_t tryWrite(const byte *buf, size_t count)
Attempts to write without indefinite blocking.
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
size_t count
Definition compare.h:45
Status
Definition status.h:7
@ kOk
Definition status.h:8