Skip to content

Commit 550255f

Browse files
authored
Merge pull request #1743 from evoskuil/master
Simplify and optimize JSON annotation defines.
2 parents 4a5f218 + 7e7a323 commit 550255f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+305
-458
lines changed

include/bitcoin/system/boost.hpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef LIBBITCOIN_SYSTEM_BOOST_HPP
2020
#define LIBBITCOIN_SYSTEM_BOOST_HPP
2121

22+
#include <utility>
23+
2224
// Apply any warning suppressions to boost.
2325
// Any boost includes within headers will not benefit from suppression, as the
2426
// warnings are included by define.hpp which follows boost includes.
@@ -41,27 +43,54 @@
4143
#include <boost/program_options.hpp>
4244
#include <boost/url.hpp>
4345

44-
// ADL free functions for use with boost-json.
45-
#define DECLARE_JSON_VALUE_CONVERTORS(name) \
46-
BC_API name tag_invoke(boost::json::value_to_tag<name>, \
47-
const boost::json::value& value) NOEXCEPT; \
48-
BC_API void tag_invoke(boost::json::value_from_tag, \
49-
boost::json::value& value, const name& instance) NOEXCEPT
46+
// Declares ADL free functions for use with boost-json.
47+
#define DECLARE_JSON_TAG_INVOKE(type) \
48+
BC_API type tag_invoke(to_tag<type>, const json_value& value) THROWS; \
49+
BC_API void tag_invoke(from_tag, json_value& value, \
50+
const type& instance) THROWS
51+
52+
// Define ADL free functions for use with boost-json.
53+
#define DEFINE_JSON_TO_TAG(type) \
54+
type tag_invoke(to_tag<type>, const json_value& value) THROWS
55+
#define DEFINE_JSON_FROM_TAG(type) \
56+
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED) \
57+
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR) \
58+
void tag_invoke(from_tag, json_value& value, const type& instance) THROWS \
59+
BC_POP_WARNING() \
60+
BC_POP_WARNING()
5061

5162
namespace libbitcoin {
52-
5363
namespace system {
5464
namespace asio {
65+
/// asio aliases
5566
typedef boost::asio::ip::address address;
5667
typedef boost::asio::ip::tcp::endpoint endpoint;
57-
} // system
5868
} // asio
5969

70+
} // system
71+
72+
/// json aliases
73+
template <typename Value, typename ...Args>
74+
inline auto value_to(Args&&... args) THROWS
75+
{
76+
return boost::json::value_to<Value>(std::forward<Args>(args)...);
77+
}
78+
template <typename ...Args>
79+
inline auto value_from(Args&&... args) THROWS
80+
{
81+
return boost::json::value_from(std::forward<Args>(args)...);
82+
}
83+
template <typename Type>
84+
using to_tag = boost::json::value_to_tag<Type>;
85+
typedef boost::json::value_from_tag from_tag;
86+
typedef boost::json::object json_object;
87+
typedef boost::json::value json_value;
88+
89+
/// program_options aliases
6090
typedef boost::program_options::variables_map variables_map;
6191
typedef boost::program_options::option_description option_metadata;
6292
typedef boost::program_options::options_description options_metadata;
6393
typedef boost::program_options::positional_options_description arguments_metadata;
64-
6594
} // namespace libbitcoin
6695

6796
#endif

include/bitcoin/system/chain/block.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ class BC_API block
232232

233233
typedef std_vector<block> blocks;
234234

235-
DECLARE_JSON_VALUE_CONVERTORS(block);
236-
DECLARE_JSON_VALUE_CONVERTORS(block::cptr);
235+
DECLARE_JSON_TAG_INVOKE(block);
236+
DECLARE_JSON_TAG_INVOKE(block::cptr);
237237

238238
} // namespace chain
239239
} // namespace system

include/bitcoin/system/chain/checkpoint.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ std::ostream& operator<<(std::ostream& stream, const checkpoint& in) NOEXCEPT;
103103

104104
typedef std::vector<checkpoint> checkpoints;
105105

106-
DECLARE_JSON_VALUE_CONVERTORS(checkpoint);
106+
////DECLARE_JSON_TAG_INVOKE(hash_digest);
107+
DECLARE_JSON_TAG_INVOKE(checkpoint);
107108

108109
} // namespace chain
109110
} // namespace system

include/bitcoin/system/chain/enums/opcode.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,14 @@ BC_API std::string opcode_to_mnemonic(opcode value,
310310

311311
/// Convert a string to an opcode.
312312
BC_API bool opcode_from_mnemonic(opcode& out_code,
313-
const std::string& value) NOEXCEPT;
313+
const std::string_view& value) NOEXCEPT;
314314

315315
/// Convert any opcode to a string hexadecimal representation.
316316
BC_API std::string opcode_to_hexadecimal(opcode code) NOEXCEPT;
317317

318318
/// Convert any hexadecimal byte to an opcode.
319319
BC_API bool opcode_from_hexadecimal(opcode& out_code,
320-
const std::string& value) NOEXCEPT;
320+
const std::string_view& value) NOEXCEPT;
321321

322322
} // namespace chain
323323
} // namespace system

include/bitcoin/system/chain/header.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ typedef std_vector<header::cptr> header_cptrs;
168168
typedef std::shared_ptr<header_cptrs> headers_ptr;
169169
typedef std::shared_ptr<const header_cptrs> headers_cptr;
170170

171-
DECLARE_JSON_VALUE_CONVERTORS(header);
172-
DECLARE_JSON_VALUE_CONVERTORS(header::cptr);
171+
DECLARE_JSON_TAG_INVOKE(header);
172+
DECLARE_JSON_TAG_INVOKE(header::cptr);
173173

174174
} // namespace chain
175175
} // namespace system

include/bitcoin/system/chain/input.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ BC_API bool operator<(const cref_point& left, const cref_point& right) NOEXCEPT;
172172
BC_API bool operator==(const cref_point& left, const cref_point& right) NOEXCEPT;
173173
BC_API bool operator!=(const cref_point& left, const cref_point& right) NOEXCEPT;
174174

175-
DECLARE_JSON_VALUE_CONVERTORS(input);
176-
DECLARE_JSON_VALUE_CONVERTORS(input::cptr);
175+
DECLARE_JSON_TAG_INVOKE(input);
176+
DECLARE_JSON_TAG_INVOKE(input::cptr);
177177

178178
} // namespace chain
179179
} // namespace system

include/bitcoin/system/chain/operation.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ class BC_API operation
117117
// TODO: byte size cannot overflow, but can underflow (remains valid).
118118
// TODO: but mnemonic decoding may be invalid. moving this to config
119119
// TODO: allows string decoding to throw, consistent with other config.
120-
/// Literal string is disallowed, as it conflicts with const data_slice&.
121-
explicit operation(const std::string& mnemonic) NOEXCEPT;
120+
operation(const std::string_view& mnemonic) NOEXCEPT;
122121

123122
/// Operators.
124123
/// -----------------------------------------------------------------------
@@ -161,7 +160,7 @@ class BC_API operation
161160
bool minimal) NOEXCEPT;
162161

163162
// TODO: move to config serialization wrapper.
164-
static operation from_string(const std::string& mnemonic) NOEXCEPT;
163+
static operation from_string(const std::string_view& mnemonic) NOEXCEPT;
165164

166165
static const data_chunk& no_data() NOEXCEPT;
167166
static const chunk_cptr& no_data_cptr() NOEXCEPT;
@@ -189,8 +188,8 @@ class BC_API operation
189188

190189
typedef std_vector<operation> operations;
191190

192-
DECLARE_JSON_VALUE_CONVERTORS(operation);
193-
DECLARE_JSON_VALUE_CONVERTORS(operation::cptr);
191+
DECLARE_JSON_TAG_INVOKE(operation);
192+
DECLARE_JSON_TAG_INVOKE(operation::cptr);
194193

195194
} // namespace chain
196195
} // namespace system

include/bitcoin/system/chain/output.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ typedef std::shared_ptr<const output_cptrs> outputs_cptr;
124124
/// Constant reference optimizers.
125125
using output_cptr_cref = std::reference_wrapper<const output::cptr>;
126126

127-
DECLARE_JSON_VALUE_CONVERTORS(output);
128-
DECLARE_JSON_VALUE_CONVERTORS(output::cptr);
127+
DECLARE_JSON_TAG_INVOKE(output);
128+
DECLARE_JSON_TAG_INVOKE(output::cptr);
129129

130130
} // namespace chain
131131
} // namespace system

include/bitcoin/system/chain/point.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ BC_API bool operator<(const point_cref& left, const point_cref& right) NOEXCEPT;
117117
BC_API bool operator==(const point_cref& left, const point_cref& right) NOEXCEPT;
118118
BC_API bool operator!=(const point_cref& left, const point_cref& right) NOEXCEPT;
119119

120-
DECLARE_JSON_VALUE_CONVERTORS(point);
121-
DECLARE_JSON_VALUE_CONVERTORS(point::cptr);
120+
DECLARE_JSON_TAG_INVOKE(point);
121+
DECLARE_JSON_TAG_INVOKE(point::cptr);
122122

123123
} // namespace chain
124124
} // namespace system

include/bitcoin/system/chain/script.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class BC_API script
110110
script(reader& source, bool prefix) NOEXCEPT;
111111

112112
// TODO: move to config serialization wrapper.
113-
script(const std::string& mnemonic) NOEXCEPT;
113+
script(const std::string_view& mnemonic) NOEXCEPT;
114114

115115
/// Operators.
116116
/// -----------------------------------------------------------------------
@@ -174,7 +174,7 @@ class BC_API script
174174
static inline size_t op_size(size_t total, const operation& op) NOEXCEPT;
175175
static script from_operations(operations&& ops) NOEXCEPT;
176176
static script from_operations(const operations& ops) NOEXCEPT;
177-
static script from_string(const std::string& mnemonic) NOEXCEPT;
177+
static script from_string(const std::string_view& mnemonic) NOEXCEPT;
178178
static size_t op_count(reader& source) NOEXCEPT;
179179
static size_t serialized_size(const operations& ops) NOEXCEPT;
180180
void assign_data(reader& source, bool prefix) NOEXCEPT;
@@ -198,8 +198,8 @@ class BC_API script
198198

199199
typedef std_vector<script> scripts;
200200

201-
DECLARE_JSON_VALUE_CONVERTORS(script);
202-
DECLARE_JSON_VALUE_CONVERTORS(script::cptr);
201+
DECLARE_JSON_TAG_INVOKE(script);
202+
DECLARE_JSON_TAG_INVOKE(script::cptr);
203203

204204
} // namespace chain
205205
} // namespace system

0 commit comments

Comments
 (0)