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