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