10 #include "ypc/common/byte.h"
11 #include <boost/endian/conversion.hpp>
12 #include <boost/property_tree/ptree.hpp>
13 #include <ff/network.h>
14 #include <glog/logging.h>
20 using byte_t = uint8_t;
22 using hex_bytes = bytes::hex_bytes_t;
23 using base58_bytes = bytes::base58_bytes_t;
24 using base64_bytes = bytes::base64_bytes_t;
27 auto byte_to_number(byte_t *bytes,
size_t len) ->
28 typename std::enable_if<std::is_arithmetic<T>::value, T>::type {
29 if (len <
sizeof(T)) {
34 T ret = boost::endian::big_to_native(*val);
39 auto number_to_byte(T val, byte_t *bytes,
size_t len) ->
40 typename std::enable_if<std::is_arithmetic<T>::value,
void>::type {
41 if (len <
sizeof(T)) {
45 T v = boost::endian::native_to_big(val);
51 template <
typename T,
typename BytesType>
52 auto byte_to_number(
const BytesType &v) ->
53 typename std::enable_if<std::is_arithmetic<T>::value, T>::type {
54 if (v.size() <
sizeof(T)) {
58 T *val = (T *)v.data();
59 T ret = boost::endian::big_to_native(*val);
63 template <
typename BytesType,
typename T>
64 auto number_to_byte(T val) ->
65 typename std::enable_if<std::is_arithmetic<T>::value &&
66 utc::is_bytes<BytesType>::value,
68 T v = boost::endian::native_to_big(val);
69 BytesType b((byte_t *)&v,
sizeof(v));
73 template <
typename BytesType,
typename T>
74 auto number_to_byte(T val) ->
75 typename std::enable_if<std::is_arithmetic<T>::value &&
76 utc::is_fix_bytes<BytesType>::value,
78 T v = boost::endian::native_to_big(val);
80 T *rval = (T *)b.data();
86 template <
typename ByteType, ::ypc::utc::
byte_encode Format>
87 auto operator<<(std::ostream &out,
88 const ::ypc::utc::bytes<ByteType, Format> &data) ->
89 typename std::enable_if<Format == ::ypc::utc::byte_encode::raw_bytes,
90 std::ostream &>::type {
92 using hex_bytes_t = typename ::ypc::utc::bytes<ByteType, Format>::hex_bytes_t;
93 hex_bytes_t hex = data.template as<hex_bytes_t>();
94 std::string s((
const char *)hex.data(), hex.size());
99 template <
typename ByteType, ::ypc::utc::
byte_encode Format>
100 auto operator<<(std::ostream &out,
101 const ::ypc::utc::bytes<ByteType, Format> &data) ->
102 typename std::enable_if<Format != ::ypc::utc::byte_encode::raw_bytes,
103 std::ostream &>::type {
105 std::string s((
const char *)data.data(), data.size());
110 template <
typename ByteType, ::ypc::utc::
byte_encode Format>
112 typename std::enable_if<Format == ::ypc::utc::byte_encode::raw_bytes,
113 std::istream &>::type {
114 using hex_bytes_t = typename ::ypc::utc::bytes<ByteType, Format>::hex_bytes_t;
117 hex_bytes_t hex(s.c_str(), s.size());
118 obj = hex.template as<::ypc::utc::bytes<ByteType, Format>>();
122 template <
typename ByteType, ::ypc::utc::
byte_encode Format>
124 typename std::enable_if<Format != ::ypc::utc::byte_encode::raw_bytes,
125 std::istream &>::type {
134 namespace property_tree {
137 template <
typename String,
typename ByteType, ::ypc::utc::
byte_encode Format>
139 using internal_type = String;
142 boost::optional<external_type> get_value(
const internal_type &str) {
143 std::istringstream stream(str);
145 istream(stream, result);
146 if (stream.rdstate() == std::ios::failbit) {
152 boost::optional<internal_type> put_value(
const external_type &obj) {
153 std::ostringstream result;
154 ostream(result, obj);
159 template <
typename BT, ::ypc::utc::
byte_encode F>
160 static auto ostream(std::ostream &out, const ::ypc::utc::bytes<BT, F> &data)
161 ->
typename std::enable_if<F == ::ypc::utc::byte_encode::raw_bytes,
162 std::ostream &>::type {
164 using hex_bytes_t = typename ::ypc::utc::bytes<BT, F>::hex_bytes_t;
165 hex_bytes_t hex = data.template as<hex_bytes_t>();
166 std::string s((
const char *)hex.data(), hex.size());
171 template <
typename BT, ::ypc::utc::
byte_encode F>
172 static auto ostream(std::ostream &out, const ::ypc::utc::bytes<BT, F> &data)
173 ->
typename std::enable_if<F != ::ypc::utc::byte_encode::raw_bytes,
174 std::ostream &>::type {
176 std::string s((
const char *)data.data(), data.size());
181 template <
typename BT, ::ypc::utc::
byte_encode F>
183 typename std::enable_if<F == ::ypc::utc::byte_encode::raw_bytes,
184 std::istream &>::type {
185 using hex_bytes_t = typename ::ypc::utc::bytes<BT, F>::hex_bytes_t;
188 hex_bytes_t hex(s.c_str(), s.size());
189 obj = hex.template as<::ypc::utc::bytes<BT, F>>();
193 template <
typename BT, ::ypc::utc::
byte_encode F>
195 typename std::enable_if<F != ::ypc::utc::byte_encode::raw_bytes,
196 std::istream &>::type {
204 using string_type = std::string;
206 template <
typename ByteType, ::ypc::utc::
byte_encode Format>
212 template <>
struct translator_between<string_type, string_type> {
213 using type = id_translator<string_type>;