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.
 
 
 
 
 
 

102 lines
1.9 KiB

  1. #ifndef CPPCMS_UTIL_CALLBACK4_H
  2. #define CPPCMS_UTIL_CALLBACK4_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,P3,P4)>, or boost::function<void(P1,P2,P3,P4)>
  7. ///
  8. /// Callback object, it can be created with any "function like object" -- a class with operator()(P1,P2,P3,P4) or C function
  9. /// with appropriate signature.
  10. ///
  11. template<typename P1,typename P2,typename P3,typename P4>
  12. class callback4 {
  13. struct callable {
  14. virtual void call(P1,P2,P3,P4) = 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,P3 p3,P4 p4) { func(p1,p2,p3,p4); }
  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. ///
  30. /// Call the assigned function, does nothing if function was not assigned
  31. ///
  32. void operator()(P1 p1,P2 p2,P3 p3,P4 p4) const
  33. {
  34. if(call_ptr.get()) {
  35. call_ptr->call(p1,p2,p3,p4);
  36. }
  37. }
  38. ///
  39. /// Create an empty callback
  40. ///
  41. callback4(){}
  42. ///
  43. /// Copy constructor
  44. ///
  45. callback4(callback4 const &other) : call_ptr(other.call_ptr)
  46. {
  47. }
  48. ///
  49. /// Assignment operator
  50. ///
  51. callback4 const &operator=(callback4 const &other)
  52. {
  53. if(this!=&other)
  54. call_ptr = other.call_ptr;
  55. return *this;
  56. }
  57. ///
  58. /// Create a callback and copy callable object T to it.
  59. ///
  60. template<typename T>
  61. callback4(T c) : call_ptr(new callable_functor<T>(c))
  62. {
  63. }
  64. ///
  65. /// Assign a callable object to it
  66. ///
  67. template<typename T>
  68. callback4 const &operator=(T c)
  69. {
  70. call_ptr.reset(new callable_functor<T>(c));
  71. return *this;
  72. }
  73. ///
  74. /// Swap two callbacks
  75. ///
  76. void swap(callback4 &other)
  77. {
  78. call_ptr.swap(other.call_ptr);
  79. }
  80. };
  81. }} // cppcms util
  82. #endif