event/WinSelect: use unordered_map::emplace() in Add()

This allow using erase() with iterator, without a key lookup.
This commit is contained in:
Max Kellermann 2020-10-30 15:30:11 +01:00
parent 6d894a1806
commit fee29001fa
1 changed files with 4 additions and 3 deletions

View File

@ -64,7 +64,8 @@ bool
WinSelectBackend::Add(SOCKET fd, unsigned events, void *obj) noexcept WinSelectBackend::Add(SOCKET fd, unsigned events, void *obj) noexcept
{ {
assert(items.find(fd) == items.end()); assert(items.find(fd) == items.end());
auto &item = items[fd]; auto i = items.emplace(fd, Item{}).first;
auto &item = i->second;
item.index[EVENT_READ] = -1; item.index[EVENT_READ] = -1;
item.index[EVENT_WRITE] = -1; item.index[EVENT_WRITE] = -1;
@ -72,11 +73,11 @@ WinSelectBackend::Add(SOCKET fd, unsigned events, void *obj) noexcept
item.events = 0; item.events = 0;
if (!CanModify(item, events, EVENT_READ)) { if (!CanModify(item, events, EVENT_READ)) {
items.erase(fd); items.erase(i);
return false; return false;
} }
if (!CanModify(item, events, EVENT_WRITE)) { if (!CanModify(item, events, EVENT_WRITE)) {
items.erase(fd); items.erase(i);
return false; return false;
} }