9#ifndef BOTAN_STRONG_TYPE_H_
10#define BOTAN_STRONG_TYPE_H_
12#include <botan/concepts.h>
31template <
typename CapabilityT,
typename... Tags>
50 constexpr explicit
Strong_Base(T v) : m_value(std::move(v)) {}
52 constexpr T&
get() & {
return m_value; }
54 constexpr const T&
get() const& {
return m_value; }
56 constexpr T&&
get() && {
return std::move(m_value); }
58 constexpr const T&&
get() const&& {
return std::move(m_value); }
67template <std::
integral T>
73template <concepts::container T>
88 template <
typename InputIt>
92 decltype(
auto)
begin() noexcept(noexcept(this->
get().
begin())) {
return this->
get().begin(); }
94 decltype(
auto)
begin()
const noexcept(
noexcept(this->
get().begin())) {
return this->
get().begin(); }
96 decltype(
auto)
end()
noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
98 decltype(
auto)
end()
const noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
100 decltype(
auto)
cbegin()
noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
102 decltype(
auto)
cbegin()
const noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
104 decltype(
auto)
cend()
noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
106 decltype(
auto)
cend()
const noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
113 return this->
get().empty();
128 template <
typename U>
129 decltype(
auto)
operator[](U&& i)
const noexcept(
noexcept(this->
get().operator[](i))) {
130 return this->
get()[std::forward<U>(i)];
133 template <
typename U>
134 decltype(
auto)
operator[](U&& i)
noexcept(
noexcept(this->
get().operator[](i))) {
135 return this->
get()[std::forward<U>(i)];
138 template <
typename U>
139 decltype(
auto)
at(U&& i)
const noexcept(
noexcept(this->
get().at(i)))
142 return this->
get().at(std::forward<U>(i));
145 template <
typename U>
146 decltype(
auto)
at(U&& i)
noexcept(
noexcept(this->
get().at(i)))
149 return this->
get().at(std::forward<U>(i));
153template <concepts::container T>
154class Strong_Adapter<T> :
public Container_Strong_Adapter_Base<T> {
156 using Container_Strong_Adapter_Base<T>::Container_Strong_Adapter_Base;
159template <concepts::contiguous_container T>
160class Strong_Adapter<T> :
public Container_Strong_Adapter_Base<T> {
175 requires(std::same_as<T, std::string>)
179 decltype(
auto)
data() noexcept(noexcept(this->
get().
data())) {
return this->
get().data(); }
181 decltype(
auto)
data()
const noexcept(
noexcept(this->
get().data())) {
return this->
get().data(); }
197template <
typename T,
typename TagTypeT,
typename... Capabilities>
202 template <
typename CapabilityT>
204 return (std::is_same_v<CapabilityT, Capabilities> || ...);
208 using Tag = TagTypeT;
227 return std::forward<T>(t);
230 return std::forward<T>(t).get();
246template <
typename T,
typename ParamT>
247 requires std::constructible_from<T, ParamT> ||
248 (concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>)
250 if constexpr(std::same_as<std::remove_cvref_t<ParamT>, T>) {
252 return std::forward<ParamT>(t);
253 }
else if constexpr(std::constructible_from<T, ParamT>) {
255 return T{std::forward<ParamT>(t)};
260 return T{
typename T::wrapped_type{std::forward<ParamT>(t)}};
271template <concepts::strong_type T>
273 using type =
typename T::wrapped_type;
290template <
typename T,
typename... Tags>
293 return os << v.
get();
296template <
typename T,
typename... Tags>
297 requires(std::equality_comparable<T>)
299 return lhs.
get() == rhs.
get();
302template <
typename T,
typename... Tags>
303 requires(std::three_way_comparable<T>)
305 return lhs.
get() <=> rhs.
get();
308template <std::integral T1, std::integral T2,
typename... Tags>
310 return a <=> b.
get();
313template <std::integral T1, std::integral T2,
typename... Tags>
315 return a.
get() <=> b;
318template <std::integral T1, std::integral T2,
typename... Tags>
323template <std::integral T1, std::integral T2,
typename... Tags>
328template <std::integral T1, std::integral T2,
typename... Tags>
334template <std::integral T1, std::integral T2,
typename... Tags>
340template <std::integral T,
typename... Tags>
345template <std::integral T1, std::integral T2,
typename... Tags>
351template <std::integral T1, std::integral T2,
typename... Tags>
357template <std::integral T,
typename... Tags>
362template <std::integral T1, std::integral T2,
typename... Tags>
368template <std::integral T1, std::integral T2,
typename... Tags>
374template <std::integral T,
typename... Tags>
379template <std::integral T1, std::integral T2,
typename... Tags>
385template <std::integral T1, std::integral T2,
typename... Tags>
391template <std::integral T,
typename... Tags>
396template <std::integral T1, std::integral T2,
typename... Tags>
402template <std::integral T1, std::integral T2,
typename... Tags>
408template <std::integral T,
typename... Tags>
413template <std::integral T1, std::integral T2,
typename... Tags>
419template <std::integral T1, std::integral T2,
typename... Tags>
425template <std::integral T,
typename... Tags>
430template <std::integral T1, std::integral T2,
typename... Tags>
436template <std::integral T1, std::integral T2,
typename... Tags>
442template <std::integral T,
typename... Tags>
447template <std::integral T1, std::integral T2,
typename... Tags>
450 return Strong<T2, Tags...>(a >> b.
get());
453template <std::integral T1, std::integral T2,
typename... Tags>
456 return Strong<T1, Tags...>(a.
get() >> b);
459template <std::integral T,
typename... Tags>
464template <std::integral T1, std::integral T2,
typename... Tags>
467 return Strong<T2, Tags...>(a << b.
get());
470template <std::integral T1, std::integral T2,
typename... Tags>
473 return Strong<T1, Tags...>(a.
get() << b);
476template <std::integral T,
typename... Tags>
481template <std::integral T1, std::integral T2,
typename... Tags>
488template <std::integral T,
typename... Tags>
494template <std::integral T1, std::integral T2,
typename... Tags>
501template <std::integral T,
typename... Tags>
507template <std::integral T1, std::integral T2,
typename... Tags>
514template <std::integral T,
typename... Tags>
520template <std::integral T1, std::integral T2,
typename... Tags>
527template <std::integral T,
typename... Tags>
533template <std::integral T1, std::integral T2,
typename... Tags>
540template <std::integral T,
typename... Tags>
546template <std::integral T1, std::integral T2,
typename... Tags>
553template <std::integral T,
typename... Tags>
559template <std::integral T1, std::integral T2,
typename... Tags>
566template <std::integral T,
typename... Tags>
572template <std::integral T1, std::integral T2,
typename... Tags>
579template <std::integral T,
typename... Tags>
585template <std::integral T1, std::integral T2,
typename... Tags>
592template <std::integral T,
typename... Tags>
598template <std::integral T,
typename... Tags>
605template <std::integral T,
typename... Tags>
611template <std::integral T,
typename... Tags>
618template <std::integral T,
typename... Tags>
639template <concepts::contiguous_strong_type T>
641 using underlying_span = std::
642 conditional_t<std::is_const_v<T>, std::span<const typename T::value_type>, std::span<typename T::value_type>>;
647 using iterator =
typename underlying_span::iterator;
648 using pointer =
typename underlying_span::pointer;
668 template <concepts::contiguous_strong_type T2>
671 requires(std::is_same_v<T2, std::remove_const_t<T>>)
672 : m_span(other.
get()) {}
684 underlying_span
get()
const {
return m_span; }
689 underlying_span
get() {
return m_span; }
691 decltype(
auto)
data()
noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
693 decltype(
auto)
data()
const noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
695 decltype(
auto)
size()
const noexcept(
noexcept(this->m_span.size())) {
return this->m_span.size(); }
697 bool empty() const noexcept(noexcept(this->m_span.
empty())) {
return this->m_span.empty(); }
699 decltype(
auto)
begin() noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
701 decltype(
auto)
begin() const noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
703 decltype(
auto)
end() noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
705 decltype(
auto)
end() const noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
707 decltype(
auto)
operator[](
typename underlying_span::size_type i)
const noexcept {
return this->m_span[i]; }
710 underlying_span m_span;
typename underlying_span::size_type size_type
decltype(auto) begin() noexcept(noexcept(this->m_span.begin()))
decltype(auto) data() noexcept(noexcept(this->m_span.data()))
underlying_span get() const
typename underlying_span::value_type value_type
decltype(auto) begin() const noexcept(noexcept(this->m_span.begin()))
StrongSpan(const StrongSpan &other)=default
StrongSpan(const StrongSpan< T2 > &other)
decltype(auto) end() noexcept(noexcept(this->m_span.end()))
StrongSpan & operator=(StrongSpan &&other)=default
StrongSpan(underlying_span span)
typename underlying_span::const_pointer const_pointer
bool empty() const noexcept(noexcept(this->m_span.empty()))
decltype(auto) operator[](typename underlying_span::size_type i) const noexcept
typename underlying_span::pointer pointer
decltype(auto) data() const noexcept(noexcept(this->m_span.data()))
decltype(auto) end() const noexcept(noexcept(this->m_span.end()))
StrongSpan & operator=(const StrongSpan &other)=default
typename underlying_span::iterator iterator
StrongSpan(StrongSpan &&other)=default
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
static constexpr bool has_capability()
typename T::iterator iterator
typename T::value_type value_type
decltype(auto) cbegin() const noexcept(noexcept(this->get().cbegin()))
decltype(auto) begin() noexcept(noexcept(this->get().begin()))
Container_Strong_Adapter_Base(InputIt begin, InputIt end)
decltype(auto) cend() noexcept(noexcept(this->get().cend()))
typename T::size_type size_type
decltype(auto) cend() const noexcept(noexcept(this->get().cend()))
decltype(auto) end() const noexcept(noexcept(this->get().end()))
void reserve(size_type size) noexcept(noexcept(this->get().reserve(size)))
size_type size() const noexcept(noexcept(this->get().size()))
decltype(auto) at(U &&i) noexcept(noexcept(this->get().at(i)))
decltype(auto) end() noexcept(noexcept(this->get().end()))
bool empty() const noexcept(noexcept(this->get().empty()))
decltype(auto) begin() const noexcept(noexcept(this->get().begin()))
Container_Strong_Adapter_Base(size_t size)
void resize(size_type size) noexcept(noexcept(this->get().resize(size)))
typename T::const_iterator const_iterator
decltype(auto) at(U &&i) const noexcept(noexcept(this->get().at(i)))
decltype(auto) cbegin() noexcept(noexcept(this->get().cbegin()))
Strong_Adapter(const char *str)
decltype(auto) data() const noexcept(noexcept(this->get().data()))
decltype(auto) data() noexcept(noexcept(this->get().data()))
typename T::const_pointer const_pointer
typename T::pointer pointer
Strong_Adapter(std::span< const typename Container_Strong_Adapter_Base< T >::value_type > span)
constexpr const T & get() const &
Strong_Base(Strong_Base &&) noexcept=default
constexpr const T && get() const &&
Strong_Base(const Strong_Base &)=default
constexpr bool has_capability
ASN1_Type operator|(ASN1_Type x, ASN1_Type y)
BigInt operator*(const BigInt &x, const BigInt &y)
constexpr auto operator>>=(Strong< T1, Tags... > &a, T2 b)
OctetString operator^(const OctetString &k1, const OctetString &k2)
OctetString operator+(const OctetString &k1, const OctetString &k2)
constexpr auto operator++(Strong< T, Tags... > &a, int)
std::ostream & operator<<(std::ostream &out, const OID &oid)
BigInt operator/(const BigInt &x, const BigInt &y)
BigInt operator-(const BigInt &x, const BigInt &y)
constexpr auto operator/=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator|=(Strong< T1, Tags... > &a, T2 b)
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.
constexpr auto operator<<=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator&=(Strong< T1, Tags... > &a, T2 b)
typename detail::wrapped_type_helper< std::remove_cvref_t< T > >::type strong_type_wrapped_type
Extracts the wrapped type from a strong type.
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
std::vector< uint8_t, Alloc > & operator^=(std::vector< uint8_t, Alloc > &out, const std::vector< uint8_t, Alloc2 > &in)
constexpr decltype(auto) wrap_strong_type(ParamT &&t)
Wraps a value into a caller-defined (strong) type.
constexpr bool is_strong_span_v
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
int operator>>(int fd, Pipe &pipe)
constexpr auto operator-=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator--(Strong< T, Tags... > &a, int)
auto operator<=>(const Strong< T, Tags... > &lhs, const Strong< T, Tags... > &rhs)
ECIES_Flags operator&(ECIES_Flags a, ECIES_Flags b)
constexpr auto operator*=(Strong< T1, Tags... > &a, T2 b)
typename T::wrapped_type type