Value interface

Bvalue

using bencode::bvalue = basic_bvalue<default_bvalue_policy>

Alias for basic_bvalue<default_bvalue_policy>

template<typename Policy>
class basic_bvalue

A class template storing a bencoded bvalue.

An instance of basic_bvalue at any given time either holds a bvalue of one of the bencode data types, or uninitialized_type in the case of error or no bvalue. A single bvalue can store large and complicated bencoded data structures consisting of arbitrarily nested lists and dicts with many sub-values, that are again bvalue instances.

Template Parameters

Policy – instantation of bvalue_policy to defining the storage types

Public Functions

template<typename U, typename T = std::remove_cvref_t<U>>
inline constexpr basic_bvalue(U &&v) noexcept(noexcept(storage_type(std::in_place_type<std::remove_cvref_t<T>>, std::forward<U>(v))))

Copy/move constructor for exact matches to one of the storage_types.

template<typename U, typename T = std::remove_cvref_t<U>>
inline constexpr basic_bvalue(U &&v) noexcept(noexcept(detail::assign_to_bvalue(std::declval<basic_bvalue&>(), std::forward<U>(v))))

Copy/move constructor for types that can be converted to bvalue using built-in conversions or used defined types that implement are convertible to bvalue through bencode_assign_to_bvalue().

inline void discard()

Discards the current active alternative and sets the storage to the uninitialized state.

template<typename ...Args>
inline constexpr integer_type &emplace_integer(Args&&... args)

Construct an integer in place.

Parameters

args – arguments to forward to the integer alternative constructor.

Returns

A reference to the new contained bvalue.

template<typename ...Args>
inline string_type &emplace_string(Args&&... args)

Construct a string in place.

Parameters

args – arguments to forward to the integer alternative constructor.

Returns

A reference to the new contained bvalue.

template<typename ...Args>
inline string_type &emplace_string(string_init_list il, Args&&... args)

Construct a string in place.

Parameters

args – arguments to forward to the integer alternative constructor.

Returns

A reference to the new contained bvalue.

template<typename ...Args>
inline list_type &emplace_list(Args&&... args)

Construct a list in place.

Parameters

args – arguments to forward to the list alternative constructor.

Returns

A reference to the new contained bvalue.

template<typename ...Args>
inline list_type &emplace_list(list_init_list il, Args&&... args)

Construct a list in place.

Parameters

args – arguments to forward to the list alternative constructor.

Returns

A reference to the new contained bvalue.

template<typename ...Args>
inline dict_type &emplace_dict(Args&&... args)

Construct a dict in place.

Parameters

args – arguments to forward to the dict alternative constructor.

Returns

A reference to the new contained bvalue.

template<bencode_type T, typename ...Args>
inline decltype(auto) emplace(Args&&... args)

Emplace a type T.

inline constexpr auto type() const noexcept -> enum bencode_type

return the type as a value_type enumeration

Returns

bencode_type enum specifying the current active alternative

inline explicit operator bool() const noexcept

Returns whether the bvalue hold a value.

Returns

Return true if the container is NOT in the unitialized state, false otherwise.

inline reference at(std::size_t pos)

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 out_of_range is thrown. If the current active alternative is not a list, and exception of type bencode::bad_bvalue_access is thrown.

Parameters

pos – position of the element to return

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

  • bad_bvalue_access – if the current active alternative is not list.

Returns

Reference to the requested element.

inline const_reference 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 out_of_range is thrown. If the current active alternative is not a list, and exception of type bencode::bad_bvalue_access is thrown.

Parameters

pos – position of the element to return

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

  • bad_bvalue_access – if the current active alternative is not list.

Returns

Reference to the requested element.

inline reference at(const string_type &key)

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 bencode::bad_bvalue_access is thrown. If no such element exists, an exception of type out_of_range is thrown.

Parameters

pos – the key of the element to find

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

  • bencode::bad_bvalue_access – if the current active alternative is not dict.

Returns

Reference to the mapped bvalue of the requested element

inline const_reference at(const string_type &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 bencode::bad_bvalue_access is thrown. If no such element exists, an exception of type out_of_range is thrown.

Parameters

pos – the key of the element to find

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

  • bencode::bad_bvalue_access – if the current active alternative is not dict.

Returns

Reference to the mapped bvalue of the requested element

inline const_reference at(const bpointer &pointer) const

Return a reference to the bvalue referenced by a bpointer.

Throws

bpointer_error – when the pointer does not resolve for this value

inline reference operator[](std::size_t pos)

Returns a reference to the element at specified location pos. No bounds checking is performed. If the active alternative is not a list an exception of type bencode::bad_bvalue_access is thrown.

Parameters

pos – position of the element to return

Returns

Reference to the requested element

inline const_reference operator[](std::size_t pos) const

Returns a reference to the element at specified location pos. No bounds checking is performed. If the active alternative is not a list an exception of type bencode::bad_bvalue_access is thrown.

Parameters

pos – position of the element to return

Returns

Reference to the requested element

inline reference operator[](const string_type &key)

Returns a reference to the bvalue that is mapped to a key equivalent to key, performing an insertion if such key does not already exist. If the bvalue is unintialized an empty dict will be created in place. If the active alternative is not a dict an exception of type bencode::bad_bvalue_access is thrown.

Parameters

key – the key of the element to find

Returns

Reference to the mapped bvalue of the new element if no element with key key existed. Otherwise a reference to the mapped bvalue of the existing element whose key is equivalent to key.

inline reference operator[](string_type &&key)

Returns a reference to the bvalue that is mapped to a key equivalent to key, performing an insertion if such key does not already exist. If the bvalue is unintialized an empty dict will be created in place. If the active alternative is not a dict an exception of type bencode::bad_bvalue_access is thrown.

Parameters

key – the key of the element to find

Returns

Reference to the mapped bvalue of the new element if no element with key key existed. Otherwise a reference to the mapped bvalue of the existing element whose key is equivalent to key.

inline reference front()

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

Throws

bad_bvalue_access – when current active alternative is not a list.

Returns

reference to the first element

inline const_reference front() const

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

Throws

bad_bvalue_access – when current active alternative is not a list.

Returns

reference to the first element

inline reference back()

Returns a reference to the last element in the container. Calling back on an empty container is undefined

Throws

bad_bvalue_access – when current active alternative is not a list.

Returns

reference to the last element

inline const_reference back() const

Returns a reference to the last element in the container. Calling back on an empty container is undefined

Throws

bad_bvalue_access – when current active alternative is not a list.

Returns

reference to the last element

template<typename ...Args>
inline reference emplace_back(Args&&... args)

Appends a new element to the end of a list. If the active alternative is uninintialized, first constructs an empty list in place. If the active alternative is not a list, an exception of type bencode::bad_bvalue_access is thrown. The arguments args… are forwarded to the constructor of the list type.

Parameters

args – arguments to forward to the constructor of the element

Returns

Reference to the mapped bvalue of the new element if no element with key key existed. Otherwise a reference to the mapped bvalue of the existing element whose key is equivalent to key.

inline void push_back(const list_value_type &value)

Appends the given element bvalue to the end of a list. If the active alternative is uninintialized, first constructs an empty list in place. If the active alternative is not a list, an exception of type bencode::bad_bvalue_access is thrown. The arguments args… are forwarded to the constructor of the list type.

Parameters

value – the bvalue of the element to append

inline void push_back(list_value_type &&value)

Appends the given element bvalue to the end of a list. If the active alternative is uninintialized, first constructs an empty list in place. If the active alternative is not a list, an exception of type bencode::bad_bvalue_access is thrown. The arguments args… are forwarded to the constructor of the list type.

Parameters

value – the bvalue of the element to append

inline bool contains(const dict_key_type &key) const

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

Parameters

key – key bvalue of the element to search for

Returns

true if there is such an element, otherwise false.

template<typename K>
inline bool contains(const K &key) const

Checks if there is an element that compares equivalent to key in the container. If the active alternative is not a dict, an exception of type bad_bvalue_access is thrown. This overload only participates in overload resolution if the map_type key_compare function supports transparent comparison.

Parameters

key – key bvalue of the element to search for

Returns

true if there is such an element, otherwise false.

inline bool contains(const bpointer &pointer) const

Return a reference to the bvalue referenced by a bpointer.

Throws

bpointer_error – when the pointer does not resolve for this value

inline void clear() noexcept

Remove all elements from the current alternative. If the current alternative is a dict, list, string, calls clear on the underlying container. If the current alternative is an integer, set the bvalue to zero. If the current alternative is uninitialized, do nothing.

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

Compares the contents of two basic_bvalue types.

Parameters

that – the basic_bvalue whose content to compare

Returns

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

inline constexpr std::weak_ordering operator (const basic_bvalue &that) const noexcept

Compares the content of the current alternative with the content of that lexicographically.

Parameters

that – the basic_bvalue 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.

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 auto operator (const T &that) const noexcept -> std::weak_ordering

Compares the current alternatives content with that.

Parameters

that – the basic_bvalue 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.

Policy

namespace bencode
template<typename Policy>
class basic_bvalue
#include <basic_bvalue.hpp>
template<typename IntegralType, typename StringType, template<typename T> typename ListType, template<typename K, typename V> typename DictType>
struct bvalue_policy
#include <bvalue_policy.hpp>
struct default_bvalue_policy : public bencode::bvalue_policy<std::int64_t, std::string, detail::default_policy_helper::template list_type, detail::default_policy_helper::template dict_type>

Accessors

namespace bencode

Typedefs

template<bencode_type E, basic_bvalue_instantiation T>
using bvalue_alternative_t = typename bvalue_alternative<E, T>::type

Helper template alias for bvalue_alternative.

Functions

template<enum bencode_type E, typename BV>
constexpr decltype(auto) get(BV &&v)

Enum based bvalue accessor. If v holds an alternative of type category E, returns a reference to the storage type used for the for type category E. Otherwise, throws bad_bvalue_access

.

Type based bvalue accessor. If v holds a alternative of type T, returns a reference to the value stored in the

basic_bvalue instance. Otherwise, throws bad_bvalue_access.

Parameters
  • v – reference to a basic_value<Policy> instance

  • v – reference to a basic_value<Policy> instance

Throws
  • bad_bvalue_access – when current active alternative is not of category E.

  • bad_bvalue_access – when the current active alternative type is not of type T.

Returns

a reference to the value stored in the basic_bvalue instance.

template<enum bencode_type E, basic_bvalue_instantiation BV>
constexpr bvalue_alternative_t<E, BV> *get_if(BV *pv)

Enum based non-throwing bvalue accessor. If pv is not nullptr and holds an value of type category E, returns a pointer to the type stored in pv. Otherwise return a nullptr.

Parameters

pv – pointer to a basic_value<Policy> instance

Returns

a pointer to the type stored in value if the active alternative is of category E or a nullptr

template<enum bencode_type E, basic_bvalue_instantiation BV>
constexpr const bvalue_alternative_t<E, BV> *get_if(const BV *pv)

Eum based non-throwing bvalue accessor. If pv is not nullptr and holds a alternative of type T, returns a pointer to the type stored in value. Otherwise return a nullptr.

Parameters

pv – const pointer to a basic_value<Policy> instance

Returns

a const pointer to the type stored in value if E matches the active alternative or a nullptr

template<typename T, basic_bvalue_instantiation BV>
constexpr auto *get_if(BV *value)
template<basic_bvalue_instantiation BV, bvalue_alternative_type<BV> T>
constexpr const auto *get_if(const BV *value)
template<typename T, basic_bvalue_instantiation BV>
constexpr auto try_get_as(BV &&value) -> nonstd::expected<T, conversion_errc>

Non-throwing converting accessor. Try to convert a basic_bvalue instantiation to T.

Returns

expected object containing the value or an conversion_errc.

template<typename T, basic_bvalue_instantiation BV>
constexpr auto get_as(BV &&value) -> T

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<typename Policy>
constexpr const auto &get_integer(const basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::integer>

template<typename Policy>
constexpr auto &get_integer(basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::integer>

template<typename Policy>
constexpr auto &&get_integer(basic_bvalue<Policy> &&value)

Alias for bencode::get<bencode_type::integer>

template<typename Policy>
constexpr const auto *get_if_integer(const basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::integer>

template<typename Policy>
constexpr auto *get_if_integer(basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::integer>

template<typename Policy>
constexpr const auto &get_string(const basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::string>

template<typename Policy>
constexpr auto &get_string(basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::string>

template<typename Policy>
constexpr auto &&get_string(basic_bvalue<Policy> &&value)

Alias for bencode::get_if<bencode_type::string>

template<typename Policy>
constexpr const auto *get_if_string(const basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::string>

template<typename Policy>
constexpr auto *get_if_string(basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::string>

template<typename Policy>
constexpr const auto &get_list(const basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::list>

template<typename Policy>
constexpr auto &get_list(basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::list>

template<typename Policy>
constexpr auto &&get_list(basic_bvalue<Policy> &&value)

Alias for bencode::get<bencode_type::list>

template<typename Policy>
constexpr const auto *get_if_list(const basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::list>

template<typename Policy>
constexpr auto *get_if_list(basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::list>

template<typename Policy>
constexpr const auto &get_dict(const basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::dict>

template<typename Policy>
constexpr auto &get_dict(basic_bvalue<Policy> &value)

Alias for bencode::get<bencode_type::dict>(value)

template<typename Policy>
constexpr auto &&get_dict(basic_bvalue<Policy> &&value)

Alias for bencode::get<bencode_type::dict>

template<typename Policy>
constexpr const auto *get_if_dict(const basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::dict>

template<typename Policy>
constexpr auto *get_if_dict(basic_bvalue<Policy> *value)

Alias for bencode::get_if<bencode_type::dict>

template<enum bencode_type E, typename Policy>
constexpr bool holds_alternative(const basic_bvalue<Policy> &bv)

Returns true if the type of the current alternative is equal to E.

Parameters

bv – a basic_bvalue instance.

template<typename Policy>
constexpr bool holds_uninitialized(const basic_bvalue<Policy> &value) noexcept
template<typename Policy>
constexpr bool holds_integer(const basic_bvalue<Policy> &value) noexcept
template<typename Policy>
constexpr bool holds_string(const basic_bvalue<Policy> &value) noexcept
template<typename Policy>
constexpr bool holds_list(const basic_bvalue<Policy> &value) noexcept
template<typename Policy>
constexpr bool holds_dict(const basic_bvalue<Policy> &value) noexcept
template<bencode_type E, basic_bvalue_instantiation T>
struct bvalue_alternative
#include <accessors.hpp>

Provides compile-time tag based access to the types of the of the alternatives of the possibly cv-qualified basic_bvalue, combining cv-qualifications of the basic_bvalue (if any) with the cv-qualifications of the alternative.

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

  • T – basic_bvalue template instantiation

Public Types

using type = std::variant_alternative_t<static_cast<std::underlying_type_t<bencode_type>>(E), typename T::storage_type>
template<bencode_type E, basic_bvalue_instantiation T>
struct bvalue_alternative<E, const T>

Public Types

using type = std::add_const_t<typename bvalue_alternative<E, T>::type>
template<bencode_type E, basic_bvalue_instantiation T>
struct bvalue_alternative<E, volatile const T>

Public Types

using type = std::add_cv_t<typename bvalue_alternative<E, T>::type>
template<bencode_type E, basic_bvalue_instantiation T>
struct bvalue_alternative<E, volatile T>

Public Types

using type = std::add_volatile_t<typename bvalue_alternative<E, T>::type>

Concepts

namespace bencode

Variables

template<typename T> concept basic_bvalue_instantiation  =detail::is_instantiation_of_v<basic_bvalue, std::remove_cvref_t<T> >

The concept basic_bvalue_instantiation<T> is satisfied if T is a template instantiation of the basic_bvalue template class. Ignores cv-qualifiers and reference types.

template<typename T, typename BV> concept bvalue_alternative_type  =basic_bvalue_instantiation<BV> && (std::same_as<std::remove_cvref_t<T>, typename std::remove_cvref_t<BV>::uninitialized_type> ||std::same_as<std::remove_cvref_t<T>, typename std::remove_cvref_t<BV>::integer_type> ||std::same_as<std::remove_cvref_t<T>, typename std::remove_cvref_t<BV>::string_type> ||std::same_as<std::remove_cvref_t<T>, typename std::remove_cvref_t<BV>::list_type> ||std::same_as<std::remove_cvref_t<T>, typename std::remove_cvref_t<BV>::dict_type>)

The concept bvalue_alternative_for<T, BV> specifies that T is one of the types that can be stored in the basic_bvalue variant type BV. Ignores cv-qualifiers and reference types.

template<typename T, typename Policy> concept assignable_to_bvalue_for  =serializable<T> &&requires(basic_bvalue<Policy> b, T x) {detail::assign_to_bvalue(b, x);}

Check if a type can be assigned to a basic_bvalue<Policy> type using built-in conversions or by using the user-defined extension point function bencode_assign_to_value for given T.

template<typename T, typename Policy> concept retrievable_from_bvalue_for  =serializable<T> &&requires(basic_bvalue<Policy> b) {{ detail::convert_from_bvalue_to<T>(b) } -> std::same_as<nonstd::expected<T, conversion_errc> >;}

Check if a basic_bvalue<Policy> can be converted to type T using built-in conversions or the user defined extension point function ‘bencode_convert_from_bvalue.

Exceptions