Added move semantics to `Future::set`. Review: https://reviews.apache.org/r/47989/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/bf716220 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/bf716220 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/bf716220 Branch: refs/heads/master Commit: bf7162205b53114eb7367fa322951d573cbb716d Parents: 6ce7279 Author: Anand Mazumdar <mazumdar.an...@gmail.com> Authored: Tue May 31 13:28:56 2016 -0600 Committer: Vinod Kone <vinodk...@gmail.com> Committed: Tue May 31 13:28:56 2016 -0600 ---------------------------------------------------------------------- 3rdparty/libprocess/include/process/future.hpp | 49 +++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/bf716220/3rdparty/libprocess/include/process/future.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/include/process/future.hpp b/3rdparty/libprocess/include/process/future.hpp index 455b750..175214a 100644 --- a/3rdparty/libprocess/include/process/future.hpp +++ b/3rdparty/libprocess/include/process/future.hpp @@ -461,6 +461,10 @@ private: // Sets the value for this future, unless the future is already set, // failed, or discarded, in which case it returns false. bool set(const T& _t); + bool set(T&& _t); + + template <typename U> + bool _set(U&& _u); // Sets this future as failed, unless the future is already set, // failed, or discarded, in which case it returns false. @@ -552,6 +556,7 @@ public: bool discard(); bool set(const T& _t); + bool set(T&& _t); bool set(const Future<T>& future); // Alias for associate. bool associate(const Future<T>& future); bool fail(const std::string& message); @@ -563,6 +568,9 @@ private: template <typename U> friend void internal::discarded(Future<U> future); + template <typename U> + bool _set(U&& u); + // Not copyable, not assignable. Promise(const Promise<T>&); Promise<T>& operator=(const Promise<T>&); @@ -646,10 +654,25 @@ bool Promise<T>::discard() template <typename T> +bool Promise<T>::set(T&& t) +{ + return _set(std::move(t)); +} + + +template <typename T> bool Promise<T>::set(const T& t) { + return _set(t); +} + + +template <typename T> +template <typename U> +bool Promise<T>::_set(U&& u) +{ if (!f.data->associated) { - return f.set(t); + return f.set(std::forward<U>(u)); } return false; } @@ -699,8 +722,11 @@ bool Promise<T>::associate(const Future<T>& future) // associated. f.onDiscard(lambda::bind(&internal::discard<T>, WeakFuture<T>(future))); + // Need to disambiguate for the compiler. + bool (Future<T>::*set)(const T&) = &Future<T>::set; + future - .onReady(lambda::bind(&Future<T>::set, f, lambda::_1)) + .onReady(lambda::bind(set, f, lambda::_1)) .onFailed(lambda::bind(&Future<T>::fail, f, lambda::_1)) .onDiscarded(lambda::bind(&internal::discarded<T>, f)); } @@ -1398,13 +1424,28 @@ Future<T> Future<T>::after( template <typename T> -bool Future<T>::set(const T& _t) +bool Future<T>::set(T&& t) +{ + return _set(std::move(t)); +} + + +template <typename T> +bool Future<T>::set(const T& t) +{ + return _set(t); +} + + +template <typename T> +template <typename U> +bool Future<T>::_set(U&& u) { bool result = false; synchronized (data->lock) { if (data->state == PENDING) { - data->result = _t; + data->result = std::forward<U>(u); data->state = READY; result = true; }