Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
name.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
4#include <iosfwd>
5
6namespace sysio::chain {
7 struct name;
8}
9namespace fc {
10 class variant;
11 void to_variant(const sysio::chain::name& c, fc::variant& v);
12 void from_variant(const fc::variant& v, sysio::chain::name& check);
13} // fc
14
15namespace sysio::chain {
16 inline constexpr uint64_t char_to_symbol( char c ) {
17 if( c >= 'a' && c <= 'z' )
18 return (c - 'a') + 6;
19 if( c >= '1' && c <= '5' )
20 return (c - '1') + 1;
21 return 0;
22 }
23
24 inline constexpr uint64_t string_to_uint64_t( std::string_view str ) {
25 uint64_t n = 0;
26 size_t i = 0;
27 for ( ; i < str.size() && i < 12; ++i) {
28 // NOTE: char_to_symbol() returns char type, and without this explicit
29 // expansion to uint64 type, the compilation fails at the point of usage
30 // of string_to_name(), where the usage requires constant (compile time) expression.
31 n |= (char_to_symbol(str[i]) & 0x1f) << (64 - 5 * (i + 1));
32 }
33
34 // The for-loop encoded up to 60 high bits into uint64 'name' variable,
35 // if (strlen(str) > 12) then encode str[12] into the low (remaining)
36 // 4 bits of 'name'
37 if (i < str.size() && i == 12)
38 n |= char_to_symbol(str[12]) & 0x0F;
39 return n;
40 }
41
43 struct name {
44 private:
45 uint64_t value = 0;
46
47 friend struct fc::reflector<name>;
48 friend void fc::from_variant(const fc::variant& v, sysio::chain::name& check);
49
50 void set( std::string_view str );
51
52 public:
53 constexpr bool empty()const { return 0 == value; }
54 constexpr bool good()const { return !empty(); }
55
56 explicit name( std::string_view str ) { set( str ); }
57 constexpr explicit name( uint64_t v ) : value(v) {}
58 constexpr name() = default;
59
60 std::string to_string()const;
61 constexpr uint64_t to_uint64_t()const { return value; }
62
63 friend std::ostream& operator << ( std::ostream& out, const name& n ) {
64 return out << n.to_string();
65 }
66
67 friend constexpr bool operator < ( const name& a, const name& b ) { return a.value < b.value; }
68 friend constexpr bool operator > ( const name& a, const name& b ) { return a.value > b.value; }
69 friend constexpr bool operator <= ( const name& a, const name& b ) { return a.value <= b.value; }
70 friend constexpr bool operator >= ( const name& a, const name& b ) { return a.value >= b.value; }
71 friend constexpr bool operator == ( const name& a, const name& b ) { return a.value == b.value; }
72 friend constexpr bool operator != ( const name& a, const name& b ) { return a.value != b.value; }
73
74 friend constexpr bool operator == ( const name& a, uint64_t b ) { return a.value == b; }
75 friend constexpr bool operator != ( const name& a, uint64_t b ) { return a.value != b; }
76
77 constexpr explicit operator bool()const { return value != 0; }
78 };
79
80 // Each char of the string is encoded into 5-bit chunk and left-shifted
81 // to its 5-bit slot starting with the highest slot for the first char.
82 // The 13th char, if str is long enough, is encoded into 4-bit chunk
83 // and placed in the lowest 4 bits. 64 = 12 * 5 + 4
84 inline constexpr name string_to_name( std::string_view str )
85 {
86 return name( string_to_uint64_t( str ) );
87 }
88
89 inline namespace literals {
90#if defined(__clang__)
91# pragma clang diagnostic push
92# pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
93#endif
94 template <typename T, T... Str>
95 inline constexpr name operator""_n() {
96 constexpr const char buf[] = {Str...};
97 return name{std::integral_constant<uint64_t, string_to_uint64_t(std::string_view{buf, sizeof(buf)})>::value};
98 }
99#if defined(__clang__)
100# pragma clang diagnostic pop
101#endif
102 } // namespace literals
103
104} // sysio::chain
105
106namespace std {
107 template<> struct hash<sysio::chain::name> : private hash<uint64_t> {
109 size_t operator()(const argument_type& name) const noexcept
110 {
111 return hash<uint64_t>::operator()(name.to_uint64_t());
112 }
113 };
114};
115
std::string name
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
namespace sysio::chain
Definition authority.cpp:3
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
Definition name.hpp:106
constexpr uint64_t string_to_uint64_t(std::string_view str)
Definition name.hpp:24
constexpr name string_to_name(std::string_view str)
Definition name.hpp:84
constexpr uint64_t char_to_symbol(char c)
Definition name.hpp:16
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
unsigned __int64 uint64_t
Definition stdint.h:136
defines visit functions for T Unless this is specialized, visit() will not be defined for T.
Definition reflect.hpp:33
sysio::chain::name argument_type
Definition name.hpp:108
size_t operator()(const argument_type &name) const noexcept
Definition name.hpp:109
Immutable except for fc::from_variant.
Definition name.hpp:43
constexpr bool good() const
Definition name.hpp:54
constexpr name(uint64_t v)
Definition name.hpp:57
constexpr name()=default
friend constexpr bool operator<=(const name &a, const name &b)
Definition name.hpp:69
friend constexpr bool operator<(const name &a, const name &b)
Definition name.hpp:67
friend constexpr bool operator==(const name &a, const name &b)
Definition name.hpp:71
name(std::string_view str)
Definition name.hpp:56
friend constexpr bool operator>(const name &a, const name &b)
Definition name.hpp:68
friend std::ostream & operator<<(std::ostream &out, const name &n)
Definition name.hpp:63
constexpr bool empty() const
Definition name.hpp:53
constexpr uint64_t to_uint64_t() const
Definition name.hpp:61
std::string to_string() const
Definition name.cpp:19
friend constexpr bool operator!=(const name &a, const name &b)
Definition name.hpp:72
friend constexpr bool operator>=(const name &a, const name &b)
Definition name.hpp:70
uint8_t buf[2048]
bool set