10#ifndef BOTAN_STL_UTIL_H_
11#define BOTAN_STL_UTIL_H_
13#include <botan/assert.h>
28template <
typename RetT,
typename KeyT,
typename ReducerT>
29RetT
reduce(
const std::vector<KeyT>& keys, RetT acc, ReducerT reducer)
30 requires std::invocable<ReducerT&, RetT, const KeyT&> &&
31 std::convertible_to<std::invoke_result_t<ReducerT&, RetT, const KeyT&>, RetT>
33 for(
const KeyT& key : keys) {
34 acc = reducer(std::move(acc), key);
42template <
typename T,
typename V>
44 for(
const auto& elem : vec) {
52template <
typename T,
typename Pred>
54 auto i = assoc.begin();
55 while(i != assoc.end()) {
64template <
typename... Alts,
typename... Ts>
65constexpr bool holds_any_of(
const std::variant<Ts...>& v)
noexcept {
66 return (std::holds_alternative<Alts>(v) || ...);
69template <
typename GeneralVariantT,
typename SpecialT>
71 return std::is_constructible_v<GeneralVariantT, SpecialT>;
74template <
typename GeneralVariantT,
typename... SpecialTs>
76 return (std::is_constructible_v<GeneralVariantT, SpecialTs> && ...);
86template <
typename GeneralVariantT,
typename SpecialT>
88 requires(std::is_constructible_v<GeneralVariantT, std::decay_t<SpecialT>>)
90 return std::forward<SpecialT>(specific);
100template <
typename GeneralVariantT,
typename... SpecialTs>
101constexpr GeneralVariantT
generalize_to(std::variant<SpecialTs...> specific) {
104 "Desired general type must be implicitly constructible by all types of the specialized std::variant<>");
105 return std::visit([](
auto s) -> GeneralVariantT {
return s; }, std::move(specific));
110template <
class... Ts>
112 using Ts::operator()...;
115template <
class... Ts>
120 requires std::is_enum_v<T>
122 return static_cast<std::underlying_type_t<T>
>(e);
127[[nodiscard]]
constexpr auto out_ptr(T& outptr)
noexcept {
130 constexpr ~out_ptr_t()
noexcept {
131 m_ptr.reset(m_rawptr);
135 constexpr explicit out_ptr_t(T& outptr) noexcept : m_ptr(outptr), m_rawptr(
nullptr) {}
137 out_ptr_t(
const out_ptr_t&) =
delete;
138 out_ptr_t(out_ptr_t&&) =
delete;
139 out_ptr_t& operator=(
const out_ptr_t&) =
delete;
140 out_ptr_t& operator=(out_ptr_t&&) =
delete;
143 [[nodiscard]]
constexpr operator typename T::element_type **() &&
noexcept {
return &m_rawptr; }
147 typename T::element_type* m_rawptr;
150 return out_ptr_t{outptr};
void map_remove_if(Pred pred, T &assoc)
constexpr auto out_ptr(T &outptr) noexcept
constexpr bool holds_any_of(const std::variant< Ts... > &v) noexcept
bool value_exists(const std::vector< T > &vec, const V &val)
RetT reduce(const std::vector< KeyT > &keys, RetT acc, ReducerT reducer)
constexpr bool is_generalizable_to(const SpecialT &) noexcept
auto to_underlying(T e) noexcept
overloaded(Ts...) -> overloaded< Ts... >
constexpr GeneralVariantT generalize_to(SpecialT &&specific)
Converts a given variant into another variant-ish whose type states are a super set of the given vari...