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