Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_COMPHELPER_ASYNCNOTIFICATION_HXX
21 : #define INCLUDED_COMPHELPER_ASYNCNOTIFICATION_HXX
22 :
23 : #include <sal/config.h>
24 :
25 : #include <boost/scoped_ptr.hpp>
26 : #include <comphelper/comphelperdllapi.h>
27 : #include <rtl/ref.hxx>
28 : #include <sal/types.h>
29 : #include <salhelper/thread.hxx>
30 : #include <salhelper/simplereferenceobject.hxx>
31 :
32 :
33 : namespace comphelper
34 : {
35 :
36 :
37 :
38 : //= AnyEvent
39 :
40 : /** the very basic instance to hold a description of an event
41 : */
42 : class COMPHELPER_DLLPUBLIC AnyEvent : public salhelper::SimpleReferenceObject
43 : {
44 : public:
45 : AnyEvent();
46 :
47 : protected:
48 : virtual ~AnyEvent();
49 :
50 : private:
51 : AnyEvent( AnyEvent& ); // not defined
52 : void operator=( AnyEvent& ); // not defined
53 : };
54 :
55 :
56 : //= typedefs
57 :
58 : typedef ::rtl::Reference< AnyEvent > AnyEventRef;
59 :
60 :
61 : //= IEventProcessor
62 :
63 : /** an event processor
64 :
65 : @see AsyncEventNotifier
66 : */
67 246 : class SAL_NO_VTABLE IEventProcessor
68 : {
69 : public:
70 : /** process a single event
71 : */
72 : virtual void processEvent( const AnyEvent& _rEvent ) = 0;
73 :
74 : virtual void SAL_CALL acquire() throw () = 0;
75 : virtual void SAL_CALL release() throw () = 0;
76 :
77 : protected:
78 208 : ~IEventProcessor() {}
79 : };
80 :
81 :
82 : //= AsyncEventNotifier
83 :
84 : struct EventNotifierImpl;
85 :
86 : /** a helper class for notifying events asynchronously
87 :
88 : If you need to notify certain events to external components, you usually should
89 : not do this while you have mutexes locked, to prevent multi-threading issues.
90 :
91 : However, you do not always have complete control over all mutex guards on the stack.
92 : If, in such a case, the listener notification is one-way, you can decide to do it
93 : asynchronously.
94 :
95 : The ->AsyncEventNotifier helps you to process such events asynchronously. Every
96 : event is tied to an ->IEventProcessor which is responsible for processing it.
97 :
98 : The AsyncEventNotifier is implemented as a thread itself, which sleeps as long as there are no
99 : events in the queue. As soon as you add an event, the thread is woken up, processes the event,
100 : and sleeps again.
101 : */
102 : class COMPHELPER_DLLPUBLIC AsyncEventNotifier: public salhelper::Thread
103 : {
104 : friend struct EventNotifierImpl;
105 :
106 : private:
107 : boost::scoped_ptr< EventNotifierImpl > m_pImpl;
108 :
109 : SAL_DLLPRIVATE virtual ~AsyncEventNotifier();
110 :
111 : // Thread
112 : SAL_DLLPRIVATE virtual void execute() SAL_OVERRIDE;
113 :
114 : public:
115 : /** constructs a notifier thread
116 :
117 : @param name the thread name, see ::osl_setThreadName; must not be
118 : null
119 : */
120 : AsyncEventNotifier(char const * name);
121 :
122 : /** terminates the thread
123 :
124 : Note that this is a cooporative termination - if you call this from a thread different
125 : from the notification thread itself, then it will block until the notification thread
126 : finished processing the current event. If you call it from the notification thread
127 : itself, it will return immediately, and the thread will be terminated as soon as
128 : the current notification is finished.
129 : */
130 : virtual void SAL_CALL terminate() SAL_OVERRIDE;
131 :
132 : /** adds an event to the queue, together with the instance which is responsible for
133 : processing it
134 :
135 : @param _rEvent
136 : the event to add to the queue
137 : @param _xProcessor
138 : the processor for the event.<br/>
139 : Beware of life time issues here. If your event processor dies or becomes otherwise
140 : nonfunctional, you are responsible for removing all respective events from the queue.
141 : You can do this by calling ->removeEventsForProcessor
142 : */
143 : void addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor );
144 :
145 : /** removes all events for the given event processor from the queue
146 : */
147 : void removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor );
148 : };
149 :
150 :
151 : //= EventHolder
152 :
153 : /** AnyEvent derivee holding an foreign event instance
154 : */
155 : template < typename EVENT_OBJECT >
156 852 : class EventHolder : public AnyEvent
157 : {
158 : public:
159 : typedef EVENT_OBJECT EventObjectType;
160 :
161 : private:
162 : EventObjectType m_aEvent;
163 :
164 : public:
165 428 : inline EventHolder( const EventObjectType& _rEvent )
166 428 : :m_aEvent( _rEvent )
167 : {
168 428 : }
169 :
170 428 : inline const EventObjectType& getEventObject() const { return m_aEvent; }
171 : };
172 :
173 :
174 : } // namespace comphelper
175 :
176 :
177 : #endif // INCLUDED_COMPHELPER_ASYNCNOTIFICATION_HXX
178 :
179 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|