Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include "functorwrapper.hpp"
  2.  
  3. // primary template (declaration)
  4. template <typename Signature>
  5. class Function;
  6.  
  7. // partial class template specialization
  8. template <typename ReturnType, typename... Args>
  9. class Function<ReturnType(Args...)>
  10. {
  11. public:
  12. // constructors
  13. Function() : mFunctorWrapper(nullptr) {} // default constructor
  14. Function(const Function &); // copy constructor
  15. Function(Function &&); // move constructor
  16. template <typename Functor> Function(Functor &&); // generalized constructor
  17.  
  18. // destructor
  19. ~Function() { delete mFunctorWrapper; }
  20.  
  21. // copy/move operations
  22. Function &operator=(Function const &); // copy assignmaent operator
  23. Function &operator=(Function &&); // move assignment operator
  24. template <typename Functor> Function &operator=(Functor &&); // generalized assignment operator
  25.  
  26. // overloaded function call operator
  27. ReturnType operator()(Args...);
  28. private:
  29. FunctorWrapperBase<ReturnType(Args...)> *mFunctorWrapper;
  30. };
  31.  
  32. template <typename ReturnType, typename... Args>
  33. Function<ReturnType(Args...)>::Function(const Function &other) : mFunctorWrapper(nullptr)
  34. {
  35. if (other.mFunctorWrapper)
  36. mFunctorWrapper = other.mFunctorWrapper->Clone();
  37. }
  38.  
  39. template <typename ReturnType, typename... Args>
  40. Function<ReturnType(Args...)>::Function(Function &&other) : mFunctorWrapper(other.mFunctorWrapper)
  41. {
  42. other.mFunctorWrapper = nullptr;
  43. }
  44.  
  45. template <typename ReturnType, typename... Args>
  46. template <typename Functor>
  47. Function<ReturnType(Args...)>::Function(Functor &&functor)
  48. {
  49. // remove reference if l-value (template type argument deduced as Functor &)
  50. mFunctorWrapper = new FunctorWrapper<typename std::remove_reference<Functor>::type, ReturnType(Args...)>(std::forward<Functor>(functor));
  51. }
  52.  
  53. template <typename ReturnType, typename... Args>
  54. Function<ReturnType(Args...)> &Function<ReturnType(Args...)>::operator=(const Function &other)
  55. {
  56. mFunctorWrapper = other.mFunctorWrapper->Clone();
  57.  
  58. return *this;
  59. }
  60.  
  61. template <typename ReturnType, typename... Args>
  62. Function<ReturnType(Args...)> &Function<ReturnType(Args...)>::operator=(Function &&other)
  63. {
  64. mFunctorWrapper = other.mFunctorWrapper;
  65. other.mFunctorWrapper = nullptr;
  66.  
  67. return *this;
  68. }
  69.  
  70. template <typename ReturnType, typename... Args>
  71. template <typename Functor>
  72. Function<ReturnType(Args...)> &Function<ReturnType(Args...)>::operator=(Functor &&functor)
  73. {
  74. mFunctorWrapper = new FunctorWrapper<typename std::remove_reference<Functor>::type, ReturnType(Args...)>(std::forward<Functor>(functor));
  75. }
  76.  
  77. template <typename ReturnType, typename... Args>
  78. ReturnType Function<ReturnType(Args...)>::operator()(Args... args)
  79. {
  80. mFunctorWrapper->Invoke(args...);
  81. }
  82.  
  83. Function<void(double)> fp4(&FreeFunction);
  84. fp4(1.2);
  85.  
  86. Function<void(double)> fp5 = fp4; // copy construction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement