Skip to content

Commit 1e5d24e

Browse files
authored
Use explicit lambda capture lists (#103)
1 parent 327d5dd commit 1e5d24e

13 files changed

+60
-67
lines changed

trantor/net/EventLoopThread.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
3-
* EventLoopThread.cc
4-
* An Tao
3+
* @file EventLoopThread.cc
4+
* @author An Tao
55
*
66
* Public header file in trantor lib.
77
*
@@ -22,7 +22,7 @@ using namespace trantor;
2222
EventLoopThread::EventLoopThread(const std::string &threadName)
2323
: loop_(nullptr),
2424
loopThreadName_(threadName),
25-
thread_([=]() { loopFuncs(); })
25+
thread_([this]() { loopFuncs(); })
2626
{
2727
auto f = promiseForLoopPointer_.get_future();
2828
loop_ = f.get();
@@ -53,7 +53,7 @@ void EventLoopThread::loopFuncs()
5353
::prctl(PR_SET_NAME, loopThreadName_.c_str());
5454
#endif
5555
EventLoop loop;
56-
loop.queueInLoop([=]() { promiseForLoop_.set_value(1); });
56+
loop.queueInLoop([this]() { promiseForLoop_.set_value(1); });
5757
promiseForLoopPointer_.set_value(&loop);
5858
auto f = promiseForRun_.get_future();
5959
(void)f.get();

trantor/net/TcpClient.cc

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,12 @@
2323
using namespace trantor;
2424
using namespace std::placeholders;
2525

26-
// TcpClient::TcpClient(EventLoop* loop)
27-
// : loop_(loop)
28-
// {
29-
// }
30-
31-
// TcpClient::TcpClient(EventLoop* loop, const string& host, uint16_t port)
32-
// : loop_(CHECK_NOTNULL(loop)),
33-
// serverAddr_(host, port)
34-
// {
35-
// }
36-
3726
namespace trantor
3827
{
39-
void removeConnector(const ConnectorPtr &)
40-
{
41-
// connector->
42-
}
28+
// void removeConnector(const ConnectorPtr &)
29+
// {
30+
// // connector->
31+
// }
4332
#ifndef _WIN32
4433
TcpClient::IgnoreSigPipe TcpClient::initObj;
4534
#endif
@@ -73,7 +62,7 @@ TcpClient::TcpClient(EventLoop *loop,
7362
{
7463
connector_->setNewConnectionCallback(
7564
std::bind(&TcpClient::newConnection, this, _1));
76-
connector_->setErrorCallback([=]() {
65+
connector_->setErrorCallback([this]() {
7766
if (connectionErrorCallback_)
7867
{
7968
connectionErrorCallback_();
@@ -109,7 +98,6 @@ TcpClient::~TcpClient()
10998
{
11099
/// TODO need test in this condition
111100
connector_->stop();
112-
loop_->runAfter(1, [=]() { trantor::removeConnector(connector_); });
113101
}
114102
}
115103

trantor/net/TcpServer.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ void TcpServer::newConnection(int sockfd, const InetAddress &peer)
9292
}
9393
newPtr->setRecvMsgCallback(recvMessageCallback_);
9494

95-
newPtr->setConnectionCallback([=](const TcpConnectionPtr &connectionPtr) {
96-
if (connectionCallback_)
97-
connectionCallback_(connectionPtr);
98-
});
95+
newPtr->setConnectionCallback(
96+
[this](const TcpConnectionPtr &connectionPtr) {
97+
if (connectionCallback_)
98+
connectionCallback_(connectionPtr);
99+
});
99100
newPtr->setWriteCompleteCallback(
100-
[=](const TcpConnectionPtr &connectionPtr) {
101+
[this](const TcpConnectionPtr &connectionPtr) {
101102
if (writeCompleteCallback_)
102103
writeCompleteCallback_(connectionPtr);
103104
});
@@ -108,7 +109,7 @@ void TcpServer::newConnection(int sockfd, const InetAddress &peer)
108109

109110
void TcpServer::start()
110111
{
111-
loop_->runInLoop([=]() {
112+
loop_->runInLoop([this]() {
112113
assert(!started_);
113114
started_ = true;
114115
if (idleTimeout_ > 0)
@@ -165,7 +166,7 @@ void TcpServer::connectionClosed(const TcpConnectionPtr &connectionPtr)
165166
{
166167
LOG_TRACE << "connectionClosed";
167168
// loop_->assertInLoopThread();
168-
loop_->runInLoop([=]() {
169+
loop_->runInLoop([this, connectionPtr]() {
169170
size_t n = connSet_.erase(connectionPtr);
170171
(void)n;
171172
assert(n == 1);

trantor/net/TcpServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class TcpServer : NonCopyable
188188
*/
189189
void kickoffIdleConnections(size_t timeout)
190190
{
191-
loop_->runInLoop([=]() {
191+
loop_->runInLoop([this, timeout]() {
192192
assert(!started_);
193193
idleTimeout_ = timeout;
194194
});

trantor/net/inner/Connector.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
3-
* Connector.cc
4-
* An Tao
3+
* @file Connector.cc
4+
* @author An Tao
55
*
66
* Public header file in trantor lib.
77
*
@@ -30,7 +30,7 @@ Connector::Connector(EventLoop *loop, InetAddress &&addr, bool retry)
3030
void Connector::start()
3131
{
3232
connect_ = true;
33-
loop_->runInLoop([=]() { startInLoop(); });
33+
loop_->runInLoop([this]() { startInLoop(); });
3434
}
3535
void Connector::restart()
3636
{
@@ -132,7 +132,7 @@ int Connector::removeAndResetChannel()
132132
channelPtr_->remove();
133133
int sockfd = channelPtr_->fd();
134134
// Can't reset channel_ here, because we are inside Channel::handleEvent
135-
loop_->queueInLoop([=]() { channelPtr_.reset(); });
135+
loop_->queueInLoop([this]() { channelPtr_.reset(); });
136136
return sockfd;
137137
}
138138

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,20 +605,22 @@ void TcpConnectionImpl::connectEstablished()
605605
}
606606
else
607607
{
608-
loop_->runInLoop([=]() {
608+
loop_->runInLoop([thisPtr = shared_from_this()]() {
609609
LOG_TRACE << "connectEstablished";
610-
assert(status_ == ConnStatus::Connecting);
611-
ioChannelPtr_->tie(shared_from_this());
612-
ioChannelPtr_->enableReading();
613-
status_ = ConnStatus::Connected;
614-
if (sslEncryptionPtr_->isServer_)
610+
assert(thisPtr->status_ == ConnStatus::Connecting);
611+
thisPtr->ioChannelPtr_->tie(thisPtr);
612+
thisPtr->ioChannelPtr_->enableReading();
613+
thisPtr->status_ = ConnStatus::Connected;
614+
if (thisPtr->sslEncryptionPtr_->isServer_)
615615
{
616-
SSL_set_accept_state(sslEncryptionPtr_->sslPtr_->get());
616+
SSL_set_accept_state(
617+
thisPtr->sslEncryptionPtr_->sslPtr_->get());
617618
}
618619
else
619620
{
620-
ioChannelPtr_->enableWriting();
621-
SSL_set_connect_state(sslEncryptionPtr_->sslPtr_->get());
621+
thisPtr->ioChannelPtr_->enableWriting();
622+
SSL_set_connect_state(
623+
thisPtr->sslEncryptionPtr_->sslPtr_->get());
622624
}
623625
});
624626
}

trantor/net/inner/TimerQueue.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
3-
* TimerQueue.cc
4-
* An Tao
3+
* @file TimerQueue.cc
4+
* @author An Tao
55
*
66
* Public header file in trantor lib.
77
*
@@ -190,7 +190,7 @@ TimerId TimerQueue::addTimer(const TimerCallback &cb,
190190
std::shared_ptr<Timer> timerPtr =
191191
std::make_shared<Timer>(cb, when, interval);
192192

193-
loop_->runInLoop([=]() { addTimerInLoop(timerPtr); });
193+
loop_->runInLoop([this, timerPtr]() { addTimerInLoop(timerPtr); });
194194
return timerPtr->id();
195195
}
196196
TimerId TimerQueue::addTimer(TimerCallback &&cb,
@@ -200,7 +200,7 @@ TimerId TimerQueue::addTimer(TimerCallback &&cb,
200200
std::shared_ptr<Timer> timerPtr =
201201
std::make_shared<Timer>(std::move(cb), when, interval);
202202

203-
loop_->runInLoop([=]() { addTimerInLoop(timerPtr); });
203+
loop_->runInLoop([this, timerPtr]() { addTimerInLoop(timerPtr); });
204204
return timerPtr->id();
205205
}
206206
void TimerQueue::addTimerInLoop(const TimerPtr &timer)
@@ -218,7 +218,7 @@ void TimerQueue::addTimerInLoop(const TimerPtr &timer)
218218

219219
void TimerQueue::invalidateTimer(TimerId id)
220220
{
221-
loop_->runInLoop([=]() { timerIdSet_.erase(id); });
221+
loop_->runInLoop([this, id]() { timerIdSet_.erase(id); });
222222
}
223223

224224
bool TimerQueue::insert(const TimerPtr &timerPtr)

trantor/tests/DelayedSSLClientTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main()
2525
serverAddr,
2626
"tcpclienttest");
2727
client[i]->setConnectionCallback(
28-
[=, &loop, &connCount](const TcpConnectionPtr &conn) {
28+
[i, &loop, &connCount](const TcpConnectionPtr &conn) {
2929
if (conn->connected())
3030
{
3131
}
@@ -38,7 +38,7 @@ int main()
3838
}
3939
});
4040
client[i]->setMessageCallback(
41-
[=](const TcpConnectionPtr &conn, MsgBuffer *buf) {
41+
[](const TcpConnectionPtr &conn, MsgBuffer *buf) {
4242
auto msg = std::string(buf->peek(), buf->readableBytes());
4343

4444
LOG_INFO << msg;

trantor/tests/SSLClientTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main()
2626
"tcpclienttest");
2727
client[i]->enableSSL();
2828
client[i]->setConnectionCallback(
29-
[=, &loop, &connCount](const TcpConnectionPtr &conn) {
29+
[i, &loop, &connCount](const TcpConnectionPtr &conn) {
3030
if (conn->connected())
3131
{
3232
LOG_DEBUG << i << " connected!";
@@ -43,7 +43,7 @@ int main()
4343
}
4444
});
4545
client[i]->setMessageCallback(
46-
[=](const TcpConnectionPtr &conn, MsgBuffer *buf) {
46+
[](const TcpConnectionPtr &conn, MsgBuffer *buf) {
4747
LOG_DEBUG << std::string(buf->peek(), buf->readableBytes());
4848
buf->retrieveAll();
4949
conn->shutdown();

trantor/tests/SendfileTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ int main(int argc, char *argv[])
5555
});
5656
int counter = 0;
5757
server.setConnectionCallback(
58-
[=, &counter](const TcpConnectionPtr &connPtr) {
58+
[argv, &counter](const TcpConnectionPtr &connPtr) {
5959
if (connPtr->connected())
6060
{
6161
LOG_DEBUG << "New connection";
62-
std::thread t([=, &counter]() {
62+
std::thread t([connPtr, argv, &counter]() {
6363
for (int i = 0; i < 5; ++i)
6464
{
6565
connPtr->sendFile(argv[1]);

trantor/tests/TcpClientTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main()
2525
serverAddr,
2626
"tcpclienttest");
2727
client[i]->setConnectionCallback(
28-
[=, &loop, &connCount](const TcpConnectionPtr &conn) {
28+
[i, &loop, &connCount](const TcpConnectionPtr &conn) {
2929
if (conn->connected())
3030
{
3131
LOG_DEBUG << i << " connected!";
@@ -42,7 +42,7 @@ int main()
4242
}
4343
});
4444
client[i]->setMessageCallback(
45-
[=](const TcpConnectionPtr &conn, MsgBuffer *buf) {
45+
[](const TcpConnectionPtr &conn, MsgBuffer *buf) {
4646
LOG_DEBUG << std::string(buf->peek(), buf->readableBytes());
4747
buf->retrieveAll();
4848
conn->shutdown();

trantor/tests/TimerTest1.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ int main()
66
trantor::EventLoop loop;
77
LOG_FATAL << trantor::Date::date().roundDay().microSecondsSinceEpoch();
88
trantor::Date begin = trantor::Date::date().roundSecond().after(2);
9-
auto id = loop.runAt(begin, [=, &loop]() {
9+
auto id = loop.runAt(begin, [begin, &loop]() {
1010
LOG_DEBUG << "test begin:";
1111
srand((unsigned int)time(NULL));
1212
for (int i = 0; i < 10000; ++i)
1313
{
1414
int aa = rand() % 10000;
1515
double s = (double)aa / 1000.0 + 1;
1616
loop.runAt(begin.after(s),
17-
[=]() { LOG_ERROR << "run After:" << s; });
17+
[s]() { LOG_ERROR << "run After:" << s; });
1818
}
1919
LOG_DEBUG << "timer created!";
2020
});

trantor/utils/TimingWheel.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TimingWheel::TimingWheel(trantor::EventLoop *loop,
4040
{
4141
wheels_[i].resize(bucketsNumPerWheel_);
4242
}
43-
timerId_ = loop_->runEvery(ticksInterval_, [=]() {
43+
timerId_ = loop_->runEvery(ticksInterval_, [this]() {
4444
++ticksCounter_;
4545
size_t t = ticksCounter_;
4646
size_t pow = 1;
@@ -85,7 +85,8 @@ void TimingWheel::insertEntry(size_t delay, EntryPtr entryPtr)
8585
}
8686
else
8787
{
88-
loop_->runInLoop([=]() { insertEntryInloop(delay, entryPtr); });
88+
loop_->runInLoop(
89+
[this, delay, entryPtr]() { insertEntryInloop(delay, entryPtr); });
8990
}
9091
}
9192

@@ -104,14 +105,15 @@ void TimingWheel::insertEntryInloop(size_t delay, EntryPtr entryPtr)
104105
}
105106
if (i < (wheelsNum_ - 1))
106107
{
107-
entryPtr = std::make_shared<CallbackEntry>([=]() {
108-
if (delay > 0)
109-
{
110-
wheels_[i][(delay + (t % bucketsNumPerWheel_) - 1) %
111-
bucketsNumPerWheel_]
112-
.insert(entryPtr);
113-
}
114-
});
108+
entryPtr = std::make_shared<CallbackEntry>(
109+
[this, delay, i, t, entryPtr]() {
110+
if (delay > 0)
111+
{
112+
wheels_[i][(delay + (t % bucketsNumPerWheel_) - 1) %
113+
bucketsNumPerWheel_]
114+
.insert(entryPtr);
115+
}
116+
});
115117
}
116118
else
117119
{

0 commit comments

Comments
 (0)