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>
92 template <
typename InputIt>
99 requires(std::same_as<T, std::string>)
103 decltype(
auto)
begin() noexcept(noexcept(this->
get().begin())) {
return this->
get().begin(); }
105 decltype(
auto)
begin()
const noexcept(
noexcept(this->
get().begin())) {
return this->
get().begin(); }
107 decltype(
auto)
end()
noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
109 decltype(
auto)
end()
const noexcept(
noexcept(this->
get().end())) {
return this->
get().end(); }
111 decltype(
auto)
cbegin()
noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
113 decltype(
auto)
cbegin()
const noexcept(
noexcept(this->
get().cbegin())) {
return this->
get().cbegin(); }
115 decltype(
auto)
cend()
noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
117 decltype(
auto)
cend()
const noexcept(
noexcept(this->
get().cend())) {
return this->
get().cend(); }
121 decltype(
auto)
data()
noexcept(
noexcept(this->
get().data()))
124 return this->
get().data();
127 decltype(
auto)
data()
const noexcept(
noexcept(this->
get().data()))
130 return this->
get().data();
133 bool empty() const noexcept(noexcept(this->
get().empty()))
134 requires(concepts::has_empty<
T>)
136 return this->
get().empty();
142 this->
get().resize(size);
148 this->
get().reserve(size);
151 decltype(
auto)
operator[](
size_type i)
const noexcept(
noexcept(this->
get().operator[](i))) {
152 return this->
get()[i];
155 decltype(
auto)
operator[](
size_type i)
noexcept(
noexcept(this->
get().operator[](i))) {
return this->
get()[i]; }
171template <
typename T,
typename TagTypeT,
typename... Capabilities>
176 template <
typename CapabilityT>
178 return (std::is_same_v<CapabilityT, Capabilities> || ...);
182 using Tag = TagTypeT;
201 return std::forward<T>(t);
204 return std::forward<T>(t).get();
220template <
typename T,
typename ParamT>
221 requires std::constructible_from<T, ParamT> ||
222 (concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>)
224 if constexpr(std::same_as<std::remove_cvref_t<ParamT>,
T>) {
226 return std::forward<ParamT>(t);
227 }
else if constexpr(std::constructible_from<T, ParamT>) {
229 return T{std::forward<ParamT>(t)};
234 return T{
typename T::wrapped_type{std::forward<ParamT>(t)}};
245template <concepts::strong_type T>
247 using type =
typename T::wrapped_type;
264template <
typename T,
typename... Tags>
267 return os << v.
get();
270template <
typename T,
typename... Tags>
271 requires(std::equality_comparable<T>)
273 return lhs.
get() == rhs.
get();
276template <
typename T,
typename... Tags>
277 requires(std::three_way_comparable<T>)
279 return lhs.
get() <=> rhs.
get();
282template <std::integral T1, std::integral T2,
typename... Tags>
284 return a <=>
b.get();
287template <std::integral T1, std::integral T2,
typename... Tags>
289 return a.
get() <=>
b;
292template <std::integral T1, std::integral T2,
typename... Tags>
297template <std::integral T1, std::integral T2,
typename... Tags>
302template <std::integral T1, std::integral T2,
typename... Tags>
305 return Strong<T2, Tags...>(a +
b.get());
308template <std::integral T1, std::integral T2,
typename... Tags>
314template <std::integral
T,
typename... Tags>
319template <std::integral T1, std::integral T2,
typename... Tags>
322 return Strong<T2, Tags...>(a -
b.get());
325template <std::integral T1, std::integral T2,
typename... Tags>
331template <std::integral
T,
typename... Tags>
336template <std::integral T1, std::integral T2,
typename... Tags>
339 return Strong<T2, Tags...>(a *
b.get());
342template <std::integral T1, std::integral T2,
typename... Tags>
348template <std::integral
T,
typename... Tags>
353template <std::integral T1, std::integral T2,
typename... Tags>
356 return Strong<T2, Tags...>(a /
b.get());
359template <std::integral T1, std::integral T2,
typename... Tags>
365template <std::integral
T,
typename... Tags>
370template <std::integral T1, std::integral T2,
typename... Tags>
373 return Strong<T2, Tags...>(a ^
b.get());
376template <std::integral T1, std::integral T2,
typename... Tags>
382template <std::integral
T,
typename... Tags>
387template <std::integral T1, std::integral T2,
typename... Tags>
390 return Strong<T2, Tags...>(a &
b.get());
393template <std::integral T1, std::integral T2,
typename... Tags>
399template <std::integral
T,
typename... Tags>
404template <std::integral T1, std::integral T2,
typename... Tags>
407 return Strong<T2, Tags...>(a |
b.get());
410template <std::integral T1, std::integral T2,
typename... Tags>
416template <std::integral
T,
typename... Tags>
421template <std::integral T1, std::integral T2,
typename... Tags>
424 return Strong<T2, Tags...>(a >>
b.get());
427template <std::integral T1, std::integral T2,
typename... Tags>
433template <std::integral
T,
typename... Tags>
438template <std::integral T1, std::integral T2,
typename... Tags>
441 return Strong<T2, Tags...>(a <<
b.get());
444template <std::integral T1, std::integral T2,
typename... Tags>
450template <std::integral
T,
typename... Tags>
455template <std::integral T1, std::integral T2,
typename... Tags>
462template <std::integral
T,
typename... Tags>
468template <std::integral T1, std::integral T2,
typename... Tags>
475template <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
T,
typename... Tags>
579template <std::integral
T,
typename... Tags>
585template <std::integral
T,
typename... Tags>
592template <std::integral
T,
typename... Tags>
613template <concepts::contiguous_strong_type T>
615 using underlying_span = std::
616 conditional_t<std::is_const_v<T>, std::span<const typename T::value_type>, std::span<typename T::value_type>>;
621 using iterator =
typename underlying_span::iterator;
622 using pointer =
typename underlying_span::pointer;
642 typename = std::enable_if_t<std::is_same_v<T2, std::remove_const_t<T>>>>
652 underlying_span
get()
const {
return m_span; }
657 underlying_span
get() {
return m_span; }
659 decltype(
auto)
data()
noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
661 decltype(
auto)
data()
const noexcept(
noexcept(this->m_span.data())) {
return this->m_span.data(); }
663 decltype(
auto)
size()
const noexcept(
noexcept(this->m_span.size())) {
return this->m_span.size(); }
665 bool empty() const noexcept(noexcept(this->m_span.
empty())) {
return this->m_span.empty(); }
667 decltype(
auto)
begin() noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
669 decltype(
auto)
begin() const noexcept(noexcept(this->m_span.
begin())) {
return this->m_span.begin(); }
671 decltype(
auto)
end() noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
673 decltype(
auto)
end() const noexcept(noexcept(this->m_span.
end())) {
return this->m_span.end(); }
675 decltype(
auto)
operator[](
typename underlying_span::size_type i)
const noexcept {
return this->m_span[i]; }
678 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()
decltype(auto) data() const noexcept(noexcept(this->get().data()))
typename T::size_type size_type
Strong_Adapter(const char *str)
decltype(auto) end() const noexcept(noexcept(this->get().end()))
Strong_Adapter(size_t size)
void resize(size_type size) noexcept(noexcept(this->get().resize(size)))
typename T::iterator iterator
void reserve(size_type size) noexcept(noexcept(this->get().reserve(size)))
decltype(auto) begin() noexcept(noexcept(this->get().begin()))
size_type size() const noexcept(noexcept(this->get().size()))
decltype(auto) cbegin() noexcept(noexcept(this->get().cbegin()))
typename T::const_pointer const_pointer
typename T::pointer pointer
typename T::value_type value_type
decltype(auto) begin() const noexcept(noexcept(this->get().begin()))
decltype(auto) cend() const noexcept(noexcept(this->get().cend()))
decltype(auto) end() noexcept(noexcept(this->get().end()))
typename T::const_iterator const_iterator
bool empty() const noexcept(noexcept(this->get().empty()))
decltype(auto) data() noexcept(noexcept(this->get().data()))
Strong_Adapter(std::span< const value_type > span)
decltype(auto) cend() noexcept(noexcept(this->get().cend()))
decltype(auto) cbegin() const noexcept(noexcept(this->get().cbegin()))
Strong_Adapter(InputIt begin, InputIt end)
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