Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
symbol.hpp
Go to the documentation of this file.
1#pragma once
4#include <sysio/chain/core_symbol.hpp>
5#include <string>
6#include <functional>
7
8namespace sysio {
9 namespace chain {
10
20 static constexpr uint64_t string_to_symbol_c(uint8_t precision, const char* str) {
21 uint32_t len = 0;
22 while (str[len]) ++len;
23
24 uint64_t result = 0;
25 // No validation is done at compile time
26 for (uint32_t i = 0; i < len; ++i) {
27 result |= (uint64_t(str[i]) << (8*(1+i)));
28 }
29
30 result |= uint64_t(precision);
31 return result;
32 }
33
34#define SY(P,X) ::sysio::chain::string_to_symbol_c(P,#X)
35
36 static uint64_t string_to_symbol(uint8_t precision, const char* str) {
37 try {
38 uint32_t len = 0;
39 while(str[len]) ++len;
40 uint64_t result = 0;
41 for (uint32_t i = 0; i < len; ++i) {
42 // All characters must be upper case alphabets
43 SYS_ASSERT (str[i] >= 'A' && str[i] <= 'Z', symbol_type_exception, "invalid character in symbol name");
44 result |= (uint64_t(str[i]) << (8*(i+1)));
45 }
46 result |= uint64_t(precision);
47 return result;
49 }
50
51 struct symbol_code {
53
54 operator uint64_t()const { return value; }
55 };
56
58 public:
59
60 static constexpr uint8_t max_precision = 18;
61
62 explicit symbol(uint8_t p, const char* s): m_value(string_to_symbol(p, s)) {
63 SYS_ASSERT(valid(), symbol_type_exception, "invalid symbol: ${s}", ("s",s));
64 }
65 explicit symbol(uint64_t v = CORE_SYMBOL): m_value(v) {
66 SYS_ASSERT(valid(), symbol_type_exception, "invalid symbol: ${name}", ("name",name()));
67 }
68 static symbol from_string(const string& from)
69 {
70 try {
71 string s = fc::trim(from);
72 SYS_ASSERT(!s.empty(), symbol_type_exception, "creating symbol from empty string");
73 auto comma_pos = s.find(',');
74 SYS_ASSERT(comma_pos != string::npos, symbol_type_exception, "missing comma in symbol");
75 auto prec_part = s.substr(0, comma_pos);
76 uint8_t p = fc::to_int64(prec_part);
77 string name_part = s.substr(comma_pos + 1);
78 SYS_ASSERT( p <= max_precision, symbol_type_exception, "precision ${p} should be <= 18", ("p", p));
79 return symbol(string_to_symbol(p, name_part.c_str()));
81 }
82 uint64_t value() const { return m_value; }
83 bool valid() const
84 {
85 const auto& s = name();
86 return decimals() <= max_precision && valid_name(s);
87 }
88 static bool valid_name(const string& name)
89 {
90 return all_of(name.begin(), name.end(), [](char c)->bool { return (c >= 'A' && c <= 'Z'); });
91 }
92
93 uint8_t decimals() const { return m_value & 0xFF; }
95 {
96 SYS_ASSERT( decimals() <= max_precision, symbol_type_exception, "precision ${p} should be <= 18", ("p", decimals()) );
97 uint64_t p10 = 1;
99 while( p > 0 ) {
100 p10 *= 10; --p;
101 }
102 return p10;
103 }
104 string name() const
105 {
106 uint64_t v = m_value;
107 v >>= 8;
108 string result;
109 while (v > 0) {
110 char c = v & 0xFF;
111 result += c;
112 v >>= 8;
113 }
114 return result;
115 }
116
117 symbol_code to_symbol_code()const { return {m_value >> 8}; }
118
119 explicit operator string() const
120 {
121 uint64_t v = m_value;
122 uint8_t p = v & 0xFF;
123 string ret = sysio::chain::to_string(p);
124 ret += ',';
125 ret += name();
126 return ret;
127 }
128
129 string to_string() const { return string(*this); }
130 template <typename DataStream>
131 friend DataStream& operator<< (DataStream& ds, const symbol& s)
132 {
133 return ds << s.to_string();
134 }
135
136 void reflector_init()const {
137 SYS_ASSERT( decimals() <= max_precision, symbol_type_exception, "precision ${p} should be <= 18", ("p", decimals()) );
138 SYS_ASSERT( valid_name(name()), symbol_type_exception, "invalid symbol: ${name}", ("name",name()));
139 }
140
141 private:
142 uint64_t m_value;
143 friend struct fc::reflector<symbol>;
144 }; // class symbol
145
150
151 inline bool operator== (const symbol& lhs, const symbol& rhs)
152 {
153 return lhs.value() == rhs.value();
154 }
155 inline bool operator!= (const symbol& lhs, const symbol& rhs)
156 {
157 return lhs.value() != rhs.value();
158 }
159 inline bool operator< (const symbol& lhs, const symbol& rhs)
160 {
161 return lhs.value() < rhs.value();
162 }
163 inline bool operator> (const symbol& lhs, const symbol& rhs)
164 {
165 return lhs.value() > rhs.value();
166 }
167
168 } // namespace chain
169} // namespace sysio
170
171namespace fc {
172 inline void to_variant(const sysio::chain::symbol& var, fc::variant& vo) { vo = var.to_string(); }
173 inline void from_variant(const fc::variant& var, sysio::chain::symbol& vo) {
175 }
176}
177
178namespace fc {
179 inline void to_variant(const sysio::chain::symbol_code& var, fc::variant& vo) {
180 vo = sysio::chain::symbol(var.value << 8).name();
181 }
183 vo = sysio::chain::symbol(0, var.get_string().c_str()).to_symbol_code();
184 }
185}
186
const mie::Vuint & p
Definition bn.cpp:27
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
const string & get_string() const
Definition variant.cpp:606
string name() const
Definition symbol.hpp:104
bool valid() const
Definition symbol.hpp:83
friend DataStream & operator<<(DataStream &ds, const symbol &s)
Definition symbol.hpp:131
symbol(uint64_t v=CORE_SYMBOL)
Definition symbol.hpp:65
uint8_t decimals() const
Definition symbol.hpp:93
static bool valid_name(const string &name)
Definition symbol.hpp:88
static constexpr uint8_t max_precision
Definition symbol.hpp:60
void reflector_init() const
Definition symbol.hpp:136
symbol(uint8_t p, const char *s)
Definition symbol.hpp:62
symbol_code to_symbol_code() const
Definition symbol.hpp:117
string to_string() const
Definition symbol.hpp:129
uint64_t precision() const
Definition symbol.hpp:94
static symbol from_string(const string &from)
Definition symbol.hpp:68
uint64_t value() const
Definition symbol.hpp:82
Defines exception's used by fc.
#define FC_CAPTURE_LOG_AND_RETHROW(...)
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
fc::string to_string(double)
Definition string.cpp:131
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
fc::string trim(const fc::string &)
Definition string.cpp:152
int64_t to_int64(const fc::string &)
Definition string.cpp:92
bool operator<(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:21
bool operator==(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:13
bool operator!=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:17
bool operator>(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:29
#define value
Definition pkcs11.h:157
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
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
Immutable except for fc::from_variant.
Definition name.hpp:43
CK_RV ret
char * s
size_t len