Commit 5f195e92 authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Nit simplify setContext

Summary: I keep modifying this code, and I keep feeling like it would be more readable without std::swap. Thoughts?

Reviewed By: yfeldblum

Differential Revision: D9140662

fbshipit-source-id: a86862dbfd1c0caeb69cc66b215994a3d1dd0294
parent 3e915e8b
......@@ -158,39 +158,42 @@ void RequestContext::clearContextData(const std::string& val) {
std::shared_ptr<RequestContext> RequestContext::setContext(
std::shared_ptr<RequestContext> newCtx) {
auto& curCtx = getStaticContext();
if (newCtx != curCtx) {
FOLLY_SDT(folly, request_context_switch_before, curCtx.get(), newCtx.get());
// Call set/unset for all request data that differs
if (newCtx && curCtx) {
auto newLock = newCtx->state_.rlock();
auto curLock = curCtx->state_.rlock();
auto& newData = newLock->callbackData_;
auto& curData = curLock->callbackData_;
for (auto* callback : curData) {
if (newData.find(callback) == newData.end()) {
callback->onUnset();
}
auto& staticCtx = getStaticContext();
if (newCtx == staticCtx) {
return newCtx;
}
FOLLY_SDT(
folly, request_context_switch_before, staticCtx.get(), newCtx.get());
auto curCtx = staticCtx;
if (newCtx && curCtx) {
// Only call set/unset for all request data that differs
auto newLock = newCtx->state_.rlock();
auto curLock = curCtx->state_.rlock();
auto& newData = newLock->callbackData_;
auto& curData = curLock->callbackData_;
for (auto* callback : curData) {
if (newData.find(callback) == newData.end()) {
callback->onUnset();
}
std::swap(curCtx, newCtx);
for (auto* callback : newData) {
if (curData.find(callback) == curData.end()) {
callback->onSet();
}
}
staticCtx = newCtx;
for (auto* callback : newData) {
if (curData.find(callback) == curData.end()) {
callback->onSet();
}
} else if (newCtx) {
std::swap(curCtx, newCtx);
// Note: actually newCtx
curCtx->onSet();
} else if (curCtx) {
}
} else {
if (curCtx) {
curCtx->onUnset();
std::swap(curCtx, newCtx);
}
staticCtx = newCtx;
if (newCtx) {
newCtx->onSet();
}
}
// Note: actually curCtx
return newCtx;
return curCtx;
}
std::shared_ptr<RequestContext>& RequestContext::getStaticContext() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment