diff --git a/src/httprpc.cpp b/src/httprpc.cpp --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -393,8 +393,7 @@ RegisterHTTPHandler("/wallet/", false, rpcFunction); #endif assert(EventBase()); - httpRPCTimerInterface = std::unique_ptr( - new HTTPRPCTimerInterface(EventBase())); + httpRPCTimerInterface = MakeUnique(EventBase()); RPCSetTimerInterface(httpRPCTimerInterface.get()); return true; } diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -2459,12 +2459,12 @@ if (semOutbound == nullptr) { // initialize semaphore - semOutbound = std::unique_ptr(new CSemaphore( - std::min((nMaxOutbound + nMaxFeeler), nMaxConnections))); + semOutbound = MakeUnique( + std::min((nMaxOutbound + nMaxFeeler), nMaxConnections)); } if (semAddnode == nullptr) { // initialize semaphore - semAddnode = std::unique_ptr(new CSemaphore(nMaxAddnode)); + semAddnode = MakeUnique(nMaxAddnode); } // @@ -2886,7 +2886,7 @@ nNextInvSend = 0; fRelayTxes = false; fSentAddr = false; - pfilter = std::unique_ptr(new CBloomFilter()); + pfilter = MakeUnique(); timeLastMempoolReq = 0; nLastBlockTime = 0; nLastTXTime = 0; diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -123,8 +123,8 @@ create_directories(ph); // Set up a non-obfuscated wrapper to write some initial data. - std::unique_ptr dbw = std::unique_ptr( - new CDBWrapper(ph, (1 << 10), false, false, false)); + std::unique_ptr dbw = + MakeUnique(ph, (1 << 10), false, false, false); char key = 'k'; uint256 in = InsecureRand256(); uint256 res; @@ -166,8 +166,8 @@ create_directories(ph); // Set up a non-obfuscated wrapper to write some initial data. - std::unique_ptr dbw = std::unique_ptr( - new CDBWrapper(ph, (1 << 10), false, false, false)); + std::unique_ptr dbw = + MakeUnique(ph, (1 << 10), false, false, false); char key = 'k'; uint256 in = InsecureRand256(); uint256 res; diff --git a/src/util.h b/src/util.h --- a/src/util.h +++ b/src/util.h @@ -258,4 +258,10 @@ std::string CopyrightHolders(const std::string &strPrefix); +//! Substitute for C++14 std::make_unique. +template +std::unique_ptr MakeUnique(Args &&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + #endif // BITCOIN_UTIL_H diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -189,7 +189,7 @@ } LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); - std::unique_ptr pdbCopy(new Db(bitdb.dbenv.get(), 0)); + std::unique_ptr pdbCopy = MakeUnique(bitdb.dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer filename.c_str(), // Filename "main", // Logical db name @@ -397,7 +397,7 @@ pdb = env->mapDb[strFilename]; if (pdb == nullptr) { - std::unique_ptr pdb_temp(new Db(env->dbenv.get(), 0)); + std::unique_ptr pdb_temp = MakeUnique(env->dbenv.get(), 0); bool fMockDb = env->IsMock(); if (fMockDb) { @@ -512,7 +512,7 @@ // surround usage of db with extra {} CDB db(dbw, "r"); std::unique_ptr pdbCopy = - std::unique_ptr(new Db(env->dbenv.get(), 0)); + MakeUnique(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp --- a/src/wallet/test/wallet_test_fixture.cpp +++ b/src/wallet/test/wallet_test_fixture.cpp @@ -19,8 +19,7 @@ bool fFirstRun; std::unique_ptr dbw( new CWalletDBWrapper(&bitdb, "wallet_test.dat")); - pwalletMain = - std::unique_ptr(new CWallet(Params(), std::move(dbw))); + pwalletMain = MakeUnique(Params(), std::move(dbw)); pwalletMain->LoadWallet(fFirstRun); RegisterValidationInterface(pwalletMain.get()); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3977,8 +3977,8 @@ std::unique_ptr dbw( new CWalletDBWrapper(&bitdb, walletFile)); - std::unique_ptr tempWallet( - new CWallet(chainParams, std::move(dbw))); + std::unique_ptr tempWallet = + MakeUnique(chainParams, std::move(dbw)); DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx); if (nZapWalletRet != DB_LOAD_OK) { InitError(