Botan 3.14.1
Crypto and TLS for C&
strong_type_helpers.h
Go to the documentation of this file.
1/**
2 * A bunch of helpers to allow considering the Botan-internal strong types in
3 * public APIs without depending on the de-facto internal strong_type.h header.
4 *
5 * Users of the library should not depend on any of the facilities provided in
6 * this header. This is meant to be a bridge to the internal Strong<> template.
7 *
8 * (C) 2022,2026 Jack Lloyd
9 * 2022 René Meusel - Rohde & Schwarz Cybersecurity
10 * 2026 René Meusel - Rohde & Schwarz Networks & Cybersecurity
11 *
12 * Botan is released under the Simplified BSD License (see license.txt)
13 */
14
15#ifndef BOTAN_STRONG_TYPE_HELPERS_H_
16#define BOTAN_STRONG_TYPE_HELPERS_H_
17
18#include <botan/concepts.h>
19#include <concepts>
20#include <type_traits>
21#include <utility>
22
23namespace Botan {
24
25template <typename T, typename Tag, typename... Capabilities>
26class Strong;
27
28/**
29 * Trait that detects whether the given types are a Strong<> instantiation
30 */
31template <typename... Ts>
32struct is_strong_type : std::false_type {};
33
34/// @copydoc is_strong_type
35template <typename... Ts>
36struct is_strong_type<Strong<Ts...>> : std::true_type {};
37
38template <typename... Ts>
40
41namespace concepts {
42
43template <class T>
45
46template <class T>
48
49template <class T>
50concept integral_strong_type = strong_type<T> && std::integral<typename T::wrapped_type>;
51
52template <class T>
53concept unsigned_integral_strong_type = strong_type<T> && std::unsigned_integral<typename T::wrapped_type>;
54
55} // namespace concepts
56
57template <concepts::contiguous_strong_type T>
58class StrongSpan;
59
60/**
61 * Trait that detects whether the given type is a StrongSpan<> instantiation
62 */
63template <typename>
64struct is_strong_span : std::false_type {};
65
66/// @copydoc is_strong_span
67template <typename T>
68struct is_strong_span<StrongSpan<T>> : std::true_type {};
69
70template <typename T>
72
73namespace detail {
74
75/**
76 * Resolves to the type wrapped by a strong type, or to T itself if T is
77 * not a strong type
78 */
79template <typename T>
81 /// The resolved type
82 using type = T;
83};
84
85/// @copydoc wrapped_type_helper
86template <concepts::strong_type T>
88 /// The resolved type
89 using type = typename T::wrapped_type;
90};
91
92} // namespace detail
93
94/**
95 * @brief Extracts the wrapped type from a strong type.
96 *
97 * If the provided type is not a strong type, it is returned as is.
98 *
99 * @note This is meant as a helper for generic code that needs to deal with both
100 * wrapped strong types and bare objects. Use the ordinary `::wrapped_type`
101 * declaration if you know that you are dealing with a strong type.
102 */
103template <typename T>
105
106/**
107 * @brief Generically unwraps a strong type to its underlying type.
108 *
109 * If the provided type is not a strong type, it is returned as is.
110 *
111 * @note This is meant as a helper for generic code that needs to deal with both
112 * wrapped strong types and bare objects. Use the ordinary `get()` method
113 * if you know that you are dealing with a strong type.
114 *
115 * @param t value to be unwrapped
116 * @return the unwrapped value
117 */
118template <typename T>
119[[nodiscard]] constexpr decltype(auto) unwrap_strong_type(T&& t) {
121 // If the parameter type isn't a strong type, return it as is.
122 return std::forward<T>(t);
123 } else {
124 // Unwrap the strong type and return the underlying value.
125 return std::forward<T>(t).get();
126 }
127}
128
129/**
130 * @brief Wraps a value into a caller-defined (strong) type.
131 *
132 * If the provided object @p t is already of type @p T, it is returned as is.
133 *
134 * @note This is meant as a helper for generic code that needs to deal with both
135 * wrapped strong types and bare objects. Use the ordinary constructor if
136 * you know that you are dealing with a bare value type.
137 *
138 * @param t value to be wrapped
139 * @return the wrapped value
140 */
141template <typename T, typename ParamT>
142 requires std::constructible_from<T, ParamT> ||
143 (concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>)
144[[nodiscard]] constexpr decltype(auto) wrap_strong_type(ParamT&& t) {
145 if constexpr(std::same_as<std::remove_cvref_t<ParamT>, T>) {
146 // Noop, if the parameter type already is the desired return type.
147 return std::forward<ParamT>(t);
148 } else if constexpr(std::constructible_from<T, ParamT>) {
149 // Implicit conversion from the parameter type to the return type.
150 return T{std::forward<ParamT>(t)};
151 } else {
152 // Explicitly calling the wrapped type's constructor to support
153 // implicit conversions on types that mark their constructors as explicit.
154 static_assert(concepts::strong_type<T> && std::constructible_from<typename T::wrapped_type, ParamT>);
155 return T{typename T::wrapped_type{std::forward<ParamT>(t)}};
156 }
157}
158
159} // namespace Botan
160
161#endif
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.
typename detail::wrapped_type_helper< std::remove_cvref_t< T > >::type strong_type_wrapped_type
Extracts the wrapped type from a strong type.
constexpr decltype(auto) wrap_strong_type(ParamT &&t)
Wraps a value into a caller-defined (strong) type.
constexpr bool is_strong_span_v
constexpr bool is_strong_type_v
typename T::wrapped_type type
The resolved type.