C/C++ API Reference
Loading...
Searching...
No Matches
pw::stream::LimitedStreamWriter Class Reference

Overview

Wraps a stream to ensure only a limited number of bytes can be written.

Write attempts which would exceed the current limit will return OUT_OF_RANGE.

Inheritance diagram for pw::stream::LimitedStreamWriter:
pw::stream::NonSeekableWriter pw::stream::Writer pw::stream::Stream

Public Member Functions

 LimitedStreamWriter (Writer &writer, size_t limit=kUnlimited)
 
size_t limit () const
 Returns the current limit of this writer.
 
void set_limit (size_t limit)
 
size_t bytes_written () const
 Returns the number of bytes written to the stream.
 
- Public Member Functions inherited from pw::stream::Stream
constexpr bool readable () const
 
constexpr bool writable () const
 
constexpr bool seekable () const
 
constexpr bool seekable (Whence origin) const
 True if the stream supports seeking from the specified origin.
 
Result< ByteSpanRead (ByteSpan dest)
 
Result< ByteSpanRead (void *dest, size_t size_bytes)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Result< ByteSpanReadExact (ByteSpan const buffer)
 
Status Write (ConstByteSpan data)
 
Status Write (const void *data, size_t size_bytes)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Status Write (const std::byte b)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Status Seek (ptrdiff_t offset, Whence origin=kBeginning)
 
size_t Tell ()
 
size_t ConservativeReadLimit () const
 
size_t ConservativeWriteLimit () const
 

Private Member Functions

Status DoWrite (ConstByteSpan data) override
 Virtual Write() function implemented by derived classes.
 
size_t ConservativeLimit (LimitType limit_type) const override
 

Additional Inherited Members

- Public Types inherited from pw::stream::Stream
enum  Whence : uint8_t { kBeginning = 0b001 , kCurrent = 0b010 , kEnd = 0b100 }
 Positions from which to seek. More...
 
- Static Public Attributes inherited from pw::stream::Stream
static constexpr size_t kUnlimited = std::numeric_limits<size_t>::max()
 Value returned from read/write limit if unlimited.
 
static constexpr size_t kUnknownPosition = std::numeric_limits<size_t>::max()
 Returned by Tell() if getting the position is not supported.
 
- Protected Types inherited from pw::stream::Stream
enum class  LimitType : bool { kRead , kWrite }
 

Constructor & Destructor Documentation

◆ LimitedStreamWriter()

pw::stream::LimitedStreamWriter::LimitedStreamWriter ( Writer writer,
size_t  limit = kUnlimited 
)
inline

Constructs a LimitedStreamWriter which wraps another stream.

Parameters
[in]writerThe stream to wrap.
[in]limitThe maximum number of bytes which can be written. Defaults to kUnlimited.

Member Function Documentation

◆ ConservativeLimit()

size_t pw::stream::LimitedStreamWriter::ConservativeLimit ( LimitType  limit_type) const
inlineoverrideprivatevirtual

Virtual function optionally implemented by derived classes that is used for ConservativeReadLimit() and ConservativeWriteLimit().

The default implementation returns kUnlimited or 0 depending on whether the stream is readable/writable.

Reimplemented from pw::stream::Stream.

◆ DoWrite()

Status pw::stream::LimitedStreamWriter::DoWrite ( ConstByteSpan  data)
inlineoverrideprivatevirtual

Virtual Write() function implemented by derived classes.

Implements pw::stream::Stream.

◆ set_limit()

void pw::stream::LimitedStreamWriter::set_limit ( size_t  limit)
inline

Changes the current limit of this writer.

NOTE: If the limit is set to a value <= bytes_written(), no additional data can be written.


The documentation for this class was generated from the following file: