xentara-utils v2.0.4
The Xentara Utility Library
|
Base class for input streams. More...
#include <xentara/utils/io/InputStream.hpp>
Classes | |
class | ReadPosition |
A class used to remember positions with the stream. More... | |
Public Types | |
using | element_type = Element |
The element type. | |
Public Member Functions | |
virtual | ~InputStream () noexcept(false)=0 |
Virtual, throwing destructor. | |
Read Functions | |
auto | readElement () -> Element |
Read a single element from the stream. | |
auto | tryReadElement () -> std::optional< Element > |
Tries to read a single element from a stream. | |
template<Element kNoMoreData> | |
auto | readElementOr () -> Element |
Tries to read a single element from a stream. | |
auto | peek () -> Element |
Look at the next element in the stream without actually reading it. | |
auto | tryPeek () -> std::optional< Element > |
Look at the next element in the stream without actually reading it. | |
template<Element kNoMoreData> | |
auto | peekOr () -> Element |
Look at the next element in the stream without actually reading it. | |
auto | readFromBuffer (std::size_t maxSize) -> std::span< const Element > |
Reads data from the buffer only. | |
auto | eof () -> bool |
Checks if the end of the stream has been reached. | |
Conditional Read Functions | |
template<Element... kAllowedElements> | |
auto | readElementIf () -> std::optional< Element > |
Read an element from the stream if it is one of a list of allowed elements. | |
template<std::predicate< Element > Predicate> | |
auto | readElementIf (Predicate predicate) -> std::optional< Element > |
Read an element from the stream if it fits a predicate. | |
template<Element kFallback, Element... kAllowedElements> | |
auto | readElementIfOr () -> Element |
Read an element from the stream if it is one of a list of allowed elements. | |
template<Element kFallback, std::predicate< Element > Predicate> | |
auto | readElementIfOr (Predicate predicate) -> Element |
Read an element from the stream if it fits a predicate. | |
Block Read Functions | |
template<tools::Allocator< Element > Allocator = std::allocator<Element>> requires (!tools::CharType<Element>) | |
auto | read (typename std::allocator_traits< Allocator >::size_type size, const Allocator &allocator=Allocator()) -> core::RawVector< Element, Allocator > |
Read a block of data of a specific size. | |
template<typename CharTraits = std::char_traits<Element>, tools::Allocator< Element > Allocator = std::allocator<Element>> requires tools::CharType<Element> | |
auto | read (typename std::allocator_traits< Allocator >::size_type size, const Allocator &allocator=Allocator()) -> std::basic_string< Element, CharTraits, Allocator > |
Read a block of text of a specific size. | |
template<std::size_t kArraySize> | |
auto | read (Element(&array)[kArraySize]) -> void |
Read a block of data into an array. | |
template<std::size_t kArraySize> | |
auto | read (std::array< Element, kArraySize > &array) -> void |
Read a block of data into an array. | |
template<std::contiguous_iterator Iterator, std::sized_sentinel_for< Iterator > Sentinel> requires std::same_as<std::remove_volatile_t<std::iter_value_t<Iterator>>, Element> | |
auto | read (Iterator first, Sentinel last) -> void |
Read a block of data into an iterator range. | |
auto | read (Element *buffer, std::size_t size) -> void |
Read a block of data into a buffer. | |
Terminated Read Functions | |
template<Element... kTerminations> requires (!tools::CharType<Element>) | |
auto | readUntil () -> core::RawVector< Element > |
Reads all elements until a termination is found. | |
template<tools::Allocator< Element > Allocator, Element... kTerminations> requires (!tools::CharType<Element>) | |
auto | readUntil (const Allocator &allocator=Allocator()) -> core::RawVector< Element, Allocator > |
Reads all elements until a termination is found. | |
template<tools::Allocator< Element > Allocator = std::allocator<Element>, std::predicate< Element > Predicate> requires (!tools::CharType<Element>) | |
auto | readUntil (Predicate predicate, const Allocator &allocator=Allocator()) -> core::RawVector< Element, Allocator > |
Skips all elements until a termination element that fits a predicate. | |
template<Element... kTerminations> requires tools::CharType<Element> | |
auto | readUntil () -> std::basic_string< Element > |
Reads all characters until a termination is found. | |
template<typename CharTraits , Element... kTerminations> requires tools::CharType<Element> | |
auto | readUntil () -> std::basic_string< Element, CharTraits > |
Reads all characters until a termination is found. | |
template<typename CharTraits , tools::Allocator< Element > Allocator, Element... kTerminations> requires tools::CharType<Element> | |
auto | readUntil (const Allocator &allocator=Allocator()) -> std::basic_string< Element, CharTraits, Allocator > |
Reads all characters until a termination is found. | |
template<typename CharTraits = std::char_traits<Element>, tools::Allocator< Element > Allocator = std::allocator<Element>, std::predicate< Element > Predicate> requires tools::CharType<Element> | |
auto | readUntil (Predicate predicate, const Allocator &allocator=Allocator()) -> std::basic_string< Element, CharTraits, Allocator > |
Skips all characters until a termination character that fits a predicate. | |
Functions for Skipping Data | |
auto | skipElement () -> void |
Skips a single element from the stream. | |
auto | trySkipElement () -> bool |
Tries to skip a single element from a stream. | |
auto | skip (std::size_t size) -> void |
Skips a block of data. | |
template<Element... kSkippableElements> | |
auto | skipWhile () -> std::size_t |
Skips all elements than appear in a list of skippable elements. | |
template<std::predicate< Element > Predicate> | |
auto | skipWhile (Predicate predicate) -> std::size_t |
Skips all elements that fit a predicate. | |
Accessing the Read Position | |
auto | readPosition () const -> std::optional< ReadPosition > |
Gets the current read position within the stream. | |
auto | setReadPosition (ReadPosition position) -> void |
Sets the current read position within the device. | |
auto | rewind () -> void |
Sets the current read position to the beginning of the stream. | |
Text stream functions | |
template<typename CharTraits = std::char_traits<Element>, tools::Allocator< Element > Allocator = std::allocator<Element>> requires tools::CharType<Element> | |
auto | readLine (const Allocator &allocator=Allocator()) -> std::basic_string< Element, CharTraits, Allocator > |
Reads a single line. | |
auto | skipLine () -> std::size_t |
Skips the rest of the line. | |
template<typename CharTraits = std::char_traits<Element>, tools::Allocator< Element > Allocator = std::allocator<Element>> requires tools::CharType<Element> | |
auto | readWord (const Allocator &allocator=Allocator()) -> std::basic_string< Element, CharTraits, Allocator > |
Reads a single whitespace-separated word. | |
auto | skipWord () -> std::size_t |
Skips the rest of a whitespace-separated word. | |
auto | skipLineEnding () -> bool |
Skips a single line termination. | |
auto | skipWhitespace () -> bool |
Skips all ASCII whitespace elements. | |
Alternate function names for byte streams | |
auto | readByte () -> Element |
Alternate name for readElement() | |
auto | tryReadByte () -> std::optional< Element > |
Alternate name for tryReadElement() | |
template<Element kNoMoreData> requires (!tools::CharType<Element>) | |
auto | readByteOr () -> Element |
Alternate name for readElementOr() | |
template<Element... kAllowedElements> requires (!tools::CharType<Element>) | |
auto | readByteIf () -> std::optional< Element > |
Alternate name for readElementIf() | |
template<std::predicate< Element > Predicate> requires (!tools::CharType<Element>) | |
auto | readByteIf (Predicate predicate) -> std::optional< Element > |
Alternate name for readElementIf(). | |
template<Element kFallback, Element... kAllowedElements> requires (!tools::CharType<Element>) | |
auto | readByteIfOr () -> Element |
Alternate name for readElementIfOr() | |
template<Element kFallback, std::predicate< Element > Predicate> requires (!tools::CharType<Element>) | |
auto | readByteIfOr (Predicate predicate) -> Element |
Alternate name for readElementIfOr(). | |
auto | skipByte () -> void |
Alternate name for skipElement() | |
auto | trySkipByte () -> bool |
Alternate name for trySkipElement() for character streams. | |
Alternate function names for character streams | |
auto | readChar () -> Element |
Alternate name for readElement() | |
auto | tryReadChar () -> std::optional< Element > |
Alternate name for tryReadElement() | |
template<Element kNoMoreData> requires tools::CharType<Element> | |
auto | readCharOr () -> Element |
Alternate name for readElementOr() | |
template<Element... kAllowedElements> requires tools::CharType<Element> | |
auto | readCharIf () -> std::optional< Element > |
Alternate name for readElementIf() | |
template<std::predicate< Element > Predicate> requires tools::CharType<Element> | |
auto | readCharIf (Predicate predicate) -> std::optional< Element > |
Alternate name for readElementIf(). | |
template<Element kFallback, Element... kAllowedElements> requires tools::CharType<Element> | |
auto | readCharIfOr () -> Element |
Alternate name for readElementIfOr() | |
template<Element kFallback, std::predicate< Element > Predicate> requires tools::CharType<Element> | |
auto | readCharIfOr (Predicate predicate) -> Element |
Alternate name for readElementIfOr(). | |
auto | skipChar () -> void |
Alternate name for skipElement() | |
auto | trySkipChar () -> bool |
Alternate name for trySkipElement() for character streams. | |
Waiting for Read Data | |
auto | waitForData () -> void |
Waits for more read data to become available. | |
auto | waitForData (std::optional< std::chrono::nanoseconds > timeout) -> bool |
Waits for more read data to become available within a certain time. | |
Public Member Functions inherited from xentara::utils::io::StreamBase | |
virtual | ~StreamBase () noexcept(false)=0 |
Virtual, throwing destructor. | |
auto | streamDescription () const -> std::string |
Get a description of the device. | |
Protected Member Functions | |
InputStream () noexcept=default | |
Default constructor. | |
InputStream (Element *bufferBegin, Element *bufferEnd) noexcept | |
Constructor that sets an initially empty buffer. | |
InputStream (Element *bufferBegin, Element *bufferEnd, Element *dataBegin, Element *dataEnd) noexcept | |
Constructor that sets a buffer that possibly already contains some data. | |
InputStream (InputStream &&other) noexcept | |
Move constructor. | |
auto | operator= (InputStream &&rhs) noexcept -> InputStream & |
Move assignemnt operator. | |
auto | setReadBuffer (Element *bufferBegin, Element *bufferEnd) noexcept -> void |
Sets a new empty buffer. | |
auto | setReadBuffer (Element *bufferBegin, Element *bufferEnd, Element *dataBegin, Element *dataEnd) noexcept -> void |
Sets the buffer. | |
auto | setReadData (Element *dataBegin, Element *dataEnd) noexcept -> void |
Sets the position of the data within the buffer. | |
auto | readBufferBegin () const -> Element * |
Reads back the buffer start position. | |
auto | readBufferEnd () const -> Element * |
Reads back the buffer end position. | |
auto | readBufferSize () const -> std::size_t |
Gets the buffer size. | |
auto | readDataBegin () const -> Element * |
Returns the beginning of the unconsumed data in the buffer. | |
auto | readDataEnd () const -> Element * |
Returns the end of the data in the buffer. | |
auto | readDataSize () const -> std::size_t |
Returns the size of the unconsumed data in the buffer. | |
virtual auto | doReplenishReadBuffer (bool waitForData) -> bool |
Called by the framework to place more data into the buffer when it is exhausted. | |
virtual auto | doRead (Element *buffer, std::size_t size, bool waitForData) -> std::optional< std::size_t >=0 |
Called by the framework to read data into a memory buffer. | |
virtual auto | doThrowEndOfStreamError () const -> void |
Called by the framework to throw an error denoting the end of the stream. | |
virtual auto | doWaitForData (std::optional< std::chrono::nanoseconds > timeout) -> bool |
Called by the framework to wait for more data to become available. | |
virtual auto | doGetReadPosition () const -> std::optional< std::size_t > |
Called by the framework to get the current read position within the device. | |
virtual auto | doSetReadPosition (std::size_t position) -> bool |
Called by the framework to set the current read position within the device. | |
virtual auto | doAdvanceReadPosition (std::size_t offset) -> std::optional< std::size_t > |
Advances the current read position within the device. | |
Protected Member Functions inherited from xentara::utils::io::StreamBase | |
virtual auto | doGetStreamDescription () const -> std::string=0 |
Called by the framework to get a description of the stream. | |
Base class for input streams.
Input streams provide an efficient way of accessing the data supplied by a device as a sequence of elements. Input streams buffer the data, and hence generally provide better performance than raw devices.
Element | The type of element the stream contains |
using xentara::utils::io::InputStream< Element >::element_type = Element |
The element type.
|
pure virtual |
Virtual, throwing destructor.
|
protecteddefaultnoexcept |
Default constructor.
This constructor creates an empty stream with no buffer. You can set a buffer later on using the function setReadBuffer()
|
protectednoexcept |
Constructor that sets an initially empty buffer.
bufferBegin | A pointer to the beginning of the read buffer |
bufferEnd | A pointer one past the end of the buffer |
|
protectednoexcept |
Constructor that sets a buffer that possibly already contains some data.
bufferBegin | A pointer to the beginning of the read buffer |
bufferEnd | A pointer one past the end of the buffer |
dataBegin | A pointer to the data already in the buffer |
dataEnd | A pointer one past the end of the data already in the buffer |
|
protectednoexcept |
Move constructor.
This function will transfer the buffer and data positions from the other stream. The buffer and data positions of the other stream will be left as nullptr.
other | The stream to move from |
|
protectedvirtual |
Advances the current read position within the device.
The framework will discard all data in the buffer before calling this function, so that the buffer read position will be at the end of the data in the buffer.
If necessary or convenient, a reimplementation of this function may place new data into the buffer using setReadData(), or relocate or resize the buffer using setReadBuffer(). The derived class is then responsible for allocating the new buffer and deallocating the old buffer, if necessary. This class only tracks the location of the buffer, it does not take ownership or responsibility for it.
offset | The desired offset for the read position from the current position. This offset may put the position past the end of the device, in which case the position must be set to the exact end of the device. |
std::runtime_error | The functionality is supported in principal, but an error occurred |
Reimplemented in xentara::utils::io::AbstractDeviceInputStream< Element >.
|
protectedvirtual |
Called by the framework to get the current read position within the device.
std::runtime_error | The functionality is supported in principal, but an error occurred |
Reimplemented in xentara::utils::io::AbstractDeviceInputStream< Element >.
|
protectedpure virtual |
Called by the framework to read data into a memory buffer.
This function must read data into the given buffer, up to the maximum size specified.
This function is used in two ways:
In either case, this function is only ever called when the normal read buffer is empty.
If no data could be read because the end of the stream has been reached (e.g. at end of a file, or if acommunication stream was closed gracefully), this function must return 0, or throw an error of type EndOfStreamError. On communication devices, any EndOfStreamError thrown should also derive from ConnectionClosed, e.g. by using throwWithConnectionClosed().
If the read on a communications stream would block, the behaviour of this function depends on the value of the parameter waitForData. If waitForData is false, the function must return std::nullopt immediately. If waitForData is true, the function must first wait for a short time for any new data to arrive, and only return std::nullopt if the wait times out. Alternatively, an exception of type ReadTimeoutError can be thrown on if the wait times out. If the wait succeeds, the function must then attempt to read the newly arrived data, and return the data length or throw an exception accordingly. The exact length of the read timeout is left up to the individual implementation.
Non-communication streams should ignore the waitForData flag, and never return std::nullopt or throw an exception of type ReadTimeoutError.
buffer | A pointer to the buffer that will receive the data. This is not necessarily the normal read buffer (see above). |
size | The amout of data to read. The framework will never pass 0 for this parameter. Please note that this value may be greater than the maximum read size of the operating system (SSIZE_MAX on Posix, maximum DWORD value on Windows). Care must be taken to account for this in the implementation. |
waitForData | A flag is used to tell a communication stream whether to wait for data or not. Non-communication streams should ignore this flag. |
The number of bytes actually read, or 0 if the end of the stream has been reached. Communication streams must return std::nullopt if no data was read, but more data might be available after calling doWaitForData(). Non-communication streams must never return std::nullopt.
Instead of returning 0 or std::nullopt to signal the end of stream or a blocking read, the function can throw an exception of type EndOfStreamError or ReadTimeoutError as described below.
EndOfStreamError | An exception of this type (or of a suitable subclass) can be thrown instead of returning 0 if the end of the stream has been reached. On communication devices, any EndOfStreamError thrown should also derive from ConnectionClosed, e.g. by using throwWithConnectionClosed(). |
ReadTimeoutError | An exception of this type (or of a suitable subclass) can be thrown by communications streams instead of returning std::nullopt if no data was read, but more data might be available later. |
StopRequested | Implementations that use a stop source should throw this exception if waitForData is true, and the stop source was triggered when waiting for data. |
std::runtime_error | This exception or an appropriate subclass must be thrown if an error occurs. |
Implemented in xentara::utils::io::AbstractDeviceInputStream< Element >.
|
protectedvirtual |
Called by the framework to place more data into the buffer when it is exhausted.
This function must replenish the buffer with new data from the stream, and call setReadData() to specify the location of the new data within the buffer. If the device supports read positions, this function should read data from the device’s current read position, and advance the read position accordingly, as if conducting a regular read.
If no data could be read because the end of the stream has been reached (e.g. at end of a file, or if acommunication stream was closed gracefully), this function must clear the buffer by setting the beginning and end to the same location. Alternatively, an error of type EndOfStreamError can be thrown. On communication devices, any EndOfStreamError thrown should also derive from ConnectionClosed, e.g. by using throwWithConnectionClosed().
If the read on a communications stream would block, the behaviour of this function depends on the value of the parameter waitForData. If waitForData is false, the function must return false immediately. If waitForData is true, the function must first wait for a short time for any new data to arrive, and only return false if the wait times out. Alternatively, an exception of type ReadTimeoutError can be thrown on if the wait times out. If the wait succeeds, the function must then attempt to read the newly arrived data, and agjust the buffer or throw an exception accordingly. The exact length of the read timeout is left up to the individual implementation.
The function must never return false if it placed any data into the read buffer. If you return false, the framework will assume no data could be read, and empty the buffer itself. This will discard any data you placed into the buffer.
A reimplementation of this function can relocate or resize the buffer, if it whishes, by calling setReadBuffer() instead of setReadData(). The derived class is then responsible for allocating the new buffer and deallocating the old buffer, if necessary. This class only tracks the location of the buffer, it does not take ownership or responsibility for it.
waitForData | A flag is used to tell a communication stream whether to wait for data or not. Non-communication streams should ignore this flag. |
This function must true if any data has been placed into the buffer, or if the end of stream has been reached.
Communication streams must return false if no data was placed into the buffer, but more data might be available after calling doWaitForData(). Non-communication streams must never return false.
Instead of returning false to signal a blocking read, a communication stream can throw an exception of type ReadTimeoutError as described below.
EndOfStreamError | An exception of this type (or of a suitable subclass) can be thrown instead of emptying the buffer if the end of the stream has been reached. On communication devices, any EndOfStreamError thrown should also derive from ConnectionClosed, e.g. by using throwWithConnectionClosed(). |
ReadTimeoutError | An exception of this type (or of a suitable subclass) can be thrown by communications streams instead of returning false if no data was placed into the buffer, but more data might be available later. |
StopRequested | Implementations that use a stop source should throw this exception if waitForData is true, and the stop source was triggered when waiting for data. |
std::runtime_error | This exception or an appropriate subclass must be thrown if an error occurs. |
|
protectedvirtual |
Called by the framework to set the current read position within the device.
The framework will clear the buffer before calling this function.
If necessary or convenient, a reimplementation of this function may place new data into the buffer using setReadData(), or relocate or resize the buffer using setReadBuffer(). The derived class is then responsible for allocating the new buffer and deallocating the old buffer, if necessary. This class only tracks the location of the buffer, it does not take ownership or responsibility for it.
If this function places new data into the buffer, the buffer read position must point to the data at position. The underlying devices's read position must, as always, point to the end of the data placed into the buffer, so that the next call to doReplenishReadBuffer() or doRead() will read the correct data.
The framework will not ask the device to set arbitrary read positions. The requested position will always be either 0, or a position you previously returned from doGetReadPosition().
position | The desired new read position. This will alway be either 0, or a position previously returned by doGetReadPosition(). |
std::runtime_error | The functionality is supported in principal, but an error occurred |
Reimplemented in xentara::utils::io::AbstractDeviceInputStream< Element >.
|
protectedvirtual |
Called by the framework to throw an error denoting the end of the stream.
The framework calls this function if doReplenishReadBuffer() return true and cleared the buffer, if or doRead() returned 0, but more data was actually needed. Derived classes should throw an error of type EndOfStreamError with a descriptive text, like e.g. “end of file”, or “connection closed”.
Communication streams should throw an exception that also derives from ConnectionClosed, e.g. by using throwWithConnectionClosed().
EndOfStreamError | This exception, or a suitable subclass, must always be thrown. Communication devices should throw an exception that derives from both EndOfStreamError and ConnectionClosed, e.g. by using throwWithConnectionClosed(). |
Reimplemented in xentara::utils::io::AbstractDeviceInputStream< Element >.
|
protectedvirtual |
Called by the framework to wait for more data to become available.
This function must wait for new data to become available for read.
This function is usually only overridden for communication streams, which will wait for the device to go into a readable state or into an error state.
timeout | The number of nanoseconds to wait, or std::nullopt to wait indefinitely. If a timeout of 0 is passed, the function should not wait, but simply return whether the device can be read without blocking or not. The framework will never pass a negative value. |
StopRequested | Implementations that use a stop source should throw this exception if the stop source was triggered. |
std::runtime_error | This exception, or an appropriate subclass, must be thrown if an error occurs waiting for the data. Throwing an exception on error is optional if a subsequent call to doRead() or doReplenishReadBuffer() will itself throw a suitable exception. Throwing an error is only required if doWaitForData() failed, but a subsequent call to doRead() or doReplenishReadBuffer() might still succeed. If this function uses the Linux poll(2) command, for example, and poll(2) sets the POLLERR flag in revents, you can just return true instead of throwing an exception, because a subsequent call to recv(2) is guaranteed to fail. If poll(2) returns -1, however, you must throw an exception, because a subsequent call to recv(2) might still be successful. |
Reimplemented in xentara::utils::io::AbstractDeviceInputStream< Element >.
auto xentara::utils::io::InputStream< Element >::eof | ( | ) | -> bool |
Checks if the end of the stream has been reached.
On communication streams, this function checks whether the connection was gracefully closed.
std::runtime_error | An error occurred accessing the underlying device |
|
protectednoexcept |
Move assignemnt operator.
This operator swaps the buffer and data positions of this object and the other object
rhs | The stream to move from |
auto xentara::utils::io::InputStream< Element >::peek | ( | ) | -> Element |
Look at the next element in the stream without actually reading it.
EndOfStreamError | This exception is thrown if we are at the end of the data. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::peekOr | ( | ) | -> Element |
Look at the next element in the stream without actually reading it.
kNoMoreData | The value to return for end of the data |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::read | ( | Element * | buffer, |
std::size_t | size | ||
) | -> void |
Read a block of data into a buffer.
buffer | A pointer to the buffer that will receive the data |
size | The size of the buffer |
EndOfStreamError | This exception is thrown if the end of the data is reached before the data was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::read | ( | Element(&) | array[kArraySize] | ) | -> void |
Read a block of data into an array.
array | The array that will receive the data |
kArraySize | The size of the array |
EndOfStreamError | This exception is thrown if the end of the data is reached before the data was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::read | ( | Iterator | first, |
Sentinel | last | ||
) | -> void |
Read a block of data into an iterator range.
first | The beginning of the range |
last | The postion after the end of the range |
EndOfStreamError | This exception is thrown if the end of the data is reached before the data was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::read | ( | std::array< Element, kArraySize > & | array | ) | -> void |
Read a block of data into an array.
array | The array that will receive the data |
kArraySize | The size of the buffer |
EndOfStreamError | This exception is thrown if the end of the data is reached before the data was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::read | ( | typename std::allocator_traits< Allocator >::size_type | size, |
const Allocator & | allocator = Allocator() |
||
) | -> core::RawVector<Element, Allocator> |
Read a block of data of a specific size.
size | The size of the data block to read |
allocator | The allocator to use for the returned vector |
EndOfStreamError | This exception is thrown if the end of the stream is reached before the requested number of elements were read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::read | ( | typename std::allocator_traits< Allocator >::size_type | size, |
const Allocator & | allocator = Allocator() |
||
) | -> std::basic_string<Element, CharTraits, Allocator> |
Read a block of text of a specific size.
size | The size of the text block to read |
allocator | The allocator to use for the returned vector |
EndOfStreamError | This exception is thrown if the end of the data is reached before the data was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
|
protected |
Reads back the buffer start position.
This function merely returns the pointer set using one of the constructors or one of the setReadBuffer() functions
|
protected |
Reads back the buffer end position.
This function merely returns the pointer set using one of the constructors or one of the setReadBuffer() functions
|
protected |
Gets the buffer size.
This function merely returns the difference between the pointers set using one of the constructors or one of the setReadBuffer() functions.
auto xentara::utils::io::InputStream< Element >::readByte | ( | ) | -> Element |
Alternate name for readElement()
auto xentara::utils::io::InputStream< Element >::readByteIf | ( | ) | -> std::optional<Element> |
Alternate name for readElementIf()
auto xentara::utils::io::InputStream< Element >::readByteIf | ( | Predicate | predicate | ) | -> std::optional<Element> |
Alternate name for readElementIf().
auto xentara::utils::io::InputStream< Element >::readByteIfOr | ( | ) | -> Element |
Alternate name for readElementIfOr()
auto xentara::utils::io::InputStream< Element >::readByteIfOr | ( | Predicate | predicate | ) | -> Element |
Alternate name for readElementIfOr().
auto xentara::utils::io::InputStream< Element >::readByteOr | ( | ) | -> Element |
Alternate name for readElementOr()
auto xentara::utils::io::InputStream< Element >::readChar | ( | ) | -> Element |
Alternate name for readElement()
auto xentara::utils::io::InputStream< Element >::readCharIf | ( | ) | -> std::optional<Element> |
Alternate name for readElementIf()
auto xentara::utils::io::InputStream< Element >::readCharIf | ( | Predicate | predicate | ) | -> std::optional<Element> |
Alternate name for readElementIf().
auto xentara::utils::io::InputStream< Element >::readCharIfOr | ( | ) | -> Element |
Alternate name for readElementIfOr()
auto xentara::utils::io::InputStream< Element >::readCharIfOr | ( | Predicate | predicate | ) | -> Element |
Alternate name for readElementIfOr().
auto xentara::utils::io::InputStream< Element >::readCharOr | ( | ) | -> Element |
Alternate name for readElementOr()
|
protected |
Returns the beginning of the unconsumed data in the buffer.
|
protected |
Returns the end of the data in the buffer.
|
protected |
Returns the size of the unconsumed data in the buffer.
auto xentara::utils::io::InputStream< Element >::readElement | ( | ) | -> Element |
Read a single element from the stream.
EndOfStreamError | This exception is thrown if the end of the stream is reached before the element was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred reading data from the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::readElementIf | ( | ) | -> std::optional< Element > |
Read an element from the stream if it is one of a list of allowed elements.
kAllowedElements | The allowed elements |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::readElementIf | ( | Predicate | predicate | ) | -> std::optional< Element > |
Read an element from the stream if it fits a predicate.
predicate | The predicate to check |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::readElementIfOr | ( | ) | -> Element |
Read an element from the stream if it is one of a list of allowed elements.
kFallback | The value to return for elements that do not fit the predicate and for end of the data |
kAllowedElements | The allowed elements |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::readElementIfOr | ( | Predicate | predicate | ) | -> Element |
Read an element from the stream if it fits a predicate.
kFallback | The value to return for elements that do not fit the predicate and for end of the data |
predicate | The predicate to check |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::readElementOr | ( | ) | -> Element |
Tries to read a single element from a stream.
kNoMoreData | The value to return for end of the data |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::readFromBuffer | ( | std::size_t | maxSize | ) | -> std::span< const Element > |
Reads data from the buffer only.
This function will read all the remaining data in the buffer, up to a maximum size. If the buffer is exhausted, then an empty span will be returned. The function will never read from the underlying device.
maxSize | The maximum size of the data to read |
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::readLine | ( | const Allocator & | allocator = Allocator() | ) | -> std::basic_string< Element, CharTraits, Allocator > |
Reads a single line.
This function reads all characters until one of the unicode characters LINE FEED or CARRIAGE RETURN is encountered, or until the stream is exhausted. The line termination itself is not read or returned. You can use skipLineEnding() to check for and skip over the line termination.
allocator | The allocator to use for the returned string |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readPosition | ( | ) | const -> std::optional< ReadPosition > |
Gets the current read position within the stream.
std::runtime_error | The device supports getting and setting the read position, but an error occurred |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | ) | -> core::RawVector<Element> |
Reads all elements until a termination is found.
This function reads all elements until one of the terminations included in kTerminations is encountered, or until the stream is exhausted. The terminating element itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating element.
kTerminations | All of the elements that should terminate the read |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | ) | -> std::basic_string<Element> |
Reads all characters until a termination is found.
This function reads all characters until one of the terminations included in kTerminations is encountered, or until the stream is exhausted. The terminating character itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating character.
kTerminations | All of the characters that should terminate the read |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | ) | -> std::basic_string<Element, CharTraits> |
Reads all characters until a termination is found.
This function reads all characters until one of the terminations included in kTerminations is encountered, or until the stream is exhausted. The terminating character itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating character.
CharTraits | The character traits to be used for the returned string |
kTerminations | All of the characters that should terminate the read |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | const Allocator & | allocator = Allocator() | ) | -> core::RawVector<Element, Allocator> |
Reads all elements until a termination is found.
This function reads all elements until one of the terminations included in kTerminations is encountered, or until the stream is exhausted. The terminating element itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating element.
Allocator | The allocator type to be used for the returned vector |
kTerminations | All of the elements that should terminate the read |
allocator | The allocator to use for the returned vector |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | const Allocator & | allocator = Allocator() | ) | -> std::basic_string<Element, CharTraits, Allocator> |
Reads all characters until a termination is found.
This function reads all characters until one of the terminations included in kTerminations is encountered, or until the stream is exhausted. The terminating character itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating character.
CharTraits | The character traits to be used for the returned string |
Allocator | The allocator type to be used for the returned string |
kTerminations | All of the characters that should terminate the read |
allocator | The allocator to use for the returned vector |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | Predicate | predicate, |
const Allocator & | allocator = Allocator() |
||
) | -> core::RawVector<Element, Allocator> |
Skips all elements until a termination element that fits a predicate.
This function reads all elements until a element is encountered for which predicate returns true, or until the stream is exhausted. The terminating element itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating element.
predicate | The predicate |
allocator | The allocator to use for the returned vector |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readUntil | ( | Predicate | predicate, |
const Allocator & | allocator = Allocator() |
||
) | -> std::basic_string<Element, CharTraits, Allocator> |
Skips all characters until a termination character that fits a predicate.
This function reads all characters until a character is encountered for which predicate returns true, or until the stream is exhausted. The terminating character itself is not read or returned. You can use readElement() or tryReadElement() to read the terminating character.
predicate | The predicate |
allocator | The allocator to use for the returned string |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::readWord | ( | const Allocator & | allocator = Allocator() | ) | -> std::basic_string< Element, CharTraits, Allocator > |
Reads a single whitespace-separated word.
This function reads all characters until one of the unicode characters SPACE, CHARACTER TABULATION, LINE FEED, or CARRIAGE RETURN is encountered, or until the stream is exhausted. The terminating character itself is not read or returned. You can use readChar() or tryReadChar() to read the terminating character.
allocator | The allocator to use for the returned string |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::rewind | ( | ) | -> void |
Sets the current read position to the beginning of the stream.
std::runtime_error | An error occurred setting the read position, or the device does not support it |
|
protectednoexcept |
Sets a new empty buffer.
This function discards any data in the old buffer.
bufferBegin | A pointer to the beginning of the read buffer |
bufferEnd | A pointer past the end of the data already in the buffer |
|
protectednoexcept |
Sets the buffer.
This function discards any data in the old buffer.
bufferBegin | A pointer to the beginning of the read buffer |
bufferEnd | A pointer one past the end of the buffer |
dataBegin | A pointer to the data already in the buffer |
dataEnd | A pointer one past the end of the data already in the buffer |
|
protectednoexcept |
Sets the position of the data within the buffer.
This function discards the old data.
dataBegin | A pointer to the data in the buffer |
dataEnd | A pointer one past the end of the data in the buffer |
auto xentara::utils::io::InputStream< Element >::setReadPosition | ( | ReadPosition | position | ) | -> void |
Sets the current read position within the device.
position | The desired position. This must be either ReadPosition(), or a value previously returned by readPosition() |
std::runtime_error | An error occurred setting the read position, or the device does not support it |
auto xentara::utils::io::InputStream< Element >::skip | ( | std::size_t | size | ) | -> void |
Skips a block of data.
size | The number of elements to skip |
EndOfStreamError | This exception is thrown if the end of the data is reached before the correct amount of data was skipped. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred accessing the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::skipByte | ( | ) | -> void |
Alternate name for skipElement()
auto xentara::utils::io::InputStream< Element >::skipChar | ( | ) | -> void |
Alternate name for skipElement()
auto xentara::utils::io::InputStream< Element >::skipElement | ( | ) | -> void |
Skips a single element from the stream.
EndOfStreamError | This exception is thrown if the end of the data is reached before the element was read. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
ReadTimeoutError | This exception is thrown on communication streams if data takes too long to arrive. This can mean that there is a network congestion, that the sender is too slow, or that the sender did not send enough data. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered while waiting for data. |
std::runtime_error | An error occurred accessing the underlying device |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
auto xentara::utils::io::InputStream< Element >::skipLine | ( | ) | -> std::size_t |
Skips the rest of the line.
This function discards all characters until one of the unicode characters LINE FEED or CARRIAGE RETURN is encountered, or until the stream is exhausted. The terminating character itself is not discarded.
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::skipLineEnding | ( | ) | -> bool |
Skips a single line termination.
This function skips the unicode character LINE FEED, or the unicode character pair CARRIAGE RETURN/LINE FEED. A single CARRIAGE RETURN without a LINE FEED is also skipped.
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::skipWhile | ( | ) | -> std::size_t |
Skips all elements than appear in a list of skippable elements.
kSkippableElements | The elements to skip |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::skipWhile | ( | Predicate | predicate | ) | -> std::size_t |
Skips all elements that fit a predicate.
predicate | The predicate |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::skipWhitespace | ( | ) | -> bool |
Skips all ASCII whitespace elements.
This funktion skips the unicode elements SPACE, CHARACTER TABULATION, LINE FEED, and CARRIAGE RETURN. Other unicode elements from the categories “Separator, Space[Zs]”, are not skipped.
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::skipWord | ( | ) | -> std::size_t |
Skips the rest of a whitespace-separated word.
This function discards all characters until one of the unicode characters SPACE, CHARACTER TABULATION, LINE FEED, or CARRIAGE RETURN is encountered, or until the stream is exhausted. . The terminating character itself is not discarded.
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::tryPeek | ( | ) | -> std::optional< Element > |
Look at the next element in the stream without actually reading it.
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::tryReadByte | ( | ) | -> std::optional<Element> |
Alternate name for tryReadElement()
auto xentara::utils::io::InputStream< Element >::tryReadChar | ( | ) | -> std::optional<Element> |
Alternate name for tryReadElement()
auto xentara::utils::io::InputStream< Element >::tryReadElement | ( | ) | -> std::optional< Element > |
Tries to read a single element from a stream.
std::runtime_error | An error occurred reading data from the underlying device |
auto xentara::utils::io::InputStream< Element >::trySkipByte | ( | ) | -> bool |
Alternate name for trySkipElement() for character streams.
auto xentara::utils::io::InputStream< Element >::trySkipChar | ( | ) | -> bool |
Alternate name for trySkipElement() for character streams.
auto xentara::utils::io::InputStream< Element >::trySkipElement | ( | ) | -> bool |
Tries to skip a single element from a stream.
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::waitForData | ( | ) | -> void |
Waits for more read data to become available.
On communication streams, this function waits until the stream is in a state where the next read operation will not block.
On non-communication streams, this function always returns immediately.
EndOfStreamError | This exception is thrown if the end of the stream has been reached. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered during the wait. |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
std::runtime_error | An error occurred accessing the underlying device |
auto xentara::utils::io::InputStream< Element >::waitForData | ( | std::optional< std::chrono::nanoseconds > | timeout | ) | -> bool |
Waits for more read data to become available within a certain time.
On communication streams, this function waits until the stream is in a state where the next read operation will not block, and return true if that state was reached before an optional timeout elapsed.
On non-communication streams, this function always returns true immediately.
timeout | The number of nanoseconds to wait, or std::nullopt to wait indefinitely. If you pass 0 or a negative value to this function, it will not wait, but simply return whether the stream can be read without blocking or not. |
EndOfStreamError | This exception is thrown if the end of the stream has been reached. On communication streams, this exception is also derived from ConnectionClosed and denotes that the connection was gracefully closed. |
StopRequested | This exception is thrown on communication streams that use a stop source if the stop source was triggered during the wait. |
ConnectionClosed | On communication streams, any EndOfStreamError thrown is also derived from ConnectionClosed. This allows you to handle closed connections during read and write operations in the same catch block by catching exceptions of type ConnectionClosed. |
std::runtime_error | An error occurred accessing the underlying device |