LCOV - code coverage report
Current view: top level - include/comphelper - asyncnotification.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 7 7 100.0 %
Date: 2015-06-13 12:38:46 Functions: 6 10 60.0 %
Legend: Lines: hit not hit

          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 <comphelper/comphelperdllapi.h>
      26             : #include <rtl/ref.hxx>
      27             : #include <sal/types.h>
      28             : #include <salhelper/thread.hxx>
      29             : #include <salhelper/simplereferenceobject.hxx>
      30             : #include <memory>
      31             : 
      32             : namespace comphelper
      33             : {
      34             :     //= AnyEvent
      35             : 
      36             :     /** the very basic instance to hold a description of an event
      37             :     */
      38             :     class COMPHELPER_DLLPUBLIC AnyEvent : public salhelper::SimpleReferenceObject
      39             :     {
      40             :     public:
      41             :         AnyEvent();
      42             : 
      43             :     protected:
      44             :         virtual ~AnyEvent();
      45             : 
      46             :     private:
      47             :         AnyEvent( AnyEvent& ) SAL_DELETED_FUNCTION;
      48             :         void operator=( AnyEvent& ) SAL_DELETED_FUNCTION;
      49             :     };
      50             : 
      51             : 
      52             :     //= typedefs
      53             : 
      54             :     typedef ::rtl::Reference< AnyEvent >    AnyEventRef;
      55             : 
      56             : 
      57             :     //= IEventProcessor
      58             : 
      59             :     /** an event processor
      60             : 
      61             :         @see AsyncEventNotifier
      62             :     */
      63         143 :     class SAL_NO_VTABLE IEventProcessor
      64             :     {
      65             :     public:
      66             :         /** process a single event
      67             :         */
      68             :         virtual void processEvent( const AnyEvent& _rEvent ) = 0;
      69             : 
      70             :         virtual void SAL_CALL acquire() throw () = 0;
      71             :         virtual void SAL_CALL release() throw () = 0;
      72             : 
      73             :     protected:
      74         123 :         ~IEventProcessor() {}
      75             :     };
      76             : 
      77             : 
      78             :     //= AsyncEventNotifier
      79             : 
      80             :     struct EventNotifierImpl;
      81             : 
      82             :     /** a helper class for notifying events asynchronously
      83             : 
      84             :         If you need to notify certain events to external components, you usually should
      85             :         not do this while you have mutexes locked, to prevent multi-threading issues.
      86             : 
      87             :         However, you do not always have complete control over all mutex guards on the stack.
      88             :         If, in such a case, the listener notification is one-way, you can decide to do it
      89             :         asynchronously.
      90             : 
      91             :         The ->AsyncEventNotifier helps you to process such events asynchronously. Every
      92             :         event is tied to an ->IEventProcessor which is responsible for processing it.
      93             : 
      94             :         The AsyncEventNotifier is implemented as a thread itself, which sleeps as long as there are no
      95             :         events in the queue. As soon as you add an event, the thread is woken up, processes the event,
      96             :         and sleeps again.
      97             :     */
      98             :     class COMPHELPER_DLLPUBLIC AsyncEventNotifier: public salhelper::Thread
      99             :     {
     100             :         friend struct EventNotifierImpl;
     101             : 
     102             :     private:
     103             :         std::unique_ptr<EventNotifierImpl>        m_xImpl;
     104             : 
     105             :         SAL_DLLPRIVATE virtual ~AsyncEventNotifier();
     106             : 
     107             :         // Thread
     108             :         SAL_DLLPRIVATE virtual void execute() SAL_OVERRIDE;
     109             : 
     110             :     public:
     111             :         /** constructs a notifier thread
     112             : 
     113             :             @param name the thread name, see ::osl_setThreadName; must not be
     114             :             null
     115             :         */
     116             :         AsyncEventNotifier(char const * name);
     117             : 
     118             :         /** terminates the thread
     119             : 
     120             :             Note that this is a cooporative termination - if you call this from a thread different
     121             :             from the notification thread itself, then it will block until the notification thread
     122             :             finished processing the current event. If you call it from the notification thread
     123             :             itself, it will return immediately, and the thread will be terminated as soon as
     124             :             the current notification is finished.
     125             :         */
     126             :         virtual void SAL_CALL terminate() SAL_OVERRIDE;
     127             : 
     128             :         /** adds an event to the queue, together with the instance which is responsible for
     129             :             processing it
     130             : 
     131             :             @param _rEvent
     132             :                 the event to add to the queue
     133             :             @param _xProcessor
     134             :                 the processor for the event.<br/>
     135             :                 Beware of life time issues here. If your event processor dies or becomes otherwise
     136             :                 nonfunctional, you are responsible for removing all respective events from the queue.
     137             :                 You can do this by calling ->removeEventsForProcessor
     138             :         */
     139             :         void addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor );
     140             : 
     141             :         /** removes all events for the given event processor from the queue
     142             :         */
     143             :         void removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor );
     144             :     };
     145             : 
     146             : 
     147             :     //= EventHolder
     148             : 
     149             :     /** AnyEvent derivee holding an foreign event instance
     150             :     */
     151             :     template < typename EVENT_OBJECT >
     152         438 :     class EventHolder : public AnyEvent
     153             :     {
     154             :     public:
     155             :         typedef EVENT_OBJECT    EventObjectType;
     156             : 
     157             :     private:
     158             :         EventObjectType m_aEvent;
     159             : 
     160             :     public:
     161         220 :         inline EventHolder( const EventObjectType& _rEvent )
     162         220 :             :m_aEvent( _rEvent )
     163             :         {
     164         220 :         }
     165             : 
     166         220 :         inline const EventObjectType& getEventObject() const { return m_aEvent; }
     167             :     };
     168             : 
     169             : 
     170             : } // namespace comphelper
     171             : 
     172             : 
     173             : #endif // INCLUDED_COMPHELPER_ASYNCNOTIFICATION_HXX
     174             : 
     175             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11