Update documentation for Synchronized
[folly.git] / folly / 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 namespace folly {
22
23 template <class T>
24 Try<T>::Try(Try<T>&& t) noexcept : contains_(t.contains_) {
25   if (contains_ == Contains::VALUE) {
26     new (&value_)T(std::move(t.value_));
27   } else if (contains_ == Contains::EXCEPTION) {
28     new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
29   }
30 }
31
32 template <class T>
33 template <class T2>
34 Try<T>::Try(typename std::enable_if<std::is_same<Unit, T2>::value,
35                                     Try<void> const&>::type t)
36     : contains_(Contains::NOTHING) {
37   if (t.hasValue()) {
38     contains_ = Contains::VALUE;
39     new (&value_) T();
40   } else if (t.hasException()) {
41     contains_ = Contains::EXCEPTION;
42     new (&e_) std::unique_ptr<exception_wrapper>(
43         folly::make_unique<exception_wrapper>(t.exception()));
44   }
45 }
46
47 template <class T>
48 Try<T>& Try<T>::operator=(Try<T>&& t) noexcept {
49   if (this == &t) {
50     return *this;
51   }
52
53   this->~Try();
54   contains_ = t.contains_;
55   if (contains_ == Contains::VALUE) {
56     new (&value_)T(std::move(t.value_));
57   } else if (contains_ == Contains::EXCEPTION) {
58     new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
59   }
60   return *this;
61 }
62
63 template <class T>
64 Try<T>::Try(const Try<T>& t) {
65   static_assert(
66       std::is_copy_constructible<T>::value,
67       "T must be copyable for Try<T> to be copyable");
68   contains_ = t.contains_;
69   if (contains_ == Contains::VALUE) {
70     new (&value_)T(t.value_);
71   } else if (contains_ == Contains::EXCEPTION) {
72     new (&e_)std::unique_ptr<exception_wrapper>();
73     e_ = folly::make_unique<exception_wrapper>(*(t.e_));
74   }
75 }
76
77 template <class T>
78 Try<T>& Try<T>::operator=(const Try<T>& t) {
79   static_assert(
80       std::is_copy_constructible<T>::value,
81       "T must be copyable for Try<T> to be copyable");
82   this->~Try();
83   contains_ = t.contains_;
84   if (contains_ == Contains::VALUE) {
85     new (&value_)T(t.value_);
86   } else if (contains_ == Contains::EXCEPTION) {
87     new (&e_)std::unique_ptr<exception_wrapper>();
88     e_ = folly::make_unique<exception_wrapper>(*(t.e_));
89   }
90   return *this;
91 }
92
93 template <class T>
94 Try<T>::~Try() {
95   if (LIKELY(contains_ == Contains::VALUE)) {
96     value_.~T();
97   } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
98     e_.~unique_ptr<exception_wrapper>();
99   }
100 }
101
102 template <class T>
103 T& Try<T>::value() & {
104   throwIfFailed();
105   return value_;
106 }
107
108 template <class T>
109 T&& Try<T>::value() && {
110   throwIfFailed();
111   return std::move(value_);
112 }
113
114 template <class T>
115 const T& Try<T>::value() const & {
116   throwIfFailed();
117   return value_;
118 }
119
120 template <class T>
121 void Try<T>::throwIfFailed() const {
122   if (contains_ != Contains::VALUE) {
123     if (contains_ == Contains::EXCEPTION) {
124       e_->throwException();
125     } else {
126       throw UsingUninitializedTry();
127     }
128   }
129 }
130
131 void Try<void>::throwIfFailed() const {
132   if (!hasValue_) {
133     e_->throwException();
134   }
135 }
136
137 template <typename T>
138 inline T moveFromTry(Try<T>& t) {
139   return std::move(t.value());
140 }
141
142 inline void moveFromTry(Try<void>& t) {
143   return t.value();
144 }
145
146 template <typename F>
147 typename std::enable_if<
148   !std::is_same<typename std::result_of<F()>::type, void>::value,
149   Try<typename std::result_of<F()>::type>>::type
150 makeTryWith(F&& f) {
151   typedef typename std::result_of<F()>::type ResultType;
152   try {
153     return Try<ResultType>(f());
154   } catch (std::exception& e) {
155     return Try<ResultType>(exception_wrapper(std::current_exception(), e));
156   } catch (...) {
157     return Try<ResultType>(exception_wrapper(std::current_exception()));
158   }
159 }
160
161 template <typename F>
162 typename std::enable_if<
163   std::is_same<typename std::result_of<F()>::type, void>::value,
164   Try<void>>::type
165 makeTryWith(F&& f) {
166   try {
167     f();
168     return Try<void>();
169   } catch (std::exception& e) {
170     return Try<void>(exception_wrapper(std::current_exception(), e));
171   } catch (...) {
172     return Try<void>(exception_wrapper(std::current_exception()));
173   }
174 }
175
176 } // folly