ActiveDispatcher.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // ActiveDispatcher.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: ActiveObjects
  7. //
  8. // Definition of the ActiveDispatcher class.
  9. //
  10. // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_ActiveDispatcher_INCLUDED
  16. #define Foundation_ActiveDispatcher_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Runnable.h"
  19. #include "Poco/Thread.h"
  20. #include "Poco/ActiveStarter.h"
  21. #include "Poco/ActiveRunnable.h"
  22. #include "Poco/NotificationQueue.h"
  23. namespace Poco {
  24. class Foundation_API ActiveDispatcher: protected Runnable
  25. /// This class is used to implement an active object
  26. /// with strictly serialized method execution.
  27. ///
  28. /// An active object, which is an ordinary object
  29. /// containing ActiveMethod members, executes all
  30. /// active methods in their own thread.
  31. /// This behavior does not fit the "classic"
  32. /// definition of an active object, which serializes
  33. /// the execution of active methods (in other words,
  34. /// only one active method can be running at any given
  35. /// time).
  36. ///
  37. /// Using this class as a base class, the serializing
  38. /// behavior for active objects can be implemented.
  39. ///
  40. /// The following example shows how this is done:
  41. ///
  42. /// class ActiveObject: public ActiveDispatcher
  43. /// {
  44. /// public:
  45. /// ActiveObject():
  46. /// exampleActiveMethod(this, &ActiveObject::exampleActiveMethodImpl)
  47. /// {
  48. /// }
  49. ///
  50. /// ActiveMethod<std::string, std::string, ActiveObject, ActiveStarter<ActiveDispatcher> > exampleActiveMethod;
  51. ///
  52. /// protected:
  53. /// std::string exampleActiveMethodImpl(const std::string& arg)
  54. /// {
  55. /// ...
  56. /// }
  57. /// };
  58. ///
  59. /// The only things different from the example in
  60. /// ActiveMethod is that the ActiveObject in this case
  61. /// inherits from ActiveDispatcher, and that the ActiveMethod
  62. /// template for exampleActiveMethod has an additional parameter,
  63. /// specifying the specialized ActiveStarter for ActiveDispatcher.
  64. {
  65. public:
  66. ActiveDispatcher();
  67. /// Creates the ActiveDispatcher.
  68. ActiveDispatcher(Thread::Priority prio);
  69. /// Creates the ActiveDispatcher and sets
  70. /// the priority of its thread.
  71. virtual ~ActiveDispatcher();
  72. /// Destroys the ActiveDispatcher.
  73. void start(ActiveRunnableBase::Ptr pRunnable);
  74. /// Adds the Runnable to the dispatch queue.
  75. void cancel();
  76. /// Cancels all queued methods.
  77. protected:
  78. void run();
  79. void stop();
  80. private:
  81. Thread _thread;
  82. NotificationQueue _queue;
  83. };
  84. template <>
  85. class ActiveStarter<ActiveDispatcher>
  86. /// A specialization of ActiveStarter
  87. /// for ActiveDispatcher.
  88. {
  89. public:
  90. static void start(ActiveDispatcher* pOwner, ActiveRunnableBase::Ptr pRunnable)
  91. {
  92. pOwner->start(pRunnable);
  93. }
  94. };
  95. } // namespace Poco
  96. #endif // Foundation_ActiveDispatcher_INCLUDED