Consistency in namespace-closing comments
[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   switch (contains_) {
121     case Contains::VALUE:
122       return;
123     case Contains::EXCEPTION:
124       e_.throw_exception();
125     default:
126       try_detail::throwUsingUninitializedTry();
127   }
128 }
129
130 void Try<void>::throwIfFailed() const {
131   if (!hasValue_) {
132     e_.throw_exception();
133   }
134 }
135
136 template <typename F>
137 typename std::enable_if<
138   !std::is_same<typename std::result_of<F()>::type, void>::value,
139   Try<typename std::result_of<F()>::type>>::type
140 makeTryWith(F&& f) {
141   typedef typename std::result_of<F()>::type ResultType;
142   try {
143     return Try<ResultType>(f());
144   } catch (std::exception& e) {
145     return Try<ResultType>(exception_wrapper(std::current_exception(), e));
146   } catch (...) {
147     return Try<ResultType>(exception_wrapper(std::current_exception()));
148   }
149 }
150
151 template <typename F>
152 typename std::enable_if<
153   std::is_same<typename std::result_of<F()>::type, void>::value,
154   Try<void>>::type
155 makeTryWith(F&& f) {
156   try {
157     f();
158     return Try<void>();
159   } catch (std::exception& e) {
160     return Try<void>(exception_wrapper(std::current_exception(), e));
161   } catch (...) {
162     return Try<void>(exception_wrapper(std::current_exception()));
163   }
164 }
165
166 template <typename... Ts>
167 std::tuple<Ts...> unwrapTryTuple(std::tuple<folly::Try<Ts>...>&& ts) {
168   return detail::TryTuple<Ts...>::unwrap(
169       std::forward<std::tuple<folly::Try<Ts>...>>(ts));
170 }
171
172 } // namespace folly