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