Ensure curly-braces around control-flow
[folly.git] / folly / futures / Promise-inl.h
index b347486a4faa0d10999e3d852462bb98fbb68985..7471228c4e2719db43c6af234a40b6994a9db3f4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 namespace folly {
 
 template <class T>
-Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
-{}
+Promise<T> Promise<T>::makeEmpty() noexcept {
+  return Promise<T>(futures::detail::EmptyConstruct{});
+}
+
+template <class T>
+Promise<T>::Promise()
+    : retrieved_(false), core_(new futures::detail::Core<T>()) {}
 
 template <class T>
-Promise<T>::Promise(Promise<T>&& other) : core_(nullptr) {
-  *this = std::move(other);
+Promise<T>::Promise(Promise<T>&& other) noexcept
+    : retrieved_(other.retrieved_), core_(other.core_) {
+  other.core_ = nullptr;
+  other.retrieved_ = false;
 }
 
 template <class T>
-Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
+Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
   std::swap(core_, other.core_);
   std::swap(retrieved_, other.retrieved_);
   return *this;
@@ -42,18 +49,25 @@ Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
 
 template <class T>
 void Promise<T>::throwIfFulfilled() {
-  if (!core_)
-    throw NoState();
-  if (core_->ready())
-    throw PromiseAlreadySatisfied();
+  if (!core_) {
+    throwNoState();
+  }
+  if (core_->ready()) {
+    throwPromiseAlreadySatisfied();
+  }
 }
 
 template <class T>
 void Promise<T>::throwIfRetrieved() {
-  if (retrieved_)
-    throw FutureAlreadyRetrieved();
+  if (retrieved_) {
+    throwFutureAlreadyRetrieved();
+  }
 }
 
+template <class T>
+Promise<T>::Promise(futures::detail::EmptyConstruct) noexcept
+    : retrieved_(false), core_(nullptr) {}
+
 template <class T>
 Promise<T>::~Promise() {
   detach();
@@ -62,8 +76,9 @@ Promise<T>::~Promise() {
 template <class T>
 void Promise<T>::detach() {
   if (core_) {
-    if (!retrieved_)
+    if (!retrieved_) {
       core_->detachFuture();
+    }
     core_->detachPromise();
     core_ = nullptr;
   }
@@ -85,13 +100,7 @@ Promise<T>::setException(E const& e) {
 
 template <class T>
 void Promise<T>::setException(std::exception_ptr const& ep) {
-  try {
-    std::rethrow_exception(ep);
-  } catch (const std::exception& e) {
-    setException(exception_wrapper(std::current_exception(), e));
-  } catch (...) {
-    setException(exception_wrapper(std::current_exception()));
-  }
+  setException(exception_wrapper::from_exception_ptr(ep));
 }
 
 template <class T>
@@ -107,7 +116,7 @@ void Promise<T>::setInterruptHandler(
 }
 
 template <class T>
-void Promise<T>::fulfilTry(Try<T> t) {
+void Promise<T>::setTry(Try<T>&& t) {
   throwIfFulfilled();
   core_->setResult(std::move(t));
 }
@@ -118,22 +127,22 @@ void Promise<T>::setValue(M&& v) {
   static_assert(!std::is_same<T, void>::value,
                 "Use setValue() instead");
 
-  fulfilTry(Try<T>(std::forward<M>(v)));
+  setTry(Try<T>(std::forward<M>(v)));
 }
 
 template <class T>
-void Promise<T>::setValue() {
-  static_assert(std::is_same<T, void>::value,
-                "Use setValue(value) instead");
-
-  fulfilTry(Try<void>());
+template <class F>
+void Promise<T>::setWith(F&& func) {
+  throwIfFulfilled();
+  setTry(makeTryWith(std::forward<F>(func)));
 }
 
 template <class T>
-template <class F>
-void Promise<T>::fulfil(F&& func) {
-  throwIfFulfilled();
-  fulfilTry(makeTryFunction(std::forward<F>(func)));
+bool Promise<T>::isFulfilled() const noexcept {
+  if (core_) {
+    return core_->hasResult();
+  }
+  return true;
 }
 
 }