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
16
17#ifndef Foundation_ActiveDispatcher_INCLUDED
18#define Foundation_ActiveDispatcher_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/Runnable.h"
23#include "Poco/Thread.h"
24#include "Poco/ActiveStarter.h"
25#include "Poco/ActiveRunnable.h"
26#include "Poco/NotificationQueue.h"
27
28
29namespace Poco {
30
31
32class Foundation_API ActiveDispatcher: protected Runnable
33 /// This class is used to implement an active object
34 /// with strictly serialized method execution.
35 ///
36 /// An active object, which is an ordinary object
37 /// containing ActiveMethod members, executes all
38 /// active methods in their own thread.
39 /// This behavior does not fit the "classic"
40 /// definition of an active object, which serializes
41 /// the execution of active methods (in other words,
42 /// only one active method can be running at any given
43 /// time).
44 ///
45 /// Using this class as a base class, the serializing
46 /// behavior for active objects can be implemented.
47 ///
48 /// The following example shows how this is done:
49 ///
50 /// class ActiveObject: public ActiveDispatcher
51 /// {
52 /// public:
53 /// ActiveObject():
54 /// exampleActiveMethod(this, &ActiveObject::exampleActiveMethodImpl)
55 /// {
56 /// }
57 ///
58 /// ActiveMethod<std::string, std::string, ActiveObject, ActiveStarter<ActiveDispatcher> > exampleActiveMethod;
59 ///
60 /// protected:
61 /// std::string exampleActiveMethodImpl(const std::string& arg)
62 /// {
63 /// ...
64 /// }
65 /// };
66 ///
67 /// The only things different from the example in
68 /// ActiveMethod is that the ActiveObject in this case
69 /// inherits from ActiveDispatcher, and that the ActiveMethod
70 /// template for exampleActiveMethod has an additional parameter,
71 /// specifying the specialized ActiveStarter for ActiveDispatcher.
72{
73public:
74 ActiveDispatcher();
75 /// Creates the ActiveDispatcher.
76
77 ActiveDispatcher(Thread::Priority prio);
78 /// Creates the ActiveDispatcher and sets
79 /// the priority of its thread.
80
81 virtual ~ActiveDispatcher();
82 /// Destroys the ActiveDispatcher.
83
84 void start(ActiveRunnableBase::Ptr pRunnable);
85 /// Adds the Runnable to the dispatch queue.
86
87 void cancel();
88 /// Cancels all queued methods.
89
90protected:
91 void run();
92 void stop();
93
94private:
95 Thread _thread;
96 NotificationQueue _queue;
97};
98
99
100template <>
101class ActiveStarter<ActiveDispatcher>
102 /// A specialization of ActiveStarter
103 /// for ActiveDispatcher.
104{
105public:
106 static void start(ActiveDispatcher* pOwner, ActiveRunnableBase::Ptr pRunnable)
107 {
108 pOwner->start(pRunnable);
109 }
110};
111
112
113} // namespace Poco
114
115
116#endif // Foundation_ActiveDispatcher_INCLUDED
117