function.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <nall/traits.hpp>
  3. namespace nall {
  4. template<typename T> struct function;
  5. template<typename R, typename... P> struct function<auto (P...) -> R> {
  6. using cast = auto (*)(P...) -> R;
  7. //value = true if auto L::operator()(P...) -> R exists
  8. template<typename L> struct is_compatible {
  9. template<typename T> static auto exists(T*) -> const typename is_same<R, decltype(declval<T>().operator()(declval<P>()...))>::type;
  10. template<typename T> static auto exists(...) -> const false_type;
  11. static constexpr bool value = decltype(exists<L>(0))::value;
  12. };
  13. function() {}
  14. function(const function& source) { operator=(source); }
  15. function(auto (*function)(P...) -> R) { callback = new global(function); }
  16. template<typename C> function(auto (C::*function)(P...) -> R, C* object) { callback = new member<C>(function, object); }
  17. template<typename C> function(auto (C::*function)(P...) const -> R, C* object) { callback = new member<C>((auto (C::*)(P...) -> R)function, object); }
  18. template<typename L, typename = enable_if_t<is_compatible<L>::value>> function(const L& object) { callback = new lambda<L>(object); }
  19. explicit function(void* function) { if(function) callback = new global((auto (*)(P...) -> R)function); }
  20. ~function() { if(callback) delete callback; }
  21. explicit operator bool() const { return callback; }
  22. auto operator()(P... p) const -> R { return (*callback)(forward<P>(p)...); }
  23. auto reset() -> void { if(callback) { delete callback; callback = nullptr; } }
  24. auto operator=(const function& source) -> function& {
  25. if(this != &source) {
  26. if(callback) { delete callback; callback = nullptr; }
  27. if(source.callback) callback = source.callback->copy();
  28. }
  29. return *this;
  30. }
  31. auto operator=(void* source) -> function& {
  32. if(callback) { delete callback; callback = nullptr; }
  33. callback = new global((auto (*)(P...) -> R)source);
  34. return *this;
  35. }
  36. private:
  37. struct container {
  38. virtual auto operator()(P... p) const -> R = 0;
  39. virtual auto copy() const -> container* = 0;
  40. virtual ~container() = default;
  41. };
  42. container* callback = nullptr;
  43. struct global : container {
  44. auto (*function)(P...) -> R;
  45. auto operator()(P... p) const -> R { return function(forward<P>(p)...); }
  46. auto copy() const -> container* { return new global(function); }
  47. global(auto (*function)(P...) -> R) : function(function) {}
  48. };
  49. template<typename C> struct member : container {
  50. auto (C::*function)(P...) -> R;
  51. C* object;
  52. auto operator()(P... p) const -> R { return (object->*function)(forward<P>(p)...); }
  53. auto copy() const -> container* { return new member(function, object); }
  54. member(auto (C::*function)(P...) -> R, C* object) : function(function), object(object) {}
  55. };
  56. template<typename L> struct lambda : container {
  57. mutable L object;
  58. auto operator()(P... p) const -> R { return object(forward<P>(p)...); }
  59. auto copy() const -> container* { return new lambda(object); }
  60. lambda(const L& object) : object(object) {}
  61. };
  62. };
  63. }