CsCrypto  1.0.1
crypto_traits.h
1 /***********************************************************************
2 *
3 * Copyright (c) 2021-2024 Tim van Deurzen
4 * Copyright (c) 2021-2024 Barbara Geller
5 * Copyright (c) 2021-2024 Ansel Sermersheim
6 *
7 * This file is part of CsCrypto.
8 *
9 * CsCrypto is free software, released under the BSD 2-Clause license.
10 * For license details refer to LICENSE provided with this project.
11 *
12 * CsCrypto is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * https://opensource.org/licenses/BSD-2-Clause
17 *
18 ***********************************************************************/
19 
20 #ifndef CS_CRYPTO_UTIL_CRYPTO_TRAITS_H
21 #define CS_CRYPTO_UTIL_CRYPTO_TRAITS_H
22 
23 #include <iterator>
24 #include <optional>
25 
26 namespace cs_crypto::traits {
27 
28 template <typename T>
29 struct always_false : public std::false_type {
30 };
31 
32 template <typename T>
33 struct identity {
34  using type = T;
35 };
36 
37 template <typename T>
38 using identity_t = typename identity<T>::type;
39 
40 template <typename T>
41 using begin_t = decltype(std::begin(std::declval<T>()));
42 
43 template <typename T>
44 using end_t = decltype(std::end(std::declval<T>()));
45 
46 template <typename T, typename U>
47 using equality_comparable = decltype(std::declval<T &>() == std::declval<U &>());
48 
49 template <typename T, typename = void>
50 struct is_iterable
51  : std::false_type
52 {
53 };
54 
55 template <typename T>
56 struct is_iterable<T, std::void_t<begin_t<T>, end_t<T>, equality_comparable<begin_t<T>, end_t<T>>>>
57  : public std::true_type
58 {
59 };
60 
61 template <typename T>
62 inline constexpr bool is_iterable_v = is_iterable<T>::value;
63 
64 template <typename T>
65 struct remove_optional {
66  using type = T;
67 };
68 
69 template <typename T>
70 struct remove_optional<std::optional<T>>
71  : public remove_optional<T>
72 {
73 };
74 
75 template <typename T>
76 using remove_optional_t = typename remove_optional<T>::type;
77 
78 template <auto A>
79 struct enum_to_type {
80  static_assert(std::is_enum_v<decltype(A)>, "Type trait enum_to_type only valid for enums");
81  constexpr static const auto value = A;
82 };
83 
84 template <typename T>
85 [[maybe_unused]] inline constexpr bool is_uniquely_represented_byte_v =
86  std::is_trivially_copyable_v<T> &&
87  std::has_unique_object_representations<T>::value && sizeof(T) == 1;
88 
89 } // namespace cs_crypto::traits
90 
91 #endif