Botan 3.14.1
Crypto and TLS for C&
concepts.h
Go to the documentation of this file.
1/**
2 * Useful concepts that are available throughout the library
3 * (C) 2023 Jack Lloyd
4 * 2023 René Meusel - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_CONCEPTS_H_
10#define BOTAN_CONCEPTS_H_
11
12#include <botan/types.h>
13#include <concepts>
14#include <vector>
15
16namespace Botan {
17
18/**
19 * Trait that checks whether all of the given types are the same type
20 */
21template <typename T0 = void, typename... Ts>
22struct all_same {
23 /// True if every type in Ts is the same as T0
24 static constexpr bool value = (std::is_same_v<T0, Ts> && ... && true);
25};
26
27template <typename... Ts>
28static constexpr bool all_same_v = all_same<Ts...>::value;
29
30namespace detail {
31
32/**
33 * Helper type to indicate that a certain type should be automatically
34 * detected based on the context.
35 */
36struct AutoDetect {
37 /// This type is a tag only and cannot be instantiated
38 constexpr AutoDetect() = delete;
39};
40
41} // namespace detail
42
43namespace concepts {
44
45// TODO: C++20 provides concepts like std::ranges::range or ::sized_range
46// but at the time of this writing clang had not caught up on all
47// platforms. E.g. clang 14 on Xcode does not support ranges properly.
48
49template <typename IterT, typename ContainerT>
51 std::same_as<IterT, typename ContainerT::iterator> || std::same_as<IterT, typename ContainerT::const_iterator>;
52
53template <typename PtrT, typename ContainerT>
55 std::same_as<PtrT, typename ContainerT::pointer> || std::same_as<PtrT, typename ContainerT::const_pointer>;
56
57template <typename T>
58concept container = requires(T a) {
59 { a.begin() } -> container_iterator<T>;
60 { a.end() } -> container_iterator<T>;
61 { a.cbegin() } -> container_iterator<T>;
62 { a.cend() } -> container_iterator<T>;
63 { a.size() } -> std::same_as<typename T::size_type>;
64 typename T::value_type;
65};
66
67template <typename T>
68concept contiguous_container = container<T> && requires(T a) {
69 { a.data() } -> container_pointer<T>;
70};
71
72template <typename T>
73concept has_empty = requires(T a) {
74 { a.empty() } -> std::same_as<bool>;
75};
76
77template <typename T>
78concept resizable_container = container<T> && requires(T& c, typename T::size_type s) {
79 T(s);
80 c.resize(s);
81};
82
83template <typename T>
84concept reservable_container = container<T> && requires(T& c, typename T::size_type s) { c.reserve(s); };
85
86template <typename T>
88 contiguous_container<T> && resizable_container<T> && std::same_as<typename T::value_type, uint8_t>;
89
90/// The concept of a std::vector<uint8_t> with an arbitrary allocator. In our
91/// context, that's usually std::vector<uint8_t> or secure_vector<uint8_t>.
92template <typename T>
93concept ordinary_byte_vector = std::same_as<T, std::vector<uint8_t, typename T::allocator_type>>;
94
95} // namespace concepts
96
97} // namespace Botan
98
99#endif
static constexpr bool value
True if every type in Ts is the same as T0.
Definition concepts.h:24
constexpr AutoDetect()=delete
This type is a tag only and cannot be instantiated.