aboutsummaryrefslogtreecommitdiff
path: root/libbutl/mingw-invoke.hxx
blob: 65810e74c9c1df5a44ed00cbd6d324bc9efef5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Lightweight std::invoke() implementation for C++11 and C++14
*
* Copyright (c) 2018-2019 by Nathaniel J. McClatchey, San Jose, CA, United States
* Copyright (c) 2022 the build2 authors
*
* Licensed under the simplified (2-clause) BSD License.
* You should have received a copy of the license along with this
* program.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#ifndef LIBBUTL_MINGW_INVOKE_HXX
#define LIBBUTL_MINGW_INVOKE_HXX

#include <type_traits>  //  For std::result_of, etc.
#include <utility>      //  For std::forward
#include <functional>   //  For std::reference_wrapper

namespace mingw_stdthread
{
  namespace detail
  {
    // For compatibility, implement std::invoke for C++11 and C++14.
    //
    template<bool PMemFunc, bool PMemData>
    struct Invoker
    {
      template<class F, class... Args>
      inline static typename std::result_of<F(Args...)>::type invoke (F&& f, Args&&... args)
      {
        return std::forward<F>(f)(std::forward<Args>(args)...);
      }
    };
    template<bool>
    struct InvokerHelper;

    template<>
    struct InvokerHelper<false>
    {
      template<class T1>
      inline static auto get (T1&& t1) -> decltype(*std::forward<T1>(t1))
      {
        return *std::forward<T1>(t1);
      }

      template<class T1>
      inline static auto get (const std::reference_wrapper<T1>& t1) -> decltype(t1.get())
      {
        return t1.get();
      }
    };

    template<>
    struct InvokerHelper<true>
    {
      template<class T1>
      inline static auto get (T1&& t1) -> decltype(std::forward<T1>(t1))
      {
        return std::forward<T1>(t1);
      }
    };

    template<>
    struct Invoker<true, false>
    {
      template<class T, class F, class T1, class... Args>
      inline static auto invoke (F T::* f, T1&& t1, Args&&... args) ->  \
        decltype((InvokerHelper<std::is_base_of<T,typename std::decay<T1>::type>::value>::get(std::forward<T1>(t1)).*f)(std::forward<Args>(args)...))
      {
        return (InvokerHelper<std::is_base_of<T,typename std::decay<T1>::type>::value>::get(std::forward<T1>(t1)).*f)(std::forward<Args>(args)...);
      }
    };

    template<>
    struct Invoker<false, true>
    {
      template<class T, class F, class T1, class... Args>
      inline static auto invoke (F T::* f, T1&& t1, Args&&... args) ->  \
        decltype(InvokerHelper<std::is_base_of<T,typename std::decay<T1>::type>::value>::get(t1).*f)
      {
        return InvokerHelper<std::is_base_of<T,typename std::decay<T1>::type>::value>::get(t1).*f;
      }
    };

    template<class F, class... Args>
    struct InvokeResult
    {
      typedef Invoker<std::is_member_function_pointer<typename std::remove_reference<F>::type>::value,
                      std::is_member_object_pointer<typename std::remove_reference<F>::type>::value &&
                      (sizeof...(Args) == 1)> invoker;
      inline static auto invoke (F&& f, Args&&... args) -> decltype(invoker::invoke(std::forward<F>(f), std::forward<Args>(args)...))
      {
        return invoker::invoke(std::forward<F>(f), std::forward<Args>(args)...);
      }
    };

    template<class F, class...Args>
    auto invoke (F&& f, Args&&... args) -> decltype(InvokeResult<F, Args...>::invoke(std::forward<F>(f), std::forward<Args>(args)...))
    {
      return InvokeResult<F, Args...>::invoke(std::forward<F>(f), std::forward<Args>(args)...);
    }
  }
}

#endif // LIBBUTL_MINGW_INVOKE_HXX