Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace std::bind with lambdas #4017

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ bool BedItem::sleep(Player* player)
g_game.addMagicEffect(player->getPosition(), CONST_ME_SLEEP);

// kick player after he sees himself walk onto the bed and it change id
uint32_t playerId = player->getID();
g_scheduler.addEvent(createSchedulerTask(SCHEDULER_MINTICKS, std::bind(&Game::kickPlayer, &g_game, playerId, false)));
g_scheduler.addEvent(createSchedulerTask(SCHEDULER_MINTICKS, [playerID = player->getID()]() { g_game.kickPlayer(playerID, false); }));

// change self and partner's appearance
updateAppearance(player);
Expand Down
2 changes: 1 addition & 1 deletion src/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool ChatChannel::addUser(Player& player)
if (id == CHANNEL_GUILD) {
Guild* guild = player.getGuild();
if (guild && !guild->getMotd().empty()) {
g_scheduler.addEvent(createSchedulerTask(150, std::bind(&Game::sendGuildMotd, &g_game, player.getID())));
g_scheduler.addEvent(createSchedulerTask(150, [playerID = player.getID()]() { g_game.sendGuildMotd(playerID); }));
}
}

Expand Down
23 changes: 10 additions & 13 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void Connection::close(bool force)
connectionState = CONNECTION_STATE_DISCONNECTED;

if (protocol) {
g_dispatcher.addTask(createTask(std::bind(&Protocol::release, protocol)));
g_dispatcher.addTask(createTask([protocol = protocol]() { protocol->release(); }));
DSpeichert marked this conversation as resolved.
Show resolved Hide resolved
}

if (messageQueue.empty() || force) {
Expand Down Expand Up @@ -87,7 +87,7 @@ Connection::~Connection()
void Connection::accept(Protocol_ptr protocol)
{
this->protocol = protocol;
g_dispatcher.addTask(createTask(std::bind(&Protocol::onConnect, protocol)));
g_dispatcher.addTask(createTask([=]() { protocol->onConnect(); }));
connectionState = CONNECTION_STATE_GAMEWORLD_AUTH;
accept();
}
Expand All @@ -101,13 +101,13 @@ void Connection::accept()
std::lock_guard<std::recursive_mutex> lockClass(connectionLock);
try {
readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT));
readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr<Connection>(shared_from_this()), std::placeholders::_1));
readTimer.async_wait([thisPtr = std::weak_ptr<Connection>(shared_from_this())](const boost::system::error_code &error) { Connection::handleTimeout(thisPtr, error); });

// Read size of the first packet
auto bufferLength = !receivedLastChar && receivedName && connectionState == CONNECTION_STATE_GAMEWORLD_AUTH ? 1 : NetworkMessage::HEADER_LENGTH;
boost::asio::async_read(socket,
boost::asio::buffer(msg.getBuffer(), bufferLength),
std::bind(&Connection::parseHeader, shared_from_this(), std::placeholders::_1));
[thisPtr = shared_from_this()](const boost::system::error_code &error, auto /*bytes_transferred*/) { thisPtr->parseHeader(error); });
} catch (boost::system::system_error& e) {
std::cout << "[Network error - Connection::accept] " << e.what() << std::endl;
close(FORCE_CLOSE);
Expand Down Expand Up @@ -172,13 +172,12 @@ void Connection::parseHeader(const boost::system::error_code& error)

try {
readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT));
readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr<Connection>(shared_from_this()),
std::placeholders::_1));
readTimer.async_wait([thisPtr = std::weak_ptr<Connection>(shared_from_this())](const boost::system::error_code &error) { Connection::handleTimeout(thisPtr, error); });

// Read packet content
msg.setLength(size + NetworkMessage::HEADER_LENGTH);
boost::asio::async_read(socket, boost::asio::buffer(msg.getBodyBuffer(), size),
std::bind(&Connection::parsePacket, shared_from_this(), std::placeholders::_1));
[thisPtr = shared_from_this()](const boost::system::error_code &error, auto /*bytes_transferred*/) { thisPtr->parsePacket(error); });
} catch (boost::system::system_error& e) {
std::cout << "[Network error - Connection::parseHeader] " << e.what() << std::endl;
close(FORCE_CLOSE);
Expand Down Expand Up @@ -227,13 +226,12 @@ void Connection::parsePacket(const boost::system::error_code& error)

try {
readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT));
readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr<Connection>(shared_from_this()),
std::placeholders::_1));
readTimer.async_wait([thisPtr = std::weak_ptr<Connection>(shared_from_this())](const boost::system::error_code &error) { Connection::handleTimeout(thisPtr, error); });

// Wait to the next packet
boost::asio::async_read(socket,
boost::asio::buffer(msg.getBuffer(), NetworkMessage::HEADER_LENGTH),
std::bind(&Connection::parseHeader, shared_from_this(), std::placeholders::_1));
[thisPtr = shared_from_this()](const boost::system::error_code &error, auto /*bytes_transferred*/) { thisPtr->parseHeader(error); });
} catch (boost::system::system_error& e) {
std::cout << "[Network error - Connection::parsePacket] " << e.what() << std::endl;
close(FORCE_CLOSE);
Expand All @@ -259,12 +257,11 @@ void Connection::internalSend(const OutputMessage_ptr& msg)
protocol->onSendMessage(msg);
try {
writeTimer.expires_from_now(std::chrono::seconds(CONNECTION_WRITE_TIMEOUT));
writeTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr<Connection>(shared_from_this()),
std::placeholders::_1));
writeTimer.async_wait([thisPtr = std::weak_ptr<Connection>(shared_from_this())](const boost::system::error_code &error) { Connection::handleTimeout(thisPtr, error); });

boost::asio::async_write(socket,
boost::asio::buffer(msg->getOutputBuffer(), msg->getLength()),
std::bind(&Connection::onWriteOperation, shared_from_this(), std::placeholders::_1));
[thisPtr = shared_from_this()](const boost::system::error_code &error, auto /*bytes_transferred*/) { thisPtr->onWriteOperation(error); });
} catch (boost::system::system_error& e) {
std::cout << "[Network error - Connection::internalSend] " << e.what() << std::endl;
close(FORCE_CLOSE);
Expand Down
16 changes: 8 additions & 8 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void Creature::addEventWalk(bool firstStep)
g_game.checkCreatureWalk(getID());
}

eventWalk = g_scheduler.addEvent(createSchedulerTask(ticks, std::bind(&Game::checkCreatureWalk, &g_game, getID())));
eventWalk = g_scheduler.addEvent(createSchedulerTask(ticks, [id = getID()]() { g_game.checkCreatureWalk(id); }));
}

void Creature::stopEventWalk()
Expand Down Expand Up @@ -613,7 +613,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
} else {
if (hasExtraSwing()) {
//our target is moving lets see if we can get in hit
g_dispatcher.addTask(createTask(std::bind(&Game::checkCreatureAttack, &g_game, getID())));
g_dispatcher.addTask(createTask([id = getID()]() { g_game.checkCreatureAttack(id); }));
}

if (newTile->getZone() != oldTile->getZone()) {
Expand Down Expand Up @@ -797,7 +797,7 @@ void Creature::changeHealth(int32_t healthChange, bool sendHealthChange/* = true
}

if (health <= 0) {
g_dispatcher.addTask(createTask(std::bind(&Game::executeDeath, &g_game, getID())));
g_dispatcher.addTask(createTask([id = getID()]() { g_game.executeDeath(id); }));
}
}

Expand Down Expand Up @@ -888,7 +888,7 @@ BlockType_t Creature::blockHit(Creature* attacker, CombatType_t combatType, int3
damage = 0;
blockType = BLOCK_ARMOR;
}

attacker->onAttackedCreature(this);
attacker->onAttackedCreatureBlockHit(blockType);
}
Expand Down Expand Up @@ -1195,7 +1195,7 @@ bool Creature::addCondition(Condition* condition, bool force/* = false*/)
if (!force && condition->getType() == CONDITION_HASTE && hasCondition(CONDITION_PARALYZE)) {
int64_t walkDelay = getWalkDelay();
if (walkDelay > 0) {
g_scheduler.addEvent(createSchedulerTask(walkDelay, std::bind(&Game::forceAddCondition, &g_game, getID(), condition)));
g_scheduler.addEvent(createSchedulerTask(walkDelay, [=, id = getID()]() { g_game.forceAddCondition(id, condition); }));
return false;
}
}
Expand Down Expand Up @@ -1243,7 +1243,7 @@ void Creature::removeCondition(ConditionType_t type, bool force/* = false*/)
if (!force && type == CONDITION_PARALYZE) {
int64_t walkDelay = getWalkDelay();
if (walkDelay > 0) {
g_scheduler.addEvent(createSchedulerTask(walkDelay, std::bind(&Game::forceRemoveCondition, &g_game, getID(), type)));
g_scheduler.addEvent(createSchedulerTask(walkDelay, [=, id = getID()] () { g_game.forceRemoveCondition(id, type); }));
return;
}
}
Expand All @@ -1270,7 +1270,7 @@ void Creature::removeCondition(ConditionType_t type, ConditionId_t conditionId,
if (!force && type == CONDITION_PARALYZE) {
int64_t walkDelay = getWalkDelay();
if (walkDelay > 0) {
g_scheduler.addEvent(createSchedulerTask(walkDelay, std::bind(&Game::forceRemoveCondition, &g_game, getID(), type)));
g_scheduler.addEvent(createSchedulerTask(walkDelay, [=, id = getID()] () { g_game.forceRemoveCondition(id, type); }));
return;
}
}
Expand Down Expand Up @@ -1308,7 +1308,7 @@ void Creature::removeCondition(Condition* condition, bool force/* = false*/)
if (!force && condition->getType() == CONDITION_PARALYZE) {
int64_t walkDelay = getWalkDelay();
if (walkDelay > 0) {
g_scheduler.addEvent(createSchedulerTask(walkDelay, std::bind(&Game::forceRemoveCondition, &g_game, getID(), condition->getType())));
g_scheduler.addEvent(createSchedulerTask(walkDelay, [id = getID(), type = condition->getType()]() { g_game.forceRemoveCondition(id, type); }));
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/databasetasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void DatabaseTasks::runTask(const DatabaseTask& task)
}

if (task.callback) {
g_dispatcher.addTask(createTask(std::bind(task.callback, result, success)));
g_dispatcher.addTask(createTask([=, callback = task.callback]() { callback(result, success); }));
}
}

Expand Down
Loading