Skip to content

Commit

Permalink
Merge branch 'add_defer_functor_util' into 'master'
Browse files Browse the repository at this point in the history
util: Refactor the Defer class to a header to be reusable

See merge request bitcoin-cash-node/bitcoin-cash-node!1183
  • Loading branch information
ftrader committed Apr 16, 2021
2 parents 0048234 + 7119bbf commit 9ea057b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ BITCOIN_CORE_H = \
ui_interface.h \
undo.h \
util/bitmanip.h \
util/defer.h \
util/moneystr.h \
util/saltedhashers.h \
util/system.h \
Expand Down
9 changes: 1 addition & 8 deletions src/test/rpc_server_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include <chainparams.h>
#include <config.h>
#include <software_outdated.h>
#include <util/defer.h>
#include <util/system.h>

#include <test/setup_common.h>

#include <boost/test/unit_test.hpp>

#include <functional>
#include <string>

BOOST_FIXTURE_TEST_SUITE(rpc_server_tests, TestingSetup)
Expand All @@ -38,13 +38,6 @@ static bool isRpcDisabled(const JSONRPCError &u) {
return u.code == RPC_DISABLED;
}

struct Defer {
const std::function<void()> func;
template <typename F>
Defer(F && f) : func(f) {}
~Defer() { func(); }
};

BOOST_AUTO_TEST_CASE(rpc_server_execute_command) {
DummyConfig config;
RPCServer rpcServer;
Expand Down
19 changes: 19 additions & 0 deletions src/util/defer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2021 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_UTIL_DEFER_H
#define BITCOIN_UTIL_DEFER_H

#include <utility>

/// Leverage RAII to run a functor at scope end
template <typename Func>
struct Defer {
Func func;
Defer(Func && f) : func(std::move(f)) {}
~Defer() { func(); }
};


#endif // BITCOIN_UTIL_DEFER_H
11 changes: 1 addition & 10 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <txmempool.h>
#include <ui_interface.h>
#include <undo.h>
#include <util/defer.h>
#include <util/moneystr.h>
#include <util/strencodings.h>
#include <util/system.h>
Expand Down Expand Up @@ -3178,16 +3179,6 @@ bool PreciousBlock(const Config &config, CValidationState &state,
return g_chainstate.PreciousBlock(config, state, pindex);
}

namespace {
// Leverage RAII to run a functor at scope end
template <typename Func>
struct Defer {
Func func;
Defer(Func && f) : func(std::move(f)) {}
~Defer() { func(); }
};
} // namespace

bool CChainState::UnwindBlock(const Config &config, CValidationState &state,
CBlockIndex *pindex, bool invalidate) {
CBlockIndex *to_mark_failed_or_parked = pindex;
Expand Down

0 comments on commit 9ea057b

Please sign in to comment.