explicit return types in Partial to work around gcc bug
[folly.git] / folly / Partial.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 <folly/ApplyTuple.h>
20
21 namespace folly {
22
23 template <typename F, typename Tuple>
24 class Partial {
25  private:
26   F f_;
27   Tuple stored_args_;
28
29  public:
30   template <typename Callable, typename... Args>
31   Partial(Callable&& callable, Args&&... args)
32       : f_(std::forward<Callable>(callable)),
33         stored_args_(std::forward<Args>(args)...) {}
34
35   // full auto doesn't work here due to
36   // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70983 :(
37
38   template <typename... CArgs>
39   auto operator()(CArgs&&... cargs) & -> decltype(applyTuple(
40       static_cast<F&>(f_),
41       static_cast<Tuple&>(stored_args_),
42       std::forward_as_tuple(std::forward<CArgs>(cargs)...))) {
43     return applyTuple(
44         static_cast<F&>(f_),
45         static_cast<Tuple&>(stored_args_),
46         std::forward_as_tuple(std::forward<CArgs>(cargs)...));
47   }
48
49   template <typename... CArgs>
50   auto operator()(CArgs&&... cargs) const& -> decltype(applyTuple(
51       static_cast<F const&>(f_),
52       static_cast<Tuple const&>(stored_args_),
53       std::forward_as_tuple(std::forward<CArgs>(cargs)...))) {
54     return applyTuple(
55         static_cast<F const&>(f_),
56         static_cast<Tuple const&>(stored_args_),
57         std::forward_as_tuple(std::forward<CArgs>(cargs)...));
58   }
59
60   template <typename... CArgs>
61   auto operator()(CArgs&&... cargs) && -> decltype(applyTuple(
62       static_cast<F&&>(f_),
63       static_cast<Tuple&&>(stored_args_),
64       std::forward_as_tuple(std::forward<CArgs>(cargs)...))) {
65     return applyTuple(
66         static_cast<F&&>(f_),
67         static_cast<Tuple&&>(stored_args_),
68         std::forward_as_tuple(std::forward<CArgs>(cargs)...));
69   }
70 };
71
72 /**
73  * Partially applies arguments to a callable
74  *
75  * `partial` takes a callable and zero or more additional arguments and returns
76  * a callable object, which when called with zero or more arguments, will invoke
77  * the original callable with the additional arguments passed to `partial`,
78  * followed by those passed to the call.
79  *
80  * E.g. `partial(Foo, 1, 2)(3)` is equivalent to `Foo(1, 2, 3)`.
81  *
82  * `partial` can be used to bind a class method to an instance:
83  * `partial(&Foo::method, foo_pointer)` returns a callable object that can be
84  * invoked in the same way as `foo_pointer->method`. In case the first
85  * argument in a call to `partial` is a member pointer, the second argument
86  * can be a reference, pointer or any object that can be dereferenced to
87  * an object of type Foo (like `std::shared_ptr` or `std::unique_ptr`).
88  *
89  * `partial` is similar to `std::bind`, but you don't have to use placeholders
90  * to have arguments passed on. Any number of arguments passed to the object
91  * returned by `partial` when called will be added to those passed to `partial`
92  * and passed to the original callable.
93  */
94 template <typename F, typename... Args>
95 auto partial(F&& f, Args&&... args) -> Partial<
96     typename std::decay<F>::type,
97     std::tuple<typename std::decay<Args>::type...>> {
98   return {std::forward<F>(f), std::forward<Args>(args)...};
99 }
100
101 } // namespace folly