Add method, that constructs Try<T> based on the result of functor.
[folly.git] / folly / wangle / Try-inl.h
1 /*
2  * Copyright 2014 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 "WangleException.h"
22
23 namespace folly { namespace wangle {
24
25 template <class T>
26 Try<T>::Try(Try<T>&& t) : contains_(t.contains_) {
27   if (contains_ == VALUE) {
28     new (&value_)T(std::move(t.value_));
29   } else if (contains_ == EXCEPTION) {
30     new (&e_)std::exception_ptr(t.e_);
31   }
32 }
33
34 template <class T>
35 Try<T>& Try<T>::operator=(Try<T>&& t) {
36   this->~Try();
37   contains_ = t.contains_;
38   if (contains_ == VALUE) {
39     new (&value_)T(std::move(t.value_));
40   } else if (contains_ == EXCEPTION) {
41     new (&e_)std::exception_ptr(t.e_);
42   }
43   return *this;
44 }
45
46 template <class T>
47 Try<T>::~Try() {
48   if (contains_ == VALUE) {
49     value_.~T();
50   } else if (contains_ == EXCEPTION) {
51     e_.~exception_ptr();
52   }
53 }
54
55 template <class T>
56 T& Try<T>::value() {
57   throwIfFailed();
58   return value_;
59 }
60
61 template <class T>
62 const T& Try<T>::value() const {
63   throwIfFailed();
64   return value_;
65 }
66
67 template <class T>
68 void Try<T>::throwIfFailed() const {
69   if (contains_ != VALUE) {
70     if (contains_ == EXCEPTION) {
71       std::rethrow_exception(e_);
72     } else {
73       throw UsingUninitializedTry();
74     }
75   }
76 }
77
78 void Try<void>::throwIfFailed() const {
79   if (!hasValue_) {
80     std::rethrow_exception(e_);
81   }
82 }
83
84 template <typename T>
85 inline T moveFromTry(wangle::Try<T>&& t) {
86   return std::move(t.value());
87 }
88
89 inline void moveFromTry(wangle::Try<void>&& t) {
90   return t.value();
91 }
92
93 template <typename F>
94 typename std::enable_if<
95   !std::is_same<typename std::result_of<F()>::type, void>::value,
96   Try<typename std::result_of<F()>::type>>::type
97 makeTryFunction(F&& f) {
98   typedef typename std::result_of<F()>::type ResultType;
99   try {
100     auto value = f();
101     return Try<ResultType>(std::move(value));
102   } catch (...) {
103     return Try<ResultType>(std::current_exception());
104   }
105 }
106
107 template <typename F>
108 typename std::enable_if<
109   std::is_same<typename std::result_of<F()>::type, void>::value,
110   Try<void>>::type
111 makeTryFunction(F&& f) {
112   try {
113     f();
114     return Try<void>();
115   } catch (...) {
116     return Try<void>(std::current_exception());
117   }
118 }
119
120 }}