9#ifndef BOTAN_STRONG_TYPE_H_
10#define BOTAN_STRONG_TYPE_H_
15#include <botan/concepts.h>
30template <
typename CapabilityT,
typename... Tags>
50 constexpr T&
get() & {
return m_value; }
52 constexpr const T&
get() const& {
return m_value; }
54 constexpr T&&
get() && {
return std::move(m_value); }
56 constexpr const T&&
get() const&& {
return std::move(m_value); }
65template <std::
integral T>
71template <concepts::container T>
86 template <
typename InputIt>
90 decltype(
auto)
begin() noexcept(noexcept(this->
get().
begin())) {
return this->
get().begin(); }
92 decltype(
auto)
begin()
const noexcept(
noexcept(this->
get().begin())) {
return this->
get().begin(); }
94 decltype(
auto)
end()
noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
96 decltype(
auto)
end()
const noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
98 decltype(
auto)
cbegin()
noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
100 decltype(
auto)
cbegin()
const noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
102 decltype(
auto)
cend()
noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
104 decltype(
auto)
cend()
const noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
109 requires(concepts::has_empty<
T>)
111 return this->
get().empty();
126 template <
typename U>
127 decltype(
auto)
operator[](U&& i)
const noexcept(
noexcept(this->
get().operator[](i))) {
128 return this->
get()[std::forward<U>(i)];
127 decltype(
auto)
operator[](U&& i)
const noexcept(
noexcept(this->
get().operator[](i))) {
…}
131 template <
typename U>
132 decltype(
auto)
operator[](U&& i)
noexcept(
noexcept(this->
get().operator[](i))) {
133 return this->
get()[std::forward<U>(i)];
132 decltype(
auto)
operator[](U&& i)
noexcept(
noexcept(this->
get().operator[](i))) {
…}
136 template <
typename U>
137 decltype(
auto)
at(U&& i)
const noexcept(
noexcept(this->
get().at(i)))
140 return this->
get().at(std::forward<U>(i));
137 decltype(
auto)
at(U&& i)
const noexcept(
noexcept(this->
get().at(i))) {
…}
143 template <
typename U>
144 decltype(
auto)
at(U&& i)
noexcept(
noexcept(this->
get().at(i)))
147 return this->
get().at(std::forward<U>(i));
144 decltype(
auto)
at(U&& i)
noexcept(
noexcept(this->
get().at(i))) {
…}
151template <concepts::container T>
152class Strong_Adapter<
T> :
public Container_Strong_Adapter_Base<T> {
154 using Container_Strong_Adapter_Base<
T>::Container_Strong_Adapter_Base;
157template <concepts::contiguous_container T>
173 requires(std::same_as<T, std::string>)
177 decltype(
auto)
data() noexcept(noexcept(this->
get().data())) {
return this->
get().data(); }
179 decltype(
auto)
data()
const noexcept(
noexcept(this->
get().data())) {
return this->
get().data(); }
195template <
typename T,
typename TagTypeT,
typename... Capabilities>
200 template <
typename CapabilityT>
202 return (std::is_same_v<CapabilityT, Capabilities> || ...);
206 using Tag = TagTypeT;
225 return std::forward<T>(t);
228 return std::forward<T>(t).get();
244template <
typename T,
typename ParamT>
245 requires std::constructible_from<T, ParamT> ||
246 (concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>)
248 if constexpr(std::same_as<std::remove_cvref_t<ParamT>,
T>) {
250 return std::forward<ParamT>(t);
251 }
else if constexpr(std::constructible_from<T, ParamT>) {
253 return T{std::forward<ParamT>(t)};
258 return T{
typename T::wrapped_type{std::forward<ParamT>(t)}};
269template <concepts::strong_type T>
271 using type =
typename T::wrapped_type;
288template <
typename T,
typename... Tags>
291 return os << v.
get();
294template <
typename T,
typename... Tags>
295 requires(std::equality_comparable<T>)
297 return lhs.
get() == rhs.
get();
300template <
typename T,
typename... Tags>
301 requires(std::three_way_comparable<T>)
303 return lhs.
get() <=> rhs.
get();
306template <std::integral T1, std::integral T2,
typename... Tags>
308 return a <=>
b.get();
311template <std::integral T1, std::integral T2,
typename... Tags>
313 return a.
get() <=>
b;
316template <std::integral T1, std::integral T2,
typename... Tags>
321template <std::integral T1, std::integral T2,
typename... Tags>
326template <std::integral T1, std::integral T2,
typename... Tags>
329 return Strong<T2, Tags...>(a +
b.get());
332template <std::integral T1, std::integral T2,
typename... Tags>
338template <std::integral
T,
typename... Tags>
343template <std::integral T1, std::integral T2,
typename... Tags>
346 return Strong<T2, Tags...>(a -
b.get());
349template <std::integral T1, std::integral T2,
typename... Tags>
355template <std::integral
T,
typename... Tags>
360template <std::integral T1, std::integral T2,
typename... Tags>
363 return Strong<T2, Tags...>(a *
b.get());
366template <std::integral T1, std::integral T2,
typename... Tags>
372template <std::integral
T,
typename... Tags>
377template <std::integral T1, std::integral T2,
typename... Tags>
380 return Strong<T2, Tags...>(a /
b.get());
383template <std::integral T1, std::integral T2,
typename... Tags>
389template <std::integral
T,
typename... Tags>
394template <std::integral T1, std::integral T2,
typename... Tags>
397 return Strong<T2, Tags...>(a ^
b.get());
400template <std::integral T1, std::integral T2,
typename... Tags>
406template <std::integral
T,
typename... Tags>
411template <std::integral T1, std::integral T2,
typename... Tags>
414 return Strong<T2, Tags...>(a &
b.get());
417template <std::integral T1, std::integral T2,
typename... Tags>
423template <std::integral
T,
typename... Tags>
428template <std::integral T1, std::integral T2,
typename... Tags>
431 return Strong<T2, Tags...>(a |
b.get());
434template <std::integral T1, std::integral T2,
typename... Tags>
440template <std::integral
T,
typename... Tags>
445template <std::integral T1, std::integral T2,
typename... Tags>
448 return Strong<T2, Tags...>(a >>
b.get());
451template <std::integral T1, std::integral T2,
typename... Tags>
457template <std::integral
T,
typename... Tags>
462template <std::integral T1, std::integral T2,
typename... Tags>
465 return Strong<T2, Tags...>(a <<
b.get());
468template <std::integral T1, std::integral T2,
typename... Tags>
474template <std::integral
T,
typename... Tags>
479template <std::integral T1, std::integral T2,
typename... Tags>
486template <std::integral
T,
typename... Tags>
492template <std::integral T1, std::integral T2,
typename... Tags>
499template <std::integral
T,
typename... Tags>
505template <std::integral T1, std::integral T2,
typename... Tags>
512template <std::integral
T,
typename... Tags>
518template <std::integral T1, std::integral T2,
typename... Tags>
525template <std::integral
T,
typename... Tags>
531template <std::integral T1, std::integral T2,
typename... Tags>
538template <std::integral
T,
typename... Tags>
544template <std::integral T1, std::integral T2,
typename... Tags>
551template <std::integral
T,
typename... Tags>
557template <std::integral T1, std::integral T2,
typename... Tags>
564template <std::integral
T,
typename... Tags>
570template <std::integral T1, std::integral T2,
typename... Tags>
577template <std::integral
T,
typename... Tags>
583template <std::integral T1, std::integral T2,
typename... Tags>
590template <std::integral
T,
typename... Tags>
596template <std::integral
T,
typename... Tags>
603template <std::integral
T,
typename... Tags>
609template <std::integral
T,
typename... Tags>
616template <std::integral
T,
typename... Tags>
637template <concepts::contiguous_strong_type T>
639 using underlying_span = std::
640 conditional_t<std::is_const_v<T>, std::span<const typename T::value_type>, std::span<typename T::value_type>>;
645 using iterator =
typename underlying_span::iterator;
646 using pointer =
typename underlying_span::pointer;
666 typename = std::enable_if_t<std::is_same_v<T2, std::remove_const_t<T>>>>
676 underlying_span
get()
const {
return m_span; }
681 underlying_span
get() {
return m_span; }
683 decltype(
auto)
data()
noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
685 decltype(
auto)
data()
const noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
687 decltype(
auto)
size()
const noexcept(
noexcept(this->m_span.size())) {
return this->m_span.size(); }
689 bool empty() const noexcept(noexcept(this->m_span.
empty())) {
return this->m_span.empty(); }
691 decltype(
auto)
begin() noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
693 decltype(
auto)
begin() const noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
695 decltype(
auto)
end() noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
697 decltype(
auto)
end() const noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
699 decltype(
auto)
operator[](
typename underlying_span::size_type i)
const noexcept {
return this->m_span[i]; }
702 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
StrongSpan(const StrongSpan< T2 > &other)
typename underlying_span::value_type value_type
decltype(auto) begin() const noexcept(noexcept(this->m_span.begin()))
StrongSpan(const StrongSpan &other)=default
decltype(auto) end() noexcept(noexcept(this->m_span.end()))
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()))
typename underlying_span::iterator iterator
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