templating folly::fibers::await by Baton
authorShubhanshu Agrawal <shubhanshu@fb.com>
Fri, 20 May 2016 05:03:16 +0000 (22:03 -0700)
committerFacebook Github Bot 4 <facebook-github-bot-4-bot@fb.com>
Fri, 20 May 2016 05:08:34 +0000 (22:08 -0700)
Summary:
This diff templates folly::fibers::await by Baton.
This would be helpful in passing in custom BatonType's, which is needed for D3007734

Depends on: D3314891

Reviewed By: andriigrynenko

Differential Revision: D3314925

fbshipit-source-id: 9052dc503b9509f16cd41b5e3ede0479a98067aa

folly/fibers/FiberManager-inl.h
folly/fibers/Promise-inl.h
folly/fibers/Promise.h

index 0e14525dec1b9c0b50e8d0634b6703dbbf31e708..003da1bdb1186ff0adc95725c082a2e0874a6f4b 100644 (file)
@@ -545,8 +545,9 @@ FiberManager::FiberManager(
 template <typename F>
 typename FirstArgOf<F>::type::value_type inline await(F&& func) {
   typedef typename FirstArgOf<F>::type::value_type Result;
+  typedef typename FirstArgOf<F>::type::baton_type BatonT;
 
-  return Promise<Result>::await(std::forward<F>(func));
+  return Promise<Result, BatonT>::await(std::forward<F>(func));
 }
 }
 }
index 2adaf781ab03f17008faeb2713f0a0cd5e0f43fe..f4219c517670a7d79a2459dab7c1a8b77047d4ff 100644 (file)
 namespace folly {
 namespace fibers {
 
-template <class T>
-Promise<T>::Promise(folly::Try<T>& value, Baton& baton)
+template <class T, class BatonT>
+Promise<T, BatonT>::Promise(folly::Try<T>& value, BatonT& baton)
     : value_(&value), baton_(&baton) {}
 
-template <class T>
-Promise<T>::Promise(Promise&& other) noexcept
+template <class T, class BatonT>
+Promise<T, BatonT>::Promise(Promise&& other) noexcept
     : value_(other.value_), baton_(other.baton_) {
   other.value_ = nullptr;
   other.baton_ = nullptr;
 }
 
-template <class T>
-Promise<T>& Promise<T>::operator=(Promise&& other) {
+template <class T, class BatonT>
+Promise<T, BatonT>& Promise<T, BatonT>::operator=(Promise&& other) {
   std::swap(value_, other.value_);
   std::swap(baton_, other.baton_);
   return *this;
 }
 
-template <class T>
-void Promise<T>::throwIfFulfilled() const {
+template <class T, class BatonT>
+void Promise<T, BatonT>::throwIfFulfilled() const {
   if (!value_) {
     throw std::logic_error("promise already fulfilled");
   }
 }
 
-template <class T>
-Promise<T>::~Promise() {
+template <class T, class BatonT>
+Promise<T, BatonT>::~Promise() {
   if (value_) {
     setException(folly::make_exception_wrapper<std::logic_error>(
         "promise not fulfilled"));
   }
 }
 
-template <class T>
-void Promise<T>::setException(folly::exception_wrapper e) {
+template <class T, class BatonT>
+void Promise<T, BatonT>::setException(folly::exception_wrapper e) {
   setTry(folly::Try<T>(e));
 }
 
-template <class T>
-void Promise<T>::setTry(folly::Try<T>&& t) {
+template <class T, class BatonT>
+void Promise<T, BatonT>::setTry(folly::Try<T>&& t) {
   throwIfFulfilled();
 
   *value_ = std::move(t);
@@ -68,37 +68,37 @@ void Promise<T>::setTry(folly::Try<T>&& t) {
   baton_->post();
 }
 
-template <class T>
+template <class T, class BatonT>
 template <class M>
-void Promise<T>::setValue(M&& v) {
+void Promise<T, BatonT>::setValue(M&& v) {
   static_assert(!std::is_same<T, void>::value, "Use setValue() instead");
 
   setTry(folly::Try<T>(std::forward<M>(v)));
 }
 
-template <class T>
-void Promise<T>::setValue() {
+template <class T, class BatonT>
+void Promise<T, BatonT>::setValue() {
   static_assert(std::is_same<T, void>::value, "Use setValue(value) instead");
 
   setTry(folly::Try<void>());
 }
 
-template <class T>
+template <class T, class BatonT>
 template <class F>
-void Promise<T>::setWith(F&& func) {
+void Promise<T, BatonT>::setWith(F&& func) {
   setTry(makeTryWith(std::forward<F>(func)));
 }
 
-template <class T>
+template <class T, class BatonT>
 template <class F>
-typename Promise<T>::value_type Promise<T>::await(F&& func) {
+typename Promise<T, BatonT>::value_type Promise<T, BatonT>::await(F&& func) {
   folly::Try<value_type> result;
   std::exception_ptr funcException;
 
-  Baton baton;
+  BatonT baton;
   baton.wait([&func, &result, &baton, &funcException]() mutable {
     try {
-      func(Promise<value_type>(result, baton));
+      func(Promise<value_type, BatonT>(result, baton));
     } catch (...) {
       // Save the exception, but still wait for baton to be posted by user code
       // or promise destructor.
index fde863dafdc9baa3f3e021bd297d4143aee8757e..6e4a99a801e7c1c26de4f00c2b2bed1453d86594 100644 (file)
@@ -23,10 +23,11 @@ namespace fibers {
 
 class Baton;
 
-template <typename T>
+template <typename T, typename BatonT = Baton>
 class Promise {
  public:
   typedef T value_type;
+  typedef BatonT baton_type;
 
   ~Promise();
 
@@ -80,9 +81,9 @@ class Promise {
   static value_type await(F&& func);
 
  private:
-  Promise(folly::Try<T>& value, Baton& baton);
+  Promise(folly::Try<T>& value, BatonT& baton);
   folly::Try<T>* value_;
-  Baton* baton_;
+  BatonT* baton_;
 
   void throwIfFulfilled() const;