fork(9) download
  1. namespace bicycle
  2. {
  3. template<typename Result,typename ...Args>
  4. struct abstract_function
  5. {
  6. virtual Result operator()(Args... args)=0;
  7. virtual abstract_function *clone() const =0;
  8. virtual ~abstract_function() = default;
  9. };
  10.  
  11. template<typename Func,typename Result,typename ...Args>
  12. class concrete_function: public abstract_function<Result,Args...>
  13. {
  14. Func f;
  15. public:
  16. concrete_function(const Func &x)
  17. : f(x)
  18. {}
  19. Result operator()(Args... args) override
  20. {
  21. return f(args...);
  22. }
  23. concrete_function *clone() const override
  24. {
  25. return new concrete_function{f};
  26. }
  27. };
  28.  
  29. template<typename Func>
  30. struct func_filter
  31. {
  32. typedef Func type;
  33. };
  34. template<typename Result,typename ...Args>
  35. struct func_filter<Result(Args...)>
  36. {
  37. typedef Result (*type)(Args...);
  38. };
  39.  
  40. template<typename signature>
  41. class function;
  42.  
  43. template<typename Result,typename ...Args>
  44. class function<Result(Args...)>
  45. {
  46. abstract_function<Result,Args...> *f;
  47. public:
  48. function()
  49. : f(nullptr)
  50. {}
  51. template<typename Func> function(const Func &x)
  52. : f(new concrete_function<typename func_filter<Func>::type,Result,Args...>(x))
  53. {}
  54. function(const function &rhs)
  55. : f(rhs.f ? rhs.f->clone() : nullptr)
  56. {}
  57. function &operator=(const function &rhs)
  58. {
  59. if( (&rhs != this ) && (rhs.f) )
  60. {
  61. auto *temp = rhs.f->clone();
  62. delete f;
  63. f = temp;
  64. }
  65. return *this;
  66. }
  67. template<typename Func> function &operator=(const Func &x)
  68. {
  69. auto *temp = new concrete_function<typename func_filter<Func>::type,Result,Args...>(x);
  70. delete f;
  71. f = temp;
  72. return *this;
  73. }
  74. Result operator()(Args... args)
  75. {
  76. if(f)
  77. return (*f)(args...);
  78. else
  79. return Result{};
  80. }
  81. ~function()
  82. {
  83. delete f;
  84. }
  85. };
  86. }
  87.  
  88. // ___________________[ Example of usage ]___________________ //
  89.  
  90. int func1(double)
  91. {
  92. return 1;
  93. }
  94. struct Functor2
  95. {
  96. int operator()(double)
  97. {
  98. return 2;
  99. }
  100. };
  101.  
  102. double func3(bool,int)
  103. {
  104. return 3.0;
  105. }
  106. struct Functor4
  107. {
  108. double operator()(bool,int)
  109. {
  110. return 4.0;
  111. }
  112. };
  113.  
  114. int main()
  115. {
  116. int res = 10;
  117. {
  118. bicycle::function<int(double)> f{func1};
  119.  
  120. res -= f(1.0);
  121. f = Functor2{};
  122. res -= f(2.0);
  123. }
  124. {
  125. bicycle::function<double(bool,int)> f1;
  126. f1 = func3;
  127.  
  128. bicycle::function<double(bool,int)> f2{f1};
  129. res -= f2(true,1);
  130.  
  131. f1 = Functor4{};
  132. f2 = f1;
  133. res -= f2(false,2);
  134. }
  135. return res;
  136. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
Standard output is empty