xentara-utils v2.0.4
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::io::MemoryOutputDevice< Element > Class Template Referencefinal

A device that writes data to a memory region. More...

#include <xentara/utils/io/MemoryOutputDevice.hpp>

+ Inheritance diagram for xentara::utils::io::MemoryOutputDevice< Element >:

Public Types

using value_type = Element
 The element type.
 
using reference = value_type &
 A reference to an element.
 
using const_reference = const value_type &
 A const reference to an element.
 
using pointer = value_type *
 A pointer to an element.
 
using const_pointer = const value_type *
 A const pointer to an element.
 
using const_iterator = core::ContiguousIterator< const_pointer, MemoryOutputDevice >
 A const iterator.
 
using iterator = const_iterator
 An iterator.
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 A const iterator for iterating in reverse order.
 
using reverse_iterator = const_reverse_iterator
 An iterator for iterating in reverse order.
 
- Public Types inherited from xentara::utils::io::MemoryOutputDeviceBase
using size_type = std::size_t
 The type used for sizes.
 
using difference_type = std::ptrdiff_t
 The type used for distances.
 

Public Member Functions

Constructors
template<memory::MemoryIterator Iterator>
requires std::is_same_v<std::remove_volatile_t<std::iter_value_t<Iterator>>, Element>
 MemoryOutputDevice (Iterator begin, Iterator end)
 Constructs a stream that writes to a memory region defined by a pair of iterators.
 
template<memory::MemoryIterator Iterator>
requires std::is_same_v<std::remove_volatile_t<std::iter_value_t<Iterator>>, Element>
 MemoryOutputDevice (Iterator begin, std::iter_difference_t< Iterator > size)
 Constructs a stream that writes to a memory region defined by an iterator and a size.
 
template<memory::MemoryRegion Region>
requires std::is_same_v<std::remove_volatile_t<std::ranges::range_value_t<Region>>, Element>
 MemoryOutputDevice (Region &region)
 Constructs a stream that writes to a memory region.
 
Access to the written data
auto at (size_type index) const -> const_reference
 Accesses the element at a certain index.
 
auto operator[] (size_type index) const noexcept -> const_reference
 Subscript operator.
 
auto front () const noexcept -> const_reference
 Returns the first element.
 
auto back () const noexcept -> const_reference
 Returns the last element.
 
auto data () const noexcept -> const value_type *
 Raw access to the data.
 
template<typename CharTraits = std::char_traits<Element>>
requires std::same_as<Element, char> || std::same_as<Element, char8_t>
 operator std::basic_string_view< Element, CharTraits > () const
 Returns the data written so far as a string view.
 
Iterators
auto begin () const noexcept -> const_iterator
 Creates a begin iterator.
 
auto cbegin () const noexcept -> const_iterator
 Creates a begin iterator.
 
auto end () const noexcept -> const_iterator
 Creates an end iterator.
 
auto cend () const noexcept -> const_iterator
 Creates an end iterator.
 
auto rbegin () const noexcept -> const_reverse_iterator
 Creates a reverse begin iterator.
 
auto crbegin () const noexcept -> const_reverse_iterator
 Creates a reverse begin iterator.
 
auto rend () const noexcept -> const_reverse_iterator
 Creates a reverse end iterator.
 
auto crend () const noexcept -> const_reverse_iterator
 Creates a reverse end iterator.
 
- Public Member Functions inherited from xentara::utils::io::MemoryOutputDeviceBase
auto empty () const noexcept -> bool
 Determines whether the data is empty.
 
auto size () const noexcept -> size_type
 Gets the size of the data.
 
auto capacity () const noexcept -> size_type
 Gets the capacity of the data.
 
auto truncate () -> void
 Truncates the data at the current position.
 
auto clear () -> void
 Clears all written data and resets the write position to the beginning.
 
- Public Member Functions inherited from xentara::utils::io::OutputDevice
virtual ~OutputDevice () noexcept(false)=0
 Virtual, throwing destructor.
 
template<std::ranges::contiguous_range Data>
requires std::same_as<std::remove_cv_t<std::ranges::range_value_t<Data>>, std::byte>
auto write (const Data &data) -> void
 Writes a block of data.
 
auto writeByte (std::byte byte) -> void
 Write a single byte.
 
template<std::ranges::contiguous_range Data>
requires std::same_as<std::remove_cv_t<std::ranges::range_value_t<Data>>, std::byte>
auto writeChunk (const Data &data) -> std::ranges::range_size_t< Data >
 Writes a single chunk of data contained in an iterator range.
 
template<std::ranges::contiguous_range Data>
requires std::same_as<std::remove_cv_t<std::ranges::range_value_t<Data>>, std::byte>
auto tryWrite (const Data &data) -> std::ranges::range_size_t< Data >
 Write as much of a data block as is possible without blocking.
 
auto tryWriteByte (std::byte byte) -> bool
 Write a single byte, if this can be done without blocking.
 
template<std::contiguous_iterator Iterator, std::sized_sentinel_for< Iterator > Sentinel>
requires std::same_as<std::remove_cv_t<std::iter_value_t<Iterator>>, std::byte>
auto write (Iterator first, Sentinel last) -> void
 Writes the data contained in an iterator range.
 
template<std::contiguous_iterator Iterator, std::sized_sentinel_for< Iterator > Sentinel>
requires std::same_as<std::remove_cv_t<std::iter_value_t<Iterator>>, std::byte>
auto writeChunk (Iterator first, Sentinel last) -> Iterator
 Writes a single chunk of data from an an iterator range.
 
template<std::contiguous_iterator Iterator, std::sized_sentinel_for< Iterator > Sentinel>
requires std::same_as<std::iter_value_t<Iterator>, std::byte>
auto tryWrite (Iterator first, Sentinel last) -> Iterator
 Write as much of the data contained in an iterator range as is possible without blocking.
 
auto write (const std::byte *data, std::size_t size) -> void
 Write a block of data.
 
auto writeChunk (const std::byte *data, std::size_t size) -> std::size_t
 Writes a single chunk of data.
 
auto tryWrite (const std::byte *data, std::size_t size) -> std::size_t
 Write as much of a data block as is possible without blocking.
 
auto writePosition () const -> std::optional< std::size_t >
 Gets the current write position within the device.
 
auto setWritePosition (std::size_t position) -> std::optional< std::size_t >
 Sets the current write position within the device.
 
auto moveWritePosition (std::ptrdiff_t offset, std::ios_base::seekdir origin=std::ios_base::cur) -> std::optional< std::size_t >
 Advances or retreats the current write position within the device.
 
auto waitUntilWritable (std::chrono::nanoseconds timeout) -> bool
 Waits for the device to be able to accept more write data within a certain time.
 
auto writeTimeout () const noexcept -> std::chrono::nanoseconds
 Returns the write timeout for communication devices.
 
- Public Member Functions inherited from xentara::utils::io::DeviceBase
virtual ~DeviceBase () noexcept(false)=0
 Virtual, throwing destructor.
 
auto deviceDescription () const -> std::string
 Get a description of the device.
 

Additional Inherited Members

- Protected Member Functions inherited from xentara::utils::io::OutputDevice
virtual auto doWrite (const std::byte *data, std::size_t size) -> std::size_t=0
 Called by the framework to write data up to a maximum size.
 
virtual auto doWaitUntilWritable (std::optional< std::chrono::nanoseconds > timeout) -> bool
 Called by the framework to wait for the device to be able to accept more write data with an optional timeout.
 
virtual auto doWaitUntilWritableAndWrite (const std::byte *data, std::size_t size, std::chrono::nanoseconds timeout) -> std::size_t
 Called by the framework to wait for the device to be able to accept more write data, and then write to it.
 
virtual auto doGetWriteTimeout () const noexcept -> std::chrono::nanoseconds
 Called by the framework to get the write timeout.
 
virtual auto doGetWritePosition () const -> std::optional< std::size_t >
 Called by the framework to get the current write position within the device.
 
virtual auto doSetWritePosition (std::size_t position) -> std::optional< std::size_t >
 Called by the framework to set the current write position within the device.
 
virtual auto doMoveWritePosition (std::ptrdiff_t offset, std::ios_base::seekdir origin=std::ios_base::cur) -> std::optional< std::size_t >
 Called by the framework to advance or retreat the current write position within the device.
 
- Protected Member Functions inherited from xentara::utils::io::DeviceBase
virtual auto doGetDeviceDescription () const -> std::string=0
 Called by the framework to get a description of the device.
 

Detailed Description

template<typename Element = std::byte>
class xentara::utils::io::MemoryOutputDevice< Element >

A device that writes data to a memory region.

Template Parameters
ElementThe type for the elements. Must be std::byte, or another trivial type that is the same size as std::byte. Common types include char, unsigned char, and char8_t.
See also
MemoryOutputDevice<Void>

Member Typedef Documentation

◆ const_iterator

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::const_iterator = core::ContiguousIterator<const_pointer, MemoryOutputDevice>

A const iterator.

◆ const_pointer

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::const_pointer = const value_type *

A const pointer to an element.

◆ const_reference

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::const_reference = const value_type &

A const reference to an element.

◆ const_reverse_iterator

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::const_reverse_iterator = std::reverse_iterator<const_iterator>

A const iterator for iterating in reverse order.

◆ iterator

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::iterator = const_iterator

An iterator.

◆ pointer

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::pointer = value_type *

A pointer to an element.

◆ reference

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::reference = value_type &

A reference to an element.

◆ reverse_iterator

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::reverse_iterator = const_reverse_iterator

An iterator for iterating in reverse order.

◆ value_type

template<typename Element = std::byte>
using xentara::utils::io::MemoryOutputDevice< Element >::value_type = Element

The element type.

Constructor & Destructor Documentation

◆ MemoryOutputDevice() [1/3]

template<typename Element = std::byte>
template<memory::MemoryIterator Iterator>
requires std::is_same_v<std::remove_volatile_t<std::iter_value_t<Iterator>>, Element>
xentara::utils::io::MemoryOutputDevice< Element >::MemoryOutputDevice ( Iterator  begin,
Iterator  end 
)

Constructs a stream that writes to a memory region defined by a pair of iterators.

Parameters
beginAn iterator that points at the beginning of the memory region
endAn iterator that points at the end of the memory region

◆ MemoryOutputDevice() [2/3]

template<typename Element = std::byte>
template<memory::MemoryIterator Iterator>
requires std::is_same_v<std::remove_volatile_t<std::iter_value_t<Iterator>>, Element>
xentara::utils::io::MemoryOutputDevice< Element >::MemoryOutputDevice ( Iterator  begin,
std::iter_difference_t< Iterator size 
)

Constructs a stream that writes to a memory region defined by an iterator and a size.

Parameters
beginAn iterator that points at the beginning of the memory region
sizeThe size of the memory region

◆ MemoryOutputDevice() [3/3]

template<typename Element = std::byte>
template<memory::MemoryRegion Region>
requires std::is_same_v<std::remove_volatile_t<std::ranges::range_value_t<Region>>, Element>
xentara::utils::io::MemoryOutputDevice< Element >::MemoryOutputDevice ( Region &  region)

Constructs a stream that writes to a memory region.

Parameters
regionThe memory region

Member Function Documentation

◆ at()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::at ( size_type  index) const -> const_reference

Accesses the element at a certain index.

Parameters
indexThe index
Returns
A reference to the element at the given index
Exceptions
std::out_of_rangeindex is out of range

◆ back()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::back ( ) const -> const_reference
noexcept

Returns the last element.

Note
Must not be called on an empty vector
Returns
A reference to last element

◆ begin()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::begin ( ) const -> const_iterator
noexcept

Creates a begin iterator.

Returns
An iterator pointing at the first element, or end(), if the data is empty

◆ cbegin()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::cbegin ( ) const -> const_iterator
noexcept

Creates a begin iterator.

Returns
An iterator pointing at the first element, or end(), if the data is empty

◆ cend()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::cend ( ) const -> const_iterator
noexcept

Creates an end iterator.

Returns
An iterator pointing at the position past the last element

◆ crbegin()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::crbegin ( ) const -> const_reverse_iterator
noexcept

Creates a reverse begin iterator.

Returns
A reverse iterator pointing at the last element, or rend(), if the data is empty

◆ crend()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::crend ( ) const -> const_reverse_iterator
noexcept

Creates a reverse end iterator.

Returns
A reverse iterator pointing at the position before the first element

◆ data()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::data ( ) const -> const value_type *
noexcept

Raw access to the data.

Returns
A pointer to the begining of the written data.

◆ end()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::end ( ) const -> const_iterator
noexcept

Creates an end iterator.

Returns
An iterator pointing at the position past the last element

◆ front()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::front ( ) const -> const_reference
noexcept

Returns the first element.

Note
Must not be called on an empty vector
Returns
A reference to first element

◆ operator std::basic_string_view< Element, CharTraits >()

template<typename Element = std::byte>
template<typename CharTraits = std::char_traits<Element>>
requires std::same_as<Element, char> || std::same_as<Element, char8_t>
xentara::utils::io::MemoryOutputDevice< Element >::operator std::basic_string_view< Element, CharTraits > ( ) const

Returns the data written so far as a string view.

Requirements
This operator is only available for the element types char and char8_t.
Returns
The data as a string view

◆ operator[]()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::operator[] ( size_type  index) const -> const_reference
noexcept

Subscript operator.

Returns a reference to the element at a certain index

Parameters
indexThe index
Returns
A reference to the value at the given offset

◆ rbegin()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::rbegin ( ) const -> const_reverse_iterator
noexcept

Creates a reverse begin iterator.

Returns
A reverse iterator pointing at the last element, or rend(), if the data is empty

◆ rend()

template<typename Element = std::byte>
auto xentara::utils::io::MemoryOutputDevice< Element >::rend ( ) const -> const_reverse_iterator
noexcept

Creates a reverse end iterator.

Returns
A reverse iterator pointing at the position before the first element