Botan 3.14.1
Crypto and TLS for C&
strong_type.h
Go to the documentation of this file.
1/**
2 * A wrapper class to implement strong types
3 * (C) 2022 Jack Lloyd
4 * 2022 René Meusel - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_STRONG_TYPE_H_
10#define BOTAN_STRONG_TYPE_H_
11
12#include <botan/concepts.h>
13#include <botan/strong_type_helpers.h>
14#include <iosfwd>
15#include <span>
16#include <string>
17
18namespace Botan {
19
20namespace concepts {
21
22template <typename T>
23concept streamable = requires(std::ostream& os, T a) { os << a; };
24template <typename T, typename Capability>
25concept strong_type_with_capability = T::template has_capability<Capability>();
26
27} // namespace concepts
28
29/**
30 * Added as an additional "capability tag" to enable arithmetic operators with
31 * plain numbers for Strong<> types that wrap a number.
32 */
34
35namespace detail {
36
37/**
38 * Checks whether the @p CapabilityT is included in the @p Tags type pack.
39 */
40template <typename CapabilityT, typename... Tags>
41constexpr bool has_capability = (std::is_same_v<CapabilityT, Tags> || ...);
42
43/**
44 * Storage for the wrapped value of a strong type, and access to it via get()
45 */
46template <typename T>
48 private:
49 T m_value;
50
51 public:
52 /// The type wrapped by this strong type
53 using wrapped_type = T;
54
55 public:
56 /// Default constructor, value initializes the wrapped value
57 Strong_Base() = default;
58
59 /// Copy constructor
60 Strong_Base(const Strong_Base&) = default;
61
62 /// Move constructor
63 Strong_Base(Strong_Base&&) noexcept = default;
64
65 /// Copy assignment
66 /// @return reference to this
67 Strong_Base& operator=(const Strong_Base&) = default;
68
69 /// Move assignment
70 /// @return reference to this
71 Strong_Base& operator=(Strong_Base&&) noexcept = default;
72
73 ~Strong_Base() = default;
74
75 /// Wrap the given value
76 /// @param v the value to wrap
77 constexpr explicit Strong_Base(T v) : m_value(std::move(v)) {}
78
79 /// Access the wrapped value
80 /// @return reference to the wrapped value
81 constexpr T& get() & { return m_value; }
82
83 /// Access the wrapped value
84 /// @return const reference to the wrapped value
85 constexpr const T& get() const& { return m_value; }
86
87 /// Access the wrapped value
88 /// @return rvalue reference to the wrapped value
89 constexpr T&& get() && { return std::move(m_value); }
90
91 /// Access the wrapped value
92 /// @return const rvalue reference to the wrapped value
93 constexpr const T&& get() const&& { return std::move(m_value); }
94};
95
96/**
97 * Adds functionality to Strong_Base depending on the wrapped type
98 *
99 * The primary template adds nothing; the specializations below expose
100 * container and contiguous container operations where applicable.
101 */
102template <typename T>
103class Strong_Adapter : public Strong_Base<T> {
104 public:
106};
107
108template <std::integral T>
109class Strong_Adapter<T> : public Strong_Base<T> {
110 public:
112};
113
114/**
115 * Forwards the container interface of the wrapped type
116 */
117template <concepts::container T>
119 public:
120 /// The element type of the wrapped container
121 using value_type = typename T::value_type;
122
123 /// The size type of the wrapped container
124 using size_type = typename T::size_type;
125
126 /// The iterator type of the wrapped container
127 using iterator = typename T::iterator;
128
129 /// The const iterator type of the wrapped container
130 using const_iterator = typename T::const_iterator;
131
132 public:
134
135 /// Create a container holding the given number of default constructed elements
136 /// @param size the number of elements
140
141 /// Create a container from the elements of an iterator range
142 /// @param begin start of the range
143 /// @param end one past the end of the range
144 template <typename InputIt>
146
147 public:
148 /// Iterate the wrapped container
149 /// @return an iterator to the first element
150 decltype(auto) begin() noexcept(noexcept(this->get().begin())) { return this->get().begin(); }
151
152 /// Iterate the wrapped container
153 /// @return a const iterator to the first element
154 decltype(auto) begin() const noexcept(noexcept(this->get().begin())) { return this->get().begin(); }
155
156 /// Iterate the wrapped container
157 /// @return an iterator one past the last element
158 decltype(auto) end() noexcept(noexcept(this->get().end())) { return this->get().end(); }
159
160 /// Iterate the wrapped container
161 /// @return a const iterator one past the last element
162 decltype(auto) end() const noexcept(noexcept(this->get().end())) { return this->get().end(); }
163
164 /// Iterate the wrapped container
165 /// @return a const iterator to the first element
166 decltype(auto) cbegin() noexcept(noexcept(this->get().cbegin())) { return this->get().cbegin(); }
167
168 /// Iterate the wrapped container
169 /// @return a const iterator to the first element
170 decltype(auto) cbegin() const noexcept(noexcept(this->get().cbegin())) { return this->get().cbegin(); }
171
172 /// Iterate the wrapped container
173 /// @return a const iterator one past the last element
174 decltype(auto) cend() noexcept(noexcept(this->get().cend())) { return this->get().cend(); }
175
176 /// Iterate the wrapped container
177 /// @return a const iterator one past the last element
178 decltype(auto) cend() const noexcept(noexcept(this->get().cend())) { return this->get().cend(); }
179
180 /// Query the size of the wrapped container
181 /// @return the number of elements
182 size_type size() const noexcept(noexcept(this->get().size())) { return this->get().size(); }
183
184 /// Query whether the wrapped container is empty
185 /// @return true if the container holds no elements
186 bool empty() const noexcept(noexcept(this->get().empty()))
187 requires(concepts::has_empty<T>)
188 {
189 return this->get().empty();
190 }
191
192 /// Change the number of elements held
193 /// @param size the new number of elements
194 void resize(size_type size) noexcept(noexcept(this->get().resize(size)))
196 {
197 this->get().resize(size);
198 }
199
200 /// Preallocate storage for the given number of elements
201 /// @param size the number of elements to reserve capacity for
202 void reserve(size_type size) noexcept(noexcept(this->get().reserve(size)))
204 {
205 this->get().reserve(size);
206 }
207
208 /// Element access
209 /// @param i the index of the element
210 /// @return const reference to the element at index i
211 template <typename U>
212 decltype(auto) operator[](U&& i) const noexcept(noexcept(this->get().operator[](i))) {
213 return this->get()[std::forward<U>(i)];
214 }
215
216 /// Element access
217 /// @param i the index of the element
218 /// @return reference to the element at index i
219 template <typename U>
220 decltype(auto) operator[](U&& i) noexcept(noexcept(this->get().operator[](i))) {
221 return this->get()[std::forward<U>(i)];
222 }
223};
224
225template <concepts::container T>
226class Strong_Adapter<T> : public Container_Strong_Adapter_Base<T> {
227 public:
228 using Container_Strong_Adapter_Base<T>::Container_Strong_Adapter_Base;
229};
230
231template <concepts::contiguous_container T>
232class Strong_Adapter<T> : public Container_Strong_Adapter_Base<T> {
233 public:
234 using pointer = typename T::pointer;
235 using const_pointer = typename T::const_pointer;
236
237 public:
239
240 explicit Strong_Adapter(std::span<const typename Container_Strong_Adapter_Base<T>::value_type> span) :
241 Strong_Adapter(T(span.begin(), span.end())) {}
242
243 // Disambiguates the usage of string literals, otherwise:
244 // Strong_Adapter(std::span<>) and Strong_Adapter(const char*)
245 // would be ambiguous.
246 explicit Strong_Adapter(const char* str)
247 requires(std::same_as<T, std::string>)
248 : Strong_Adapter(std::string(str)) {}
249
250 public:
251 decltype(auto) data() noexcept(noexcept(this->get().data())) { return this->get().data(); }
252
253 decltype(auto) data() const noexcept(noexcept(this->get().data())) { return this->get().data(); }
254};
255
256} // namespace detail
257
258/**
259 * Strong types can be used as wrappers around common types to provide
260 * compile time semantics. They usually contribute to more maintainable and
261 * less error-prone code especially when dealing with function parameters.
262 *
263 * Internally, this provides adapters so that the wrapping strong type behaves
264 * as much as the underlying type as possible and desirable.
265 *
266 * This implementation was inspired by:
267 * https://stackoverflow.com/a/69030899
268 */
269template <typename T, typename TagTypeT, typename... Capabilities>
270class Strong final : public detail::Strong_Adapter<T> {
271 public:
273
274 /**
275 * Check whether this strong type was declared with the given capability tag
276 * @return true if CapabilityT is one of this type's Capabilities
277 */
278 template <typename CapabilityT>
279 constexpr static bool has_capability() {
280 return (std::is_same_v<CapabilityT, Capabilities> || ...);
281 }
282
283 private:
284 using Tag = TagTypeT;
285};
286
287/**
288 * Write the wrapped value to an output stream
289 * @param os the output stream
290 * @param v the strong type to write
291 * @return reference to the output stream
292 */
293template <typename T, typename... Tags>
294 requires(concepts::streamable<T>)
295decltype(auto) operator<<(std::ostream& os, const Strong<T, Tags...>& v) {
296 return os << v.get();
297}
298
299/**
300 * Compare for equality
301 * @param lhs the first operand (strong type)
302 * @param rhs the second operand (strong type)
303 * @return true if lhs and rhs are equal
304 */
305template <typename T, typename... Tags>
306 requires(std::equality_comparable<T>)
307bool operator==(const Strong<T, Tags...>& lhs, const Strong<T, Tags...>& rhs) {
308 return lhs.get() == rhs.get();
309}
310
311/**
312 * Three-way comparison
313 * @param lhs the first operand (strong type)
314 * @param rhs the second operand (strong type)
315 * @return the ordering of lhs relative to rhs
316 */
317template <typename T, typename... Tags>
318 requires(std::three_way_comparable<T>)
320 return lhs.get() <=> rhs.get();
321}
322
323/**
324 * Three-way comparison
325 * @param a the first operand (plain number)
326 * @param b the second operand (strong type)
327 * @return the ordering of a relative to b
328 */
329template <std::integral T1, std::integral T2, typename... Tags>
331 return a <=> b.get();
332}
333
334/**
335 * Three-way comparison
336 * @param a the first operand (strong type)
337 * @param b the second operand (plain number)
338 * @return the ordering of a relative to b
339 */
340template <std::integral T1, std::integral T2, typename... Tags>
342 return a.get() <=> b;
343}
344
345/**
346 * Compare for equality
347 * @param a the first operand (plain number)
348 * @param b the second operand (strong type)
349 * @return true if a and b are equal
350 */
351template <std::integral T1, std::integral T2, typename... Tags>
353 return a == b.get();
354}
355
356/**
357 * Compare for equality
358 * @param a the first operand (strong type)
359 * @param b the second operand (plain number)
360 * @return true if a and b are equal
361 */
362template <std::integral T1, std::integral T2, typename... Tags>
364 return a.get() == b;
365}
366
367/**
368 * Add the wrapped values
369 * @param a the left hand operand (plain number)
370 * @param b the right hand operand (strong type)
371 * @return the result, wrapped in the strong type
372 */
373template <std::integral T1, std::integral T2, typename... Tags>
374 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
375constexpr decltype(auto) operator+(T1 a, Strong<T2, Tags...> b) {
376 return Strong<T2, Tags...>(a + b.get());
377}
378
379/**
380 * Add the wrapped values
381 * @param a the left hand operand (strong type)
382 * @param b the right hand operand (plain number)
383 * @return the result, wrapped in the strong type
384 */
385template <std::integral T1, std::integral T2, typename... Tags>
386 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
387constexpr decltype(auto) operator+(Strong<T1, Tags...> a, T2 b) {
388 return Strong<T1, Tags...>(a.get() + b);
389}
390
391/**
392 * Add the wrapped values
393 * @param a the left hand operand (strong type)
394 * @param b the right hand operand (strong type)
395 * @return the result, wrapped in the strong type
396 */
397template <std::integral T, typename... Tags>
398constexpr decltype(auto) operator+(Strong<T, Tags...> a, Strong<T, Tags...> b) {
399 return Strong<T, Tags...>(a.get() + b.get());
400}
401
402/**
403 * Subtract the wrapped values
404 * @param a the left hand operand (plain number)
405 * @param b the right hand operand (strong type)
406 * @return the result, wrapped in the strong type
407 */
408template <std::integral T1, std::integral T2, typename... Tags>
409 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
410constexpr decltype(auto) operator-(T1 a, Strong<T2, Tags...> b) {
411 return Strong<T2, Tags...>(a - b.get());
412}
413
414/**
415 * Subtract the wrapped values
416 * @param a the left hand operand (strong type)
417 * @param b the right hand operand (plain number)
418 * @return the result, wrapped in the strong type
419 */
420template <std::integral T1, std::integral T2, typename... Tags>
421 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
422constexpr decltype(auto) operator-(Strong<T1, Tags...> a, T2 b) {
423 return Strong<T1, Tags...>(a.get() - b);
424}
425
426/**
427 * Subtract the wrapped values
428 * @param a the left hand operand (strong type)
429 * @param b the right hand operand (strong type)
430 * @return the result, wrapped in the strong type
431 */
432template <std::integral T, typename... Tags>
433constexpr decltype(auto) operator-(Strong<T, Tags...> a, Strong<T, Tags...> b) {
434 return Strong<T, Tags...>(a.get() - b.get());
435}
436
437/**
438 * Multiply the wrapped values
439 * @param a the left hand operand (plain number)
440 * @param b the right hand operand (strong type)
441 * @return the result, wrapped in the strong type
442 */
443template <std::integral T1, std::integral T2, typename... Tags>
444 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
445constexpr decltype(auto) operator*(T1 a, Strong<T2, Tags...> b) {
446 return Strong<T2, Tags...>(a * b.get());
447}
448
449/**
450 * Multiply the wrapped values
451 * @param a the left hand operand (strong type)
452 * @param b the right hand operand (plain number)
453 * @return the result, wrapped in the strong type
454 */
455template <std::integral T1, std::integral T2, typename... Tags>
456 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
457constexpr decltype(auto) operator*(Strong<T1, Tags...> a, T2 b) {
458 return Strong<T1, Tags...>(a.get() * b);
459}
460
461/**
462 * Multiply the wrapped values
463 * @param a the left hand operand (strong type)
464 * @param b the right hand operand (strong type)
465 * @return the result, wrapped in the strong type
466 */
467template <std::integral T, typename... Tags>
468constexpr decltype(auto) operator*(Strong<T, Tags...> a, Strong<T, Tags...> b) {
469 return Strong<T, Tags...>(a.get() * b.get());
470}
471
472/**
473 * Divide the wrapped values
474 * @param a the left hand operand (plain number)
475 * @param b the right hand operand (strong type)
476 * @return the result, wrapped in the strong type
477 */
478template <std::integral T1, std::integral T2, typename... Tags>
479 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
480constexpr decltype(auto) operator/(T1 a, Strong<T2, Tags...> b) {
481 return Strong<T2, Tags...>(a / b.get());
482}
483
484/**
485 * Divide the wrapped values
486 * @param a the left hand operand (strong type)
487 * @param b the right hand operand (plain number)
488 * @return the result, wrapped in the strong type
489 */
490template <std::integral T1, std::integral T2, typename... Tags>
491 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
492constexpr decltype(auto) operator/(Strong<T1, Tags...> a, T2 b) {
493 return Strong<T1, Tags...>(a.get() / b);
494}
495
496/**
497 * Divide the wrapped values
498 * @param a the left hand operand (strong type)
499 * @param b the right hand operand (strong type)
500 * @return the result, wrapped in the strong type
501 */
502template <std::integral T, typename... Tags>
503constexpr decltype(auto) operator/(Strong<T, Tags...> a, Strong<T, Tags...> b) {
504 return Strong<T, Tags...>(a.get() / b.get());
505}
506
507/**
508 * Bitwise XOR of the wrapped values
509 * @param a the left hand operand (plain number)
510 * @param b the right hand operand (strong type)
511 * @return the result, wrapped in the strong type
512 */
513template <std::integral T1, std::integral T2, typename... Tags>
514 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
515constexpr decltype(auto) operator^(T1 a, Strong<T2, Tags...> b) {
516 return Strong<T2, Tags...>(a ^ b.get());
517}
518
519/**
520 * Bitwise XOR of the wrapped values
521 * @param a the left hand operand (strong type)
522 * @param b the right hand operand (plain number)
523 * @return the result, wrapped in the strong type
524 */
525template <std::integral T1, std::integral T2, typename... Tags>
526 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
527constexpr decltype(auto) operator^(Strong<T1, Tags...> a, T2 b) {
528 return Strong<T1, Tags...>(a.get() ^ b);
529}
530
531/**
532 * Bitwise XOR of the wrapped values
533 * @param a the left hand operand (strong type)
534 * @param b the right hand operand (strong type)
535 * @return the result, wrapped in the strong type
536 */
537template <std::integral T, typename... Tags>
538constexpr decltype(auto) operator^(Strong<T, Tags...> a, Strong<T, Tags...> b) {
539 return Strong<T, Tags...>(a.get() ^ b.get());
540}
541
542/**
543 * Bitwise AND of the wrapped values
544 * @param a the left hand operand (plain number)
545 * @param b the right hand operand (strong type)
546 * @return the result, wrapped in the strong type
547 */
548template <std::integral T1, std::integral T2, typename... Tags>
549 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
550constexpr decltype(auto) operator&(T1 a, Strong<T2, Tags...> b) {
551 return Strong<T2, Tags...>(a & b.get());
552}
553
554/**
555 * Bitwise AND of the wrapped values
556 * @param a the left hand operand (strong type)
557 * @param b the right hand operand (plain number)
558 * @return the result, wrapped in the strong type
559 */
560template <std::integral T1, std::integral T2, typename... Tags>
561 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
562constexpr decltype(auto) operator&(Strong<T1, Tags...> a, T2 b) {
563 return Strong<T1, Tags...>(a.get() & b);
564}
565
566/**
567 * Bitwise AND of the wrapped values
568 * @param a the left hand operand (strong type)
569 * @param b the right hand operand (strong type)
570 * @return the result, wrapped in the strong type
571 */
572template <std::integral T, typename... Tags>
573constexpr decltype(auto) operator&(Strong<T, Tags...> a, Strong<T, Tags...> b) {
574 return Strong<T, Tags...>(a.get() & b.get());
575}
576
577/**
578 * Bitwise OR of the wrapped values
579 * @param a the left hand operand (plain number)
580 * @param b the right hand operand (strong type)
581 * @return the result, wrapped in the strong type
582 */
583template <std::integral T1, std::integral T2, typename... Tags>
584 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
585constexpr decltype(auto) operator|(T1 a, Strong<T2, Tags...> b) {
586 return Strong<T2, Tags...>(a | b.get());
587}
588
589/**
590 * Bitwise OR of the wrapped values
591 * @param a the left hand operand (strong type)
592 * @param b the right hand operand (plain number)
593 * @return the result, wrapped in the strong type
594 */
595template <std::integral T1, std::integral T2, typename... Tags>
596 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
597constexpr decltype(auto) operator|(Strong<T1, Tags...> a, T2 b) {
598 return Strong<T1, Tags...>(a.get() | b);
599}
600
601/**
602 * Bitwise OR of the wrapped values
603 * @param a the left hand operand (strong type)
604 * @param b the right hand operand (strong type)
605 * @return the result, wrapped in the strong type
606 */
607template <std::integral T, typename... Tags>
608constexpr decltype(auto) operator|(Strong<T, Tags...> a, Strong<T, Tags...> b) {
609 return Strong<T, Tags...>(a.get() | b.get());
610}
611
612/**
613 * Right shift the wrapped values
614 * @param a the left hand operand (plain number)
615 * @param b the right hand operand (strong type)
616 * @return the result, wrapped in the strong type
617 */
618template <std::integral T1, std::integral T2, typename... Tags>
619 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
620constexpr decltype(auto) operator>>(T1 a, Strong<T2, Tags...> b) {
621 return Strong<T2, Tags...>(a >> b.get());
622}
623
624/**
625 * Right shift the wrapped values
626 * @param a the left hand operand (strong type)
627 * @param b the right hand operand (plain number)
628 * @return the result, wrapped in the strong type
629 */
630template <std::integral T1, std::integral T2, typename... Tags>
631 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
632constexpr decltype(auto) operator>>(Strong<T1, Tags...> a, T2 b) {
633 return Strong<T1, Tags...>(a.get() >> b);
634}
635
636/**
637 * Right shift the wrapped values
638 * @param a the left hand operand (strong type)
639 * @param b the right hand operand (strong type)
640 * @return the result, wrapped in the strong type
641 */
642template <std::integral T, typename... Tags>
643constexpr decltype(auto) operator>>(Strong<T, Tags...> a, Strong<T, Tags...> b) {
644 return Strong<T, Tags...>(a.get() >> b.get());
645}
646
647/**
648 * Left shift the wrapped values
649 * @param a the left hand operand (plain number)
650 * @param b the right hand operand (strong type)
651 * @return the result, wrapped in the strong type
652 */
653template <std::integral T1, std::integral T2, typename... Tags>
654 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
655constexpr decltype(auto) operator<<(T1 a, Strong<T2, Tags...> b) {
656 return Strong<T2, Tags...>(a << b.get());
657}
658
659/**
660 * Left shift the wrapped values
661 * @param a the left hand operand (strong type)
662 * @param b the right hand operand (plain number)
663 * @return the result, wrapped in the strong type
664 */
665template <std::integral T1, std::integral T2, typename... Tags>
666 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
667constexpr decltype(auto) operator<<(Strong<T1, Tags...> a, T2 b) {
668 return Strong<T1, Tags...>(a.get() << b);
669}
670
671/**
672 * Left shift the wrapped values
673 * @param a the left hand operand (strong type)
674 * @param b the right hand operand (strong type)
675 * @return the result, wrapped in the strong type
676 */
677template <std::integral T, typename... Tags>
678constexpr decltype(auto) operator<<(Strong<T, Tags...> a, Strong<T, Tags...> b) {
679 return Strong<T, Tags...>(a.get() << b.get());
680}
681
682/**
683 * Add to the wrapped value
684 * @param a the strong type to modify
685 * @param b the right hand operand (plain number)
686 * @return reference to a
687 */
688template <std::integral T1, std::integral T2, typename... Tags>
689 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
690constexpr auto operator+=(Strong<T1, Tags...>& a, T2 b) {
691 a.get() += b;
692 return a;
693}
694
695/**
696 * Add to the wrapped value
697 * @param a the strong type to modify
698 * @param b the right hand operand (strong type)
699 * @return reference to a
700 */
701template <std::integral T, typename... Tags>
703 a.get() += b.get();
704 return a;
705}
706
707/**
708 * Subtract from the wrapped value
709 * @param a the strong type to modify
710 * @param b the right hand operand (plain number)
711 * @return reference to a
712 */
713template <std::integral T1, std::integral T2, typename... Tags>
714 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
715constexpr auto operator-=(Strong<T1, Tags...>& a, T2 b) {
716 a.get() -= b;
717 return a;
718}
719
720/**
721 * Subtract from the wrapped value
722 * @param a the strong type to modify
723 * @param b the right hand operand (strong type)
724 * @return reference to a
725 */
726template <std::integral T, typename... Tags>
728 a.get() -= b.get();
729 return a;
730}
731
732/**
733 * Multiply in place the wrapped value
734 * @param a the strong type to modify
735 * @param b the right hand operand (plain number)
736 * @return reference to a
737 */
738template <std::integral T1, std::integral T2, typename... Tags>
739 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
740constexpr auto operator*=(Strong<T1, Tags...>& a, T2 b) {
741 a.get() *= b;
742 return a;
743}
744
745/**
746 * Multiply in place the wrapped value
747 * @param a the strong type to modify
748 * @param b the right hand operand (strong type)
749 * @return reference to a
750 */
751template <std::integral T, typename... Tags>
753 a.get() *= b.get();
754 return a;
755}
756
757/**
758 * Divide in place the wrapped value
759 * @param a the strong type to modify
760 * @param b the right hand operand (plain number)
761 * @return reference to a
762 */
763template <std::integral T1, std::integral T2, typename... Tags>
764 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
765constexpr auto operator/=(Strong<T1, Tags...>& a, T2 b) {
766 a.get() /= b;
767 return a;
768}
769
770/**
771 * Divide in place the wrapped value
772 * @param a the strong type to modify
773 * @param b the right hand operand (strong type)
774 * @return reference to a
775 */
776template <std::integral T, typename... Tags>
778 a.get() /= b.get();
779 return a;
780}
781
782/**
783 * Bitwise XOR in place the wrapped value
784 * @param a the strong type to modify
785 * @param b the right hand operand (plain number)
786 * @return reference to a
787 */
788template <std::integral T1, std::integral T2, typename... Tags>
789 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
790constexpr auto operator^=(Strong<T1, Tags...>& a, T2 b) {
791 a.get() ^= b;
792 return a;
793}
794
795/**
796 * Bitwise XOR in place the wrapped value
797 * @param a the strong type to modify
798 * @param b the right hand operand (strong type)
799 * @return reference to a
800 */
801template <std::integral T, typename... Tags>
803 a.get() ^= b.get();
804 return a;
805}
806
807/**
808 * Bitwise AND in place the wrapped value
809 * @param a the strong type to modify
810 * @param b the right hand operand (plain number)
811 * @return reference to a
812 */
813template <std::integral T1, std::integral T2, typename... Tags>
814 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
815constexpr auto operator&=(Strong<T1, Tags...>& a, T2 b) {
816 a.get() &= b;
817 return a;
818}
819
820/**
821 * Bitwise AND in place the wrapped value
822 * @param a the strong type to modify
823 * @param b the right hand operand (strong type)
824 * @return reference to a
825 */
826template <std::integral T, typename... Tags>
828 a.get() &= b.get();
829 return a;
830}
831
832/**
833 * Bitwise OR in place the wrapped value
834 * @param a the strong type to modify
835 * @param b the right hand operand (plain number)
836 * @return reference to a
837 */
838template <std::integral T1, std::integral T2, typename... Tags>
839 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
840constexpr auto operator|=(Strong<T1, Tags...>& a, T2 b) {
841 a.get() |= b;
842 return a;
843}
844
845/**
846 * Bitwise OR in place the wrapped value
847 * @param a the strong type to modify
848 * @param b the right hand operand (strong type)
849 * @return reference to a
850 */
851template <std::integral T, typename... Tags>
853 a.get() |= b.get();
854 return a;
855}
856
857/**
858 * Right shift in place the wrapped value
859 * @param a the strong type to modify
860 * @param b the right hand operand (plain number)
861 * @return reference to a
862 */
863template <std::integral T1, std::integral T2, typename... Tags>
864 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
865constexpr auto operator>>=(Strong<T1, Tags...>& a, T2 b) {
866 a.get() >>= b;
867 return a;
868}
869
870/**
871 * Right shift in place the wrapped value
872 * @param a the strong type to modify
873 * @param b the right hand operand (strong type)
874 * @return reference to a
875 */
876template <std::integral T, typename... Tags>
878 a.get() >>= b.get();
879 return a;
880}
881
882/**
883 * Left shift in place the wrapped value
884 * @param a the strong type to modify
885 * @param b the right hand operand (plain number)
886 * @return reference to a
887 */
888template <std::integral T1, std::integral T2, typename... Tags>
889 requires(detail::has_capability<EnableArithmeticWithPlainNumber, Tags...>)
890constexpr auto operator<<=(Strong<T1, Tags...>& a, T2 b) {
891 a.get() <<= b;
892 return a;
893}
894
895/**
896 * Left shift in place the wrapped value
897 * @param a the strong type to modify
898 * @param b the right hand operand (strong type)
899 * @return reference to a
900 */
901template <std::integral T, typename... Tags>
903 a.get() <<= b.get();
904 return a;
905}
906
907/**
908 * Increment the wrapped value (postfix)
909 * @param a the strong type to modify
910 * @return the value before the operation
911 */
912template <std::integral T, typename... Tags>
913constexpr auto operator++(Strong<T, Tags...>& a, int) {
914 auto tmp = a;
915 ++a.get();
916 return tmp;
917}
918
919/**
920 * Increment the wrapped value (prefix)
921 * @param a the strong type to modify
922 * @return the value after the operation
923 */
924template <std::integral T, typename... Tags>
925constexpr auto operator++(Strong<T, Tags...>& a) {
926 ++a.get();
927 return a;
928}
929
930/**
931 * Decrement the wrapped value (postfix)
932 * @param a the strong type to modify
933 * @return the value before the operation
934 */
935template <std::integral T, typename... Tags>
936constexpr auto operator--(Strong<T, Tags...>& a, int) {
937 auto tmp = a;
938 --a.get();
939 return tmp;
940}
941
942/**
943 * Decrement the wrapped value (prefix)
944 * @param a the strong type to modify
945 * @return the value after the operation
946 */
947template <std::integral T, typename... Tags>
948constexpr auto operator--(Strong<T, Tags...>& a) {
949 --a.get();
950 return a;
951}
952
953/**
954 * This mimics a std::span but keeps track of the strong-type information. Use
955 * this when you would want to use `const Strong<...>&` as a parameter
956 * declaration. In particular this allows assigning strong-type information to
957 * slices of a bigger buffer without copying the bytes. E.g:
958 *
959 * using Foo = Strong<std::vector<uint8_t>, Foo_>;
960 *
961 * void bar(StrongSpan<Foo> foo) { ... }
962 *
963 * std::vector<uint8_t> buffer;
964 * BufferSlicer slicer(buffer);
965 * bar(slicer.take<Foo>()); // This does not copy the data from buffer but
966 * // just annotates the 'Foo' strong-type info.
967 */
968template <concepts::contiguous_strong_type T>
969class StrongSpan final {
970 using underlying_span = std::
971 conditional_t<std::is_const_v<T>, std::span<const typename T::value_type>, std::span<typename T::value_type>>;
972
973 public:
974 /// The element type of the underlying span
975 using value_type = typename underlying_span::value_type;
976
977 /// The size type of the underlying span
978 using size_type = typename underlying_span::size_type;
979
980 /// The iterator type of the underlying span
981 using iterator = typename underlying_span::iterator;
982
983 /// The pointer type of the underlying span
984 using pointer = typename underlying_span::pointer;
985
986 /// The const pointer type of the underlying span
987 using const_pointer = typename underlying_span::const_pointer;
988
989 /// Default constructor, creates an empty span
990 StrongSpan() = default;
991
992 /// Annotate a plain span with this strong type's information
993 /// @param span the span to annotate
994 explicit StrongSpan(underlying_span span) : m_span(span) {}
995
996 /// Create a span covering the contents of a strong type
997 /// @param strong the strong type to view
998 // NOLINTNEXTLINE(*-explicit-conversions,*-explicit-constructor)
999 StrongSpan(T& strong) : m_span(strong) {}
1000
1001 // Allows implicit conversion from `StrongSpan<T>` to `StrongSpan<const T>`.
1002 // Note that this is not bi-directional. Conversion from `StrongSpan<const T>`
1003 // to `StrongSpan<T>` is not allowed.
1004 //
1005 // TODO: Technically, we should be able to phrase this with a `requires std::is_const_v<T>`
1006 // instead of the `std::enable_if` constructions. clang-tidy (14 or 15) doesn't seem
1007 // to pick up on that (yet?). As a result, for a non-const T it assumes this to be
1008 // a declaration of an ordinary copy constructor. The existence of a copy constructor
1009 // is interpreted as "not cheap to copy", setting off the `performance-unnecessary-value-param` check.
1010 // See also: https://github.com/randombit/botan/issues/3591
1011 /// Convert a StrongSpan<T> to a StrongSpan<const T>
1012 /// @param other the span to convert
1013 template <concepts::contiguous_strong_type T2>
1014 // NOLINTNEXTLINE(*-explicit-conversions,*-explicit-constructor)
1016 requires(std::is_same_v<T2, std::remove_const_t<T>>)
1017 : m_span(other.get()) {}
1018
1019 /// Copy constructor
1020 /// @param other the span to copy
1021 StrongSpan(const StrongSpan& other) = default;
1022
1023 /// Move constructor
1024 /// @param other the span to move from
1025 StrongSpan(StrongSpan&& other) = default;
1026
1027 /// Copy assignment
1028 /// @param other the span to copy
1029 /// @return reference to this
1030 StrongSpan& operator=(const StrongSpan& other) = default;
1031
1032 /// Move assignment
1033 /// @param other the span to move from
1034 /// @return reference to this
1035 StrongSpan& operator=(StrongSpan&& other) = default;
1036
1037 ~StrongSpan() = default;
1038
1039 /**
1040 * Access the underlying span
1041 * @returns the underlying std::span without any type constraints
1042 */
1043 underlying_span get() const { return m_span; }
1044
1045 /**
1046 * Access the underlying span
1047 * @returns the underlying std::span without any type constraints
1048 */
1049 underlying_span get() { return m_span; }
1050
1051 /// Access the underlying storage
1052 /// @return a pointer to the first element
1053 decltype(auto) data() noexcept(noexcept(this->m_span.data())) { return this->m_span.data(); }
1054
1055 /// Access the underlying storage
1056 /// @return a const pointer to the first element
1057 decltype(auto) data() const noexcept(noexcept(this->m_span.data())) { return this->m_span.data(); }
1058
1059 /// Query the size of the span
1060 /// @return the number of elements
1061 decltype(auto) size() const noexcept(noexcept(this->m_span.size())) { return this->m_span.size(); }
1062
1063 /// Query whether the span is empty
1064 /// @return true if the span covers no elements
1065 bool empty() const noexcept(noexcept(this->m_span.empty())) { return this->m_span.empty(); }
1066
1067 /// Iterate the span
1068 /// @return an iterator to the first element
1069 decltype(auto) begin() noexcept(noexcept(this->m_span.begin())) { return this->m_span.begin(); }
1070
1071 /// Iterate the span
1072 /// @return a const iterator to the first element
1073 decltype(auto) begin() const noexcept(noexcept(this->m_span.begin())) { return this->m_span.begin(); }
1074
1075 /// Iterate the span
1076 /// @return an iterator one past the last element
1077 decltype(auto) end() noexcept(noexcept(this->m_span.end())) { return this->m_span.end(); }
1078
1079 /// Iterate the span
1080 /// @return a const iterator one past the last element
1081 decltype(auto) end() const noexcept(noexcept(this->m_span.end())) { return this->m_span.end(); }
1082
1083 /// Element access
1084 /// @param i the index of the element
1085 /// @return reference to the element at index i
1086 decltype(auto) operator[](typename underlying_span::size_type i) const noexcept { return this->m_span[i]; }
1087
1088 /// Create a typed subspan of this span
1089 /// @param offset the index of the first element of the subspan
1090 /// @param count the number of elements in the subspan
1091 /// @return a new StrongSpan<T> covering the subspan
1092 StrongSpan<T> subspan(size_type offset, size_type count = std::dynamic_extent) const {
1093 return StrongSpan<T>(m_span.subspan(offset, count));
1094 }
1095
1096 private:
1097 underlying_span m_span;
1098};
1099
1100} // namespace Botan
1101
1102#endif
typename underlying_span::size_type size_type
The size type of the underlying span.
decltype(auto) begin() noexcept(noexcept(this->m_span.begin()))
StrongSpan()=default
Default constructor, creates an empty span.
StrongSpan< T > subspan(size_type offset, size_type count=std::dynamic_extent) const
decltype(auto) data() noexcept(noexcept(this->m_span.data()))
underlying_span get() const
StrongSpan(T &strong)
typename underlying_span::value_type value_type
The element type of the underlying span.
~StrongSpan()=default
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
The const pointer type of the underlying span.
bool empty() const noexcept(noexcept(this->m_span.empty()))
underlying_span get()
decltype(auto) operator[](typename underlying_span::size_type i) const noexcept
typename underlying_span::pointer pointer
The pointer type of the underlying span.
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
The iterator type of the underlying span.
StrongSpan(StrongSpan &&other)=default
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
static constexpr bool has_capability()
typename T::iterator iterator
The iterator type of the wrapped container.
typename T::value_type value_type
The element type of the wrapped container.
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
The size type of the wrapped container.
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()))
void resize(size_type size) noexcept(noexcept(this->get().resize(size)))
typename T::const_iterator const_iterator
The const iterator type of the wrapped container.
decltype(auto) cbegin() noexcept(noexcept(this->get().cbegin()))
decltype(auto) data() const noexcept(noexcept(this->get().data()))
decltype(auto) data() noexcept(noexcept(this->get().data()))
typename T::const_pointer const_pointer
Strong_Adapter(std::span< const typename Container_Strong_Adapter_Base< T >::value_type > span)
T wrapped_type
The type wrapped by this strong type.
Definition strong_type.h:53
constexpr const T & get() const &
Definition strong_type.h:85
Strong_Base(Strong_Base &&) noexcept=default
Move constructor.
constexpr T & get() &
Definition strong_type.h:81
Strong_Base()=default
Default constructor, value initializes the wrapped value.
constexpr const T && get() const &&
Definition strong_type.h:93
constexpr T && get() &&
Definition strong_type.h:89
Strong_Base(const Strong_Base &)=default
Copy constructor.
constexpr bool has_capability
Definition strong_type.h:41
ASN1_Type operator|(ASN1_Type x, ASN1_Type y)
Definition asn1_obj.h:84
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:57
constexpr auto operator&=(Strong< T1, Tags... > &a, T2 b)
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:109
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
constexpr auto operator++(Strong< T, Tags... > &a, int)
std::ostream & operator<<(std::ostream &out, const OID &oid)
Definition asn1_oid.cpp:308
constexpr auto operator>>=(Strong< T1, Tags... > &a, T2 b)
BigInt operator/(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:102
BigInt operator-(const BigInt &x, const BigInt &y)
Definition bigint.h:1216
constexpr auto operator<<=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator|=(Strong< T1, Tags... > &a, T2 b)
std::vector< uint8_t, Alloc > & operator^=(std::vector< uint8_t, Alloc > &out, const std::vector< uint8_t, Alloc2 > &in)
Definition mem_ops.h:532
auto operator<=>(const Strong< T, Tags... > &lhs, const Strong< T, Tags... > &rhs)
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
Definition secmem.h:168
int operator>>(int fd, Pipe &pipe)
Definition fd_unix.cpp:43
bool operator==(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:54
constexpr auto operator*=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator--(Strong< T, Tags... > &a, int)
constexpr auto operator/=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator-=(Strong< T1, Tags... > &a, T2 b)
ECIES_Flags operator&(ECIES_Flags a, ECIES_Flags b)
Definition ecies.h:70