folly copyright 2015 -> copyright 2016
[folly.git] / folly / futures / Try-inl.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <stdexcept>
20
21 #include <folly/futures/FutureException.h>
22
23 namespace folly {
24
25 template <class T>
26 Try<T>::Try(Try<T>&& t) noexcept : contains_(t.contains_) {
27   if (contains_ == Contains::VALUE) {
28     new (&value_)T(std::move(t.value_));
29   } else if (contains_ == Contains::EXCEPTION) {
30     new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
31   }
32 }
33
34 template <class T>
35 template <class T2>
36 Try<T>::Try(typename std::enable_if<std::is_same<Unit, T2>::value,
37                                     Try<void> const&>::type t)
38     : contains_(Contains::NOTHING) {
39   if (t.hasValue()) {
40     contains_ = Contains::VALUE;
41     new (&value_) T();
42   } else if (t.hasException()) {
43     contains_ = Contains::EXCEPTION;
44     new (&e_) std::unique_ptr<exception_wrapper>(
45         folly::make_unique<exception_wrapper>(t.exception()));
46   }
47 }
48
49 template <class T>
50 Try<T>& Try<T>::operator=(Try<T>&& t) noexcept {
51   if (this == &t) {
52     return *this;
53   }
54
55   this->~Try();
56   contains_ = t.contains_;
57   if (contains_ == Contains::VALUE) {
58     new (&value_)T(std::move(t.value_));
59   } else if (contains_ == Contains::EXCEPTION) {
60     new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
61   }
62   return *this;
63 }
64
65 template <class T>
66 Try<T>::Try(const Try<T>& t) {
67   static_assert(
68       std::is_copy_constructible<T>::value,
69       "T must be copyable for Try<T> to be copyable");
70   contains_ = t.contains_;
71   if (contains_ == Contains::VALUE) {
72     new (&value_)T(t.value_);
73   } else if (contains_ == Contains::EXCEPTION) {
74     new (&e_)std::unique_ptr<exception_wrapper>();
75     e_ = folly::make_unique<exception_wrapper>(*(t.e_));
76   }
77 }
78
79 template <class T>
80 Try<T>& Try<T>::operator=(const Try<T>& t) {
81   static_assert(
82       std::is_copy_constructible<T>::value,
83       "T must be copyable for Try<T> to be copyable");
84   this->~Try();
85   contains_ = t.contains_;
86   if (contains_ == Contains::VALUE) {
87     new (&value_)T(t.value_);
88   } else if (contains_ == Contains::EXCEPTION) {
89     new (&e_)std::unique_ptr<exception_wrapper>();
90     e_ = folly::make_unique<exception_wrapper>(*(t.e_));
91   }
92   return *this;
93 }
94
95 template <class T>
96 Try<T>::~Try() {
97   if (LIKELY(contains_ == Contains::VALUE)) {
98     value_.~T();
99   } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
100     e_.~unique_ptr<exception_wrapper>();
101   }
102 }
103
104 template <class T>
105 T& Try<T>::value() {
106   throwIfFailed();
107   return value_;
108 }
109
110 template <class T>
111 const T& Try<T>::value() const {
112   throwIfFailed();
113   return value_;
114 }
115
116 template <class T>
117 void Try<T>::throwIfFailed() const {
118   if (contains_ != Contains::VALUE) {
119     if (contains_ == Contains::EXCEPTION) {
120       e_->throwException();
121     } else {
122       throw UsingUninitializedTry();
123     }
124   }
125 }
126
127 void Try<void>::throwIfFailed() const {
128   if (!hasValue_) {
129     e_->throwException();
130   }
131 }
132
133 template <typename T>
134 inline T moveFromTry(Try<T>& t) {
135   return std::move(t.value());
136 }
137
138 inline void moveFromTry(Try<void>& t) {
139   return t.value();
140 }
141
142 template <typename F>
143 typename std::enable_if<
144   !std::is_same<typename std::result_of<F()>::type, void>::value,
145   Try<typename std::result_of<F()>::type>>::type
146 makeTryWith(F&& f) {
147   typedef typename std::result_of<F()>::type ResultType;
148   try {
149     return Try<ResultType>(f());
150   } catch (std::exception& e) {
151     return Try<ResultType>(exception_wrapper(std::current_exception(), e));
152   } catch (...) {
153     return Try<ResultType>(exception_wrapper(std::current_exception()));
154   }
155 }
156
157 template <typename F>
158 typename std::enable_if<
159   std::is_same<typename std::result_of<F()>::type, void>::value,
160   Try<void>>::type
161 makeTryWith(F&& f) {
162   try {
163     f();
164     return Try<void>();
165   } catch (std::exception& e) {
166     return Try<void>(exception_wrapper(std::current_exception(), e));
167   } catch (...) {
168     return Try<void>(exception_wrapper(std::current_exception()));
169   }
170 }
171
172 } // folly