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

Commit a0bfcee

Browse files
committed
Type traits updated. Documentation updated #3
1 parent 5544ca5 commit a0bfcee

File tree

3 files changed

+157
-55
lines changed

3 files changed

+157
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2018-2020 Nil Foundation AG
3+
// Copyright (c) 2018-2020 Mikhail Komarov <[email protected]>
4+
//
5+
// Distributed under the Boost Software License, Version 1.0
6+
// See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt
8+
//---------------------------------------------------------------------------//
9+
10+
#ifndef CRYPTO3_CODEC_ALGORITHM_HPP
11+
#define CRYPTO3_CODEC_ALGORITHM_HPP
12+
13+
namespace nil {
14+
namespace crypto3 {
15+
namespace codec {
16+
/*!
17+
* @defgroup codec Encoding & Decoding
18+
*
19+
* @defgroup codec_algorithms Algorithms
20+
* @ingroup codec
21+
* @brief Encoding algorithms are meant to provide encoding interface similar to STL algorithms' one.
22+
*/
23+
}
24+
} // namespace crypto3
25+
} // namespace nil
26+
27+
#endif // CRYPTO3_CODEC_HPP

include/nil/crypto3/codec/algorithm/encode.hpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,15 @@
1010
#ifndef CRYPTO3_ENCODE_HPP
1111
#define CRYPTO3_ENCODE_HPP
1212

13+
#include <nil/crypto3/codec/algorithm/codec.hpp>
14+
1315
#include <nil/crypto3/codec/codec_value.hpp>
1416
#include <nil/crypto3/codec/codec_state.hpp>
1517

1618
#include <nil/crypto3/detail/type_traits.hpp>
1719

1820
namespace nil {
1921
namespace crypto3 {
20-
namespace codec {
21-
/*!
22-
* @defgroup codec Encoding & Decoding
23-
*
24-
* @defgroup codec_algorithms Algorithms
25-
* @ingroup codec
26-
* @brief Encoding algorithms are meant to provide encoding interface similar to STL algorithms' one.
27-
*/
28-
}
29-
3022
/*!
3123
* @brief Encodes the elements with particular codec defined with Encoder
3224
* in the range, defined by [first, last), and inserts the result to

include/nil/crypto3/detail/type_traits.hpp

+128-45
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)
@@ -89,6 +190,18 @@ namespace nil {
89190

90191
GENERATE_HAS_MEMBER(rounds)
91192

193+
GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(begin, const_iterator)
194+
GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(end, const_iterator)
195+
196+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(encode, block_type)
197+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(decode, block_type)
198+
199+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(encrypt, block_type)
200+
GENERATE_HAS_MEMBER_RETURN_FUNCTION(decrypt, block_type)
201+
202+
GENERATE_HAS_MEMBER_FUNCTION(generate)
203+
GENERATE_HAS_MEMBER_CONST_FUNCTION(check)
204+
92205
template<typename T>
93206
struct is_iterator {
94207
static char test(...);
@@ -105,62 +218,26 @@ namespace nil {
105218

106219
template<typename Container>
107220
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;
221+
static const bool value =
222+
has_const_iterator<Container>::value && has_begin<Container>::value && has_end<Container>::value;
147223
};
148224

149225
template<typename T>
150226
struct is_codec {
151227
static const bool value = has_encoded_value_type<T>::value && has_encoded_value_bits<T>::value &&
152228
has_decoded_value_type<T>::value && has_decoded_value_bits<T>::value &&
153229
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;
230+
has_decoded_block_type<T>::value && has_decoded_block_bits<T>::value &&
231+
has_encode<T>::value && has_decode<T>::value;
155232
typedef T type;
156233
};
157234

158235
template<typename T>
159236
struct is_block_cipher {
160237
static const bool value = has_word_type<T>::value && has_word_bits<T>::value &&
161238
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+
has_key_type<T>::value && has_key_bits<T>::value && has_rounds<T>::value &&
240+
has_encrypt<T>::value && has_decrypt<T>::value;
164241
typedef T type;
165242
};
166243

@@ -198,8 +275,14 @@ namespace nil {
198275
has_key_type<T>::value && has_key_bits<T>::value;
199276
typedef T type;
200277
};
278+
279+
template<typename T>
280+
struct is_passhash {
281+
static const bool value = has_generate<T>::value && has_check<T>::value;
282+
typedef T type;
283+
};
201284
} // namespace detail
202285
} // namespace crypto3
203286
} // namespace nil
204287

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

0 commit comments

Comments
 (0)