9#ifndef BOTAN_STRONG_TYPE_H_
10#define BOTAN_STRONG_TYPE_H_
12#include <botan/concepts.h>
19template <
typename T,
typename Tag,
typename... Capabilities>
22template <
typename... Ts>
25template <
typename... Ts>
28template <
typename... Ts>
34concept streamable =
requires(std::ostream& os, T a) { os << a; };
48template <
typename T,
typename Capability>
64template <
typename CapabilityT,
typename... Tags>
83 constexpr explicit
Strong_Base(T v) : m_value(std::move(v)) {}
85 constexpr T&
get() & {
return m_value; }
87 constexpr const T&
get() const& {
return m_value; }
89 constexpr T&&
get() && {
return std::move(m_value); }
91 constexpr const T&&
get() const&& {
return std::move(m_value); }
100template <std::
integral T>
106template <concepts::container T>
121 template <
typename InputIt>
125 decltype(
auto)
begin() noexcept(noexcept(this->
get().
begin())) {
return this->
get().begin(); }
127 decltype(
auto)
begin()
const noexcept(
noexcept(this->
get().begin())) {
return this->
get().begin(); }
129 decltype(
auto)
end()
noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
131 decltype(
auto)
end()
const noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
133 decltype(
auto)
cbegin()
noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
135 decltype(
auto)
cbegin()
const noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
137 decltype(
auto)
cend()
noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
139 decltype(
auto)
cend()
const noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
146 return this->
get().empty();
161 template <
typename U>
162 decltype(
auto)
operator[](U&& i)
const noexcept(
noexcept(this->
get().operator[](i))) {
163 return this->
get()[std::forward<U>(i)];
166 template <
typename U>
167 decltype(
auto)
operator[](U&& i)
noexcept(
noexcept(this->
get().operator[](i))) {
168 return this->
get()[std::forward<U>(i)];
172template <concepts::container T>
173class Strong_Adapter<T> :
public Container_Strong_Adapter_Base<T> {
175 using Container_Strong_Adapter_Base<T>::Container_Strong_Adapter_Base;
178template <concepts::contiguous_container T>
179class Strong_Adapter<T> :
public Container_Strong_Adapter_Base<T> {
194 requires(std::same_as<T, std::string>)
198 decltype(
auto)
data() noexcept(noexcept(this->
get().
data())) {
return this->
get().data(); }
200 decltype(
auto)
data()
const noexcept(
noexcept(this->
get().data())) {
return this->
get().data(); }
216template <
typename T,
typename TagTypeT,
typename... Capabilities>
221 template <
typename CapabilityT>
223 return (std::is_same_v<CapabilityT, Capabilities> || ...);
227 using Tag = TagTypeT;
246 return std::forward<T>(t);
249 return std::forward<T>(t).get();
265template <
typename T,
typename ParamT>
266 requires std::constructible_from<T, ParamT> ||
267 (concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>)
269 if constexpr(std::same_as<std::remove_cvref_t<ParamT>, T>) {
271 return std::forward<ParamT>(t);
272 }
else if constexpr(std::constructible_from<T, ParamT>) {
274 return T{std::forward<ParamT>(t)};
279 return T{
typename T::wrapped_type{std::forward<ParamT>(t)}};
290template <concepts::strong_type T>
292 using type =
typename T::wrapped_type;
309template <
typename T,
typename... Tags>
312 return os << v.
get();
315template <
typename T,
typename... Tags>
316 requires(std::equality_comparable<T>)
318 return lhs.
get() == rhs.
get();
321template <
typename T,
typename... Tags>
322 requires(std::three_way_comparable<T>)
324 return lhs.
get() <=> rhs.
get();
327template <std::integral T1, std::integral T2,
typename... Tags>
329 return a <=> b.
get();
332template <std::integral T1, std::integral T2,
typename... Tags>
334 return a.
get() <=> b;
337template <std::integral T1, std::integral T2,
typename... Tags>
342template <std::integral T1, std::integral T2,
typename... Tags>
347template <std::integral T1, std::integral T2,
typename... Tags>
353template <std::integral T1, std::integral T2,
typename... Tags>
359template <std::integral T,
typename... Tags>
364template <std::integral T1, std::integral T2,
typename... Tags>
370template <std::integral T1, std::integral T2,
typename... Tags>
376template <std::integral T,
typename... Tags>
381template <std::integral T1, std::integral T2,
typename... Tags>
387template <std::integral T1, std::integral T2,
typename... Tags>
393template <std::integral T,
typename... Tags>
398template <std::integral T1, std::integral T2,
typename... Tags>
404template <std::integral T1, std::integral T2,
typename... Tags>
410template <std::integral T,
typename... Tags>
415template <std::integral T1, std::integral T2,
typename... Tags>
421template <std::integral T1, std::integral T2,
typename... Tags>
427template <std::integral T,
typename... Tags>
432template <std::integral T1, std::integral T2,
typename... Tags>
438template <std::integral T1, std::integral T2,
typename... Tags>
444template <std::integral T,
typename... Tags>
449template <std::integral T1, std::integral T2,
typename... Tags>
455template <std::integral T1, std::integral T2,
typename... Tags>
461template <std::integral T,
typename... Tags>
466template <std::integral T1, std::integral T2,
typename... Tags>
469 return Strong<T2, Tags...>(a >> b.
get());
472template <std::integral T1, std::integral T2,
typename... Tags>
475 return Strong<T1, Tags...>(a.
get() >> b);
478template <std::integral T,
typename... Tags>
483template <std::integral T1, std::integral T2,
typename... Tags>
486 return Strong<T2, Tags...>(a << b.
get());
489template <std::integral T1, std::integral T2,
typename... Tags>
492 return Strong<T1, Tags...>(a.
get() << b);
495template <std::integral T,
typename... Tags>
500template <std::integral T1, std::integral T2,
typename... Tags>
507template <std::integral T,
typename... Tags>
513template <std::integral T1, std::integral T2,
typename... Tags>
520template <std::integral T,
typename... Tags>
526template <std::integral T1, std::integral T2,
typename... Tags>
533template <std::integral T,
typename... Tags>
539template <std::integral T1, std::integral T2,
typename... Tags>
546template <std::integral T,
typename... Tags>
552template <std::integral T1, std::integral T2,
typename... Tags>
559template <std::integral T,
typename... Tags>
565template <std::integral T1, std::integral T2,
typename... Tags>
572template <std::integral T,
typename... Tags>
578template <std::integral T1, std::integral T2,
typename... Tags>
585template <std::integral T,
typename... Tags>
591template <std::integral T1, std::integral T2,
typename... Tags>
598template <std::integral T,
typename... Tags>
604template <std::integral T1, std::integral T2,
typename... Tags>
611template <std::integral T,
typename... Tags>
617template <std::integral T,
typename... Tags>
624template <std::integral T,
typename... Tags>
630template <std::integral T,
typename... Tags>
637template <std::integral T,
typename... Tags>
658template <concepts::contiguous_strong_type T>
660 using underlying_span = std::
661 conditional_t<std::is_const_v<T>, std::span<const typename T::value_type>, std::span<typename T::value_type>>;
666 using iterator =
typename underlying_span::iterator;
667 using pointer =
typename underlying_span::pointer;
687 template <concepts::contiguous_strong_type T2>
690 requires(std::is_same_v<T2, std::remove_const_t<T>>)
691 : m_span(other.
get()) {}
703 underlying_span
get()
const {
return m_span; }
708 underlying_span
get() {
return m_span; }
710 decltype(
auto)
data()
noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
712 decltype(
auto)
data()
const noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
714 decltype(
auto)
size()
const noexcept(
noexcept(this->m_span.size())) {
return this->m_span.size(); }
716 bool empty() const noexcept(noexcept(this->m_span.
empty())) {
return this->m_span.empty(); }
718 decltype(
auto)
begin() noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
720 decltype(
auto)
begin() const noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
722 decltype(
auto)
end() noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
724 decltype(
auto)
end() const noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
726 decltype(
auto)
operator[](
typename underlying_span::size_type i)
const noexcept {
return this->m_span[i]; }
729 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) 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) 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 bool is_strong_type_v
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