roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
memory_output_stream.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace roo_io {
6
7template <typename PtrType>
9 public:
10 MemoryOutputStream() : ptr_(nullptr), end_(nullptr), status_(kClosed) {}
11
12 MemoryOutputStream(PtrType begin, PtrType end)
13 : ptr_(begin), end_(end), status_(kOk) {}
14
15 size_t write(const byte* buf, size_t count) override {
16 if (status_ != kOk) return 0;
17 const size_t available = static_cast<size_t>(end_ - ptr_);
18 if (count > available) {
19 count = available;
20 status_ = kNoSpaceLeftOnDevice;
21 }
22 memcpy(ptr_, buf, count);
23 ptr_ += count;
24 return count;
25 }
26
27 void close() override {
28 if (status_ == kOk) {
29 status_ = kClosed;
30 }
31 }
32
33 Status status() const override { return status_; }
34
35 byte* ptr() const { return ptr_; }
36
37 private:
38 byte* ptr_;
39 const byte* end_;
40 Status status_;
41};
42
43} // namespace roo_io
void close() override
Flushes and closes this stream.
MemoryOutputStream(PtrType begin, PtrType end)
Status status() const override
Returns underlying stream status.
size_t write(const byte *buf, size_t count) override
Attempts to write up to count bytes from buf.
Virtualizes access to writable sinks (files, memory, devices).
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
@ kNoSpaceLeftOnDevice
Definition status.h:55
@ kOk
Definition status.h:8
@ kClosed
Definition status.h:10