1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef COMPHELPER_PROXY_AGGREGATION
25 #define COMPHELPER_PROXY_AGGREGATION
26 
27 #include <com/sun/star/uno/XAggregation.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <cppuhelper/implbase1.hxx>
31 #include <cppuhelper/interfacecontainer.hxx>
32 #include <comphelper/uno3.hxx>
33 #include <comphelper/broadcasthelper.hxx>
34 #include <cppuhelper/compbase_ex.hxx>
35 #include "comphelper/comphelperdllapi.h"
36 
37 /* class hierarchy herein:
38 
39          +-------------------+          helper class for aggregating the proxy to another object
40          | OProxyAggregation |          - not ref counted
41          +-------------------+          - no UNO implementation, i.e. not derived from XInterface
42                    ^                      (neither direct nor indirect)
43                    |
44 			       |
45   +----------------------------------+  helper class for aggregating a proxy to an XComponent
46   | OComponentProxyAggregationHelper |  - life time coupling: if the inner component (the "aggregate")
47   +----------------------------------+    is disposed, the outer (the delegator) is disposed, too, and
48                    ^                      vice versa
49 				   |					- UNO based, implementing XEventListener
50 				   |
51      +----------------------------+     component aggregating another XComponent
52      | OComponentProxyAggregation |     - life time coupling as above
53      +----------------------------+     - ref-counted
54 	                                    - implements an XComponent itself
55 
56   If you need to
57 
58   - wrap a foreign object which is a XComponent
59     => use OComponentProxyAggregation
60 	   - call componentAggregateProxyFor in your ctor
61 	   - call implEnsureDisposeInDtor in your dtor
62 
63   - wrap a foreign object which is a XComponent, but already have ref-counting mechanisms
64     inherited from somewhere else
65 	=> use OComponentProxyAggregationHelper
66 	   - override dispose - don't forget to call the base class' dispose!
67 	   - call componentAggregateProxyFor in your ctor
68 
69   - wrap a foreign object which is no XComponent
70     => use OProxyAggregation
71 	   - call baseAggregateProxyFor in your ctor
72 */
73 
74 //.............................................................................
75 namespace comphelper
76 {
77 //.............................................................................
78 
79 	//=========================================================================
80 	//= OProxyAggregation
81 	//=========================================================================
82 	/** helper class for aggregating a proxy for a foreign object
83 	*/
84 	class OProxyAggregation
85 	{
86 	private:
87 		::com::sun::star::uno::Reference< ::com::sun::star::uno::XAggregation >				m_xProxyAggregate;
88 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XTypeProvider >			m_xProxyTypeAccess;
89 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >	m_xORB;
90 
91 	protected:
getORB()92 		inline const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& getORB()
93 		{
94 			return m_xORB;
95 		}
96 
97 	protected:
98 		OProxyAggregation( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB );
99 		~OProxyAggregation();
100 
101 		/// to be called from within your ctor
102 		void baseAggregateProxyFor(
103 			const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent,
104 			oslInterlockedCount& _rRefCount,
105 			::cppu::OWeakObject& _rDelegator
106 		);
107 
108 		// XInterface and XTypeProvider
109 		::com::sun::star::uno::Any SAL_CALL queryAggregation( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException);
110 		::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw (::com::sun::star::uno::RuntimeException);
111 
112 	private:
113 		OProxyAggregation( );										// never implemented
114 		OProxyAggregation( const OProxyAggregation& );				// never implemented
115 		OProxyAggregation& operator=( const OProxyAggregation& );	// never implemented
116 	};
117 
118 	//=========================================================================
119 	//= OComponentProxyAggregationHelper
120 	//=========================================================================
121 	/** a helper class for aggregating a proxy to an XComponent
122 
123 		<p>The object couples the life time of itself and the component: if one of the both
124 		dies (in a sense of being disposed), the other one dies, too.</p>
125 
126 		<p>The class itself does not implement XComponent so you need to forward any XComponent::dispose
127 		calls which your derived class gets to the dispose method of this class.</p>
128 	*/
129 
130 	class COMPHELPER_DLLPUBLIC OComponentProxyAggregationHelper	:public ::cppu::ImplHelper1	<	com::sun::star::lang::XEventListener
131 																		>
132 											,private OProxyAggregation
133 	{
134 	private:
135 		typedef	::cppu::ImplHelper1	<	::com::sun::star::lang::XEventListener
136 									>	BASE;	// prevents some MSVC problems
137 
138 	protected:
139 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
140 											m_xInner;
141 		::cppu::OBroadcastHelper&			m_rBHelper;
142 
143 	protected:
144 		// OProxyAggregation
145 		using OProxyAggregation::getORB;
146 
147 		// XInterface
148 		::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException);
149 
150 		// XTypeProvider
151 		DECLARE_XTYPEPROVIDER( )
152 
153 	protected:
154 		OComponentProxyAggregationHelper(
155 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
156 			::cppu::OBroadcastHelper& _rBHelper
157 		);
158 		virtual ~OComponentProxyAggregationHelper( );
159 
160 		/// to be called from within your ctor
161 		void componentAggregateProxyFor(
162 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComponent,
163 			oslInterlockedCount& _rRefCount,
164 			::cppu::OWeakObject& _rDelegator
165 		);
166 
167 	    // XEventListener
168 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
169 
170 		// XComponent
171 		virtual void SAL_CALL dispose() throw( ::com::sun::star::uno::RuntimeException );
172 
173 	private:
174 		COMPHELPER_DLLPRIVATE OComponentProxyAggregationHelper( );													// never implemented
175 		COMPHELPER_DLLPRIVATE OComponentProxyAggregationHelper( const OComponentProxyAggregationHelper& );			// never implemented
176 		COMPHELPER_DLLPRIVATE OComponentProxyAggregationHelper& operator=( const OComponentProxyAggregationHelper& );	// never implemented
177 	};
178 
179 	//=========================================================================
180 	//= OComponentProxyAggregation
181 	//=========================================================================
182 	typedef ::cppu::WeakComponentImplHelperBase	OComponentProxyAggregation_CBase;
183 
184 	class COMPHELPER_DLLPUBLIC OComponentProxyAggregation	:public OBaseMutex
185 										,public OComponentProxyAggregation_CBase
186 										,public OComponentProxyAggregationHelper
187 	{
188 	protected:
189 		OComponentProxyAggregation(
190 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
191 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComponent
192 		);
193 
194 		virtual ~OComponentProxyAggregation();
195 
196 		// XInterface
197 		DECLARE_XINTERFACE()
198 		// XTypeProvider
199 		DECLARE_XTYPEPROVIDER()
200 
201 		// OComponentHelper
202 		virtual void SAL_CALL disposing()  throw (::com::sun::star::uno::RuntimeException);
203 
204 	    // XEventListener
205 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& _rSource ) throw (::com::sun::star::uno::RuntimeException);
206 
207 		// XComponent/OComponentProxyAggregationHelper
208 		virtual void SAL_CALL dispose() throw( ::com::sun::star::uno::RuntimeException );
209 
210 	protected:
211 		// be called from within the dtor of derived classes
212 		void implEnsureDisposeInDtor( );
213 
214 	private:
215 		COMPHELPER_DLLPRIVATE OComponentProxyAggregation( );												// never implemented
216 		COMPHELPER_DLLPRIVATE OComponentProxyAggregation( const OComponentProxyAggregation& );			// never implemented
217 		COMPHELPER_DLLPRIVATE OComponentProxyAggregation& operator=( const OComponentProxyAggregation& );	// never implemented
218 	};
219 
220 //.............................................................................
221 }	// namespace comphelper
222 //.............................................................................
223 
224 
225 #endif // COMPHELPER_PROXY_AGGREGATION
226