View interface

bview

class bview

The class bview provides read-only access to a bencoded value. The implementation holds only two members: a pointer pointing into contiguous sequence of descriptors and pointer to the buffer containing the bencoded data. Descriptors provide the structural information necessary to navigate the bencoded data. The bview class and it’s subclasses provide a convenient interface to interact with the bencoded data.

The bview class describes bencoded data of unknown type. The type can be queried by type(). bview subclasses describe bencoded data with a known type and can thus provide a richer interface.

Subclassed by bencode::dict_bview, bencode::integer_bview, bencode::list_bview, bencode::string_bview

Public Functions

inline constexpr bview() noexcept

Default constructor. Constructs an empty bview. After construction, data() is equal to nullptr, and size() is equal to 0.

inline constexpr bview(const descriptor *data, const char *buffer) noexcept

Constructs a bview from a descriptor and a character buffer. Behavior is undefined when data or buffer is nullptr

constexpr bview(const bview&) noexcept = default

Copy constructor.

constexpr bview &operator=(const bview&) noexcept = default

Copy assignment.

inline constexpr bencode::bencode_type type() const noexcept

Returns the type of the descriptor this bview points to.

Returns

The bencode data type described by this bview.

inline constexpr std::string_view bencoded_view() const noexcept

Return the bencoded representation of the bvalue.

Returns

Bencoded representation of the described value.

template<typename T>
inline constexpr auto operator==(const T &that) const noexcept -> bool

Compares the current alternatives content with that.

Parameters

that – the bvalue to compare to.

Returns

true of the lhs and rhs compare equal, false otherwise.

template<typename T> inline constexpr std::weak_ordering operator (const T &that) const noexcept

Compares the current alternatives content with that.

Parameters

that – the bview whose content to compare

Returns

std::partial_ordering::unordered if the current alternatives are of different types, otherwise return the result of the comparison as a std::weak_ordering.

constexpr bview operator[](std::size_t pos) const

Returns a view to the element at specified location pos. No bounds checking is performed. Out of bounds access results in undefined behavior. If the active alternative is not a list an exception of type bad_bview_access is thrown.

Parameters

pos – position of the element to return

Returns

view to the requested element.

constexpr bview at(std::size_t pos) const

Returns a view to the element at specified location pos, with bounds checking. If pos is not within the range of the list, an exception of type out_of_range is thrown. If the current active alternative is not a list, and exception of type bad_bview_access is thrown.

Parameters

pos – position of the element to return

Throws
  • out_of_range – if !(pos < size())

  • bad_bview_access – if the current active alternative is not a list.

Returns

View of the requested element.

constexpr bview at(std::string_view key) const

Returns a reference to the mapped bvalue of the element with key equivalent to key. If the current active alternative is not a dict, and exception of type bad_bview_access is thrown. If no such element exists, an exception of type std::out_of_range is thrown.

Parameters

key – the key of the element to find

Throws
  • out_of_range – if the container does not have an element with the specified key,

  • bad_bview_access – if the current active alternative is not dict.

Returns

View to the mapped value of the requested element.

inline bview at(const bpointer &pointer) const

Return a view to the value referenced by a bpointer. If the bpointer does not resolve an exceptionn of type out_of_range is thrown.

Parameters

pointer – the bpointer to the element to return

Throws

out_of_range – if the pointer does not resolve for this value

Returns

View to the element pointed to by pointer.

constexpr bview front() const

Returns a view to the first element in the list. Calling front on an empty container is undefined behavior.

Throws

bad_bview_access – when the current active alternative is not a list.

Returns

reference to the first element

constexpr bview back() const

Returns a view to the last element in the list. Calling back on an empty list is undefined behavior.

Throws

bad_bview_access – when current active alternative is not a list.

Returns

reference to the last element

constexpr bool contains(std::string_view key) const

Checks if there is an element with key equivalent to key in the dict. If the active alternative is not a dict, an exception of type bad_bview_access is thrown.

Parameters

key – key bvalue of the element to search for

Returns

true if there is such an element, otherwise false.

integer_bview

class integer_bview : public bencode::bview

The class integer_bview is a bview of which the descriptor describes an integer value. integer_bview does not holds any additional members over bview.

Public Functions

constexpr integer_bview(const integer_bview&) noexcept = default

Copy constructor.

constexpr integer_bview &operator=(const integer_bview&) noexcept = default

Copy assignment.

inline constexpr auto value() const noexcept -> std::int64_t

Return the value of the integer.

Returns

an integral value

inline constexpr operator std::int64_t() const noexcept

Implicit conversion operator.

Returns

an integral value

inline constexpr bool operator==(const integer_bview &that) const noexcept

Compare the content of lhs and rhs for equality.

Parameters

that – value to compare

inline constexpr std::strong_ordering operator (const integer_bview &that) const noexcept

Compares the contents of lhs and rhs.

Parameters

that – value to compare

string_bview

class string_bview : public bencode::bview

A view into a bencoded list that provides access to the data similar to a std::string_view.

Public Functions

inline explicit constexpr string_bview() noexcept

Default constructor. Constructs an empty string_bview. After construction, data() is equal to nullptr, and size() is equal to 0.

inline explicit constexpr string_bview(const bview &desc) noexcept

Downcasting constructor. Constructs a string_bview from a bview. Behavior is undefined when the bview type is not a string.

inline constexpr string_bview(const descriptor *data, const char *buffer) noexcept

Constructs a string_bview from a descriptor and a character buffer. Behavior is undefined when the descriptor type is not a string.

constexpr string_bview(const string_bview&) noexcept = default

Copy constructor.

constexpr string_bview &operator=(const string_bview&) noexcept = default

Copy assignment.

inline constexpr const_iterator begin() const noexcept

Returns an iterator to the first character of the view.

Returns

const_iterator to the first character

inline constexpr const_iterator cbegin() const noexcept

Returns an iterator to the first character of the view.

Returns

const_iterator to the first character

inline constexpr const_iterator end() const noexcept

Returns an iterator to the character following the last character. This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns

const_iterator to the character following the last character.

inline constexpr const_iterator cend() const noexcept

Returns an iterator to the character following the last character. This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns

const_iterator to the character following the last character.

inline constexpr const_reverse_iterator rbegin() const noexcept

Returns a reverse iterator to the first character of the reversed view. It corresponds to the last character of the non-reversed view.

Returns

const_reverse_iterator to the first character

inline constexpr const_reverse_iterator crbegin() const noexcept

Returns a reverse iterator to the first character of the reversed view. It corresponds to the last character of the non-reversed view.

Returns

const_reverse_iterator to the first character

inline constexpr const_reverse_iterator rend() const noexcept

Returns a reverse iterator to the character following the last character of the reversed view. It corresponds to the character preceding the first character of the non-reversed view. This character acts as a placeholder, attempting to access it results in undefined behavior.

Returns

const_reverse_iterator to the character following the last character.

inline constexpr const_reverse_iterator crend() const noexcept

Returns a reverse iterator to the character following the last character of the reversed view. It corresponds to the character preceding the first character of the non-reversed view. This character acts as a placeholder, attempting to access it results in undefined behavior.

Returns

const_reverse_iterator to the character following the last character.

inline constexpr const_reference operator[](size_type pos) const

Returns a const reference to the character at specified location pos. No bounds checking is performed: the behavior is undefined if pos >= size().

Parameters

pos – position of the character to return

Returns

Const reference to the requested character.

inline constexpr const_reference at(size_type pos) const

Returns a const reference to the character at specified location pos. Bounds checking is performed, exception of type std::out_of_range will be thrown on invalid access.

inline constexpr const_reference front() const

Returns reference to the first character in the view. The behavior is undefined if empty() == true.

Returns

Reference to the first character, equivalent to operator[](0).

inline constexpr const_reference back() const

Returns reference to the last character in the view. The behavior is undefined if empty() == true.

Returns

Reference to the last character, equivalent to operator[](size() - 1).

inline constexpr const_pointer data() const noexcept

Returns a pointer to the underlying character array. The pointer is such that the range [data(); data() + size()) is valid and the values in it correspond to the values of the view.

Returns

A pointer to the underlying character array.

inline constexpr size_type size() const noexcept

Returns the number of CharT elements in the view, i.e. std::distance(begin(), end()).

Returns

The number of CharT elements in the view.

inline constexpr size_type length() const noexcept

Returns the number of CharT elements in the view, i.e. std::distance(begin(), end()).

Returns

The number of CharT elements in the view.

inline constexpr size_type max_size() const noexcept

The largest possible number of char-like objects that can be referred to by a string_bview.

Returns

Maximum number of characters.

inline constexpr bool empty() const noexcept

Checks if the view has no characters, i.e. whether size() == 0.

Returns

true if the view is empty, false otherwise

inline constexpr std::string_view substr(size_type pos = 0, size_type count = npos) const

Returns a std::string_View of the substring [pos, pos + rcount), where rcount is the smaller of count and size() - pos.

Parameters
  • pos – position of the first character

  • count – requested length

Throws

std::out_of_range – if pos > size()

Returns

View of the substring [pos, pos + rcount).

inline constexpr bool starts_with(std::string_view sv) const noexcept

Checks if the string view begins with the given prefix.

Parameters

sv – a string view which may be a result of implicit conversion from std::basic_string

Returns

true if the string view begins with the provided prefix, false otherwise.

inline constexpr bool starts_with(char c) const noexcept

Checks if the string view begins with the given character.

Parameters

c – a single character

Returns

true if the string view begins with the provided prefix, false otherwise.

inline constexpr bool starts_with(const char *s) const

Checks if the string view begins with the given prefix.

Parameters

s – a null-terminated character string

Returns

true if the string view begins with the provided prefix, false otherwise.

inline constexpr bool ends_with(std::string_view sv) const noexcept

Checks if the string view ends with the given prefix.

Parameters

sv – a string view which may be a result of implicit conversion from std::basic_string

Returns

true if the string view ends with the provided prefix, false otherwise.

inline constexpr bool ends_with(char c) const noexcept

Checks if the string view ends with the given character.

Parameters

c – a single character

Returns

true if the string view ends with the provided prefix, false otherwise.

inline constexpr bool ends_with(const char *s) const

Checks if the string view ends with the given prefix.

Parameters

s – a null-terminated character string

Returns

true if the string view ends with the provided prefix, false otherwise.

inline constexpr operator std::string_view() const noexcept

Implicit conversion operator to std::string_view.

inline constexpr bool operator==(const string_bview &rhs) const noexcept

Compare equality with a bview.

Parameters

rhs – a bvalue to compare

inline constexpr std::strong_ordering operator (const string_bview &rhs) const noexcept

Compare order with a bview.

Parameters

rhs – a bvalue to compare

inline constexpr bool operator==(std::string_view rhs) const noexcept

Compare equality with a std::string_view.

Note

this overload is used to compare with std::string and c-strings as well.

Parameters

rhs – a string_view to compare

inline constexpr std::strong_ordering operator (std::string_view rhs) const noexcept

Compare order with a std::string_view.

Note

this overload is used to compare with std::string and c-strings as well.

Parameters

rhs – the value to compare with

list_bview

class list_bview : public bencode::bview

The class list_bview is a bview of which the descriptor describes a list value. list_bview does not holds any additional members over bview. This class provides a similar interface to a read-only list.

Public Functions

inline explicit constexpr list_bview()

Default constructor. Constructs an empty list_bview. After construction, data() is equal to nullptr, and size() is equal to 0.

inline explicit constexpr list_bview(const bview &bref) noexcept

Copy constructor. Constructs an list_bview from a list_bview. Behavior is undefined when the bview type is not a list_bview.

inline constexpr list_bview(const descriptor *data, const char *buffer) noexcept

Constructs a list_bview from a descriptor and a character buffer. Behavior is undefined when the descriptor type is not a list.

constexpr list_bview(const list_bview&) noexcept = default

Copy constructor.

constexpr list_bview &operator=(const list_bview&) noexcept = default

Copy assignment.

inline constexpr const_iterator begin() const noexcept

Returns an iterator to the first element of the list_bview.

Returns

Iterator to the first element

inline constexpr const_iterator cbegin() const noexcept

Returns an iterator to the first element of the list_bview.

Returns

Iterator to the first element

inline constexpr const_iterator end() const noexcept

Returns an iterator to the element following the last element. This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns

Iterator to the character following the last element.

inline constexpr const_iterator cend() const noexcept

Returns an iterator to the element following the last element. This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns

Iterator to the character following the last element.

inline constexpr const_reverse_iterator rbegin() const noexcept

Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().

Returns

Reverse iterator to the first element.

inline constexpr const_reverse_iterator crbegin() const noexcept

Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().

Returns

Reverse iterator to the first element.

inline constexpr const_reverse_iterator rend() const noexcept

Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.

Returns

Reverse iterator to the element following the last element.

inline constexpr const_reverse_iterator crend() const noexcept

Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.

Returns

Reverse iterator to the element following the last element.

inline constexpr bool empty() const noexcept

Checks if the container has no elements, i.e. whether begin() == end().

Returns

true if the container is empty, false otherwise.

inline constexpr size_type size() const noexcept

Returns the number of elements in the container, i.e. std::distance(begin(), end()).

Returns

The number of elements in the container.

inline constexpr size_type max_size() const noexcept

Returns the maximum number of elements the container is able to hold, i.e. std::distance(begin(), end()) for the largest container.

Returns

Maximum number of elements.

inline constexpr value_type operator[](std::size_t pos) const noexcept

Returns a reference to the element at specified location pos. No bounds checking is performed. Accessing element out of bound is undefined behavior.

Parameters

pos – position of the element to return

Returns

Reference to the requested element. @complexity linear in size of the container

inline constexpr value_type at(std::size_t pos) const

Returns a reference to the element at specified location pos. With bounds checking. If pos is not within the range of the container, an exception of type std::out_of_range is thrown.

Parameters

pos – position of the element to return

Returns

Reference to the requested element. @complexity linear in size of the container

inline constexpr value_type front() const noexcept

Returns a reference to the first element in the container. Calling front on an empty container is undefined.

Returns

Reference to the first element.

inline constexpr value_type back() const noexcept

Returns reference to the last element in the container. Calling back on an empty container causes undefined behavior. Reference to the last element.

inline constexpr bool operator==(const list_bview &rhs) const noexcept

Compare equality with a list_bview.

Parameters

rhs – a bvalue to compare

inline constexpr std::weak_ordering operator (const list_bview &rhs) const noexcept

Compare equality with a list_bview.

Parameters

rhs – a bvalue to compare

template<typename T>
inline constexpr auto operator==(const T &that) const noexcept -> bool

Compares the current alternatives content with that.

Parameters

that – the bvalue to compare to.

Returns

true of the lhs and rhs compare equal, false otherwise.

dict_bview

class dict_bview : public bencode::bview

A view into a bencoded dict that provides access to the elements similar to a std::map.

Note

dereferencing an iterator return a reference to a std::pair<string_bview, bview> but this reference is invalidated upon the next dereference.

Public Functions

inline explicit constexpr dict_bview(const bview &bref) noexcept

Converting constructor from bview.

constexpr dict_bview(const dict_bview&) noexcept = default

Copy constructor.

constexpr dict_bview &operator=(const dict_bview &view) noexcept = default

Copy assignment.

inline constexpr mapped_type at(std::string_view key) const

Returns a reference to the mapped value of the element with key equivalent to key. If no such element exists, an exception of type std::out_of_range is thrown.

Parameters

key – the key of the element to find

Returns

Reference to the mapped value of the requested element.

inline constexpr const_iterator begin() const noexcept

Returns an iterator to the first element of the dict_bview. If the dict_bview is empty, the returned iterator will be equal to end().

Returns

Iterator to the first element.

inline constexpr const_iterator cbegin() const noexcept

Returns an iterator to the first element of the dict_bview. If the dict_bview is empty, the returned iterator will be equal to end().

Returns

Iterator to the first element.

inline constexpr const_iterator end() const noexcept

Returns an iterator to the element following the last element of the dict_bview. This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns

Iterator to the element following the last element.

inline constexpr const_iterator cend() const noexcept

Returns an iterator to the element following the last element of the dict_bview. This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns

Iterator to the element following the last element.

inline constexpr const_reverse_iterator rbegin() const noexcept

Returns a reverse iterator to the first element of the reversed dict_bview. It corresponds to the last element of the non-reversed dict_bview. If the dict_bview is empty, the returned iterator is equal to rend().

inline constexpr const_reverse_iterator crbegin() const noexcept

Returns a reverse iterator to the first element of the reversed dict_bview. It corresponds to the last element of the non-reversed dict_bview. If the dict_bview is empty, the returned iterator is equal to rend().

inline constexpr const_reverse_iterator rend() const noexcept

Returns a reverse iterator to the element following the last element of the reversed dict_bview. It corresponds to the element preceding the first element of the non-reversed dict_bview. This element acts as a placeholder, attempting to access it results in undefined behavior.

inline constexpr const_reverse_iterator crend() const noexcept

Returns a reverse iterator to the element following the last element of the reversed dict_bview. It corresponds to the element preceding the first element of the non-reversed dict_bview. This element acts as a placeholder, attempting to access it results in undefined behavior.

inline constexpr bool empty() const noexcept

Checks if the container has no elements, i.e. whether begin() == end().

Returns

true if the container is empty, false otherwise.

inline constexpr std::size_t size() const noexcept

Returns the number of elements in the container, i.e. std::distance(begin(), end()).

Returns

The number of elements in the container.

inline constexpr std::size_t max_size() const noexcept

Returns the maximum number of elements the container is able to hold. i.e. std::distance(begin(), end()) for the largest container.

Returns

Maximum number of elements.

inline constexpr size_type count(std::string_view key) const noexcept

Returns the number of elements with key that compares equivalent to the specified argument, which is either 1 or 0 since this container does not allow duplicates.

Parameters

key – value of the elements to count

Returns

Number of elements with key that compares equivalent to key or x, which is either 1 or 0. @complexity linear in the size of the container.

inline constexpr const_iterator find(std::string_view key) const noexcept

Finds an element with key equivalent to key.

Parameters

key – value of the element to search for

Returns

Iterator to an element with key equivalent to key. If no such element is found, past-the-end (see end()) iterator is returned. @complexity linear in the size of the container.

inline constexpr bool contains(std::string_view key) const noexcept

Checks if there is an element with key equivalent to key in the container.

Parameters

key – value of the element to search for

Returns

true if there is such an element, otherwise false. @complexity Linear in the size of the container.

inline constexpr std::pair<const_iterator, const_iterator> equal_range(std::string_view key) const noexcept

Returns a range containing all elements with the given key in the container. The range is defined by two iterators, one pointing to the first element that is not less than key and another pointing to the first element greater than key. Alternatively, the first iterator may be obtained with lower_bound(), and the second with upper_bound().

See also

end()) iterator is returned as the first element. Similarly if there are no elements greater than key, past-the-end iterator is returned as the second element.

Parameters

key – value to compare the elements to

Returns

std::pair containing a pair of iterators defining the wanted range the first pointing to the first element that is not less than key and the second pointing to the first element greater than key. If there are no elements not less than key, past-the-end (

inline constexpr const_iterator lower_bound(std::string_view key) const noexcept

Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) key.

See also

end()) is returned.

Parameters

key – value to compare the elements to

Returns

Iterator pointing to the first element that is not less than key. If no such element is found, a past-the-end iterator (

inline constexpr const_iterator upper_bound(std::string_view key) const noexcept

Returns an iterator pointing to the first element that is greater than key.

See also

end()) iterator is returned.

Parameters

key – value to compare the elements to

Returns

Iterator pointing to the first element that is greater than key. If no such element is found, past-the-end (

inline constexpr bool operator==(const dict_bview &rhs) const noexcept

Compare equality with a dict_bview.

Parameters

rhs – a value to compare

inline constexpr std::weak_ordering operator (const dict_bview &rhs) const noexcept

Compare order with a dict_bview.

Parameters

rhs – a value to compare

template<typename T>
inline constexpr auto operator==(const T &that) const noexcept -> bool

Compares the current alternatives content with that.

Parameters

that – the bvalue to compare to.

Returns

true of the lhs and rhs compare equal, false otherwise.

Accessor functions

Provide accessors for bview, integer_bview, string_bview, list_bview and dict_bview types.

namespace bencode

Typedefs

template<bencode_type token>
using bview_alternative_t = typename bview_alternative<token>::type

Helper alias template for bview_alternative.

Functions

template<enum bencode_type E>
constexpr bview_alternative_t<E> &get(bview &v)

Enum based bview accessor. If v holds the alternative type specified b, returns a reference to the bview class for the type stored in v. Otherwise, throws bad_bview_access.

Template Parameters

E – type to look up

Parameters

v – reference to a bview

Returns

reference to the value described in the view.

template<enum bencode_type E>
constexpr const bview_alternative_t<E> &get(const bview &v)

Enum based bview accessor. If v holds the alternative type specified b, returns a reference to the bview class for the type stored in v. Otherwise, throws bad_bview_access.

Template Parameters

E – type to look up

Parameters

v – const reference to a bview

Returns

const reference to the value described in the view.

template<typename T>
constexpr T &get(bview &v)

Type based bview accessor. If v holds the alternative type specified b, returns a reference to the bview class for the type stored in v. Otherwise, throws bad_bview_access.

Template Parameters

E – type to look up

Parameters

v – reference to a bview

Returns

reference to the value described in the view.

template<typename T>
constexpr const T &get(const bview &v)

Enum based bview accessor. If v holds the alternative type specified b, returns a reference to the bview class for the type stored in v. Otherwise, throws bad_bview_access.

Template Parameters

E – type to look up

Parameters

v – const reference to a bview

Returns

const reference to the value described in the view.

template<enum bencode_type E>
constexpr bview_alternative_t<E> *get_if(bview *pv) noexcept

Enum-based non-throwing accessor. If pv is not a null pointer and pv->type() == E, returns a pointer to the bview described in the bview. Otherwise, returns a null pointer.

Parameters

pv – pointer to a bview

Returns

A pointer to the value decribed by the bview, nullptr on error.

template<enum bencode_type E>
constexpr const bview_alternative_t<E> *get_if(const bview *desc) noexcept

Enum-based non-throwing accessor. If pv is not a null pointer and pv->type() == E, returns a pointer to the bview described in the bview. Otherwise, returns a null pointer.

Parameters

pv – const pointer to a bview

Returns

A const pointer to the value decribed by the bview, nullptr on error.

template<typename T>
constexpr T *get_if(bview *pv) noexcept

Type-based non-throwing accessor. If pv is not a null pointer and pv is of type T, returns a pointer to T. Otherwise, returns a null pointer.

Parameters

pv – pointer to a bview

Returns

a pointer to T

template<typename T>
constexpr const T *get_if(const bview *pv) noexcept

Type-based non-throwing accessor. If pv is not a null pointer and pv is of type T, returns a const pointer to T. Otherwise, returns a null pointer.

Parameters

pv – pointer to a bview

Returns

a const pointer to T

constexpr const integer_bview &get_integer(const bview &desc)

Alias for get<bencode_type::integer>(desc)

constexpr integer_bview &get_integer(bview &desc)

Alias for get<bencode_type::integer>(desc)

constexpr const integer_bview *get_if_integer(const bview *desc) noexcept

Alias for get_if<bencode_type::integer>(desc)

constexpr integer_bview *get_if_integer(bview *desc) noexcept

Alias for get_if<bencode_type::integer>(desc)

constexpr const string_bview &get_string(const bview &desc)

Alias for get<bencode_type::string>(desc)

constexpr string_bview &get_string(bview &desc)

Alias for get<bencode_type::string>(desc)

constexpr const string_bview *get_if_string(const bview *desc) noexcept

Alias for get_if<bencode_type::string>(desc)

constexpr string_bview *get_if_string(bview *desc) noexcept

Alias for get_if<bencode_type::string>(desc)

constexpr const list_bview &get_list(const bview &desc)

Alias for get<bencode_type::list>(desc)

constexpr list_bview &get_list(bview &desc)

Alias for get<bencode_type::list>(desc)

constexpr const list_bview *get_if_list(const bview *desc) noexcept

Alias for get_if_list<bencode_type::list>(desc)

constexpr list_bview *get_if_list(bview *desc) noexcept

Alias for get_if_list<bencode_type::list>(desc)

constexpr const dict_bview &get_dict(const bview &desc)

Alias for get<bencode_type::dict>(desc)

constexpr dict_bview &get_dict(bview &desc)

Alias for get<bencode_type::dict>(desc)

constexpr const dict_bview *get_if_dict(const bview *desc) noexcept

Alias for get_if<bencode_type::dict>(desc)

constexpr dict_bview *get_if_dict(bview *desc) noexcept

Alias for get_if<bencode_type::dict>(desc)

template<enum bencode_type E>
constexpr bool holds_alternative(const bview &v) noexcept

Check if the bview described a bencode data type category E.

Check if the bview describes a bencode data type of type T

Template Parameters
  • E – the bencode data type to check for

  • E – the bencode data type to check for

Parameters
  • v – the bview to check

  • v – the bview to check

Returns

true if the bview described the given bencode data type E, false otherwise.

Returns

true if the bview described the given bencode data type E, false otherwise.

constexpr bool holds_uninitialized(const bview &v) noexcept

Alias for holds_alternative<bencode_type::unknown>(v).

Note

this is only true for a default constructed bview.

constexpr bool holds_integer(const bview &v) noexcept

Alias for holds_alternative<bencode_type::integer>(v).

constexpr bool holds_string(const bview &v) noexcept

Alias for holds_alternative<bencode_type::string>(v).

constexpr bool holds_list(const bview &v) noexcept

Alias for holds_alternative<bencode_type::list>(v).

constexpr bool holds_dict(const bview &v) noexcept

Alias for holds_alternative<bencode_type::dict>(v).

template<typename T>
constexpr nonstd::expected<T, conversion_errc> try_get_as(const bview &v)

Throwing converting bview accessor. If serialization_traits<T>::type == bview.type(), converts bvalue to T. If the active alternative type could not be converted returns bad_conversion.

Template Parameters

the – type to convert to

Parameters

v – reference to bview

Returns

an expected instance with the returned value or an error code.

template<typename T>
constexpr T get_as(const bview &value)

Throwing converting accessor. Try to convert a basic_bvalue instantiation to T.

Throws

bad_conversion – when the value could not be converted to the requested type.

Returns

the converted value

template<bencode_type E>
struct bview_alternative
#include <accessors.hpp>

Provides compile-time tag based access to the alternative interfaces to bview.

Template Parameters

E – enum representing the bencode data type.

Return

the matching bview class for a given bencode data type

Public Types

using type = std::tuple_element_t<static_cast<std::size_t>(E) - 1, alterative_typelist>

Private Types

using alterative_typelist = std::tuple<integer_bview, string_bview, list_bview, dict_bview>

Exceptions

namespace bencode
class bad_bview_access : public bencode::bad_access
#include <bad_bview_access.hpp>

Exception thrown when trying to access a bview with a type that does not match the currently active alternative.

template<>
struct is_error_code_enum<bencode::conversion_errc> : public true_type
namespace bencode

Enums

enum class conversion_errc : std::uint8_t

Error code enumeration for type conversion errors.

Values:

enumerator not_integer_type

The active alternative is not an integral type.

enumerator not_string_type

The active alternative is not a string type.

enumerator not_list_type

The active alternative is not a list type.

enumerator not_dict_type

The active alternative is not a dict type.

enumerator size_mismatch

Conversion to a fixed size type and the size of the active alternative does not match.

enumerator construction_error

Unspecified exception was thrown during construction of the requested type.

enumerator dict_mapped_type_construction_error

The mapped type of a dict cannot be converted to the requested type.

enumerator list_value_type_construction_error

The bvalue type of a list cannot be converted to the requested type.

enumerator undefined_conversion

The conversion to the given type is not defined.

Functions

constexpr std::string_view to_string(const conversion_errc &ec)

Returns a description of the ec bvalue.

inline std::error_code make_error_code(conversion_errc e)
class bad_conversion : public bencode::exception
#include <bad_conversion.hpp>

Error thrown when trying to convert a bvalue or bview to a type that does not match the type of the current alternative.

Public Functions

inline explicit bad_conversion(const conversion_errc &ec)
inline const char *what() const noexcept override

Returns the explanatory string.

inline conversion_errc errc() const noexcept

Return the error code enumeration matching this exception.

Private Members

conversion_errc errc_
struct conversion_category : public error_category

Public Functions

inline const char *name() const noexcept override
inline std::string message(int ev) const override
namespace std
template<> conversion_errc > : public true_type

Concepts

namespace bencode

Variables

template<typename T> concept bview_alternative_type  =std::same_as<std::remove_cvref_t<T>,integer_bview> ||std::same_as<std::remove_cvref_t<T>,string_bview> ||std::same_as<std::remove_cvref_t<T>,list_bview> ||std::same_as<std::remove_cvref_t<T>,dict_bview>
template<typename T> concept retrievable_from_bview  =requires(bviewdesc) {{ detail::convert_from_bview_to<T>(desc) }-> std::same_as<nonstd::expected<T, conversion_errc>>;}

The concept convert_from_bview_to is satisfied when the bvalue described by a destriptor can be converted to given type with the get_as<T> accessor function.