Move address caching logic from AsyncSSLSocket to AsyncSocket.
[folly.git] / folly / Try-inl.h
1 /*
2  * Copyright 2017 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 <folly/detail/TryDetail.h>
20 #include <stdexcept>
21
22 namespace folly {
23
24 template <class T>
25 Try<T>::Try(Try<T>&& t) noexcept : contains_(t.contains_) {
26   if (contains_ == Contains::VALUE) {
27     new (&value_)T(std::move(t.value_));
28   } else if (contains_ == Contains::EXCEPTION) {
29     new (&e_) exception_wrapper(std::move(t.e_));
30   }
31 }
32
33 template <class T>
34 template <class T2>
35 Try<T>::Try(typename std::enable_if<std::is_same<Unit, T2>::value,
36                                     Try<void> const&>::type t)
37     : contains_(Contains::NOTHING) {
38   if (t.hasValue()) {
39     contains_ = Contains::VALUE;
40     new (&value_) T();
41   } else if (t.hasException()) {
42     contains_ = Contains::EXCEPTION;
43     new (&e_) 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_) 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_) exception_wrapper(t.e_);
73   }
74 }
75
76 template <class T>
77 Try<T>& Try<T>::operator=(const Try<T>& t) {
78   static_assert(
79       std::is_copy_constructible<T>::value,
80       "T must be copyable for Try<T> to be copyable");
81   this->~Try();
82   contains_ = t.contains_;
83   if (contains_ == Contains::VALUE) {
84     new (&value_)T(t.value_);
85   } else if (contains_ == Contains::EXCEPTION) {
86     new (&e_) exception_wrapper(t.e_);
87   }
88   return *this;
89 }
90
91 template <class T>
92 Try<T>::~Try() {
93   if (LIKELY(contains_ == Contains::VALUE)) {
94     value_.~T();
95   } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
96     e_.~exception_wrapper();
97   }
98 }
99
100 template <class T>
101 T& Try<T>::value() & {
102   throwIfFailed();
103   return value_;
104 }
105
106 template <class T>
107 T&& Try<T>::value() && {
108   throwIfFailed();
109   return std::move(value_);
110 }
111
112 template <class T>
113 const T& Try<T>::value() const & {
114   throwIfFailed();
115   return value_;
116 }
117
118 template <class T>
119 void Try<T>::throwIfFailed() const {
120   if (contains_ != Contains::VALUE) {
121     if (contains_ == Contains::EXCEPTION) {
122       e_.throw_exception();
123     } else {
124       throw UsingUninitializedTry();
125     }
126   }
127 }
128
129 void Try<void>::throwIfFailed() const {
130   if (!hasValue_) {
131     e_.throw_exception();
132   }
133 }
134
135 template <typename T>
136 inline T moveFromTry(Try<T>& t) {
137   return std::move(t.value());
138 }
139
140 inline void moveFromTry(Try<void>& t) {
141   return t.value();
142 }
143
144 template <typename F>
145 typename std::enable_if<
146   !std::is_same<typename std::result_of<F()>::type, void>::value,
147   Try<typename std::result_of<F()>::type>>::type
148 makeTryWith(F&& f) {
149   typedef typename std::result_of<F()>::type ResultType;
150   try {
151     return Try<ResultType>(f());
152   } catch (std::exception& e) {
153     return Try<ResultType>(exception_wrapper(std::current_exception(), e));
154   } catch (...) {
155     return Try<ResultType>(exception_wrapper(std::current_exception()));
156   }
157 }
158
159 template <typename F>
160 typename std::enable_if<
161   std::is_same<typename std::result_of<F()>::type, void>::value,
162   Try<void>>::type
163 makeTryWith(F&& f) {
164   try {
165     f();
166     return Try<void>();
167   } catch (std::exception& e) {
168     return Try<void>(exception_wrapper(std::current_exception(), e));
169   } catch (...) {
170     return Try<void>(exception_wrapper(std::current_exception()));
171   }
172 }
173
174 template <typename... Ts>
175 std::tuple<Ts...> unwrapTryTuple(std::tuple<folly::Try<Ts>...>&& ts) {
176   return detail::TryTuple<Ts...>::unwrap(
177       std::forward<std::tuple<folly::Try<Ts>...>>(ts));
178 }
179
180 } // folly