0f16dd6c3055abf9263c2fd88494630ae4266c3a
[folly.git] / folly / experimental / fibers / traits.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 #pragma once
17
18 #include <boost/type_traits.hpp>
19
20 namespace folly { namespace fibers {
21
22 /**
23  * For any functor F taking >= 1 argument,
24  * FirstArgOf<F>::type is the type of F's first parameter.
25  *
26  * Rationale: we want to declare a function func(F), where F has the
27  * signature `void(X)` and func should return T<X> (T and X are some types).
28  * Solution:
29  *
30  * template <typename F>
31  * T<typename FirstArgOf<F>::type>
32  * func(F&& f);
33  */
34
35 namespace detail {
36
37 /**
38  * If F is a pointer-to-member, will contain a typedef type
39  * with the type of F's first parameter
40  */
41 template<typename>
42 struct ExtractFirstMemfn;
43
44 template <typename Ret, typename T, typename First, typename... Args>
45 struct ExtractFirstMemfn<Ret (T::*)(First, Args...)> {
46   typedef First type;
47 };
48
49 template <typename Ret, typename T, typename First, typename... Args>
50 struct ExtractFirstMemfn<Ret (T::*)(First, Args...) const> {
51   typedef First type;
52 };
53
54 }  // detail
55
56 /** Default - use boost */
57 template <typename F, typename Enable = void>
58 struct FirstArgOf {
59   typedef typename boost::function_traits<
60     typename std::remove_pointer<F>::type>::arg1_type type;
61 };
62
63 /** Specialization for function objects */
64 template <typename F>
65 struct FirstArgOf<F, typename std::enable_if<std::is_class<F>::value>::type> {
66   typedef typename detail::ExtractFirstMemfn<
67     decltype(&F::operator())>::type type;
68 };
69
70 }}  // folly::fibers