Monero
Loading...
Searching...
No Matches
epee::byte_stream Class Reference

A partial drop-in replacement for std::ostream. More...

#include <byte_stream.h>

Public Types

using char_type = std::uint8_t
using Ch = char_type
using value_type = char_type

Public Member Functions

 byte_stream () noexcept
 Increase internal buffer by at least byte_stream_increase bytes.
 byte_stream (byte_stream &&rhs) noexcept
 ~byte_stream () noexcept=default
byte_streamoperator= (byte_stream &&rhs) noexcept
std::uint8_t * data () noexcept
const std::uint8_t * data () const noexcept
std::uint8_t * tellp () const noexcept
std::size_t available () const noexcept
std::size_t size () const noexcept
std::size_t capacity () const noexcept
void Flush () const noexcept
 Compatibility with rapidjson.
void reserve (const std::size_t more)
void clear () noexcept
 Reset write position, but do not release internal memory.
void write (const std::uint8_t *ptr, const std::size_t length)
void write (const char *ptr, const std::size_t length)
void write (const epee::span< const std::uint8_t > source)
void write (const epee::span< const char > source)
void put (const std::uint8_t ch)
void Put (const std::uint8_t ch)
void put_unsafe (const std::uint8_t ch) noexcept
void put_n (const std::uint8_t ch, const std::size_t count)
void push_back (const std::uint8_t ch)
byte_buffer take_buffer () noexcept

Private Member Functions

void overflow (const std::size_t requested)
 End of buffer.
void check (const std::size_t requested)
 Ensures that at least requested bytes are available.

Private Attributes

byte_buffer buffer_
std::uint8_t * next_write_
 Beginning of buffer.
const std::uint8_t * end_
 Current write position.

Detailed Description

A partial drop-in replacement for std::ostream.

Only a few base std::ostream functions are implemented - enough for rapidjson output currently.

Advantages over std::stringstream or rapidjson::StringBuffer:

  • The internal buffer can be taken without a copy.
  • The internal buffer can be given to byte_slice with zero allocations for reference count.
  • The internal buffer can be given to zmq_msg_data_init without a copy or extra allocation. an additional advantage over std::stringstream:
  • Construction is significantly faster - the global std::locale does not have to be acquired (global thread synchronization), and an extra allocation for std::stringbuf is not needed (which is an addition to the buffer inside of that object).

Member Typedef Documentation

◆ Ch

◆ char_type

using epee::byte_stream::char_type = std::uint8_t

◆ value_type

Constructor & Destructor Documentation

◆ byte_stream() [1/2]

epee::byte_stream::byte_stream ( )
inlinenoexcept

Increase internal buffer by at least byte_stream_increase bytes.

◆ byte_stream() [2/2]

epee::byte_stream::byte_stream ( byte_stream && rhs)
noexcept

◆ ~byte_stream()

epee::byte_stream::~byte_stream ( )
defaultnoexcept

Member Function Documentation

◆ available()

std::size_t epee::byte_stream::available ( ) const
inlinenoexcept

◆ capacity()

std::size_t epee::byte_stream::capacity ( ) const
inlinenoexcept

◆ check()

void epee::byte_stream::check ( const std::size_t requested)
inlineprivate

Ensures that at least requested bytes are available.

◆ clear()

void epee::byte_stream::clear ( )
inlinenoexcept

Reset write position, but do not release internal memory.

Postcondition
size() == 0.

◆ data() [1/2]

const std::uint8_t * epee::byte_stream::data ( ) const
inlinenoexcept

◆ data() [2/2]

std::uint8_t * epee::byte_stream::data ( )
inlinenoexcept

◆ Flush()

void epee::byte_stream::Flush ( ) const
inlinenoexcept

Compatibility with rapidjson.

◆ operator=()

byte_stream & epee::byte_stream::operator= ( byte_stream && rhs)
noexcept

◆ overflow()

void epee::byte_stream::overflow ( const std::size_t requested)
private

End of buffer.

Postcondition
requested <= available()

◆ push_back()

void epee::byte_stream::push_back ( const std::uint8_t ch)
inline

Copy ch to end of stream.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ Put()

void epee::byte_stream::Put ( const std::uint8_t ch)
inline

Copy ch to end of stream. Provides rapidjson compatability.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ put()

void epee::byte_stream::put ( const std::uint8_t ch)
inline

Copy ch to end of stream.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ put_n()

void epee::byte_stream::put_n ( const std::uint8_t ch,
const std::size_t count )
inline

Write ch to end of stream count times.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ put_unsafe()

void epee::byte_stream::put_unsafe ( const std::uint8_t ch)
inlinenoexcept

Writes ch to end of stream without runtime capacity checks. Must use reserve before calling this function. Primarily for use with rapidjson, which writes characters at a time but reserves memory in blocks. Most applications want to use put or write.

◆ reserve()

void epee::byte_stream::reserve ( const std::size_t more)
inline

Reserve at least more bytes.

Postcondition
size() + more <= available().
Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ size()

std::size_t epee::byte_stream::size ( ) const
inlinenoexcept

◆ take_buffer()

byte_buffer epee::byte_stream::take_buffer ( )
noexcept
Returns
The internal buffer.
Postcondition
size() == capacity() == 0.

◆ tellp()

std::uint8_t * epee::byte_stream::tellp ( ) const
inlinenoexcept

◆ write() [1/4]

void epee::byte_stream::write ( const char * ptr,
const std::size_t length )
inline

Copy length bytes starting at ptr to end of stream.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ write() [2/4]

void epee::byte_stream::write ( const epee::span< const char > source)
inline

Copy source to end of stream.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ write() [3/4]

void epee::byte_stream::write ( const epee::span< const std::uint8_t > source)
inline

Copy source to end of stream.

Exceptions
std::range_errorif exceeding max size_t value.
std::bad_allocif allocation fails.

◆ write() [4/4]

void epee::byte_stream::write ( const std::uint8_t * ptr,
const std::size_t length )
inline

Copy length bytes starting at ptr to end of stream.

Exceptions
std::range_errorIf exceeding max size_t value.
std::bad_allocIf allocation fails.

Member Data Documentation

◆ buffer_

byte_buffer epee::byte_stream::buffer_
private

◆ end_

const std::uint8_t* epee::byte_stream::end_
private

Current write position.

◆ next_write_

std::uint8_t* epee::byte_stream::next_write_
private

Beginning of buffer.


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