Skip to content

Commit

Permalink
Replace push with emplace
Browse files Browse the repository at this point in the history
  • Loading branch information
stickz committed Dec 22, 2024
1 parent 0165b4b commit 32a7af3
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion libtorrent/src/dht/dht_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class DhtTrackerList : public std::unordered_map<HashString, DhtTracker*> {

inline DhtNode*
DhtNodeList::add_node(DhtNode* n) {
insert(std::make_pair((const HashString*)n, (DhtNode*)n));
emplace((const HashString*)n, (DhtNode*)n);
return n;
}

Expand Down
14 changes: 7 additions & 7 deletions libtorrent/src/dht/dht_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ DhtRouter::DhtRouter(const Object& cache, const rak::socket_address* sa) :
LT_LOG_THIS("creating (address:%s)", sa->pretty_address_str().c_str());

set_bucket(new DhtBucket(zero_id, ones_id));
m_routingTable.insert(std::make_pair(bucket()->id_range_end(), bucket()));
m_routingTable.emplace(bucket()->id_range_end(), bucket());

if (cache.has_key("nodes")) {
const Object::map_type& nodes = cache.get_key_map("nodes");
Expand All @@ -121,7 +121,7 @@ DhtRouter::DhtRouter(const Object& cache, const rak::socket_address* sa) :
Object::list_type::const_iterator litr = itr->as_list().begin();
const std::string& host = litr->as_string();
int port = (++litr)->as_value();
m_contacts->push_back(std::make_pair(host, port));
m_contacts->emplace_back(host, port);
}
}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ DhtRouter::get_tracker(const HashString& hash, bool create) {
if (!create)
return NULL;

std::pair<DhtTrackerList::accessor, bool> res = m_trackers.insert(std::make_pair(hash, new DhtTracker()));
std::pair<DhtTrackerList::accessor, bool> res = m_trackers.emplace(hash, new DhtTracker());

if (!res.second)
throw internal_error("DhtRouter::get_tracker did not actually insert tracker.");
Expand Down Expand Up @@ -239,7 +239,7 @@ DhtRouter::add_contact(const std::string& host, int port) {
if (m_contacts->size() >= num_bootstrap_contacts)
m_contacts->pop_front();

m_contacts->push_back(std::make_pair(host, port));
m_contacts->emplace_back(host, port);
}
}

Expand Down Expand Up @@ -364,8 +364,8 @@ DhtRouter::store_cache(Object* container) const {

for (std::deque<contact_t>::const_iterator itr = m_contacts->begin(); itr != m_contacts->end(); ++itr) {
Object::list_type& list = contacts.insert_back(Object::create_list()).as_list();
list.push_back(itr->first);
list.push_back(itr->second);
list.emplace_back(itr->first);
list.emplace_back(itr->second);
}
}

Expand Down Expand Up @@ -547,7 +547,7 @@ DhtRouter::split_bucket(const DhtBucketList::iterator& itr, DhtNode* node) {
throw internal_error("DhtRouter::split_bucket router ID ended up in wrong bucket.");

// Insert new bucket with iterator hint = just before current bucket.
DhtBucketList::iterator other = m_routingTable.insert(itr, std::make_pair(newBucket->id_range_end(), newBucket));
DhtBucketList::iterator other = m_routingTable.emplace_hint(itr, newBucket->id_range_end(), newBucket);

// Check that the bucket we're not adding the node to isn't empty.
if (other->second->is_in_range(node->id())) {
Expand Down
2 changes: 1 addition & 1 deletion libtorrent/src/dht/dht_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ DhtTracker::add_peer(uint32_t addr, uint16_t port) {

// If peer doesn't exist, append to list if the table is not full.
if (size() < max_size) {
m_peers.push_back(compact);
m_peers.emplace_back(compact);
m_lastSeen.push_back(cachedTime.seconds());

// Peer doesn't exist and table is full: replace oldest peer.
Expand Down
2 changes: 1 addition & 1 deletion libtorrent/src/dht/dht_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ DhtSearch::~DhtSearch() {
bool
DhtSearch::add_contact(const HashString& id, const rak::socket_address* sa) {
DhtNode* n = new DhtNode(id, sa);
bool added = insert(std::make_pair(n, this)).second;
bool added = emplace(n, this).second;

if (!added)
delete n;
Expand Down
8 changes: 4 additions & 4 deletions libtorrent/src/download/download_constructor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ DownloadConstructor::parse_name(const Object& b) {

std::list<Path> pathList;

pathList.push_back(Path());
pathList.emplace_back();
pathList.back().set_encoding(m_defaultEncoding);
pathList.back().push_back(b.get_key_string("name"));

for (Object::map_const_iterator itr = b.as_map().begin();
(itr = std::find_if(itr, b.as_map().end(), download_constructor_is_single_path())) != b.as_map().end();
++itr) {
pathList.push_back(Path());
pathList.emplace_back();
pathList.back().set_encoding(itr->first.substr(sizeof("name.") - 1));
pathList.back().push_back(itr->second.as_string());
}
Expand Down Expand Up @@ -269,14 +269,14 @@ DownloadConstructor::parse_single_file(const Object& b, uint32_t chunkSize) {

std::list<Path> pathList;

pathList.push_back(Path());
pathList.emplace_back();
pathList.back().set_encoding(m_defaultEncoding);
pathList.back().push_back(b.get_key_string("name"));

for (Object::map_const_iterator itr = b.as_map().begin();
(itr = std::find_if(itr, b.as_map().end(), download_constructor_is_single_path())) != b.as_map().end();
++itr) {
pathList.push_back(Path());
pathList.emplace_back();
pathList.back().set_encoding(itr->first.substr(sizeof("name.") - 1));
pathList.back().push_back(itr->second.as_string());
}
Expand Down
2 changes: 1 addition & 1 deletion libtorrent/src/download/download_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ DownloadMain::do_peer_exchange() {
const rak::socket_address* sa = rak::socket_address::cast_from(pcb->peer_info()->socket_address());

if (pcb->peer_info()->listen_port() != 0 && sa->family() == rak::socket_address::af_inet)
current.push_back(SocketAddressCompact(sa->sa_inet()->address_n(), htons(pcb->peer_info()->listen_port())));
current.emplace_back(sa->sa_inet()->address_n(), htons(pcb->peer_info()->listen_port()));

if (!pcb->extensions()->is_remote_supported(ProtocolExtension::UT_PEX))
continue;
Expand Down
4 changes: 2 additions & 2 deletions libtorrent/src/download/download_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ DownloadWrapper::receive_hash_done(ChunkHandle handle, const char* hash) {
}

if (!m_main->have_queue()->empty() && m_main->have_queue()->front().first >= cachedTime)
m_main->have_queue()->push_front(DownloadMain::have_queue_type::value_type(m_main->have_queue()->front().first + 1, handle.index()));
m_main->have_queue()->emplace_front(m_main->have_queue()->front().first + 1, handle.index());
else
m_main->have_queue()->push_front(DownloadMain::have_queue_type::value_type(cachedTime, handle.index()));
m_main->have_queue()->emplace_front(cachedTime, handle.index());

} else {
// This needs to ensure the chunk is still valid.
Expand Down
2 changes: 1 addition & 1 deletion libtorrent/src/torrent/data/block_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class LIBTORRENT_EXPORT BlockTransfer {
bool is_finished() const { return m_position == m_piece.length(); }

key_type peer_info() { return m_peer_info; }
const key_type const_peer_info() const { return m_peer_info; }
key_type const_peer_info() const { return m_peer_info; }

Block* block() { return m_block; }
const Block* const_block() const { return m_block; }
Expand Down
4 changes: 2 additions & 2 deletions libtorrent/src/torrent/data/transfer_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ TransferList::hash_succeeded(uint32_t index, Chunk* chunk) {
// pruned every 60 minutes, so any timer that reads values once
// every 30 minutes is guaranteed to get them all as long as it is
// ordered properly.
m_completedList.push_back(std::make_pair(rak::timer::current().usec(), index));
m_completedList.emplace_back(rak::timer::current().usec(), index);

if (rak::timer(m_completedList.front().first) + rak::timer::from_minutes(60) < rak::timer::current()) {
completed_list_type::iterator itr = std::find_if(m_completedList.begin(), m_completedList.end(),
Expand Down Expand Up @@ -226,7 +226,7 @@ TransferList::update_failed(BlockList* blockList, Chunk* chunk) {

chunk->to_buffer(buffer, itr->piece().offset(), itr->piece().length());

itr->failed_list()->push_back(BlockFailed::value_type(buffer, 1));
itr->failed_list()->emplace_back(buffer, 1);
failedItr = itr->failed_list()->end() - 1;

// Count how many new data sets?
Expand Down
4 changes: 2 additions & 2 deletions libtorrent/src/torrent/download/group_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ inline void group_entry::connection_unchoked(PeerConnectionBase* pcb) {

if (itr != m_unchoked.end()) throw internal_error("group_entry::connection_unchoked(pcb) failed.");

m_unchoked.push_back(weighted_connection(pcb, uint32_t()));
m_unchoked.emplace_back(pcb, uint32_t());
}

inline void group_entry::connection_queued(PeerConnectionBase* pcb) {
Expand All @@ -117,7 +117,7 @@ inline void group_entry::connection_queued(PeerConnectionBase* pcb) {

if (itr != m_queued.end()) throw internal_error("group_entry::connection_queued(pcb) failed.");

m_queued.push_back(weighted_connection(pcb, uint32_t()));
m_queued.emplace_back(pcb, uint32_t());
}

inline void
Expand Down
2 changes: 1 addition & 1 deletion libtorrent/src/torrent/rate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Rate::insert(rate_type bytes) {
throw internal_error("Rate::insert(bytes) received out-of-bounds values..");

if (m_container.empty() || m_container.front().first != cachedTime.seconds())
m_container.push_front(value_type(cachedTime.seconds(), bytes));
m_container.emplace_front(cachedTime.seconds(), bytes);
else
m_container.front().second += bytes;

Expand Down
2 changes: 1 addition & 1 deletion libtorrent/src/torrent/torrent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void
download_list(DList& dlist) {
for (DownloadManager::const_iterator itr = manager->download_manager()->begin();
itr != manager->download_manager()->end(); ++itr)
dlist.push_back(Download(*itr));
dlist.emplace_back(*itr);
}

// Make sure you check that it's valid.
Expand Down
28 changes: 14 additions & 14 deletions libtorrent/src/torrent/utils/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,22 @@ log_group::internal_print(const HashString* hash, const char* subsystem, const v
}

#define LOG_CASCADE(parent) LOG_CHILDREN_CASCADE(parent, parent)
#define LOG_LINK(parent, child) log_children.push_back(std::make_pair(parent, child))
#define LOG_LINK(parent, child) log_children.emplace_back(parent, child)

#define LOG_CHILDREN_CASCADE(parent, subgroup) \
log_children.push_back(std::make_pair(parent + LOG_ERROR, subgroup + LOG_CRITICAL)); \
log_children.push_back(std::make_pair(parent + LOG_WARN, subgroup + LOG_ERROR)); \
log_children.push_back(std::make_pair(parent + LOG_NOTICE, subgroup + LOG_WARN)); \
log_children.push_back(std::make_pair(parent + LOG_INFO, subgroup + LOG_NOTICE)); \
log_children.push_back(std::make_pair(parent + LOG_DEBUG, subgroup + LOG_INFO));
log_children.emplace_back(parent + LOG_ERROR, subgroup + LOG_CRITICAL); \
log_children.emplace_back(parent + LOG_WARN, subgroup + LOG_ERROR); \
log_children.emplace_back(parent + LOG_NOTICE, subgroup + LOG_WARN); \
log_children.emplace_back(parent + LOG_INFO, subgroup + LOG_NOTICE); \
log_children.emplace_back(parent + LOG_DEBUG, subgroup + LOG_INFO);

#define LOG_CHILDREN_SUBGROUP(parent, subgroup) \
log_children.push_back(std::make_pair(parent + LOG_CRITICAL, subgroup + LOG_CRITICAL)); \
log_children.push_back(std::make_pair(parent + LOG_ERROR, subgroup + LOG_ERROR)); \
log_children.push_back(std::make_pair(parent + LOG_WARN, subgroup + LOG_WARN)); \
log_children.push_back(std::make_pair(parent + LOG_NOTICE, subgroup + LOG_NOTICE)); \
log_children.push_back(std::make_pair(parent + LOG_INFO, subgroup + LOG_INFO)); \
log_children.push_back(std::make_pair(parent + LOG_DEBUG, subgroup + LOG_DEBUG));
log_children.emplace_back(parent + LOG_CRITICAL, subgroup + LOG_CRITICAL); \
log_children.emplace_back(parent + LOG_ERROR, subgroup + LOG_ERROR); \
log_children.emplace_back(parent + LOG_WARN, subgroup + LOG_WARN); \
log_children.emplace_back(parent + LOG_NOTICE, subgroup + LOG_NOTICE); \
log_children.emplace_back(parent + LOG_INFO, subgroup + LOG_INFO); \
log_children.emplace_back(parent + LOG_DEBUG, subgroup + LOG_DEBUG);

void
log_initialize() {
Expand Down Expand Up @@ -301,7 +301,7 @@ log_open_output(const char* name, log_slot slot) {
log_output_list::iterator itr = log_find_output_name(name);

if (itr == log_outputs.end()) {
log_outputs.push_back(std::make_pair(name, slot));
log_outputs.emplace_back(name, slot);
} else {
// by replacing the "write" slot binding, the old file gets closed
// (handles are shared pointers)
Expand Down Expand Up @@ -360,7 +360,7 @@ log_add_child(int group, int child) {
if (std::find(log_children.begin(), log_children.end(), std::make_pair(group, child)) != log_children.end())
return;

log_children.push_back(std::make_pair(group, child));
log_children.emplace_back(group, child);
std::sort(log_children.begin(), log_children.end());

log_rebuild_cache();
Expand Down
4 changes: 2 additions & 2 deletions libtorrent/src/torrent/utils/option_strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,12 @@ option_list_strings(option_enum opt_enum) {
option_pair* itr = option_pair_lists[opt_enum];

while (itr->name != NULL)
result.push_back(std::string(itr++->name));
result.emplace_back(std::string(itr++->name));

} else if (opt_enum < OPTION_MAX_SIZE) {
const char** itr = option_single_lists[opt_enum - OPTION_START_COMPACT].name;

while (*itr != NULL) result.push_back(std::string(*itr++));
while (*itr != NULL) result.emplace_back(std::string(*itr++));
}

return Object::from_list(result);
Expand Down
4 changes: 2 additions & 2 deletions libtorrent/src/tracker/tracker_http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ TrackerHttp::TrackerHttp(TrackerList* parent, const std::string& url, int flags)
m_get(Http::slot_factory()()),
m_data(NULL) {

m_get->signal_done().push_back(std::bind(&TrackerHttp::receive_done, this));
m_get->signal_failed().push_back(std::bind(&TrackerHttp::receive_failed, this, std::placeholders::_1));
m_get->signal_done().emplace_back(std::bind(&TrackerHttp::receive_done, this));
m_get->signal_failed().emplace_back(std::bind(&TrackerHttp::receive_failed, this, std::placeholders::_1));

// Haven't considered if this needs any stronger error detection,
// can dropping the '?' be used for malicious purposes?
Expand Down

0 comments on commit 32a7af3

Please sign in to comment.