BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
hash160.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 jmjatlanta and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <boost/endian/buffers.hpp>
26 #include <fc/fwd.hpp>
27 #include <fc/string.hpp>
28 #include <fc/io/raw_fwd.hpp>
29 
30 namespace fc{
31 
32 class hash160
33 {
34  public:
35  hash160();
36  explicit hash160( const string& hex_str );
37 
38  string str()const;
39  explicit operator string()const;
40 
41  char* data() const;
42  static constexpr size_t data_size() { return 160/8; }
43 
44  static hash160 hash( const char* d, uint32_t dlen );
45  static hash160 hash( const string& );
46 
47  template<typename T>
48  static hash160 hash( const T& t )
49  {
51  fc::raw::pack(e,t);
52  return e.result();
53  }
54 
55  class encoder
56  {
57  public:
58  encoder();
59  ~encoder();
60 
61  void write( const char* d, uint32_t dlen );
62  void put( char c ) { write( &c, 1 ); }
63  void reset();
64  hash160 result();
65 
66  private:
67  struct impl;
69  };
70 
71  template<typename T>
72  inline friend T& operator<<( T& ds, const hash160& ep ) {
73  ds.write( ep.data(), sizeof(ep) );
74  return ds;
75  }
76 
77  template<typename T>
78  inline friend T& operator>>( T& ds, hash160& ep ) {
79  ds.read( ep.data(), sizeof(ep) );
80  return ds;
81  }
82  friend hash160 operator << ( const hash160& h1, uint32_t i );
83  friend bool operator == ( const hash160& h1, const hash160& h2 );
84  friend bool operator != ( const hash160& h1, const hash160& h2 );
85  friend hash160 operator ^ ( const hash160& h1, const hash160& h2 );
86  friend bool operator >= ( const hash160& h1, const hash160& h2 );
87  friend bool operator > ( const hash160& h1, const hash160& h2 );
88  friend bool operator < ( const hash160& h1, const hash160& h2 );
89 
90  boost::endian::little_uint32_buf_t _hash[5];
91 };
92 
93 namespace raw {
94 
95  template<typename T>
96  inline void pack( T& ds, const hash160& ep, uint32_t _max_depth ) {
97  ds << ep;
98  }
99 
100  template<typename T>
101  inline void unpack( T& ds, hash160& ep, uint32_t _max_depth ) {
102  ds >> ep;
103  }
104 
105 }
106 
107  class variant;
108  void to_variant( const hash160& bi, variant& v, uint32_t max_depth );
109  void from_variant( const variant& v, hash160& bi, uint32_t max_depth );
110 
111  template<> struct get_typename<hash160> { static const char* name() { return "hash160"; } };
112 } // namespace fc
113 
114 namespace std
115 {
116  template<>
117  struct hash<fc::hash160>
118  {
119  size_t operator()( const fc::hash160& s )const
120  {
121  return *((size_t*)&s);
122  }
123  };
124 }
fc::hash160::encoder::~encoder
~encoder()
Definition: hash160.cpp:58
fc::hash160::data
char * data() const
Definition: hash160.cpp:52
fc::hash160::data_size
static constexpr size_t data_size()
Definition: hash160.hpp:42
fc::hash160::str
string str() const
Definition: hash160.cpp:46
fc::hash160::operator>>
friend T & operator>>(T &ds, hash160 &ep)
Definition: hash160.hpp:78
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc::hash160::operator<
friend bool operator<(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:118
fwd.hpp
fc
Definition: api.hpp:15
fc::hash160::hash
static hash160 hash(const T &t)
Definition: hash160.hpp:48
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::hash160::encoder::put
void put(char c)
Definition: hash160.hpp:62
fc::hash160::encoder::encoder
encoder()
Definition: hash160.cpp:59
fc::hash160::encoder::result
hash160 result()
Definition: hash160.cpp:76
string.hpp
fc::hash160::encoder::reset
void reset()
Definition: hash160.cpp:89
fc::hash160::operator^
friend hash160 operator^(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:100
fc::hash160::operator==
friend bool operator==(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:126
fc::get_typename
Definition: typename.hpp:20
fc::hash160::hash
static hash160 hash(const char *d, uint32_t dlen)
Definition: hash160.cpp:61
fc::get_typename< hash160 >::name
static const char * name()
Definition: hash160.hpp:111
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::hash160::encoder::write
void write(const char *d, uint32_t dlen)
Definition: hash160.cpp:71
std::hash< fc::hash160 >::operator()
size_t operator()(const fc::hash160 &s) const
Definition: hash160.hpp:119
std
Definition: zeroed_array.hpp:76
fc::hash160::operator<<
friend T & operator<<(T &ds, const hash160 &ep)
Definition: hash160.hpp:72
raw_fwd.hpp
fc::hash160::operator!=
friend bool operator!=(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:122
fc::hash160::hash160
hash160()
Definition: hash160.cpp:40
fc::hash160
Definition: hash160.hpp:32
fc::hash160::operator>
friend bool operator>(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:114
fc::hash160::operator>=
friend bool operator>=(const hash160 &h1, const hash160 &h2)
Definition: hash160.cpp:110
fc::hash160::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: hash160.hpp:90
fc::fwd
Used to forward declare value types.
Definition: fwd.hpp:10
fc::hash160::encoder
Definition: hash160.hpp:55
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11