ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

104 lines
1.9 KiB

  1. #ifndef CPPCMS_UTIL_CALLBACK2_H
  2. #define CPPCMS_UTIL_CALLBACK2_H
  3. #include "clone_ptr.h"
  4. namespace cppcms { namespace util {
  5. ///
  6. /// \brief Function object, similar to C++0x std::function<void(P1,P2)>, or boost::function<void(P1,P2)>
  7. ///
  8. /// Callback object, it can be created with any "function like object" -- a class with operator()(P1,P2) or C function
  9. /// with appropriate signature.
  10. ///
  11. template<typename P1,typename P2>
  12. class callback2 {
  13. struct callable {
  14. virtual void call(P1,P2) = 0;
  15. virtual callable *clone() const = 0;
  16. virtual ~callable() {};
  17. };
  18. template<typename T>
  19. struct callable_functor : public callable{
  20. T func;
  21. callable_functor(T f) : func(f) {}
  22. virtual ~callable_functor() { }
  23. virtual void call(P1 p1,P2 p2) { func(p1,p2); }
  24. virtual callable *clone() const { return new callable_functor<T>(func); }
  25. };
  26. clone_ptr<callable> call_ptr;
  27. public:
  28. typedef void result_type;
  29. typedef P1 first_argument_type;
  30. typedef P2 second_argument_type;
  31. ///
  32. /// Call the assigned function, does nothing if function was not assigned
  33. ///
  34. void operator()(P1 p1,P2 p2) const
  35. {
  36. if(call_ptr.get()) {
  37. call_ptr->call(p1,p2);
  38. }
  39. }
  40. ///
  41. /// Create an empty callback
  42. ///
  43. callback2(){}
  44. ///
  45. /// Copy constructor
  46. ///
  47. callback2(callback2 const &other) : call_ptr(other.call_ptr)
  48. {
  49. }
  50. ///
  51. /// Assignment operator
  52. ///
  53. callback2 const &operator=(callback2 const &other)
  54. {
  55. if(this!=&other)
  56. call_ptr = other.call_ptr;
  57. return *this;
  58. }
  59. ///
  60. /// Create a callback and copy callable object T to it.
  61. ///
  62. template<typename T>
  63. callback2(T c) : call_ptr(new callable_functor<T>(c))
  64. {
  65. }
  66. ///
  67. /// Assign a callable object to it
  68. ///
  69. template<typename T>
  70. callback2 const &operator=(T c)
  71. {
  72. call_ptr.reset(new callable_functor<T>(c));
  73. return *this;
  74. }
  75. ///
  76. /// Swap two callbacks
  77. ///
  78. void swap(callback2 &other)
  79. {
  80. call_ptr.swap(other.call_ptr);
  81. }
  82. };
  83. }} // cppcms util
  84. #endif