Skip to content

Commit 602b038

Browse files
committed
Merge #21366: refactor: replace util::Ref with std::any (C++17)
916ab01 remove unused class util::Ref and its unit test (Sebastian Falbesoner) 8dbb87a refactor: replace util::Ref by std::any (C++17) (Sebastian Falbesoner) 95cccf8 util: introduce helper AnyPtr to access std::any instances (Sebastian Falbesoner) Pull request description: As described in `util/ref.h`: "_This implements a small subset of the functionality in C++17's std::any class, and **can be dropped when the project updates to C++17**_". For accessing the contained object of a `std::any` instance, a helper template function `AnyPtr` is introduced (thanks to ryanofsky). ACKs for top commit: hebasto: re-ACK 916ab01, with command ryanofsky: Code review ACK 916ab01. Changes since last review: rebase and replacing types with `auto`. I might have used `const auto*` and `auto*` instead of plain `auto` because I think the qualifiers are useful, but this is all good. Tree-SHA512: fe2c3e4f5726f8ad40c61128339bb24ad11d2c261f71f7b934b1efe3e3279df14046452b0d9b566917ef61d5c7e0fd96ccbf35ff810357e305710f5002c27d47
2 parents b144620 + 916ab01 commit 602b038

23 files changed

+90
-156
lines changed

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ BITCOIN_CORE_H = \
243243
util/moneystr.h \
244244
util/rbf.h \
245245
util/readwritefile.h \
246-
util/ref.h \
247246
util/settings.h \
248247
util/sock.h \
249248
util/spanparsing.h \

src/Makefile.test.include

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ BITCOIN_TESTS =\
112112
test/prevector_tests.cpp \
113113
test/raii_event_tests.cpp \
114114
test/random_tests.cpp \
115-
test/ref_tests.cpp \
116115
test/reverselock_tests.cpp \
117116
test/rpc_tests.cpp \
118117
test/sanity_tests.cpp \

src/bitcoind.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
#include <node/ui_interface.h>
1717
#include <noui.h>
1818
#include <shutdown.h>
19-
#include <util/ref.h>
19+
#include <util/check.h>
2020
#include <util/strencodings.h>
2121
#include <util/system.h>
2222
#include <util/threadnames.h>
2323
#include <util/tokenpipe.h>
2424
#include <util/translation.h>
2525
#include <util/url.h>
2626

27+
#include <any>
2728
#include <functional>
2829
#include <optional>
2930

@@ -142,7 +143,7 @@ static bool AppInit(int argc, char* argv[])
142143
// end, which is interpreted as failure to start.
143144
TokenPipeEnd daemon_ep;
144145
#endif
145-
util::Ref context{node};
146+
std::any context{&node};
146147
try
147148
{
148149
if (!CheckDataDirOption()) {

src/httprpc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
144144
return multiUserAuthorized(strUserPass);
145145
}
146146

147-
static bool HTTPReq_JSONRPC(const util::Ref& context, HTTPRequest* req)
147+
static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
148148
{
149149
// JSONRPC handles only POST
150150
if (req->GetRequestMethod() != HTTPRequest::POST) {
@@ -288,7 +288,7 @@ static bool InitRPCAuthentication()
288288
return true;
289289
}
290290

291-
bool StartHTTPRPC(const util::Ref& context)
291+
bool StartHTTPRPC(const std::any& context)
292292
{
293293
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
294294
if (!InitRPCAuthentication())

src/httprpc.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
#ifndef BITCOIN_HTTPRPC_H
66
#define BITCOIN_HTTPRPC_H
77

8-
namespace util {
9-
class Ref;
10-
} // namespace util
8+
#include <any>
119

1210
/** Start HTTP RPC subsystem.
1311
* Precondition; HTTP and RPC has been started.
1412
*/
15-
bool StartHTTPRPC(const util::Ref& context);
13+
bool StartHTTPRPC(const std::any& context);
1614
/** Interrupt HTTP RPC subsystem.
1715
*/
1816
void InterruptHTTPRPC();
@@ -24,7 +22,7 @@ void StopHTTPRPC();
2422
/** Start HTTP REST subsystem.
2523
* Precondition; HTTP and RPC has been started.
2624
*/
27-
void StartREST(const util::Ref& context);
25+
void StartREST(const std::any& context);
2826
/** Interrupt RPC REST subsystem.
2927
*/
3028
void InterruptREST();

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ static bool InitSanityCheck()
788788
return true;
789789
}
790790

791-
static bool AppInitServers(const util::Ref& context, NodeContext& node)
791+
static bool AppInitServers(const std::any& context, NodeContext& node)
792792
{
793793
const ArgsManager& args = *Assert(node.args);
794794
RPCServer::OnStarted(&OnRPCStarted);
@@ -1277,7 +1277,7 @@ bool AppInitInterfaces(NodeContext& node)
12771277
return true;
12781278
}
12791279

1280-
bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1280+
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12811281
{
12821282
const ArgsManager& args = *Assert(node.args);
12831283
const CChainParams& chainparams = Params();

src/init.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_INIT_H
77
#define BITCOIN_INIT_H
88

9+
#include <any>
910
#include <memory>
1011
#include <string>
1112

@@ -22,9 +23,6 @@ struct BlockAndHeaderTipInfo;
2223
namespace boost {
2324
class thread_group;
2425
} // namespace boost
25-
namespace util {
26-
class Ref;
27-
} // namespace util
2826

2927
/** Interrupt threads */
3028
void Interrupt(NodeContext& node);
@@ -66,7 +64,7 @@ bool AppInitInterfaces(NodeContext& node);
6664
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
6765
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
6866
*/
69-
bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
67+
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
7068

7169
/**
7270
* Register all arguments with the ArgsManager

src/node/interfaces.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <uint256.h>
3939
#include <univalue.h>
4040
#include <util/check.h>
41-
#include <util/ref.h>
4241
#include <util/system.h>
4342
#include <util/translation.h>
4443
#include <validation.h>
@@ -49,6 +48,7 @@
4948
#include <config/bitcoin-config.h>
5049
#endif
5150

51+
#include <any>
5252
#include <memory>
5353
#include <optional>
5454
#include <utility>
@@ -298,13 +298,13 @@ class NodeImpl : public Node
298298
{
299299
m_context = context;
300300
if (context) {
301-
m_context_ref.Set(*context);
301+
m_context_ref = context;
302302
} else {
303-
m_context_ref.Clear();
303+
m_context_ref.reset();
304304
}
305305
}
306306
NodeContext* m_context{nullptr};
307-
util::Ref m_context_ref;
307+
std::any m_context_ref;
308308
};
309309

310310
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)

src/rest.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
#include <sync.h>
1919
#include <txmempool.h>
2020
#include <util/check.h>
21-
#include <util/ref.h>
21+
#include <util/system.h>
2222
#include <validation.h>
2323
#include <version.h>
2424

25+
#include <any>
26+
2527
#include <boost/algorithm/string.hpp>
2628

2729
#include <univalue.h>
@@ -73,18 +75,18 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
7375
* context is not found.
7476
* @returns Pointer to the node context or nullptr if not found.
7577
*/
76-
static NodeContext* GetNodeContext(const util::Ref& context, HTTPRequest* req)
78+
static NodeContext* GetNodeContext(const std::any& context, HTTPRequest* req)
7779
{
78-
NodeContext* node = context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr;
79-
if (!node) {
80+
auto node_context = util::AnyPtr<NodeContext>(context);
81+
if (!node_context) {
8082
RESTERR(req, HTTP_INTERNAL_SERVER_ERROR,
8183
strprintf("%s:%d (%s)\n"
8284
"Internal bug detected: Node context not found!\n"
8385
"You may report this issue here: %s\n",
8486
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT));
8587
return nullptr;
8688
}
87-
return node;
89+
return node_context;
8890
}
8991

9092
/**
@@ -94,14 +96,14 @@ static NodeContext* GetNodeContext(const util::Ref& context, HTTPRequest* req)
9496
* context mempool is not found.
9597
* @returns Pointer to the mempool or nullptr if no mempool found.
9698
*/
97-
static CTxMemPool* GetMemPool(const util::Ref& context, HTTPRequest* req)
99+
static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req)
98100
{
99-
NodeContext* node = context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr;
100-
if (!node || !node->mempool) {
101+
auto node_context = util::AnyPtr<NodeContext>(context);
102+
if (!node_context || !node_context->mempool) {
101103
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
102104
return nullptr;
103105
}
104-
return node->mempool.get();
106+
return node_context->mempool.get();
105107
}
106108

107109
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
@@ -151,7 +153,7 @@ static bool CheckWarmup(HTTPRequest* req)
151153
return true;
152154
}
153155

154-
static bool rest_headers(const util::Ref& context,
156+
static bool rest_headers(const std::any& context,
155157
HTTPRequest* req,
156158
const std::string& strURIPart)
157159
{
@@ -293,20 +295,20 @@ static bool rest_block(HTTPRequest* req,
293295
}
294296
}
295297

296-
static bool rest_block_extended(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
298+
static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
297299
{
298300
return rest_block(req, strURIPart, true);
299301
}
300302

301-
static bool rest_block_notxdetails(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
303+
static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
302304
{
303305
return rest_block(req, strURIPart, false);
304306
}
305307

306308
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
307309
RPCHelpMan getblockchaininfo();
308310

309-
static bool rest_chaininfo(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
311+
static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
310312
{
311313
if (!CheckWarmup(req))
312314
return false;
@@ -329,7 +331,7 @@ static bool rest_chaininfo(const util::Ref& context, HTTPRequest* req, const std
329331
}
330332
}
331333

332-
static bool rest_mempool_info(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
334+
static bool rest_mempool_info(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
333335
{
334336
if (!CheckWarmup(req))
335337
return false;
@@ -353,7 +355,7 @@ static bool rest_mempool_info(const util::Ref& context, HTTPRequest* req, const
353355
}
354356
}
355357

356-
static bool rest_mempool_contents(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
358+
static bool rest_mempool_contents(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
357359
{
358360
if (!CheckWarmup(req)) return false;
359361
const CTxMemPool* mempool = GetMemPool(context, req);
@@ -376,7 +378,7 @@ static bool rest_mempool_contents(const util::Ref& context, HTTPRequest* req, co
376378
}
377379
}
378380

379-
static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
381+
static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
380382
{
381383
if (!CheckWarmup(req))
382384
return false;
@@ -435,7 +437,7 @@ static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::strin
435437
}
436438
}
437439

438-
static bool rest_getutxos(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
440+
static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
439441
{
440442
if (!CheckWarmup(req))
441443
return false;
@@ -621,7 +623,7 @@ static bool rest_getutxos(const util::Ref& context, HTTPRequest* req, const std:
621623
}
622624
}
623625

624-
static bool rest_blockhash_by_height(const util::Ref& context, HTTPRequest* req,
626+
static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
625627
const std::string& str_uri_part)
626628
{
627629
if (!CheckWarmup(req)) return false;
@@ -669,7 +671,7 @@ static bool rest_blockhash_by_height(const util::Ref& context, HTTPRequest* req,
669671

670672
static const struct {
671673
const char* prefix;
672-
bool (*handler)(const util::Ref& context, HTTPRequest* req, const std::string& strReq);
674+
bool (*handler)(const std::any& context, HTTPRequest* req, const std::string& strReq);
673675
} uri_prefixes[] = {
674676
{"/rest/tx/", rest_tx},
675677
{"/rest/block/notxdetails/", rest_block_notxdetails},
@@ -682,7 +684,7 @@ static const struct {
682684
{"/rest/blockhashbyheight/", rest_blockhash_by_height},
683685
};
684686

685-
void StartREST(const util::Ref& context)
687+
void StartREST(const std::any& context)
686688
{
687689
for (const auto& up : uri_prefixes) {
688690
auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };

src/rpc/blockchain.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <txdb.h>
3131
#include <txmempool.h>
3232
#include <undo.h>
33-
#include <util/ref.h>
3433
#include <util/strencodings.h>
3534
#include <util/system.h>
3635
#include <util/translation.h>
@@ -56,15 +55,16 @@ static Mutex cs_blockchange;
5655
static std::condition_variable cond_blockchange;
5756
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange);
5857

59-
NodeContext& EnsureNodeContext(const util::Ref& context)
58+
NodeContext& EnsureNodeContext(const std::any& context)
6059
{
61-
if (!context.Has<NodeContext>()) {
60+
auto node_context = util::AnyPtr<NodeContext>(context);
61+
if (!node_context) {
6262
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
6363
}
64-
return context.Get<NodeContext>();
64+
return *node_context;
6565
}
6666

67-
CTxMemPool& EnsureMemPool(const util::Ref& context)
67+
CTxMemPool& EnsureMemPool(const std::any& context)
6868
{
6969
const NodeContext& node = EnsureNodeContext(context);
7070
if (!node.mempool) {
@@ -73,7 +73,7 @@ CTxMemPool& EnsureMemPool(const util::Ref& context)
7373
return *node.mempool;
7474
}
7575

76-
ChainstateManager& EnsureChainman(const util::Ref& context)
76+
ChainstateManager& EnsureChainman(const std::any& context)
7777
{
7878
const NodeContext& node = EnsureNodeContext(context);
7979
if (!node.chainman) {
@@ -82,7 +82,7 @@ ChainstateManager& EnsureChainman(const util::Ref& context)
8282
return *node.chainman;
8383
}
8484

85-
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context)
85+
CBlockPolicyEstimator& EnsureFeeEstimator(const std::any& context)
8686
{
8787
NodeContext& node = EnsureNodeContext(context);
8888
if (!node.fee_estimator) {

0 commit comments

Comments
 (0)