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_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
21 : #define INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
22 :
23 : #include <unotools/unotoolsdllapi.h>
24 :
25 : #include <boost/shared_ptr.hpp>
26 : #include <com/sun/star/uno/Reference.hxx>
27 : #include <rtl/ref.hxx>
28 :
29 : namespace com { namespace sun { namespace star {
30 : namespace lang {
31 : class XComponent;
32 : }
33 : } } }
34 :
35 : namespace utl
36 : {
37 :
38 : //= DisposableComponent
39 :
40 : /** is a class which controls lifetime of an UNO component via ->XComponent::dispose
41 :
42 : You'll usually never use this class directly, but only as parameter for a
43 : ->SharedUNOComponent class.
44 : */
45 : class UNOTOOLS_DLLPUBLIC DisposableComponent
46 : {
47 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
48 : m_xComponent;
49 :
50 : public:
51 : /** constructs a ->DisposableComponent instance
52 :
53 : @param _rxComponent
54 : the component whose life time should be controlled by the instance. Must not be <NULL/>.
55 : */
56 : DisposableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
57 :
58 : /** disposes the component represented by the instance
59 :
60 : The component is queried for ->XComponent(which <em>must</em> be supported),
61 : and ->XComponent::dispose is invoked. A failure of this invocation (e.g. a thrown
62 : exception) is silenced in release builds, and reported in debug builds.
63 : */
64 : ~DisposableComponent();
65 :
66 : private:
67 : DisposableComponent(); // never implemented
68 : DisposableComponent( const DisposableComponent& ); // never implemented
69 : DisposableComponent& operator=( const DisposableComponent& ); // never implemented
70 : };
71 :
72 : //= CloseableComponent
73 :
74 : class CloseableComponentImpl;
75 : /** is a class which controls lifetime of an UNO component via ->XCloseable::close
76 :
77 : You'll usually never use this class directly, but only as parameter for a
78 : ->SharedUNOComponent class.
79 : */
80 : class UNOTOOLS_DLLPUBLIC CloseableComponent
81 : {
82 : private:
83 : /** Our IMPL class.
84 : */
85 : ::rtl::Reference< CloseableComponentImpl > m_pImpl;
86 :
87 : public:
88 : /** constructs a ->CloseableComponent instance
89 :
90 : @param _rxComponent
91 : the component whose life time should be controlled by the instance. Must not be <NULL/>.
92 : */
93 : CloseableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
94 :
95 : /** destroys resources associated with this instance, and disposes the component
96 :
97 : The component is queried for ->XCloseable (which <em>must</em> be supported),
98 : and ->XCloseable::close is invoked, with delivering the ownership.
99 : If the invocation fails with a ->CloseVetoException, this is ignored, since in
100 : this case the vetoing instance took the ownership.
101 :
102 : Any other failure will be reported in a debug version via assertion mechanisms,
103 : and silenced in release builds.
104 : */
105 : ~CloseableComponent();
106 :
107 : private:
108 : CloseableComponent(); // never implemented
109 : CloseableComponent( const CloseableComponent& ); // never implemented
110 : CloseableComponent& operator=( const CloseableComponent& ); // never implemented
111 : };
112 :
113 : //= SharedUNOComponent
114 :
115 : /** is a helper class for sharing ownership of a UNO component
116 :
117 : If you need to share an UNO component, which normally needs a dedicated owner,
118 : and is lifetime controlled by an explicit disposal action (not necessarily ->XComponent::dispose,
119 : but <em>any</em> explicit method call, after which the object is considered
120 : to be disposed), between different classes, ->SharedUNOComponent is what you need.
121 :
122 : Instead of passing around a <code>Reference< XFoo ></code>, and bothering
123 : with ownership and disposal, you just use a <code>SharedUNOComponent< XFoo ></code>.
124 : This instance can be passed around, including copying, and in nearly all respects behaves
125 : like the original <code>Reference< XFoo ></code>. However, when the last
126 : ->SharedUNOComponent referencing a certain <code>Reference< XFoo ></code> dies, it
127 : will automatically get rid of the object held by this reference.
128 :
129 : @param INTERFACE
130 : the UNO interface type as which the component should be held
131 :
132 : @param COMPONENT_HOLDER
133 : a class which can be used to represent and dispose a UNO component.
134 : The class must support (maybe explicit only) construction from a
135 : <code>Reference< INTERFACE ></code>, and destruction. Upon destruction,
136 : the class must dispose (by any suitable means) the component instance it was
137 : constructed with.
138 : */
139 : template < class INTERFACE, class COMPONENT = DisposableComponent >
140 0 : class SharedUNOComponent
141 : {
142 : private:
143 : typedef COMPONENT Component;
144 : typedef ::boost::shared_ptr< Component > ComponentPointer;
145 :
146 : private:
147 : ComponentPointer m_pComponent;
148 : ::com::sun::star::uno::Reference< INTERFACE > m_xTypedComponent;
149 :
150 : public:
151 : enum AssignmentMode
152 : {
153 : TakeOwnership,
154 : NoTakeOwnership
155 : };
156 :
157 : public:
158 0 : inline SharedUNOComponent()
159 0 : {
160 0 : }
161 :
162 0 : explicit inline SharedUNOComponent( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
163 0 : {
164 0 : reset( _rxComponent, eMode );
165 0 : }
166 :
167 : inline SharedUNOComponent( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
168 : {
169 : set( _pInterface, _queryThrow );
170 : }
171 :
172 0 : inline SharedUNOComponent( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
173 0 : {
174 0 : set( _rRef, _queryThrow );
175 0 : }
176 :
177 : inline SharedUNOComponent( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
178 : {
179 : set( _rAny, _queryThrow );
180 : }
181 :
182 : inline SharedUNOComponent( const SharedUNOComponent& _rxComponent, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
183 : {
184 : set( _rxComponent, _setThrow );
185 : }
186 :
187 : // SharedUNOComponent& operator=( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent );
188 : // This operator is intentionally not implemented. There is no canonic ownership after this operator
189 : // would have been applied: Should the SharedUNOComponent have the ownership of the component,
190 : // or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
191 : // instead of tempting clients of this class to use such a dangerous operator, we do
192 : // not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
193 : // use the ->reset method.
194 :
195 : /** assigns a new component, and releases the old one
196 : */
197 : void reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
198 :
199 : inline bool set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query );
200 : inline bool set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query );
201 : inline bool set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query );
202 :
203 : inline void set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
204 : inline void set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
205 : inline void set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
206 :
207 : inline void set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
208 : inline void set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
209 : inline void set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
210 :
211 : INTERFACE* SAL_CALL operator->() const;
212 :
213 0 : inline operator const ::com::sun::star::uno::Reference< INTERFACE >&() const
214 : {
215 0 : return m_xTypedComponent;
216 : }
217 :
218 0 : inline const ::com::sun::star::uno::Reference< INTERFACE >& getTyped() const
219 : {
220 0 : return m_xTypedComponent;
221 : }
222 :
223 0 : inline bool is() const
224 : {
225 0 : return m_xTypedComponent.is();
226 : }
227 :
228 0 : inline void clear()
229 : {
230 0 : m_pComponent.reset();
231 0 : m_xTypedComponent.clear();
232 0 : }
233 : };
234 :
235 : template < class INTERFACE, class COMPONENT >
236 0 : INTERFACE* SAL_CALL SharedUNOComponent< INTERFACE, COMPONENT >::operator->() const
237 : {
238 0 : return m_xTypedComponent.operator->();
239 : }
240 :
241 : // assignments
242 : template < class INTERFACE, class COMPONENT >
243 0 : void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
244 : {
245 0 : m_pComponent.reset( _eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : NULL );
246 0 : m_xTypedComponent = _rxComponent;
247 0 : }
248 :
249 : // comparison operators
250 : template < class INTERFACE, class COMPONENT >
251 : bool operator==( const ::com::sun::star::uno::Reference< INTERFACE >& _rLHS, const SharedUNOComponent< INTERFACE, COMPONENT >& _rRHS )
252 : {
253 : return _rLHS == _rRHS.getTyped();
254 : }
255 :
256 : template < class INTERFACE, class COMPONENT >
257 0 : bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const ::com::sun::star::uno::Reference< INTERFACE >& _rRHS )
258 : {
259 0 : return _rLHS.getTyped() == _rRHS;
260 : }
261 :
262 : // conversion to Any
263 : template < class INTERFACE, class COMPONENT >
264 : inline void SAL_CALL operator <<= ( ::com::sun::star::uno::Any & rAny, const SharedUNOComponent< INTERFACE, COMPONENT >& value ) SAL_THROW(())
265 : {
266 : rAny <<= value.getTyped();
267 : }
268 :
269 : template < class INTERFACE, class COMPONENT >
270 0 : inline ::com::sun::star::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value ) SAL_THROW(())
271 : {
272 0 : return makeAny( value.getTyped() );
273 : }
274 :
275 : template < class INTERFACE, class COMPONENT >
276 : void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
277 : {
278 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _queryThrow ), TakeOwnership );
279 : }
280 :
281 : template < class INTERFACE, class COMPONENT >
282 0 : void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
283 : {
284 0 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
285 0 : }
286 :
287 : template < class INTERFACE, class COMPONENT >
288 : void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
289 : {
290 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _queryThrow ), TakeOwnership );
291 : }
292 :
293 : template < class INTERFACE, class COMPONENT >
294 : void SharedUNOComponent< INTERFACE, COMPONENT >::set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
295 : {
296 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _setThrow ), TakeOwnership );
297 : }
298 :
299 : template < class INTERFACE, class COMPONENT >
300 0 : void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
301 : {
302 0 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
303 0 : }
304 :
305 : template < class INTERFACE, class COMPONENT >
306 : void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
307 : {
308 : *this = _rComp;
309 : // provoke an exception in case the component is NULL
310 : m_xTypedComponent.set( m_xTypedComponent, _setThrow );
311 : }
312 :
313 : template < class INTERFACE, class COMPONENT >
314 : bool SharedUNOComponent< INTERFACE, COMPONENT >::set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query )
315 : {
316 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _query ) );
317 : return is();
318 : }
319 :
320 : template < class INTERFACE, class COMPONENT >
321 : bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query )
322 : {
323 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _query ) );
324 : return is();
325 : }
326 :
327 : template < class INTERFACE, class COMPONENT >
328 : bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query )
329 : {
330 : reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _query ) );
331 : return is();
332 : }
333 :
334 : } // namespace utl
335 :
336 : #endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
337 :
338 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|