Commit 11a44379 authored by Adam Norton's avatar Adam Norton Committed by Facebook Github Bot

Reduce Code Duplication In NotificationQueue With Universal Reference

Summary: Remove a bunch of identical code that only exists to provide interfaces for both `const MessageT&` and `MessageT&&` arguments.

Reviewed By: ot

Differential Revision: D5540560

fbshipit-source-id: ae7b9345b64130fc748033be2e0587c4f83345a7
parent cf913c0b
...@@ -352,11 +352,9 @@ class NotificationQueue { ...@@ -352,11 +352,9 @@ class NotificationQueue {
* may throw any other exception thrown by the MessageT move/copy * may throw any other exception thrown by the MessageT move/copy
* constructor. * constructor.
*/ */
void tryPutMessage(MessageT&& message) { template <typename MessageTT>
putMessageImpl(std::move(message), advisoryMaxQueueSize_); void tryPutMessage(MessageTT&& message) {
} putMessageImpl(std::forward<MessageTT>(message), advisoryMaxQueueSize_);
void tryPutMessage(const MessageT& message) {
putMessageImpl(message, advisoryMaxQueueSize_);
} }
/** /**
...@@ -367,11 +365,10 @@ class NotificationQueue { ...@@ -367,11 +365,10 @@ class NotificationQueue {
* (which indicates that the queue is being drained) are prevented from being * (which indicates that the queue is being drained) are prevented from being
* thrown. User code must still catch std::bad_alloc errors. * thrown. User code must still catch std::bad_alloc errors.
*/ */
bool tryPutMessageNoThrow(MessageT&& message) { template <typename MessageTT>
return putMessageImpl(std::move(message), advisoryMaxQueueSize_, false); bool tryPutMessageNoThrow(MessageTT&& message) {
} return putMessageImpl(
bool tryPutMessageNoThrow(const MessageT& message) { std::forward<MessageTT>(message), advisoryMaxQueueSize_, false);
return putMessageImpl(message, advisoryMaxQueueSize_, false);
} }
/** /**
...@@ -386,11 +383,9 @@ class NotificationQueue { ...@@ -386,11 +383,9 @@ class NotificationQueue {
* - std::runtime_error if the queue is currently draining * - std::runtime_error if the queue is currently draining
* - any other exception thrown by the MessageT move/copy constructor. * - any other exception thrown by the MessageT move/copy constructor.
*/ */
void putMessage(MessageT&& message) { template <typename MessageTT>
putMessageImpl(std::move(message), 0); void putMessage(MessageTT&& message) {
} putMessageImpl(std::forward<MessageTT>(message), 0);
void putMessage(const MessageT& message) {
putMessageImpl(message, 0);
} }
/** /**
...@@ -570,7 +565,8 @@ class NotificationQueue { ...@@ -570,7 +565,8 @@ class NotificationQueue {
} }
} }
bool putMessageImpl(MessageT&& message, size_t maxSize, bool throws=true) { template <typename MessageTT>
bool putMessageImpl(MessageTT&& message, size_t maxSize, bool throws = true) {
checkPid(); checkPid();
bool signal = false; bool signal = false;
{ {
...@@ -583,27 +579,8 @@ class NotificationQueue { ...@@ -583,27 +579,8 @@ class NotificationQueue {
if (numActiveConsumers_ < numConsumers_) { if (numActiveConsumers_ < numConsumers_) {
signal = true; signal = true;
} }
queue_.emplace_back(std::move(message), RequestContext::saveContext()); queue_.emplace_back(
if (signal) { std::forward<MessageTT>(message), RequestContext::saveContext());
ensureSignalLocked();
}
}
return true;
}
bool putMessageImpl(
const MessageT& message, size_t maxSize, bool throws=true) {
checkPid();
bool signal = false;
{
folly::SpinLockGuard g(spinlock_);
if (checkDraining(throws) || !checkQueueSize(maxSize, throws)) {
return false;
}
if (numActiveConsumers_ < numConsumers_) {
signal = true;
}
queue_.emplace_back(message, RequestContext::saveContext());
if (signal) { if (signal) {
ensureSignalLocked(); ensureSignalLocked();
} }
......
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