Monero
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
epee::byte_slice Class Reference

#include <byte_slice.h>

Collaboration diagram for epee::byte_slice:
Collaboration graph
[legend]

Classes

struct  adapt_buffer
 

Public Types

using value_type = std::uint8_t
 
using size_type = std::size_t
 
using difference_type = std::ptrdiff_t
 
using pointer = const std::uint8_t *
 
using const_pointer = const std::uint8_t *
 
using reference = std::uint8_t
 
using const_reference = std::uint8_t
 
using iterator = pointer
 
using const_iterator = const_pointer
 

Public Member Functions

 byte_slice () noexcept
 Construct empty slice. More...
 
 byte_slice (std::nullptr_t) noexcept
 Construct empty slice. More...
 
 byte_slice (std::initializer_list< span< const std::uint8_t >> sources)
 Scatter-gather (copy) multiple sources into a single allocated slice. More...
 
 byte_slice (std::vector< std::uint8_t > &&buffer)
 Convert buffer into a slice using one allocation for shared count. More...
 
 byte_slice (std::string &&buffer)
 Convert buffer into a slice using one allocation for shared count. More...
 
 byte_slice (byte_stream &&stream, bool shrink=true)
 Convert stream into a slice with zero allocations. More...
 
 byte_slice (byte_slice &&source) noexcept
 
 ~byte_slice () noexcept=default
 
byte_sliceoperator= (byte_slice &&) noexcept
 
byte_slice clone () const noexcept
 
iterator begin () const noexcept
 
const_iterator cbegin () const noexcept
 
iterator end () const noexcept
 
const_iterator cend () const noexcept
 
bool empty () const noexcept
 
const std::uint8_tdata () const noexcept
 
std::size_t size () const noexcept
 
std::size_t remove_prefix (std::size_t max_bytes) noexcept
 
byte_slice take_slice (std::size_t max_bytes) noexcept
 
byte_slice get_slice (std::size_t begin, std::size_t end) const
 
std::unique_ptr< byte_slice_data, release_byte_slicetake_buffer () noexcept
 

Private Member Functions

 byte_slice (byte_slice_data *storage, span< const std::uint8_t > portion) noexcept
 Internal use only; use to increase storage reference count. More...
 
template<typename T >
 byte_slice (const adapt_buffer, T &&buffer)
 

Private Attributes

std::unique_ptr< byte_slice_data, release_byte_slicestorage_
 
span< const std::uint8_tportion_
 

Detailed Description

Inspired by slices in golang. Storage is thread-safe reference counted, allowing for cheap copies or range selection on the bytes. The bytes owned by this class are always immutable.

The functions operator=, take_slice and remove_prefix may alter the reference count for the backing store, which will invalidate pointers previously returned if the reference count is zero. Be careful about "caching" pointers in these circumstances.

Member Typedef Documentation

◆ const_iterator

◆ const_pointer

◆ const_reference

◆ difference_type

using epee::byte_slice::difference_type = std::ptrdiff_t

◆ iterator

◆ pointer

◆ reference

◆ size_type

using epee::byte_slice::size_type = std::size_t

◆ value_type

Constructor & Destructor Documentation

◆ byte_slice() [1/9]

epee::byte_slice::byte_slice ( byte_slice_data storage,
span< const std::uint8_t portion 
)
privatenoexcept

Internal use only; use to increase storage reference count.

◆ byte_slice() [2/9]

template<typename T >
epee::byte_slice::byte_slice ( const adapt_buffer  ,
T &&  buffer 
)
explicitprivate

◆ byte_slice() [3/9]

epee::byte_slice::byte_slice ( )
inlinenoexcept

Construct empty slice.

◆ byte_slice() [4/9]

epee::byte_slice::byte_slice ( std::nullptr_t  )
inlinenoexcept

Construct empty slice.

◆ byte_slice() [5/9]

epee::byte_slice::byte_slice ( std::initializer_list< span< const std::uint8_t >>  sources)
explicit

Scatter-gather (copy) multiple sources into a single allocated slice.

◆ byte_slice() [6/9]

epee::byte_slice::byte_slice ( std::vector< std::uint8_t > &&  buffer)
explicit

Convert buffer into a slice using one allocation for shared count.

◆ byte_slice() [7/9]

epee::byte_slice::byte_slice ( std::string &&  buffer)
explicit

Convert buffer into a slice using one allocation for shared count.

◆ byte_slice() [8/9]

epee::byte_slice::byte_slice ( byte_stream &&  stream,
bool  shrink = true 
)
explicit

Convert stream into a slice with zero allocations.

◆ byte_slice() [9/9]

epee::byte_slice::byte_slice ( byte_slice &&  source)
noexcept

◆ ~byte_slice()

epee::byte_slice::~byte_slice ( )
defaultnoexcept

Member Function Documentation

◆ begin()

iterator epee::byte_slice::begin ( ) const
inlinenoexcept

◆ cbegin()

const_iterator epee::byte_slice::cbegin ( ) const
inlinenoexcept

◆ cend()

const_iterator epee::byte_slice::cend ( ) const
inlinenoexcept

◆ clone()

byte_slice epee::byte_slice::clone ( ) const
inlinenoexcept
Returns
A shallow (cheap) copy of the data from this slice.

◆ data()

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

◆ empty()

bool epee::byte_slice::empty ( ) const
inlinenoexcept

◆ end()

iterator epee::byte_slice::end ( ) const
inlinenoexcept

◆ get_slice()

byte_slice epee::byte_slice::get_slice ( std::size_t  begin,
std::size_t  end 
) const

Return a shallow (cheap) copy of a slice from begin and end offsets.

Exceptions
std::out_of_rangeIf end < begin.
std::out_of_rangeIf size() < end.
Returns
Slice starting at data() + begin of size end - begin.

◆ operator=()

byte_slice & epee::byte_slice::operator= ( byte_slice &&  source)
noexcept
Note
May invalidate previously retrieved pointers.

◆ remove_prefix()

std::size_t epee::byte_slice::remove_prefix ( std::size_t  max_bytes)
noexcept

Drop bytes from the beginning of this slice.

Note
May invalidate previously retrieved pointers.
Postcondition
this->size() = this->size() - std::min(this->size(), max_bytes)
if (this->size() <= max_bytes) this->data() = nullptr
Returns
Number of bytes removed.

◆ size()

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

◆ take_buffer()

std::unique_ptr< byte_slice_data, release_byte_slice > epee::byte_slice::take_buffer ( )
noexcept
Postcondition
empty()
Returns
Ownership of ref-counted buffer.

◆ take_slice()

byte_slice epee::byte_slice::take_slice ( std::size_t  max_bytes)
noexcept

"Take" bytes from the beginning of this slice.

Note
May invalidate previously retrieved pointers.
Postcondition
this->size() = this->size() - std::min(this->size(), max_bytes)
if (this->size() <= max_bytes) this->data() = nullptr
Returns
Slice containing the bytes removed from this slice.

Member Data Documentation

◆ portion_

span<const std::uint8_t> epee::byte_slice::portion_
private

◆ storage_

std::unique_ptr<byte_slice_data, release_byte_slice> epee::byte_slice::storage_
private

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