1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef UNOTOOLS_INC_SHAREDUNOCOMPONENT_HXX
21 #define UNOTOOLS_INC_SHAREDUNOCOMPONENT_HXX
23 #include "unotoolsdllapi.h"
25 #include <boost/shared_ptr.hpp>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <rtl/ref.hxx>
29 namespace com
{ namespace sun
{ namespace star
{
34 //............................................................................
37 //............................................................................
39 //========================================================================
40 //= DisposableComponent
41 //========================================================================
42 /** is a class which controls lifetime of an UNO component via ->XComponent::dispose
44 You'll usually never use this class directly, but only as parameter for a
45 ->SharedUNOComponent class.
47 class UNOTOOLS_DLLPUBLIC DisposableComponent
49 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
53 /** constructs a ->DisposableComponent instance
56 the component whose life time should be controlled by the instance. Must not be <NULL/>.
58 DisposableComponent( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxComponent
);
60 /** disposes the component represented by the instance
62 The component is queried for ->XComponent(which <em>must</em> be supported),
63 and ->XComponent::dispose is invoked. A failure of this invocation (e.g. a thrown
64 exception) is silenced in release builds, and reported in debug builds.
66 ~DisposableComponent();
69 DisposableComponent(); // never implemented
70 DisposableComponent( const DisposableComponent
& ); // never implemented
71 DisposableComponent
& operator=( const DisposableComponent
& ); // never implemented
74 //========================================================================
75 //= CloseableComponent
76 //========================================================================
77 class CloseableComponentImpl
;
78 /** is a class which controls lifetime of an UNO component via ->XCloseable::close
80 You'll usually never use this class directly, but only as parameter for a
81 ->SharedUNOComponent class.
83 class UNOTOOLS_DLLPUBLIC CloseableComponent
88 ::rtl::Reference
< CloseableComponentImpl
> m_pImpl
;
91 /** constructs a ->CloseableComponent instance
94 the component whose life time should be controlled by the instance. Must not be <NULL/>.
96 CloseableComponent( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxComponent
);
98 /** destroys resources associated with this instance, and disposes the component
100 The component is queried for ->XCloseable (which <em>must</em> be supported),
101 and ->XCloseable::close is invoked, with delivering the ownership.
102 If the invocation fails with a ->CloseVetoException, this is ignored, since in
103 this case the vetoing instance took the ownership.
105 Any other failure will be reported in a debug version via assertion mechanisms,
106 and silenced in release builds.
108 ~CloseableComponent();
111 CloseableComponent(); // never implemented
112 CloseableComponent( const CloseableComponent
& ); // never implemented
113 CloseableComponent
& operator=( const CloseableComponent
& ); // never implemented
116 //========================================================================
117 //= SharedUNOComponent
118 //========================================================================
119 /** is a helper class for sharing ownership of a UNO component
121 If you need to share an UNO component, which normally needs a dedicated owner,
122 and is lifetime controlled by an explicit disposal action (not necessarily ->XComponent::dispose,
123 but <em>any</em> explicit method call, after which the object is considered
124 to be disposed), between different classes, ->SharedUNOComponent is what you need.
126 Instead of passing around a <code>Reference< XFoo ></code>, and bothering
127 with ownership and disposal, you just use a <code>SharedUNOComponent< XFoo ></code>.
128 This instance can be passed around, including copying, and in nearly all respects behaves
129 like the original <code>Reference< XFoo ></code>. However, when the last
130 ->SharedUNOComponent referencing a certain <code>Reference< XFoo ></code> dies, it
131 will automatically get rid of the object held by this reference.
134 the UNO interface type as which the component should be held
136 @param COMPONENT_HOLDER
137 a class which can be used to represent and dispose a UNO component.
138 The class must support (maybe explicit only) construction from a
139 <code>Reference< INTERFACE ></code>, and destruction. Upon destruction,
140 the class must dispose (by any suitable means) the component instance it was
143 template < class INTERFACE
, class COMPONENT
= DisposableComponent
>
144 class SharedUNOComponent
147 typedef COMPONENT Component
;
148 typedef ::boost::shared_ptr
< Component
> ComponentPointer
;
151 ComponentPointer m_pComponent
;
152 ::com::sun::star::uno::Reference
< INTERFACE
> m_xTypedComponent
;
162 inline SharedUNOComponent()
166 explicit inline SharedUNOComponent( const ::com::sun::star::uno::Reference
< INTERFACE
>& _rxComponent
, AssignmentMode eMode
= TakeOwnership
)
168 reset( _rxComponent
, eMode
);
171 #ifndef EXCEPTIONS_OFF
172 inline SharedUNOComponent( const ::com::sun::star::uno::XInterface
* _pInterface
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
)
174 set( _pInterface
, _queryThrow
);
177 inline SharedUNOComponent( const ::com::sun::star::uno::BaseReference
& _rRef
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
)
179 set( _rRef
, _queryThrow
);
182 inline SharedUNOComponent( const ::com::sun::star::uno::Any
& _rAny
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
)
184 set( _rAny
, _queryThrow
);
187 inline SharedUNOComponent( const SharedUNOComponent
& _rxComponent
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
)
189 set( _rxComponent
, _setThrow
);
193 // SharedUNOComponent& operator=( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent );
194 // This operator is intentionally not implemented. There is no canonic ownership after this operator
195 // would have been applied: Should the SharedUNOComponent have the ownership of the component,
196 // or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
197 // instead of tempting clients of this class to use such a dangerous operator, we do
198 // not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
199 // use the ->reset method.
201 /** assigns a new component, and releases the old one
203 void reset( const ::com::sun::star::uno::Reference
< INTERFACE
>& _rxComponent
, AssignmentMode _eMode
= TakeOwnership
);
205 inline bool set( ::com::sun::star::uno::XInterface
* _pInterface
, ::com::sun::star::uno::UnoReference_Query _query
);
206 inline bool set( const ::com::sun::star::uno::BaseReference
& _rRef
, ::com::sun::star::uno::UnoReference_Query _query
);
207 inline bool set( const ::com::sun::star::uno::Any
& _rAny
, ::com::sun::star::uno::UnoReference_Query _query
);
209 #ifndef EXCEPTIONS_OFF
210 inline void set( const ::com::sun::star::uno::XInterface
* _pInterface
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
);
211 inline void set( const ::com::sun::star::uno::BaseReference
& _rRef
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
);
212 inline void set( const ::com::sun::star::uno::Any
& _rAny
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
);
214 inline void set( const INTERFACE
* _pInterface
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
);
215 inline void set( const ::com::sun::star::uno::Reference
< INTERFACE
>& _rRef
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
);
216 inline void set( const SharedUNOComponent
& _rComp
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
);
219 INTERFACE
* SAL_CALL
operator->() const;
221 inline operator const ::com::sun::star::uno::Reference
< INTERFACE
>&() const
223 return m_xTypedComponent
;
226 inline const ::com::sun::star::uno::Reference
< INTERFACE
>& getTyped() const
228 return m_xTypedComponent
;
231 inline bool is() const
233 return m_xTypedComponent
.is();
238 m_pComponent
.reset();
239 m_xTypedComponent
.clear();
243 //-------------------------------------------------------------------------
244 template < class INTERFACE
, class COMPONENT
>
245 INTERFACE
* SAL_CALL SharedUNOComponent
< INTERFACE
, COMPONENT
>::operator->() const
247 return m_xTypedComponent
.operator->();
250 //-------------------------------------------------------------------------
252 template < class INTERFACE
, class COMPONENT
>
253 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::reset( const ::com::sun::star::uno::Reference
< INTERFACE
>& _rxComponent
, AssignmentMode _eMode
)
255 m_pComponent
.reset( _eMode
== TakeOwnership
? new COMPONENT( _rxComponent
) : NULL
);
256 m_xTypedComponent
= _rxComponent
;
259 //-------------------------------------------------------------------------
260 // comparison operators
261 template < class INTERFACE
, class COMPONENT
>
262 bool operator==( const ::com::sun::star::uno::Reference
< INTERFACE
>& _rLHS
, const SharedUNOComponent
< INTERFACE
, COMPONENT
>& _rRHS
)
264 return _rLHS
== _rRHS
.getTyped();
267 template < class INTERFACE
, class COMPONENT
>
268 bool operator==( const SharedUNOComponent
< INTERFACE
, COMPONENT
>& _rLHS
, const ::com::sun::star::uno::Reference
< INTERFACE
>& _rRHS
)
270 return _rLHS
.getTyped() == _rRHS
;
273 //-------------------------------------------------------------------------
275 template < class INTERFACE
, class COMPONENT
>
276 inline void SAL_CALL
operator <<= ( ::com::sun::star::uno::Any
& rAny
, const SharedUNOComponent
< INTERFACE
, COMPONENT
>& value
) SAL_THROW(())
278 rAny
<<= value
.getTyped();
281 //-------------------------------------------------------------------------
282 template < class INTERFACE
, class COMPONENT
>
283 inline ::com::sun::star::uno::Any SAL_CALL
makeAny( const SharedUNOComponent
< INTERFACE
, COMPONENT
>& value
) SAL_THROW(())
285 return makeAny( value
.getTyped() );
288 #ifndef EXCEPTIONS_OFF
289 //-------------------------------------------------------------------------
290 template < class INTERFACE
, class COMPONENT
>
291 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const ::com::sun::star::uno::XInterface
* _pInterface
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
)
293 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _pInterface
, _queryThrow
), TakeOwnership
);
296 //-------------------------------------------------------------------------
297 template < class INTERFACE
, class COMPONENT
>
298 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const ::com::sun::star::uno::BaseReference
& _rRef
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
)
300 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _rRef
, _queryThrow
), TakeOwnership
);
303 //-------------------------------------------------------------------------
304 template < class INTERFACE
, class COMPONENT
>
305 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const ::com::sun::star::uno::Any
& _rAny
, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow
)
307 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _rAny
, _queryThrow
), TakeOwnership
);
310 //-------------------------------------------------------------------------
311 template < class INTERFACE
, class COMPONENT
>
312 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const INTERFACE
* _pInterface
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
)
314 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _pInterface
, _setThrow
), TakeOwnership
);
317 //-------------------------------------------------------------------------
318 template < class INTERFACE
, class COMPONENT
>
319 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const ::com::sun::star::uno::Reference
< INTERFACE
>& _rRef
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
)
321 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _rRef
, _setThrow
), TakeOwnership
);
324 //-------------------------------------------------------------------------
325 template < class INTERFACE
, class COMPONENT
>
326 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const SharedUNOComponent
& _rComp
, ::com::sun::star::uno::UnoReference_SetThrow _setThrow
)
329 // provoke an exception in case the component is NULL
330 m_xTypedComponent
.set( m_xTypedComponent
, _setThrow
);
334 //-------------------------------------------------------------------------
335 template < class INTERFACE
, class COMPONENT
>
336 bool SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( ::com::sun::star::uno::XInterface
* _pInterface
, ::com::sun::star::uno::UnoReference_Query _query
)
338 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _pInterface
, _query
) );
342 //-------------------------------------------------------------------------
343 template < class INTERFACE
, class COMPONENT
>
344 bool SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const ::com::sun::star::uno::BaseReference
& _rRef
, ::com::sun::star::uno::UnoReference_Query _query
)
346 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _rRef
, _query
) );
350 //-------------------------------------------------------------------------
351 template < class INTERFACE
, class COMPONENT
>
352 bool SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const ::com::sun::star::uno::Any
& _rAny
, ::com::sun::star::uno::UnoReference_Query _query
)
354 reset( ::com::sun::star::uno::Reference
< INTERFACE
>( _rAny
, _query
) );
358 //............................................................................
360 //............................................................................
362 #endif // UNOTOOLS_INC_SHAREDUNOCOMPONENT_HXX
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */