Allow PostTask() to take unique_ptr to classes derived of QueuedTask

Problem fixed by this CL: Let DerivedQueuedTask be a custom derivation of QueuedTask. Calling PostTask() with a std::unique_ptr<DerivedQueuedTask> does not work, because overload resolution sees PostTask(const Closure& closure) as a better match. The workaround of explicitly converting to std::unique_ptr<QueuedTask> before calling PostTask() results in less readable code.

Solution: Use std::enable_if to limit the template, thereby making the compiler use the right version of PostTask().

BUG=webrtc:8188

Review-Url: https://codereview.webrtc.org/3006933002
Cr-Commit-Position: refs/heads/master@{#19625}
This commit is contained in:
eladalon 2017-08-31 04:36:05 -07:00 committed by Commit Bot
parent 6e09d875fb
commit ffe2e14183

View File

@ -14,6 +14,7 @@
#include <list>
#include <memory>
#include <queue>
#include <type_traits>
#if defined(WEBRTC_MAC)
#include <dispatch/dispatch.h>
@ -189,7 +190,12 @@ class LOCKABLE TaskQueue {
// more likely). This can be mitigated by limiting the use of delayed tasks.
void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
template <class Closure>
// std::enable_if is used here to make sure that calls to PostTask() with
// std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being
// caught by this template.
template <class Closure,
typename std::enable_if<
std::is_copy_constructible<Closure>::value>::type* = nullptr>
void PostTask(const Closure& closure) {
PostTask(std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)));
}