xentara-utils v2.1.1
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::cbor::Decoder Class Reference

A class that decodes CBOR data types that includes: unsigned integers, negative integers, byte strings, text strings encoded as UTF-8, simple values and float-point number. More...

#include <xentara/utils/cbor/Decoder.hpp>

Public Member Functions

 Decoder (std::reference_wrapper< io::InputStream< std::byte > > stream)
 Constructor.
 
template<typename UserData >
 Decoder (std::reference_wrapper< io::InputStream< std::byte > > stream, UserData &&userData)
 Constructor that attaches user data to the stream.
 
template<typename UserData >
auto setUserData (UserData &&userData) -> void
 Sets the user data.
 
template<typename UserData >
auto userData () const -> const UserData *
 Gets the user data.
 
template<tools::UnsignedIntegral Type>
auto operator>> (Type &value) -> Decoder &
 Decodes an unsigned integeral value.
 
template<tools::SignedIntegral Type>
auto operator>> (Type &value) -> Decoder &
 Decodes signed/unsigned integeral value.
 
template<typename Allocator >
auto operator>> (core::RawVector< std::byte, Allocator > &data) -> Decoder &
 Decodes byte string of opaque bytes.
 
template<typename CharTraits , typename Allocator >
auto operator>> (std::basic_string< char, CharTraits, Allocator > &string) -> Decoder &
 Decodes text string of Unicode characters that is encoded as UTF-8 - Major Type 3.
 
auto operator>> (float &value) -> Decoder &
 Decodes float value.
 
auto operator>> (double &value) -> Decoder &
 Decodes double value.
 
auto operator>> (bool &value) -> Decoder &
 Decodes bool value.
 
auto operator>> (NullTag tag) -> Decoder &
 Decodes a CBOR null value (simple value 22)
 
auto operator>> (UndefinedTag tag) -> Decoder &
 Decodes a CBOR undefined value (simple value 23)
 
auto operator>> (Tag &value) -> Decoder &
 Decodes a CBOR data item tag.
 
auto operator>> (SimpleValue &value) -> Decoder &
 Decodes an arbitrary CBOR simple value.
 
auto operator>> (Array &array) -> Decoder &
 Decodes the header of a CBOR array.
 
auto operator>> (Map &map) -> Decoder &
 Decodes the header of a CBOR map.
 
template<typename Value >
requires (!std::same_as<Value, NullTag> && !std::same_as<Value, BreakTag> && !std::same_as<Value, DataType> && !std::same_as<Value, ExtendedDataType>)
auto operator>> (std::optional< Value > &optional) -> Decoder &
 Decodes an optional value.
 
auto operator>> (BreakTag tag) -> Decoder &
 Decodes a CBOR break value (simple value 31)
 
auto operator>> (DataType &value) -> Decoder &
 Checks what the data type of the next value will be.
 
auto operator>> (ExtendedDataType &value) -> Decoder &
 Checks what the data type and bit size of the next value will be.
 
auto operator>> (AlternativeValue &value) -> Decoder &
 Decodes a CBOR alternative value.
 
auto skip () -> void
 Skips the next value.
 

Detailed Description

A class that decodes CBOR data types that includes: unsigned integers, negative integers, byte strings, text strings encoded as UTF-8, simple values and float-point number.

Constructor & Destructor Documentation

◆ Decoder() [1/2]

xentara::utils::cbor::Decoder::Decoder ( std::reference_wrapper< io::InputStream< std::byte > >  stream)

Constructor.

Parameters
streamThe input stream to read the data from

◆ Decoder() [2/2]

template<typename UserData >
xentara::utils::cbor::Decoder::Decoder ( std::reference_wrapper< io::InputStream< std::byte > >  stream,
UserData &&  userData 
)

Constructor that attaches user data to the stream.

Parameters
streamThe input stream to read the data from
userDataArbitrary user data to attach to the decoder. This can be used to store an API version in the decoder, for example.

Member Function Documentation

◆ operator>>() [1/18]

auto xentara::utils::cbor::Decoder::operator>> ( AlternativeValue &  value) -> Decoder &

Decodes a CBOR alternative value.

Parameters
valueA value to decode
Returns
A reference to this object
See also
section 9.1).

◆ operator>>() [2/18]

auto xentara::utils::cbor::Decoder::operator>> ( Array &  array) -> Decoder &

Decodes the header of a CBOR array.

This function only decodes the header of the array, consisting of the major type and the length. The array elements and (for indefinite length arrays) the stop code must be decoded separately.

Parameters
arrayThe array header to decode
Returns
A reference to this object

◆ operator>>() [3/18]

auto xentara::utils::cbor::Decoder::operator>> ( bool &  value) -> Decoder &

Decodes bool value.

Parameters
valueA value to decode
Returns
A reference to this object

◆ operator>>() [4/18]

auto xentara::utils::cbor::Decoder::operator>> ( BreakTag  tag) -> Decoder &

Decodes a CBOR break value (simple value 31)

Parameters
tagAlways pass kBreak as this parameter
Returns
A reference to this object
See also
section 3.2.1) RFC 8949.

◆ operator>>() [5/18]

template<typename Allocator >
auto xentara::utils::cbor::Decoder::operator>> ( core::RawVector< std::byte, Allocator > &  data) -> Decoder &

Decodes byte string of opaque bytes.

Parameters
dataAddress of the data
Returns
A reference to this object

◆ operator>>() [6/18]

auto xentara::utils::cbor::Decoder::operator>> ( DataType &  value) -> Decoder &

Checks what the data type of the next value will be.

This function does not consume any data in the stream, it only looks ahead to what the next value will be without actually reading it. The actual value concerned can be extracted as usual after determining the data type.

Returns
The data type of the next value in the stream

◆ operator>>() [7/18]

auto xentara::utils::cbor::Decoder::operator>> ( double &  value) -> Decoder &

Decodes double value.

Parameters
valueA value to decode
Returns
A reference to this object

◆ operator>>() [8/18]

auto xentara::utils::cbor::Decoder::operator>> ( ExtendedDataType &  value) -> Decoder &

Checks what the data type and bit size of the next value will be.

This function does not consume any data in the stream, it only looks ahead to what the next value will be without actually reading it. The actual value concerned can be extracted as usual after determining the data type.

Returns
The data type of the next value in the stream

◆ operator>>() [9/18]

auto xentara::utils::cbor::Decoder::operator>> ( float &  value) -> Decoder &

Decodes float value.

Parameters
valueA value to decode
Returns
A reference to this object

◆ operator>>() [10/18]

auto xentara::utils::cbor::Decoder::operator>> ( Map &  map) -> Decoder &

Decodes the header of a CBOR map.

This function only decodes the header of the map, consisting of the major type and the length. The keys and values, and (for indefinite length maps) the stop code, must be decoded separately.

Parameters
mapThe map header to decode
Returns
A reference to this object

◆ operator>>() [11/18]

auto xentara::utils::cbor::Decoder::operator>> ( NullTag  tag) -> Decoder &

Decodes a CBOR null value (simple value 22)

Parameters
tagAlways pass kNull as this parameter
Returns
A reference to this object

◆ operator>>() [12/18]

auto xentara::utils::cbor::Decoder::operator>> ( SimpleValue &  value) -> Decoder &

Decodes an arbitrary CBOR simple value.

Parameters
valueA value to decode
Returns
A reference to this object

◆ operator>>() [13/18]

template<typename CharTraits , typename Allocator >
auto xentara::utils::cbor::Decoder::operator>> ( std::basic_string< char, CharTraits, Allocator > &  string) -> Decoder &

Decodes text string of Unicode characters that is encoded as UTF-8 - Major Type 3.

Parameters
stringAddress of the data
Returns
A reference to this object

◆ operator>>() [14/18]

template<typename Value >
requires (!std::same_as<Value, NullTag> && !std::same_as<Value, BreakTag> && !std::same_as<Value, DataType> && !std::same_as<Value, ExtendedDataType>)
auto xentara::utils::cbor::Decoder::operator>> ( std::optional< Value > &  optional) -> Decoder &

Decodes an optional value.

This function first checks the data type of the next value. If the next value is a CBOR null value (simple value 22), it is decoded and optional is set to std::nullopt. If the next value is any other type, then the value is read and moved into optional using std::move().

Parameters
optionalA optional value to decode
Returns
A reference to this object

◆ operator>>() [15/18]

auto xentara::utils::cbor::Decoder::operator>> ( Tag &  value) -> Decoder &

Decodes a CBOR data item tag.

Parameters
valueA value to decode
Returns
A reference to this object
See also
section 3.4 of RFC 8949.

◆ operator>>() [16/18]

template<tools::UnsignedIntegral Type>
auto xentara::utils::cbor::Decoder::operator>> ( Type &  value) -> Decoder &

Decodes an unsigned integeral value.

Parameters
valueA value to decode
Returns
A reference to this object

◆ operator>>() [17/18]

template<tools::SignedIntegral Type>
auto xentara::utils::cbor::Decoder::operator>> ( Type &  value) -> Decoder &

Decodes signed/unsigned integeral value.

Parameters
valueA value to decode
Returns
A reference to this object

◆ operator>>() [18/18]

auto xentara::utils::cbor::Decoder::operator>> ( UndefinedTag  tag) -> Decoder &

Decodes a CBOR undefined value (simple value 23)

Parameters
tagAlways pass kUndefined as this parameter
Returns
A reference to this object
See also
section 5.7 of RFC 8949.

◆ setUserData()

template<typename UserData >
auto xentara::utils::cbor::Decoder::setUserData ( UserData &&  userData) -> void

Sets the user data.

Any previous user data is overwritten.

Parameters
userDataArbitrary user data to attach to the decoder. This can be used to store an API version in the decoder, for example.

◆ skip()

auto xentara::utils::cbor::Decoder::skip ( ) -> void

Skips the next value.

This function skips the next value. For the types like Tags, Arrays, and Maps, it skips over all their nested data as well.

◆ userData()

template<typename UserData >
auto xentara::utils::cbor::Decoder::userData ( ) const -> const UserData *

Gets the user data.

Template Parameters
UserDataThe type of the user data to get
Returns
The user data, or nullptr if no user data is set, or if the user data is of the wrong type