24 bool operator ==(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
27 bool operator !=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
30 bool operator >(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
33 bool operator <(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
36 bool operator >=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
39 bool operator <=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2);
60 template<
bool...>
struct bool_pack;
62 using all_true = std::is_same< bool_pack<bs...,
true>, bool_pack<
true, bs...> >;
64 template<
typename Word,
size_t NumWords>
65 static void set_from_word_sequence(
const Word* arr_begin,
const Word* arr_end,
fixed_bytes<Size>& key)
67 auto itr = key._data.begin();
69 const size_t sub_word_shift = 8 *
sizeof(Word);
70 const size_t num_sub_words =
sizeof(
word_t) /
sizeof(Word);
71 auto sub_words_left = num_sub_words;
72 for(
auto w_itr = arr_begin; w_itr != arr_end; ++w_itr ) {
73 if( sub_words_left > 1 ) {
74 temp_word |=
static_cast<word_t>(*w_itr);
75 temp_word <<= sub_word_shift;
80 FC_ASSERT( sub_words_left == 1,
"unexpected error in fixed_bytes constructor" );
81 temp_word |=
static_cast<word_t>(*w_itr);
82 sub_words_left = num_sub_words;
88 if( sub_words_left != num_sub_words ) {
89 if( sub_words_left > 1 )
90 temp_word <<= 8 * (sub_words_left-1);
122 std::copy(arr.begin(), arr.end(), _data.begin());
130 template<
typename Word,
size_t NumWords,
131 typename Enable =
typename std::enable_if<std::is_integral<Word>::value &&
132 std::is_unsigned<Word>::value &&
133 !std::is_same<Word, bool>::value &&
134 std::less<size_t>{}(
sizeof(Word),
sizeof(
word_t))>::type >
137 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(Word)) *
sizeof(Word),
138 "size of the backing word size is not divisible by the size of the array element" );
139 static_assert(
sizeof(Word) * NumWords <= Size,
"too many words supplied to fixed_bytes constructor" );
141 set_from_word_sequence<Word, NumWords>(arr.data(), arr.data() + arr.size(), *
this);
149 template<
typename Word,
size_t NumWords,
150 typename Enable =
typename std::enable_if<std::is_integral<Word>::value &&
151 std::is_unsigned<Word>::value &&
152 !std::is_same<Word, bool>::value &&
153 std::less<size_t>{}(
sizeof(Word),
sizeof(
word_t))>::type >
156 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(Word)) *
sizeof(Word),
157 "size of the backing word size is not divisible by the size of the array element" );
158 static_assert(
sizeof(Word) * NumWords <= Size,
"too many words supplied to fixed_bytes constructor" );
160 set_from_word_sequence<Word, NumWords>(arr, arr + NumWords, *
this);
171 template<
typename FirstWord,
typename... Rest>
175 std::is_unsigned<FirstWord>::value &&
176 !std::is_same<FirstWord, bool>::value &&
177 sizeof(FirstWord) <=
sizeof(
word_t) &&
178 all_true<(std::is_same<FirstWord, Rest>::value)...>
::value,
179 FirstWord>::type first_word,
182 static_assert(
sizeof(
word_t) == (
sizeof(
word_t)/
sizeof(FirstWord)) *
sizeof(FirstWord),
183 "size of the backing word size is not divisible by the size of the words supplied as arguments" );
184 static_assert(
sizeof(FirstWord) * (1 +
sizeof...(Rest)) <= Size,
"too many words supplied to make_from_word_sequence" );
187 std::array<FirstWord, 1+
sizeof...(Rest)> arr{{ first_word, rest... }};
188 set_from_word_sequence<FirstWord, 1+
sizeof...(Rest)>(arr.data(), arr.data() + arr.size(), key);
200 auto data() {
return _data.data(); }
207 auto data()
const {
return _data.data(); }
214 auto size()
const {
return _data.size(); }
223 std::array<uint8_t, Size> arr;
225 const size_t num_sub_words =
sizeof(
word_t);
227 auto arr_itr = arr.begin();
228 auto data_itr = _data.begin();
230 for(
size_t counter = _data.size(); counter > 0; --counter, ++data_itr ) {
231 size_t sub_words_left = num_sub_words;
233 auto temp_word = *data_itr;
238 for( ; sub_words_left > 0; --sub_words_left ) {
239 *(arr_itr + sub_words_left - 1) =
static_cast<uint8_t>(temp_word & 0xFF);
242 arr_itr += num_sub_words;
255 printhex(
static_cast<const void*
>(arr.data()), arr.size());
288 template<
size_t Size>
289 bool operator ==(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
290 return c1._data == c2._data;
300 template<
size_t Size>
301 bool operator !=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
302 return c1._data != c2._data;
312 template<
size_t Size>
313 bool operator >(
const fixed_bytes<Size>& c1,
const fixed_bytes<Size>& c2) {
314 return c1._data > c2._data;
324 template<
size_t Size>
325 bool operator <(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
326 return c1._data < c2._data;
336 template<
size_t Size>
337 bool operator >=(
const fixed_bytes<Size>& c1,
const fixed_bytes<Size>& c2) {
338 return c1._data >= c2._data;
348 template<
size_t Size>
349 bool operator <=(
const fixed_bytes<Size> &c1,
const fixed_bytes<Size> &c2) {
350 return c1._data <= c2._data;
354 using checksum160 = fixed_bytes<20>;
355 using checksum256 = fixed_bytes<32>;
356 using checksum512 = fixed_bytes<64>;
367 template<
typename DataStream,
size_t Size>
368 inline DataStream&
operator<<(DataStream& ds,
const fixed_bytes<Size>& d) {
369 auto arr =
d.extract_as_byte_array();
370 ds.write( (
const char*)arr.data(), arr.size() );
383 template<
typename DataStream,
size_t Size>
384 inline DataStream&
operator>>(DataStream& ds, fixed_bytes<Size>& d) {
385 std::array<uint8_t, Size> arr;
386 ds.read( (
char*)arr.data(), arr.size() );
387 d = fixed_bytes<Size>( arr );
fixed_bytes(const Word(&arr)[NumWords])
fixed_bytes(const std::array< Word, NumWords > &arr)
static constexpr size_t padded_bytes()
static constexpr size_t num_words()
std::array< uint8_t, Size > extract_as_byte_array() const
static fixed_bytes< Size > make_from_word_sequence(typename std::enable_if< std::is_integral< FirstWord >::value &&std::is_unsigned< FirstWord >::value &&!std::is_same< FirstWord, bool >::value &&sizeof(FirstWord)<=sizeof(word_t) &&all_true<(std::is_same< FirstWord, Rest >::value)... >::value, FirstWord >::type first_word, Rest... rest)
fixed_bytes(const std::array< word_t, num_words()> &arr)
const auto & get_array() const
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
static const Segment ds(Segment::ds)
datastream< ST > & operator<<(datastream< ST > &s, const sysio::chain::may_not_exist< T > &v)
datastream< ST > & operator>>(datastream< ST > &s, sysio::chain::may_not_exist< T > &v)
bool operator<(const permission_level &lhs, const permission_level &rhs)
bool operator<=(const permission_level &lhs, const permission_level &rhs)
bool operator==(const permission_level &lhs, const permission_level &rhs)
bool operator!=(const permission_level &lhs, const permission_level &rhs)
bool operator>(const permission_level &lhs, const permission_level &rhs)
unsigned __int128 uint128_t
bool operator>=(const permission_level &lhs, const permission_level &rhs)