From bbff09f321b3d11799a3afd1d6890caa3bc6b220 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 26 Jul 2019 18:43:26 -0400 Subject: [PATCH] use make_shared instead of new --- src/daemon/MoneroDaemonModel.cpp | 4 +- src/utils/MoneroUtils.cpp | 22 +++++------ src/wallet/MoneroWallet.cpp | 66 ++++++++++++++++---------------- todo.txt | 4 +- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/daemon/MoneroDaemonModel.cpp b/src/daemon/MoneroDaemonModel.cpp index 5348192..82ace5c 100644 --- a/src/daemon/MoneroDaemonModel.cpp +++ b/src/daemon/MoneroDaemonModel.cpp @@ -229,12 +229,12 @@ namespace monero { // merge blocks if they're different which comes back to merging txs if (block != other->block) { if (block == boost::none) { - block = shared_ptr(new MoneroBlock()); + block = make_shared(); block.get()->txs.push_back(self); block.get()->height = other->getHeight(); } if (other->block == boost::none) { - other->block = shared_ptr(new MoneroBlock()); + other->block = make_shared(); other->block.get()->txs.push_back(other); other->block.get()->height = self->getHeight(); } diff --git a/src/utils/MoneroUtils.cpp b/src/utils/MoneroUtils.cpp index c6a8178..0daf32e 100644 --- a/src/utils/MoneroUtils.cpp +++ b/src/utils/MoneroUtils.cpp @@ -79,7 +79,7 @@ void nodeToTransfer(const boost::property_tree::ptree& node, shared_ptr nodeToTransferRequest(const boost::property_tree::ptree& node) { - shared_ptr transferRequest = shared_ptr(new MoneroTransferRequest()); + shared_ptr transferRequest = make_shared(); nodeToTransfer(node, transferRequest); // initialize request from node @@ -105,7 +105,7 @@ shared_ptr nodeToTransferRequest(const boost::property_tr shared_ptr nodeToKeyImage(const boost::property_tree::ptree& node) { // initialize key image from node - shared_ptr keyImage = shared_ptr(new MoneroKeyImage()); + shared_ptr keyImage = make_shared(); for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { string key = it->first; if (key == string("hex")) keyImage->hex = it->second.data(); @@ -141,7 +141,7 @@ void nodeToOutputWallet(const boost::property_tree::ptree& node, shared_ptr nodeToOutputRequest(const boost::property_tree::ptree& node) { - shared_ptr outputRequest = shared_ptr(new MoneroOutputRequest()); + shared_ptr outputRequest = make_shared(); nodeToOutputWallet(node, outputRequest); // initialize request from node @@ -210,7 +210,7 @@ void nodeToTxWallet(const boost::property_tree::ptree& node, shared_ptr nodeToTxRequest(const boost::property_tree::ptree& node) { - shared_ptr txRequest = shared_ptr(new MoneroTxRequest()); + shared_ptr txRequest = make_shared(); nodeToTxWallet(node, txRequest); // initialize request from node @@ -233,7 +233,7 @@ shared_ptr nodeToTxRequest(const boost::property_tree::ptree& n } shared_ptr nodeToBlockRequest(const boost::property_tree::ptree& node) { - shared_ptr block = shared_ptr(new MoneroBlock()); + shared_ptr block = make_shared(); for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { string key = it->first; if (key == string("height")) block->height = (uint64_t) 7; // TODO @@ -248,7 +248,7 @@ shared_ptr nodeToBlockRequest(const boost::property_tree::ptree& no } shared_ptr nodeToDestination(const boost::property_tree::ptree& node) { - shared_ptr destination = shared_ptr(new MoneroDestination()); + shared_ptr destination = make_shared(); for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { string key = it->first; if (key == string("address")) destination->address = it->second.data(); @@ -351,13 +351,13 @@ shared_ptr MoneroUtils::deserializeTransferRequest(const shared_ptr block = nodeToBlockRequest(blockNode); // return mpty request if no txs - if (block->txs.empty()) return shared_ptr(new MoneroTransferRequest()); + if (block->txs.empty()) return make_shared(); // get tx request shared_ptr txRequest = static_pointer_cast(block->txs[0]); // get / create transfer request - shared_ptr transferRequest = txRequest->transferRequest == boost::none ? shared_ptr(new MoneroTransferRequest()) : *txRequest->transferRequest; + shared_ptr transferRequest = txRequest->transferRequest == boost::none ? make_shared() : *txRequest->transferRequest; // transfer request references tx request but not the other way around to avoid circular loop // TODO: could add check within meetsCriterias() transferRequest->txRequest = txRequest; @@ -378,13 +378,13 @@ shared_ptr MoneroUtils::deserializeOutputRequest(const stri shared_ptr block = nodeToBlockRequest(blockNode); // empty request if no txs - if (block->txs.empty()) return shared_ptr(new MoneroOutputRequest()); + if (block->txs.empty()) return make_shared(); // get tx request shared_ptr txRequest = static_pointer_cast(block->txs[0]); // get / create output request - shared_ptr outputRequest = txRequest->outputRequest == boost::none ? shared_ptr(new MoneroOutputRequest()) : *txRequest->outputRequest; + shared_ptr outputRequest = txRequest->outputRequest == boost::none ? make_shared() : *txRequest->outputRequest; // output request references tx request but not the other way around to avoid circular loop // TODO: could add check within meetsCriterias() outputRequest->txRequest = txRequest; @@ -402,7 +402,7 @@ shared_ptr MoneroUtils::deserializeSendRequest(const string& boost::property_tree::read_json(iss, node); // convert request property tree to MoneroSendRequest - shared_ptr sendRequest = shared_ptr(new MoneroSendRequest()); + shared_ptr sendRequest = make_shared(); for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { string key = it->first; if (key == string("destinations")) { diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index 0041b8c..aacaa11 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -89,12 +89,12 @@ namespace monero { shared_ptr buildTxWithIncomingTransfer(const tools::wallet2& wallet2, uint64_t height, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd) { // construct block - shared_ptr block = shared_ptr(new MoneroBlock()); + shared_ptr block = make_shared(); block->height = pd.m_block_height; block->timestamp = pd.m_timestamp; // construct tx - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); tx->block = block; block->txs.push_back(tx); tx->id = string_tools::pod_to_hex(pd.m_tx_hash); @@ -119,7 +119,7 @@ namespace monero { else tx->numConfirmations = height - *block->height; // construct transfer - shared_ptr incomingTransfer = shared_ptr(new MoneroIncomingTransfer()); + shared_ptr incomingTransfer = make_shared(); incomingTransfer->tx = tx; tx->incomingTransfers.push_back(incomingTransfer); incomingTransfer->amount = pd.m_amount; @@ -140,12 +140,12 @@ namespace monero { shared_ptr buildTxWithOutgoingTransfer(const tools::wallet2& wallet2, uint64_t height, const crypto::hash &txid, const tools::wallet2::confirmed_transfer_details &pd) { // construct block - shared_ptr block = shared_ptr(new MoneroBlock()); + shared_ptr block = make_shared(); block->height = pd.m_block_height; block->timestamp = pd.m_timestamp; // construct tx - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); tx->block = block; block->txs.push_back(tx); tx->id = string_tools::pod_to_hex(txid); @@ -169,7 +169,7 @@ namespace monero { else tx->numConfirmations = height - *block->height; // construct transfer - shared_ptr outgoingTransfer = shared_ptr(new MoneroOutgoingTransfer()); + shared_ptr outgoingTransfer = make_shared(); outgoingTransfer->tx = tx; tx->outgoingTransfer = outgoingTransfer; uint64_t change = pd.m_change == (uint64_t)-1 ? 0 : pd.m_change; // change may not be known @@ -186,7 +186,7 @@ namespace monero { // initialize destinations for (const auto &d: pd.m_dests) { - shared_ptr destination = shared_ptr(new MoneroDestination()); + shared_ptr destination = make_shared(); destination->amount = d.amount; destination->address = d.original.empty() ? cryptonote::get_account_address_as_str(wallet2.nettype(), d.is_subaddress, d.addr) : d.original; outgoingTransfer->destinations.push_back(destination); @@ -213,7 +213,7 @@ namespace monero { // construct tx const tools::wallet2::payment_details &pd = ppd.m_pd; - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); tx->id = string_tools::pod_to_hex(pd.m_tx_hash); tx->paymentId = string_tools::pod_to_hex(payment_id); if (tx->paymentId->substr(16).find_first_not_of('0') == std::string::npos) tx->paymentId = tx->paymentId->substr(0, 16); // TODO monero core: this should be part of core wallet @@ -232,7 +232,7 @@ namespace monero { tx->numConfirmations = 0; // construct transfer - shared_ptr incomingTransfer = shared_ptr(new MoneroIncomingTransfer()); + shared_ptr incomingTransfer = make_shared(); incomingTransfer->tx = tx; tx->incomingTransfers.push_back(incomingTransfer); incomingTransfer->amount = pd.m_amount; @@ -252,7 +252,7 @@ namespace monero { shared_ptr buildTxWithOutgoingTransferUnconfirmed(const tools::wallet2& wallet2, const crypto::hash &txid, const tools::wallet2::unconfirmed_transfer_details &pd) { // construct tx - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); tx->isFailed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed; tx->id = string_tools::pod_to_hex(txid); tx->paymentId = string_tools::pod_to_hex(pd.m_payment_id); @@ -271,7 +271,7 @@ namespace monero { tx->numConfirmations = 0; // construct transfer - shared_ptr outgoingTransfer = shared_ptr(new MoneroOutgoingTransfer()); + shared_ptr outgoingTransfer = make_shared(); outgoingTransfer->tx = tx; tx->outgoingTransfer = outgoingTransfer; outgoingTransfer->amount = pd.m_amount_in - pd.m_change - tx->fee.get(); @@ -287,7 +287,7 @@ namespace monero { // initialize destinations for (const auto &d: pd.m_dests) { - shared_ptr destination = shared_ptr(new MoneroDestination()); + shared_ptr destination = make_shared(); destination->amount = d.amount; destination->address = d.original.empty() ? cryptonote::get_account_address_as_str(wallet2.nettype(), d.is_subaddress, d.addr) : d.original; outgoingTransfer->destinations.push_back(destination); @@ -313,11 +313,11 @@ namespace monero { shared_ptr buildTxWithVout(const tools::wallet2& wallet2, const tools::wallet2::transfer_details& td) { // construct block - shared_ptr block = shared_ptr(new MoneroBlock()); + shared_ptr block = make_shared(); block->height = td.m_block_height; // construct tx - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); tx->block = block; block->txs.push_back(tx); tx->id = epee::string_tools::pod_to_hex(td.m_txid); @@ -329,7 +329,7 @@ namespace monero { tx->isDoubleSpendSeen = false; // construct vout - shared_ptr vout = shared_ptr(new MoneroOutputWallet()); + shared_ptr vout = make_shared(); vout->tx = tx; tx->vouts.push_back(vout); vout->amount = td.amount(); @@ -340,7 +340,7 @@ namespace monero { vout->isUnlocked = wallet2.is_transfer_unlocked(td); vout->isFrozen = td.m_frozen; if (td.m_key_image_known) { - vout->keyImage = shared_ptr(new MoneroKeyImage()); + vout->keyImage = make_shared(); vout->keyImage.get()->hex = epee::string_tools::pod_to_hex(td.m_key_image); } @@ -884,7 +884,7 @@ namespace monero { shared_ptr MoneroWallet::getDaemonConnection() const { MTRACE("MoneroWallet::getDaemonConnection()"); if (wallet2->get_daemon_address().empty()) return nullptr; - shared_ptr connection = shared_ptr(new MoneroRpcConnection()); + shared_ptr connection = make_shared(); if (!wallet2->get_daemon_address().empty()) connection->uri = wallet2->get_daemon_address(); if (wallet2->get_daemon_login()) { if (!wallet2->get_daemon_login()->username.empty()) connection->username = wallet2->get_daemon_login()->username; @@ -1277,7 +1277,7 @@ namespace monero { // normalize request // TODO: this modifies original request so given req cannot be constant, make given request constant - if (request.transferRequest == boost::none) request.transferRequest = shared_ptr(new MoneroTransferRequest()); + if (request.transferRequest == boost::none) request.transferRequest = make_shared(); shared_ptr transferRequest = request.transferRequest.get(); // temporarily disable transfer request @@ -1384,7 +1384,7 @@ namespace monero { // normalize request // TODO: this will modify original request, construct copy? add test - MoneroTxRequest txReq = *(request.txRequest != boost::none ? *request.txRequest : shared_ptr(new MoneroTxRequest())); + MoneroTxRequest txReq = *(request.txRequest != boost::none ? *request.txRequest : make_shared()); // build parameters for wallet2->get_payments() uint64_t minHeight = txReq.minHeight == boost::none ? 0 : *txReq.minHeight; @@ -1506,7 +1506,7 @@ namespace monero { // normalize request // TODO: this will modify original request, construct copy? add test - MoneroTxRequest txReq = *(request.txRequest != boost::none ? *request.txRequest : shared_ptr(new MoneroTxRequest())); + MoneroTxRequest txReq = *(request.txRequest != boost::none ? *request.txRequest : make_shared()); // get output data from wallet2 tools::wallet2::transfer_container outputsW2; @@ -1565,7 +1565,7 @@ namespace monero { vector> keyImages; std::pair>> ski = wallet2->export_key_images(true); for (size_t n = 0; n < ski.second.size(); ++n) { - shared_ptr keyImage = shared_ptr(new MoneroKeyImage()); + shared_ptr keyImage = make_shared(); keyImages.push_back(keyImage); keyImage->hex = epee::string_tools::pod_to_hex(ski.second[n].first); keyImage->signature = epee::string_tools::pod_to_hex(ski.second[n].second); @@ -1593,7 +1593,7 @@ namespace monero { uint64_t height = wallet2->import_key_images(ski, 0, spent, unspent); // TODO: use offset? refer to wallet_rpc_server::on_import_key_images() req.offset // translate results - shared_ptr result = shared_ptr(new MoneroKeyImageImportResult()); + shared_ptr result = make_shared(); result->height = height; result->spentAmount = spent; result->unspentAmount = unspent; @@ -1673,14 +1673,14 @@ namespace monero { while (txIdsIter != txIds.end()) { // init tx with outgoing transfer from filled values - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); txs.push_back(tx); tx->id = *txIdsIter; tx->key = *txKeysIter; tx->fee = *txFeesIter; tx->fullHex = *txBlobsIter; tx->metadata = *txMetadatasIter; - shared_ptr outTransfer = shared_ptr(new MoneroOutgoingTransfer()); + shared_ptr outTransfer = make_shared(); tx->outgoingTransfer = outTransfer; outTransfer->amount = *txAmountsIter; @@ -1782,14 +1782,14 @@ namespace monero { while (txIdsIter != txIds.end()) { // init tx with outgoing transfer from filled values - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); txs.push_back(tx); tx->id = *txIdsIter; tx->key = *txKeysIter; tx->fee = *txFeesIter; tx->fullHex = *txBlobsIter; tx->metadata = *txMetadatasIter; - shared_ptr outTransfer = shared_ptr(new MoneroOutgoingTransfer()); + shared_ptr outTransfer = make_shared(); tx->outgoingTransfer = outTransfer; outTransfer->amount = *txAmountsIter; @@ -1861,14 +1861,14 @@ namespace monero { while (txIdsIter != txIds.end()) { // init tx with outgoing transfer from filled values - shared_ptr tx = shared_ptr(new MoneroTxWallet()); + shared_ptr tx = make_shared(); txs.push_back(tx); tx->id = *txIdsIter; tx->key = *txKeysIter; tx->fee = *txFeesIter; tx->fullHex = *txBlobsIter; tx->metadata = *txMetadatasIter; - shared_ptr outTransfer = shared_ptr(new MoneroOutgoingTransfer()); + shared_ptr outTransfer = make_shared(); tx->outgoingTransfer = outTransfer; outTransfer->amount = *txAmountsIter; @@ -2067,7 +2067,7 @@ namespace monero { bool inTxPool; uint64_t numConfirmations; wallet2->check_tx_key(txHash, tx_key, additionalTxKeys, info.address, receivedAmount, inTxPool, numConfirmations); - shared_ptr checkTx = shared_ptr(new MoneroCheckTx()); + shared_ptr checkTx = make_shared(); checkTx->isGood = true; // check is good if we get this far checkTx->receivedAmount = receivedAmount; checkTx->inTxPool = inTxPool; @@ -2109,7 +2109,7 @@ namespace monero { } // initialize and return tx check using wallet2 - shared_ptr checkTx = shared_ptr(new MoneroCheckTx()); + shared_ptr checkTx = make_shared(); uint64_t receivedAmount; bool inTxPool; uint64_t numConfirmations; @@ -2171,7 +2171,7 @@ namespace monero { if (info.is_subaddress) throw runtime_error("Address must not be a subaddress"); // initialize check reserve using wallet2 - shared_ptr checkReserve = shared_ptr(new MoneroCheckReserve()); + shared_ptr checkReserve = make_shared(); uint64_t totalAmount; uint64_t unconfirmedSpentAmount; checkReserve->isGood = wallet2->check_reserve_proof(info.address, message, signature, totalAmount, unconfirmedSpentAmount); @@ -2220,8 +2220,8 @@ namespace monero { } // initialize send request - shared_ptr sendRequest = shared_ptr(new MoneroSendRequest()); - shared_ptr destination = shared_ptr(new MoneroDestination()); + shared_ptr sendRequest = make_shared(); + shared_ptr destination = make_shared(); sendRequest->destinations.push_back(destination); if (!address.empty()) destination->address = address; destination->amount = amount; diff --git a/todo.txt b/todo.txt index 78ef8c3..0436d7a 100644 --- a/todo.txt +++ b/todo.txt @@ -1,7 +1,6 @@ HIGH PRIORITY -nodeToTransferRequest(), etc need a final else catch-all sign/submit tx -change shared_ptr(new Foo()) to make_shared +change shared_ptr(new Foo()) to make_shared() cpp library readme with sample code code todos strong vs weak references @@ -10,6 +9,7 @@ cleanup MoneroWallet.h/MoneroDaemon.h documentation gen documentation MEDIUM PRIORITY +nodeToTransferRequest(), etc need a final else catch-all MoneroWallet getChainHeight() calling daemon connection every time, surely better way toPropertyTree() should take node as parameter; optimize memory elsewhere (move shared_ptrs, etc) enable debug logging