aboutsummaryrefslogtreecommitdiff
path: root/libbutl/move-only-function.hxx
blob: e5cfe51eef3c20713b2a5bba8c856484cd5ebb68 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// file      : libbutl/move-only-function.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#pragma once

#include <utility>
#include <functional>
#include <type_traits>

namespace butl
{
  // This is a move-only std::function version which is implemented in terms
  // of std::function. It is similar to C++23 std::move_only_function but
  // still provides target() (but not target_type()).
  //
  template <typename>
  class move_only_function_ex;

  // Alias butl::move_only_function to std::move_only_function if available
  // and to move_only_function_ex otherwise.
  //
#ifdef __cpp_lib_move_only_function
  using std::move_only_function;
#else
  template <typename F>
  using move_only_function = move_only_function_ex<F>;
#endif

  template <typename R, typename... A>
  class move_only_function_ex<R (A...)>
  {
  public:
    using result_type = R;

    move_only_function_ex () = default;
    move_only_function_ex (std::nullptr_t) noexcept {}

    // Note: according to the spec we should also disable these if F is not
    // callable, but that is not easy to do in C++14. Maybe we should do
    // something for C++17 and later (without this the diagnostics is quite
    // hairy).
    //
    template <typename F>
    move_only_function_ex (F&& f, typename std::enable_if<!std::is_same<typename std::remove_reference<F>::type, move_only_function_ex>::value>::type* = 0)
    {
      using FV = typename std::decay<F>::type;

      if (!null (f))
        f_ = wrapper<FV> (std::forward<F> (f));
    }

    template <typename F>
    typename std::enable_if<!std::is_same<typename std::remove_reference<F>::type, move_only_function_ex>::value, move_only_function_ex>::type&
    operator= (F&& f)
    {
      move_only_function_ex (std::forward<F> (f)).swap (*this);
      return *this;
    }

    move_only_function_ex&
    operator= (std::nullptr_t) noexcept
    {
      f_ = nullptr;
      return *this;
    }

    void swap (move_only_function_ex& f) noexcept
    {
      f_.swap (f.f_);
    }

    R operator() (A... args) const
    {
      return f_ (std::forward<A> (args)...);
    }

    explicit operator bool () const noexcept
    {
      return static_cast<bool> (f_);
    }

    template <typename T>
    T* target() noexcept
    {
      wrapper<T>* r (f_.template target<wrapper<T>> ());
      return r != nullptr ? &r->f : nullptr;
    }

    template <typename T>
    const T* target() const noexcept
    {
      const wrapper<T>* r (f_.template target<wrapper<T>> ());
      return r != nullptr ? &r->f : nullptr;
    }

    move_only_function_ex (move_only_function_ex&&) = default;
    move_only_function_ex& operator= (move_only_function_ex&&) = default;

    move_only_function_ex (const move_only_function_ex&) = delete;
    move_only_function_ex& operator= (const move_only_function_ex&) = delete;

  private:
    template <typename F>
    struct wrapper
    {
      struct empty {};

      union
      {
        F f;
        empty e;
      };

      explicit wrapper (F&& f_): f (std::move (f_)) {}
      explicit wrapper (const F& f_): f (f_) {}

      R operator() (A... args)
      {
        return f (std::forward<A> (args)...);
      }

      R operator() (A... args) const
      {
        return f (std::forward<A> (args)...);
      }

      wrapper (wrapper&& w)
        noexcept (std::is_nothrow_move_constructible<F>::value)
        : f (std::move (w.f)) {}

      wrapper& operator= (wrapper&&) = delete; // Shouldn't be needed.

      ~wrapper () {f.~F ();}

      // These shouldn't be called.
      //
      wrapper (const wrapper&) {}
      wrapper& operator= (const wrapper&) {return *this;}
    };

    template <typename F>                              static bool null (const F&) {return false;}
    template <typename R1, typename... A1>             static bool null (R1 (*p) (A1...)) {return p == nullptr;}
    template <typename R1, typename... A1>             static bool null (const move_only_function_ex<R1 (A1...)>& f) {return !f;}
    template <typename R1, typename C, typename... A1> static bool null (R1 (C::*p) (A1...)) {return p == nullptr;}
    template <typename R1, typename C, typename... A1> static bool null (R1 (C::*p) (A1...) const) {return p == nullptr;}

    std::function<R (A...)> f_;
  };

  template <typename R, typename... A>
  inline bool
  operator== (const move_only_function_ex<R (A...)>& f, std::nullptr_t) noexcept
  {
    return !f;
  }

  template <typename R, typename... A>
  inline bool
  operator== (std::nullptr_t, const move_only_function_ex<R (A...)>& f) noexcept
  {
    return !f;
  }

  template <typename R, typename... A>
  inline bool
  operator!= (const move_only_function_ex<R (A...)>& f, std::nullptr_t) noexcept
  {
    return static_cast<bool> (f);
  }

  template <typename R, typename... A>
  inline bool
  operator!= (std::nullptr_t, const move_only_function_ex<R (A...)>& f) noexcept
  {
    return static_cast<bool> (f);
  }
}