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.7 KiB

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