File: function.hpp

package info (click to toggle)
higan 094-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,780 kB
  • ctags: 15,643
  • sloc: cpp: 103,963; ansic: 659; makefile: 531; sh: 25
file content (64 lines) | stat: -rwxr-xr-x 2,278 bytes parent folder | download | duplicates (5)
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
#ifndef NALL_FUNCTION_HPP
#define NALL_FUNCTION_HPP

namespace nall {

template<typename T> class function;

template<typename R, typename... P> class function<R (P...)> {
  struct container {
    virtual R operator()(P... p) const = 0;
    virtual container* copy() const = 0;
    virtual ~container() {}
  };

  container* callback = nullptr;

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

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

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

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

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

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

}

#endif