Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit e5d10a2

Browse files
committed
Type traits updated #1
1 parent e763262 commit e5d10a2

File tree

1 file changed

+149
-56
lines changed

1 file changed

+149
-56
lines changed

include/nil/crypto3/detail/type_traits.hpp

+149-56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//---------------------------------------------------------------------------//
2-
// Copyright (c) 2018-2019 Nil Foundation AG
3-
// Copyright (c) 2018-2019 Mikhail Komarov <[email protected]>
2+
// Copyright (c) 2018-2020 Nil Foundation AG
3+
// Copyright (c) 2018-2020 Mikhail Komarov <[email protected]>
44
//
55
// Distributed under the Boost Software License, Version 1.0
66
// See accompanying file LICENSE_1_0.txt or copy at
@@ -60,9 +60,110 @@
6060
template<class T> \
6161
struct has_##member : public std::integral_constant<bool, HasMember_##member<T>::RESULT> {};
6262

63+
#define GENERATE_HAS_MEMBER_FUNCTION(Function, ...) \
64+
\
65+
template<typename T> \
66+
struct has_##Function { \
67+
struct Fallback { \
68+
void Function(##__VA_ARGS__); \
69+
}; \
70+
\
71+
struct Derived : Fallback {}; \
72+
\
73+
template<typename C, C> \
74+
struct ChT; \
75+
\
76+
template<typename C> \
77+
static char (&f(ChT<void (Fallback::*)(##__VA_ARGS__), &C::Function> *))[1]; \
78+
\
79+
template<typename C> \
80+
static char (&f(...))[2]; \
81+
\
82+
static bool const value = sizeof(f<Derived>(0)) == 2; \
83+
};
84+
85+
#define GENERATE_HAS_MEMBER_CONST_FUNCTION(Function, ...) \
86+
\
87+
template<typename T> \
88+
struct has_##Function { \
89+
struct Fallback { \
90+
void Function(##__VA_ARGS__) const; \
91+
}; \
92+
\
93+
struct Derived : Fallback {}; \
94+
\
95+
template<typename C, C> \
96+
struct ChT; \
97+
\
98+
template<typename C> \
99+
static char (&f(ChT<void (Fallback::*)(##__VA_ARGS__) const, &C::Function> *))[1]; \
100+
\
101+
template<typename C> \
102+
static char (&f(...))[2]; \
103+
\
104+
static bool const value = sizeof(f<Derived>(0)) == 2; \
105+
};
106+
107+
#define GENERATE_HAS_MEMBER_RETURN_FUNCTION(Function, ReturnType, ...) \
108+
\
109+
template<typename T> \
110+
struct has_##Function { \
111+
struct Dummy { \
112+
typedef void ReturnType; \
113+
}; \
114+
typedef typename std::conditional<has_##ReturnType<T>::value, T, Dummy>::type TType; \
115+
typedef typename TType::ReturnType type; \
116+
\
117+
struct Fallback { \
118+
type Function(##__VA_ARGS__); \
119+
}; \
120+
\
121+
struct Derived : TType, Fallback {}; \
122+
\
123+
template<typename C, C> \
124+
struct ChT; \
125+
\
126+
template<typename C> \
127+
static char (&f(ChT<type (Fallback::*)(##__VA_ARGS__), &C::Function> *))[1]; \
128+
\
129+
template<typename C> \
130+
static char (&f(...))[2]; \
131+
\
132+
static bool const value = sizeof(f<Derived>(0)) == 2; \
133+
};
134+
135+
#define GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(Function, ReturnType, ...) \
136+
\
137+
template<typename T> \
138+
struct has_##Function { \
139+
struct Dummy { \
140+
typedef void ReturnType; \
141+
}; \
142+
typedef typename std::conditional<has_##ReturnType<T>::value, T, Dummy>::type TType; \
143+
typedef typename TType::ReturnType type; \
144+
\
145+
struct Fallback { \
146+
type Function(##__VA_ARGS__) const; \
147+
}; \
148+
\
149+
struct Derived : TType, Fallback {}; \
150+
\
151+
template<typename C, C> \
152+
struct ChT; \
153+
\
154+
template<typename C> \
155+
static char (&f(ChT<type (Fallback::*)(##__VA_ARGS__) const, &C::Function> *))[1]; \
156+
\
157+
template<typename C> \
158+
static char (&f(...))[2]; \
159+
\
160+
static bool const value = sizeof(f<Derived>(0)) == 2; \
161+
};
162+
63163
namespace nil {
64164
namespace crypto3 {
65165
namespace detail {
166+
GENERATE_HAS_MEMBER_TYPE(iterator)
66167
GENERATE_HAS_MEMBER_TYPE(const_iterator)
67168

68169
GENERATE_HAS_MEMBER_TYPE(encoded_value_type)
@@ -84,11 +185,25 @@ namespace nil {
84185
GENERATE_HAS_MEMBER(block_bits)
85186
GENERATE_HAS_MEMBER(digest_bits)
86187
GENERATE_HAS_MEMBER(key_bits)
188+
GENERATE_HAS_MEMBER(min_key_bits)
189+
GENERATE_HAS_MEMBER(max_key_bits)
87190
GENERATE_HAS_MEMBER(key_schedule_bits)
88191
GENERATE_HAS_MEMBER(word_bits)
89192

90193
GENERATE_HAS_MEMBER(rounds)
91194

195+
GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(begin, const_iterator)
196+
GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(end, const_iterator)
197+
198+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(encode, block_type)
199+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(decode, block_type)
200+
201+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(encrypt, block_type)
202+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(decrypt, block_type)
203+
204+
GENERATE_HAS_MEMBER_FUNCTION(generate)
205+
GENERATE_HAS_MEMBER_CONST_FUNCTION(check)
206+
92207
template<typename T>
93208
struct is_iterator {
94209
static char test(...);
@@ -105,62 +220,26 @@ namespace nil {
105220

106221
template<typename Container>
107222
struct is_container {
108-
private:
109-
template<typename T>
110-
struct has_begin_end {
111-
struct Dummy {
112-
typedef void const_iterator;
113-
};
114-
typedef typename std::conditional<has_const_iterator<T>::value, T, Dummy>::type TType;
115-
typedef typename TType::const_iterator iter;
116-
117-
struct Fallback {
118-
iter begin() const;
119-
120-
iter end() const;
121-
};
122-
123-
struct Derived : TType, Fallback {};
124-
125-
template<typename C, C>
126-
struct ChT;
127-
128-
template<typename C>
129-
static char (&f(ChT<iter (Fallback::*)() const, &C::begin> *))[1];
130-
131-
template<typename C>
132-
static char (&f(...))[2];
133-
134-
template<typename C>
135-
static char (&g(ChT<iter (Fallback::*)() const, &C::end> *))[1];
136-
137-
template<typename C>
138-
static char (&g(...))[2];
139-
140-
static bool const beg_value = sizeof(f<Derived>(0)) == 2;
141-
static bool const end_value = sizeof(g<Derived>(0)) == 2;
142-
};
143-
144-
public:
145-
static const bool value = has_const_iterator<Container>::value && has_begin_end<Container>::beg_value &&
146-
has_begin_end<Container>::end_value;
223+
static const bool value
224+
= has_const_iterator<Container>::value && has_begin<Container>::value && has_end<Container>::value;
147225
};
148226

149227
template<typename T>
150228
struct is_codec {
151-
static const bool value = has_encoded_value_type<T>::value && has_encoded_value_bits<T>::value &&
152-
has_decoded_value_type<T>::value && has_decoded_value_bits<T>::value &&
153-
has_encoded_block_type<T>::value && has_encoded_block_bits<T>::value &&
154-
has_decoded_block_type<T>::value && has_decoded_block_bits<T>::value;
229+
static const bool value = has_encoded_value_type<T>::value && has_encoded_value_bits<T>::value
230+
&& has_decoded_value_type<T>::value && has_decoded_value_bits<T>::value
231+
&& has_encoded_block_type<T>::value && has_encoded_block_bits<T>::value
232+
&& has_decoded_block_type<T>::value && has_decoded_block_bits<T>::value
233+
&& has_encode<T>::value && has_decode<T>::value;
155234
typedef T type;
156235
};
157236

158237
template<typename T>
159238
struct is_block_cipher {
160-
static const bool value = has_word_type<T>::value && has_word_bits<T>::value &&
161-
has_block_type<T>::value && has_block_bits<T>::value &&
162-
has_key_type<T>::value && has_key_bits<T>::value &&
163-
has_rounds<T>::value;
239+
static const bool value = has_word_type<T>::value && has_word_bits<T>::value && has_block_type<T>::value
240+
&& has_block_bits<T>::value && has_key_type<T>::value
241+
&& has_key_bits<T>::value && has_rounds<T>::value && has_encrypt<T>::value
242+
&& has_decrypt<T>::value;
164243
typedef T type;
165244
};
166245

@@ -185,21 +264,35 @@ namespace nil {
185264
static two test_construction_params(...);
186265

187266
public:
188-
static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
189-
sizeof(test_construction_type<T>(0)) == sizeof(one) &&
190-
sizeof(test_construction_params<T>(0)) == sizeof(one);
267+
static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value
268+
&& sizeof(test_construction_type<T>(0)) == sizeof(one)
269+
&& sizeof(test_construction_params<T>(0)) == sizeof(one);
191270
typedef T type;
192271
};
193272

194273
template<typename T>
195274
struct is_mac {
196-
static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
197-
has_block_type<T>::value && has_block_bits<T>::value &&
198-
has_key_type<T>::value && has_key_bits<T>::value;
275+
static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value
276+
&& has_block_type<T>::value && has_block_bits<T>::value
277+
&& has_key_type<T>::value && has_key_bits<T>::value;
278+
typedef T type;
279+
};
280+
281+
template<typename T>
282+
struct is_kdf {
283+
static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value
284+
&& has_key_type<T>::value && has_max_key_bits<T>::value
285+
&& has_min_key_bits<T>::value;
286+
typedef T type;
287+
};
288+
289+
template<typename T>
290+
struct is_passhash {
291+
static const bool value = has_generate<T>::value && has_check<T>::value;
199292
typedef T type;
200293
};
201294
} // namespace detail
202295
} // namespace crypto3
203296
} // namespace nil
204297

205-
#endif // CRYPTO3_CODEC_TYPE_TRAITS_HPP
298+
#endif // CRYPTO3_TYPE_TRAITS_HPP

0 commit comments

Comments
 (0)