9
9
#include < crypto/common.h>
10
10
#include < span.h>
11
11
12
- #include < assert.h>
12
+ #include < algorithm>
13
+ #include < array>
14
+ #include < cassert>
13
15
#include < cstring>
14
16
#include < stdint.h>
15
17
#include < string>
16
- #include < vector>
17
18
18
19
/* * Template base class for fixed-sized opaque blobs. */
19
20
template <unsigned int BITS>
20
21
class base_blob
21
22
{
22
23
protected:
23
24
static constexpr int WIDTH = BITS / 8 ;
24
- uint8_t m_data[WIDTH];
25
+ std::array<uint8_t , WIDTH> m_data;
26
+ static_assert (WIDTH == sizeof (m_data), " Sanity check" );
27
+
25
28
public:
26
29
/* construct 0 value by default */
27
30
constexpr base_blob () : m_data() {}
28
31
29
32
/* constructor for constants between 1 and 255 */
30
33
constexpr explicit base_blob (uint8_t v) : m_data{v} {}
31
34
32
- explicit base_blob (const std::vector<unsigned char >& vch);
35
+ constexpr explicit base_blob (Span<const unsigned char > vch)
36
+ {
37
+ assert (vch.size () == WIDTH);
38
+ std::copy (vch.begin (), vch.end (), m_data.begin ());
39
+ }
33
40
34
- bool IsNull () const
41
+ constexpr bool IsNull () const
35
42
{
36
- for (int i = 0 ; i < WIDTH; i++)
37
- if (m_data[i] != 0 )
38
- return false ;
39
- return true ;
43
+ return std::all_of (m_data.begin (), m_data.end (), [](uint8_t val) {
44
+ return val == 0 ;
45
+ });
40
46
}
41
47
42
- void SetNull ()
48
+ constexpr void SetNull ()
43
49
{
44
- memset (m_data, 0 , sizeof ( m_data) );
50
+ std::fill (m_data. begin (), m_data. end (), 0 );
45
51
}
46
52
47
- inline int Compare (const base_blob& other) const { return memcmp (m_data, other.m_data , sizeof (m_data) ); }
53
+ constexpr int Compare (const base_blob& other) const { return std:: memcmp (m_data. data () , other.m_data . data (), WIDTH ); }
48
54
49
- friend inline bool operator ==(const base_blob& a, const base_blob& b) { return a.Compare (b) == 0 ; }
50
- friend inline bool operator !=(const base_blob& a, const base_blob& b) { return a.Compare (b) != 0 ; }
51
- friend inline bool operator <(const base_blob& a, const base_blob& b) { return a.Compare (b) < 0 ; }
55
+ friend constexpr bool operator ==(const base_blob& a, const base_blob& b) { return a.Compare (b) == 0 ; }
56
+ friend constexpr bool operator !=(const base_blob& a, const base_blob& b) { return a.Compare (b) != 0 ; }
57
+ friend constexpr bool operator <(const base_blob& a, const base_blob& b) { return a.Compare (b) < 0 ; }
52
58
53
59
std::string GetHex () const ;
54
60
void SetHex (const char * psz);
55
61
void SetHex (const std::string& str);
56
62
std::string ToString () const ;
57
63
58
- const unsigned char * data () const { return m_data; }
59
- unsigned char * data () { return m_data; }
64
+ constexpr const unsigned char * data () const { return m_data. data () ; }
65
+ constexpr unsigned char * data () { return m_data. data () ; }
60
66
61
- unsigned char * begin ()
62
- {
63
- return &m_data[0 ];
64
- }
67
+ constexpr unsigned char * begin () { return m_data.data (); }
68
+ constexpr unsigned char * end () { return m_data.data () + WIDTH; }
65
69
66
- unsigned char * end ()
67
- {
68
- return &m_data[WIDTH];
69
- }
70
+ constexpr const unsigned char * begin () const { return m_data.data (); }
71
+ constexpr const unsigned char * end () const { return m_data.data () + WIDTH; }
70
72
71
- const unsigned char * begin () const
72
- {
73
- return &m_data[0 ];
74
- }
73
+ static constexpr unsigned int size () { return WIDTH; }
75
74
76
- const unsigned char * end () const
77
- {
78
- return &m_data[WIDTH];
79
- }
80
-
81
- static constexpr unsigned int size ()
82
- {
83
- return sizeof (m_data);
84
- }
85
-
86
- uint64_t GetUint64 (int pos) const
87
- {
88
- return ReadLE64 (m_data + pos * 8 );
89
- }
75
+ constexpr uint64_t GetUint64 (int pos) const { return ReadLE64 (m_data.data () + pos * 8 ); }
90
76
91
77
template <typename Stream>
92
78
void Serialize (Stream& s) const
@@ -107,8 +93,8 @@ class base_blob
107
93
*/
108
94
class uint160 : public base_blob <160 > {
109
95
public:
110
- constexpr uint160 () {}
111
- explicit uint160 (const std::vector< unsigned char >& vch) : base_blob<160>(vch) {}
96
+ constexpr uint160 () = default;
97
+ constexpr explicit uint160 (Span< const unsigned char > vch) : base_blob<160>(vch) {}
112
98
};
113
99
114
100
/* * 256-bit opaque blob.
@@ -118,9 +104,9 @@ class uint160 : public base_blob<160> {
118
104
*/
119
105
class uint256 : public base_blob <256 > {
120
106
public:
121
- constexpr uint256 () {}
107
+ constexpr uint256 () = default;
122
108
constexpr explicit uint256 (uint8_t v) : base_blob<256>(v) {}
123
- explicit uint256 (const std::vector< unsigned char >& vch) : base_blob<256>(vch) {}
109
+ constexpr explicit uint256 (Span< const unsigned char > vch) : base_blob<256>(vch) {}
124
110
static const uint256 ZERO;
125
111
static const uint256 ONE;
126
112
};
0 commit comments