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 : #ifndef INCLUDED_CHART2_SOURCE_INC_WEAKLISTENERADAPTER_HXX
20 : #define INCLUDED_CHART2_SOURCE_INC_WEAKLISTENERADAPTER_HXX
21 :
22 : #include <com/sun/star/uno/XWeak.hpp>
23 : #include <com/sun/star/lang/XEventListener.hpp>
24 : #include <com/sun/star/util/XModifyListener.hpp>
25 : #include <com/sun/star/view/XSelectionChangeListener.hpp>
26 : #include <cppuhelper/weakref.hxx>
27 : #include <cppuhelper/implbase1.hxx>
28 :
29 : namespace chart
30 : {
31 :
32 : /** Adapter that enables adding listeners as weak UNO references. Thus, adding
33 : an object as listener to a broadcaster does not increase its reference
34 : count.
35 :
36 : <p>The helper class, of course, is held as hard reference at the
37 : broadcaster, but this should never be a problem as the adapter's life time
38 : depends on no other object.</p>
39 :
40 : <p>Note that in order to remove an object as listener, you have to remove
41 : the same wrapper that you added, i.e., you should store the adapter as a
42 : member in the adaptee class for later use.</p>
43 : */
44 : template< class Listener >
45 : class WeakListenerAdapter : public
46 : ::cppu::WeakImplHelper1< Listener >
47 : {
48 : public:
49 0 : explicit WeakListenerAdapter( const ::com::sun::star::uno::Reference< Listener > & xListener ) :
50 0 : m_xListener( xListener )
51 0 : {}
52 0 : explicit WeakListenerAdapter( const ::com::sun::star::uno::WeakReference< Listener > & xListener ) :
53 0 : m_xListener( xListener )
54 0 : {}
55 0 : virtual ~WeakListenerAdapter()
56 0 : {}
57 :
58 : protected:
59 : // ____ XEventListener (base of all listeners) ____
60 0 : virtual void SAL_CALL disposing(
61 : const ::com::sun::star::lang::EventObject& Source )
62 : throw (::com::sun::star::uno::RuntimeException)
63 : {
64 : ::com::sun::star::uno::Reference<
65 : ::com::sun::star::lang::XEventListener > xEventListener =
66 : ::com::sun::star::uno::Reference<
67 : ::com::sun::star::lang::XEventListener >(
68 0 : ::com::sun::star::uno::Reference< Listener >( m_xListener), ::com::sun::star::uno::UNO_QUERY );
69 0 : if( xEventListener.is())
70 0 : xEventListener->disposing( Source );
71 0 : }
72 :
73 0 : ::com::sun::star::uno::Reference< Listener > getListener() const
74 : {
75 0 : return m_xListener;
76 : }
77 :
78 : private:
79 : ::com::sun::star::uno::WeakReference< Listener > m_xListener;
80 : };
81 :
82 : class WeakModifyListenerAdapter :
83 : public WeakListenerAdapter< ::com::sun::star::util::XModifyListener >
84 : {
85 : public:
86 : explicit WeakModifyListenerAdapter(
87 : const ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener > & xListener );
88 : virtual ~WeakModifyListenerAdapter();
89 :
90 : protected:
91 : // ____ XModifyListener ____
92 : virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent )
93 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
94 : };
95 :
96 : class WeakSelectionChangeListenerAdapter :
97 : public WeakListenerAdapter< ::com::sun::star::view::XSelectionChangeListener >
98 : {
99 : public:
100 : explicit WeakSelectionChangeListenerAdapter(
101 : const ::com::sun::star::uno::Reference< ::com::sun::star::view::XSelectionChangeListener > & xListener );
102 : virtual ~WeakSelectionChangeListenerAdapter();
103 :
104 : protected:
105 : // ____ XSelectionChangeListener ____
106 : virtual void SAL_CALL selectionChanged(
107 : const ::com::sun::star::lang::EventObject& aEvent )
108 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
109 : };
110 :
111 : } // namespace chart
112 :
113 : // INCLUDED_CHART2_SOURCE_INC_WEAKLISTENERADAPTER_HXX
114 : #endif
115 :
116 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|