OptionCallback.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // OptionCallback.h
  3. //
  4. // Library: Util
  5. // Package: Options
  6. // Module: OptionCallback
  7. //
  8. // Definition of the OptionCallback class.
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_OptionCallback_INCLUDED
  16. #define Util_OptionCallback_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. namespace Poco {
  19. namespace Util {
  20. class Util_API AbstractOptionCallback
  21. /// Base class for OptionCallback.
  22. {
  23. public:
  24. virtual void invoke(const std::string& name, const std::string& value) const = 0;
  25. /// Invokes the callback member function.
  26. virtual AbstractOptionCallback* clone() const = 0;
  27. /// Creates and returns a copy of the object.
  28. virtual ~AbstractOptionCallback();
  29. /// Destroys the AbstractOptionCallback.
  30. protected:
  31. AbstractOptionCallback();
  32. AbstractOptionCallback(const AbstractOptionCallback&);
  33. };
  34. template <class C>
  35. class OptionCallback: public AbstractOptionCallback
  36. /// This class is used as an argument to Option::callback().
  37. ///
  38. /// It stores a pointer to an object and a pointer to a member
  39. /// function of the object's class.
  40. {
  41. public:
  42. typedef void (C::*Callback)(const std::string& name, const std::string& value);
  43. OptionCallback(C* pObject, Callback method):
  44. _pObject(pObject),
  45. _method(method)
  46. /// Creates the OptionCallback for the given object and member function.
  47. {
  48. poco_check_ptr (pObject);
  49. }
  50. OptionCallback(const OptionCallback& cb):
  51. AbstractOptionCallback(cb),
  52. _pObject(cb._pObject),
  53. _method(cb._method)
  54. /// Creates an OptionCallback from another one.
  55. {
  56. }
  57. ~OptionCallback()
  58. /// Destroys the OptionCallback.
  59. {
  60. }
  61. OptionCallback& operator = (const OptionCallback& cb)
  62. {
  63. if (&cb != this)
  64. {
  65. this->_pObject = cb._pObject;
  66. this->_method = cb._method;
  67. }
  68. return *this;
  69. }
  70. void invoke(const std::string& name, const std::string& value) const
  71. {
  72. (_pObject->*_method)(name, value);
  73. }
  74. AbstractOptionCallback* clone() const
  75. {
  76. return new OptionCallback(_pObject, _method);
  77. }
  78. private:
  79. OptionCallback();
  80. C* _pObject;
  81. Callback _method;
  82. };
  83. } } // namespace Poco::Util
  84. #endif // Util_OptionCallback_INCLUDED