9#ifndef BOTAN_STRONG_TYPE_H_
10#define BOTAN_STRONG_TYPE_H_
12#include <botan/concepts.h>
31template <
typename CapabilityT,
typename... Tags>
49 constexpr explicit
Strong_Base(T v) : m_value(std::move(v)) {}
51 constexpr T&
get() & {
return m_value; }
53 constexpr const T&
get() const& {
return m_value; }
55 constexpr T&&
get() && {
return std::move(m_value); }
57 constexpr const T&&
get() const&& {
return std::move(m_value); }
66template <std::
integral T>
72template <concepts::container T>
87 template <
typename InputIt>
91 decltype(
auto)
begin() noexcept(noexcept(this->
get().
begin())) {
return this->
get().begin(); }
93 decltype(
auto)
begin()
const noexcept(
noexcept(this->
get().begin())) {
return this->
get().begin(); }
95 decltype(
auto)
end()
noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
97 decltype(
auto)
end()
const noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
99 decltype(
auto)
cbegin()
noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
101 decltype(
auto)
cbegin()
const noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
103 decltype(
auto)
cend()
noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
105 decltype(
auto)
cend()
const noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
112 return this->
get().empty();
127 template <
typename U>
128 decltype(
auto)
operator[](U&& i)
const noexcept(
noexcept(this->
get().operator[](i))) {
129 return this->
get()[std::forward<U>(i)];
132 template <
typename U>
133 decltype(
auto)
operator[](U&& i)
noexcept(
noexcept(this->
get().operator[](i))) {
134 return this->
get()[std::forward<U>(i)];
137 template <
typename U>
138 decltype(
auto)
at(U&& i)
const noexcept(
noexcept(this->
get().at(i)))
141 return this->
get().at(std::forward<U>(i));
144 template <
typename U>
145 decltype(
auto)
at(U&& i)
noexcept(
noexcept(this->
get().at(i)))
148 return this->
get().at(std::forward<U>(i));
152template <concepts::container T>
153class Strong_Adapter<T> :
public Container_Strong_Adapter_Base<T> {
155 using Container_Strong_Adapter_Base<T>::Container_Strong_Adapter_Base;
158template <concepts::contiguous_container T>
159class Strong_Adapter<T> :
public Container_Strong_Adapter_Base<T> {
174 requires(std::same_as<T, std::string>)
178 decltype(
auto)
data() noexcept(noexcept(this->
get().
data())) {
return this->
get().data(); }
180 decltype(
auto)
data()
const noexcept(
noexcept(this->
get().data())) {
return this->
get().data(); }
196template <
typename T,
typename TagTypeT,
typename... Capabilities>
201 template <
typename CapabilityT>
203 return (std::is_same_v<CapabilityT, Capabilities> || ...);
207 using Tag = TagTypeT;
226 return std::forward<T>(t);
229 return std::forward<T>(t).get();
245template <
typename T,
typename ParamT>
246 requires std::constructible_from<T, ParamT> ||
247 (concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>)
249 if constexpr(std::same_as<std::remove_cvref_t<ParamT>, T>) {
251 return std::forward<ParamT>(t);
252 }
else if constexpr(std::constructible_from<T, ParamT>) {
254 return T{std::forward<ParamT>(t)};
259 return T{
typename T::wrapped_type{std::forward<ParamT>(t)}};
270template <concepts::strong_type T>
272 using type =
typename T::wrapped_type;
289template <
typename T,
typename... Tags>
292 return os << v.
get();
295template <
typename T,
typename... Tags>
296 requires(std::equality_comparable<T>)
298 return lhs.
get() == rhs.
get();
301template <
typename T,
typename... Tags>
302 requires(std::three_way_comparable<T>)
304 return lhs.
get() <=> rhs.
get();
307template <std::integral T1, std::integral T2,
typename... Tags>
309 return a <=> b.
get();
312template <std::integral T1, std::integral T2,
typename... Tags>
314 return a.
get() <=> b;
317template <std::integral T1, std::integral T2,
typename... Tags>
322template <std::integral T1, std::integral T2,
typename... Tags>
327template <std::integral T1, std::integral T2,
typename... Tags>
333template <std::integral T1, std::integral T2,
typename... Tags>
339template <std::integral T,
typename... Tags>
344template <std::integral T1, std::integral T2,
typename... Tags>
350template <std::integral T1, std::integral T2,
typename... Tags>
356template <std::integral T,
typename... Tags>
361template <std::integral T1, std::integral T2,
typename... Tags>
367template <std::integral T1, std::integral T2,
typename... Tags>
373template <std::integral T,
typename... Tags>
378template <std::integral T1, std::integral T2,
typename... Tags>
384template <std::integral T1, std::integral T2,
typename... Tags>
390template <std::integral T,
typename... Tags>
395template <std::integral T1, std::integral T2,
typename... Tags>
401template <std::integral T1, std::integral T2,
typename... Tags>
407template <std::integral T,
typename... Tags>
412template <std::integral T1, std::integral T2,
typename... Tags>
418template <std::integral T1, std::integral T2,
typename... Tags>
424template <std::integral T,
typename... Tags>
429template <std::integral T1, std::integral T2,
typename... Tags>
435template <std::integral T1, std::integral T2,
typename... Tags>
441template <std::integral T,
typename... Tags>
446template <std::integral T1, std::integral T2,
typename... Tags>
449 return Strong<T2, Tags...>(a >> b.
get());
452template <std::integral T1, std::integral T2,
typename... Tags>
455 return Strong<T1, Tags...>(a.
get() >> b);
458template <std::integral T,
typename... Tags>
463template <std::integral T1, std::integral T2,
typename... Tags>
466 return Strong<T2, Tags...>(a << b.
get());
469template <std::integral T1, std::integral T2,
typename... Tags>
472 return Strong<T1, Tags...>(a.
get() << b);
475template <std::integral T,
typename... Tags>
480template <std::integral T1, std::integral T2,
typename... Tags>
487template <std::integral T,
typename... Tags>
493template <std::integral T1, std::integral T2,
typename... Tags>
500template <std::integral T,
typename... Tags>
506template <std::integral T1, std::integral T2,
typename... Tags>
513template <std::integral T,
typename... Tags>
519template <std::integral T1, std::integral T2,
typename... Tags>
526template <std::integral T,
typename... Tags>
532template <std::integral T1, std::integral T2,
typename... Tags>
539template <std::integral T,
typename... Tags>
545template <std::integral T1, std::integral T2,
typename... Tags>
552template <std::integral T,
typename... Tags>
558template <std::integral T1, std::integral T2,
typename... Tags>
565template <std::integral T,
typename... Tags>
571template <std::integral T1, std::integral T2,
typename... Tags>
578template <std::integral T,
typename... Tags>
584template <std::integral T1, std::integral T2,
typename... Tags>
591template <std::integral T,
typename... Tags>
597template <std::integral T,
typename... Tags>
604template <std::integral T,
typename... Tags>
610template <std::integral T,
typename... Tags>
617template <std::integral T,
typename... Tags>
638template <concepts::contiguous_strong_type T>
640 using underlying_span = std::
641 conditional_t<std::is_const_v<T>, std::span<const typename T::value_type>, std::span<typename T::value_type>>;
646 using iterator =
typename underlying_span::iterator;
647 using pointer =
typename underlying_span::pointer;
667 typename = std::enable_if_t<std::is_same_v<T2, std::remove_const_t<T>>>>
677 underlying_span
get()
const {
return m_span; }
682 underlying_span
get() {
return m_span; }
684 decltype(
auto)
data()
noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
686 decltype(
auto)
data()
const noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
688 decltype(
auto)
size()
const noexcept(
noexcept(this->m_span.size())) {
return this->m_span.size(); }
690 bool empty() const noexcept(noexcept(this->m_span.
empty())) {
return this->m_span.empty(); }
692 decltype(
auto)
begin() noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
694 decltype(
auto)
begin() const noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
696 decltype(
auto)
end() noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
698 decltype(
auto)
end() const noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
700 decltype(
auto)
operator[](
typename underlying_span::size_type i)
const noexcept {
return this->m_span[i]; }
703 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