Fix copyright lines
[folly.git] / folly / fibers / BatchDispatcher.h
index 671675dd062ec46b04831f45dbacdef54c9737d5..6cabc200faa721f5ccd5f8f14e343db7a8d92630 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2016-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 #pragma once
 
+#include <folly/Function.h>
 #include <folly/futures/Future.h>
 #include <folly/futures/Promise.h>
+#include <exception>
+#include <memory>
+#include <stdexcept>
+#include <vector>
 
 namespace folly {
 namespace fibers {
@@ -75,7 +80,7 @@ class BatchDispatcher {
       : executor_(executor),
         state_(new DispatchState(std::move(dispatchFunc))) {}
 
-  Future<ResultT> add(ValueT&& value) {
+  Future<ResultT> add(ValueT value) {
     if (state_->values.empty()) {
       executor_.add([state = state_]() { dispatchFunctionWrapper(*state); });
     }
@@ -105,18 +110,30 @@ class BatchDispatcher {
     state.values.swap(values);
     state.promises.swap(promises);
 
-    auto results = state.dispatchFunc(std::move(values));
-    if (results.size() != promises.size()) {
-      throw std::logic_error(
-          "Unexpected number of results returned from dispatch function");
-    }
-    for (size_t i = 0; i < promises.size(); i++) {
-      promises[i].setValue(std::move(results[i]));
+    try {
+      auto results = state.dispatchFunc(std::move(values));
+      if (results.size() != promises.size()) {
+        throw std::logic_error(
+            "Unexpected number of results returned from dispatch function");
+      }
+
+      for (size_t i = 0; i < promises.size(); i++) {
+        promises[i].setValue(std::move(results[i]));
+      }
+    } catch (const std::exception& ex) {
+      for (size_t i = 0; i < promises.size(); i++) {
+        promises[i].setException(
+            exception_wrapper(std::current_exception(), ex));
+      }
+    } catch (...) {
+      for (size_t i = 0; i < promises.size(); i++) {
+        promises[i].setException(exception_wrapper(std::current_exception()));
+      }
     }
   }
 
   ExecutorT& executor_;
   std::shared_ptr<DispatchState> state_;
 };
-}
-}
+} // namespace fibers
+} // namespace folly