Botan 3.13.0
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
15namespace Botan {
16
17/**
18 * Trait that checks whether all of the given types are the same type
19 */
20template <typename T0 = void, typename... Ts>
21struct all_same {
22 /// True if every type in Ts is the same as T0
23 static constexpr bool value = (std::is_same_v<T0, Ts> && ... && true);
24};
25
26template <typename... Ts>
27static constexpr bool all_same_v = all_same<Ts...>::value;
28
29namespace detail {
30
31/**
32 * Helper type to indicate that a certain type should be automatically
33 * detected based on the context.
34 */
35struct AutoDetect {
36 /// This type is a tag only and cannot be instantiated
37 constexpr AutoDetect() = delete;
38};
39
40} // namespace detail
41
42namespace concepts {
43
44// TODO: C++20 provides concepts like std::ranges::range or ::sized_range
45// but at the time of this writing clang had not caught up on all
46// platforms. E.g. clang 14 on Xcode does not support ranges properly.
47
48template <typename IterT, typename ContainerT>
50 std::same_as<IterT, typename ContainerT::iterator> || std::same_as<IterT, typename ContainerT::const_iterator>;
51
52template <typename PtrT, typename ContainerT>
54 std::same_as<PtrT, typename ContainerT::pointer> || std::same_as<PtrT, typename ContainerT::const_pointer>;
55
56template <typename T>
57concept container = requires(T a) {
58 { a.begin() } -> container_iterator<T>;
59 { a.end() } -> container_iterator<T>;
60 { a.cbegin() } -> container_iterator<T>;
61 { a.cend() } -> container_iterator<T>;
62 { a.size() } -> std::same_as<typename T::size_type>;
63 typename T::value_type;
64};
65
66template <typename T>
67concept contiguous_container = container<T> && requires(T a) {
68 { a.data() } -> container_pointer<T>;
69};
70
71template <typename T>
72concept has_empty = requires(T a) {
73 { a.empty() } -> std::same_as<bool>;
74};
75
76template <typename T>
77concept resizable_container = container<T> && requires(T& c, typename T::size_type s) {
78 T(s);
79 c.resize(s);
80};
81
82template <typename T>
83concept reservable_container = container<T> && requires(T& c, typename T::size_type s) { c.reserve(s); };
84
85template <typename T>
87 contiguous_container<T> && resizable_container<T> && std::same_as<typename T::value_type, uint8_t>;
88
89} // namespace concepts
90
91} // namespace Botan
92
93#endif
static constexpr bool value
True if every type in Ts is the same as T0.
Definition concepts.h:23
constexpr AutoDetect()=delete
This type is a tag only and cannot be instantiated.