File: function.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (78 lines) | stat: -rw-r--r-- 3,068 bytes parent folder | download
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
#pragma once

#include <nall/traits.hpp>

namespace nall {

template<typename T> struct function;

template<typename R, typename... P> struct function<auto (P...) -> R> {
  using cast = auto (*)(P...) -> R;

  //value = true if auto L::operator()(P...) -> R exists
  template<typename L> struct is_compatible {
    template<typename T> static auto exists(T*) -> const typename is_same<R, decltype(declval<T>().operator()(declval<P>()...))>::type;
    template<typename T> static auto exists(...) -> const false_type;
    static constexpr bool value = decltype(exists<L>(0))::value;
  };

  function() {}
  function(const function& source) { operator=(source); }
  function(auto (*function)(P...) -> R) { callback = new global(function); }
  template<typename C> function(auto (C::*function)(P...) -> R, C* object) { callback = new member<C>(function, object); }
  template<typename C> function(auto (C::*function)(P...) const -> R, C* object) { callback = new member<C>((auto (C::*)(P...) -> R)function, object); }
  template<typename L, typename = enable_if_t<is_compatible<L>::value>> function(const L& object) { callback = new lambda<L>(object); }
  explicit function(void* function) { if(function) callback = new global((auto (*)(P...) -> R)function); }
  ~function() { if(callback) delete callback; }

  explicit operator bool() const { return callback; }
  auto operator()(P... p) const -> R { return (*callback)(forward<P>(p)...); }
  auto reset() -> void { if(callback) { delete callback; callback = nullptr; } }

  auto operator=(const function& source) -> function& {
    if(this != &source) {
      if(callback) { delete callback; callback = nullptr; }
      if(source.callback) callback = source.callback->copy();
    }
    return *this;
  }

  auto operator=(void* source) -> function& {
    if(callback) { delete callback; callback = nullptr; }
    callback = new global((auto (*)(P...) -> R)source);
    return *this;
  }

private:
  struct container {
    virtual auto operator()(P... p) const -> R = 0;
    virtual auto copy() const -> container* = 0;
    virtual ~container() = default;
  };

  container* callback = nullptr;

  struct global : container {
    auto (*function)(P...) -> R;
    auto operator()(P... p) const -> R { return function(forward<P>(p)...); }
    auto copy() const -> container* { return new global(function); }
    global(auto (*function)(P...) -> R) : function(function) {}
  };

  template<typename C> struct member : container {
    auto (C::*function)(P...) -> R;
    C* object;
    auto operator()(P... p) const -> R { return (object->*function)(forward<P>(p)...); }
    auto copy() const -> container* { return new member(function, object); }
    member(auto (C::*function)(P...) -> R, C* object) : function(function), object(object) {}
  };

  template<typename L> struct lambda : container {
    mutable L object;
    auto operator()(P... p) const -> R { return object(forward<P>(p)...); }
    auto copy() const -> container* { return new lambda(object); }
    lambda(const L& object) : object(object) {}
  };
};

}