bump product version to 5.0.4.1
[LibreOffice.git] / include / unotools / sharedunocomponent.hxx
blob9a39134f6d2698529fd13e4d4c4f7b9fbd5aaf9b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
21 #define INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
23 #include <unotools/unotoolsdllapi.h>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <rtl/ref.hxx>
26 #include <memory>
28 namespace com { namespace sun { namespace star {
29 namespace lang {
30 class XComponent;
32 } } }
34 namespace utl
37 //= DisposableComponent
39 /** is a class which controls lifetime of an UNO component via ->XComponent::dispose
41 You'll usually never use this class directly, but only as parameter for a
42 ->SharedUNOComponent class.
44 class UNOTOOLS_DLLPUBLIC DisposableComponent
46 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
47 m_xComponent;
49 public:
50 /** constructs a ->DisposableComponent instance
52 @param _rxComponent
53 the component whose life time should be controlled by the instance. Must not be <NULL/>.
55 DisposableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
57 /** disposes the component represented by the instance
59 The component is queried for ->XComponent(which <em>must</em> be supported),
60 and ->XComponent::dispose is invoked. A failure of this invocation (e.g. a thrown
61 exception) is silenced in release builds, and reported in debug builds.
63 ~DisposableComponent();
65 private:
66 DisposableComponent( const DisposableComponent& ) SAL_DELETED_FUNCTION;
67 DisposableComponent& operator=( const DisposableComponent& ) SAL_DELETED_FUNCTION;
70 //= CloseableComponent
72 class CloseableComponentImpl;
73 /** is a class which controls lifetime of an UNO component via ->XCloseable::close
75 You'll usually never use this class directly, but only as parameter for a
76 ->SharedUNOComponent class.
78 class UNOTOOLS_DLLPUBLIC CloseableComponent
80 private:
81 /** Our IMPL class.
83 ::rtl::Reference< CloseableComponentImpl > m_pImpl;
85 public:
86 /** constructs a ->CloseableComponent instance
88 @param _rxComponent
89 the component whose life time should be controlled by the instance. Must not be <NULL/>.
91 CloseableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
93 /** destroys resources associated with this instance, and disposes the component
95 The component is queried for ->XCloseable (which <em>must</em> be supported),
96 and ->XCloseable::close is invoked, with delivering the ownership.
97 If the invocation fails with a ->CloseVetoException, this is ignored, since in
98 this case the vetoing instance took the ownership.
100 Any other failure will be reported in a debug version via assertion mechanisms,
101 and silenced in release builds.
103 ~CloseableComponent();
105 private:
106 CloseableComponent( const CloseableComponent& ) SAL_DELETED_FUNCTION;
107 CloseableComponent& operator=( const CloseableComponent& ) SAL_DELETED_FUNCTION;
110 //= SharedUNOComponent
112 /** is a helper class for sharing ownership of a UNO component
114 If you need to share an UNO component, which normally needs a dedicated owner,
115 and is lifetime controlled by an explicit disposal action (not necessarily ->XComponent::dispose,
116 but <em>any</em> explicit method call, after which the object is considered
117 to be disposed), between different classes, ->SharedUNOComponent is what you need.
119 Instead of passing around a <code>Reference&lt; XFoo &gt;</code>, and bothering
120 with ownership and disposal, you just use a <code>SharedUNOComponent&lt; XFoo &gt;</code>.
121 This instance can be passed around, including copying, and in nearly all respects behaves
122 like the original <code>Reference&lt; XFoo &gt;</code>. However, when the last
123 ->SharedUNOComponent referencing a certain <code>Reference&lt; XFoo &gt;</code> dies, it
124 will automatically get rid of the object held by this reference.
126 @param INTERFACE
127 the UNO interface type as which the component should be held
129 @param COMPONENT_HOLDER
130 a class which can be used to represent and dispose a UNO component.
131 The class must support (maybe explicit only) construction from a
132 <code>Reference&lt; INTERFACE &gt;</code>, and destruction. Upon destruction,
133 the class must dispose (by any suitable means) the component instance it was
134 constructed with.
136 template < class INTERFACE, class COMPONENT = DisposableComponent >
137 class SharedUNOComponent
139 private:
140 typedef COMPONENT Component;
141 typedef std::shared_ptr<Component> ComponentPointer;
143 private:
144 ComponentPointer m_xComponent;
145 ::com::sun::star::uno::Reference< INTERFACE > m_xTypedComponent;
147 public:
148 enum AssignmentMode
150 TakeOwnership,
151 NoTakeOwnership
154 public:
155 inline SharedUNOComponent()
159 explicit inline SharedUNOComponent( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
161 reset( _rxComponent, eMode );
164 inline SharedUNOComponent( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
166 set( _pInterface, _queryThrow );
169 inline SharedUNOComponent( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
171 set( _rRef, _queryThrow );
174 inline SharedUNOComponent( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
176 set( _rAny, _queryThrow );
179 inline SharedUNOComponent( const SharedUNOComponent& _rxComponent, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
181 set( _rxComponent, _setThrow );
184 // SharedUNOComponent& operator=( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent );
185 // This operator is intentionally not implemented. There is no canonic ownership after this operator
186 // would have been applied: Should the SharedUNOComponent have the ownership of the component,
187 // or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
188 // instead of tempting clients of this class to use such a dangerous operator, we do
189 // not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
190 // use the ->reset method.
192 /** assigns a new component, and releases the old one
194 void reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
196 inline bool set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query );
197 inline bool set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query );
198 inline bool set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query );
200 inline void set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
201 inline void set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
202 inline void set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
204 inline void set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
205 inline void set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
206 inline void set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
208 INTERFACE* SAL_CALL operator->() const;
210 operator const ::com::sun::star::uno::Reference< INTERFACE >&() const
212 return m_xTypedComponent;
215 const ::com::sun::star::uno::Reference< INTERFACE >& getTyped() const
217 return m_xTypedComponent;
220 bool is() const
222 return m_xTypedComponent.is();
225 void clear()
227 m_xComponent.reset();
228 m_xTypedComponent.clear();
232 template < class INTERFACE, class COMPONENT >
233 INTERFACE* SAL_CALL SharedUNOComponent< INTERFACE, COMPONENT >::operator->() const
235 return m_xTypedComponent.operator->();
238 // assignments
239 template < class INTERFACE, class COMPONENT >
240 void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
242 m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : NULL);
243 m_xTypedComponent = _rxComponent;
246 // comparison operators
247 template < class INTERFACE, class COMPONENT >
248 bool operator==( const ::com::sun::star::uno::Reference< INTERFACE >& _rLHS, const SharedUNOComponent< INTERFACE, COMPONENT >& _rRHS )
250 return _rLHS == _rRHS.getTyped();
253 template < class INTERFACE, class COMPONENT >
254 bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const ::com::sun::star::uno::Reference< INTERFACE >& _rRHS )
256 return _rLHS.getTyped() == _rRHS;
259 // conversion to Any
260 template < class INTERFACE, class COMPONENT >
261 inline void SAL_CALL operator <<= ( ::com::sun::star::uno::Any & rAny, const SharedUNOComponent< INTERFACE, COMPONENT >& value )
263 rAny <<= value.getTyped();
266 template < class INTERFACE, class COMPONENT >
267 inline ::com::sun::star::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value )
269 return makeAny( value.getTyped() );
272 template < class INTERFACE, class COMPONENT >
273 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
275 reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _queryThrow ), TakeOwnership );
278 template < class INTERFACE, class COMPONENT >
279 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
281 reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
284 template < class INTERFACE, class COMPONENT >
285 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
287 reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _queryThrow ), TakeOwnership );
290 template < class INTERFACE, class COMPONENT >
291 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
293 reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _setThrow ), TakeOwnership );
296 template < class INTERFACE, class COMPONENT >
297 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
299 reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
302 template < class INTERFACE, class COMPONENT >
303 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
305 *this = _rComp;
306 // provoke an exception in case the component is NULL
307 m_xTypedComponent.set( m_xTypedComponent, _setThrow );
310 template < class INTERFACE, class COMPONENT >
311 bool SharedUNOComponent< INTERFACE, COMPONENT >::set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query )
313 reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _query ) );
314 return is();
317 template < class INTERFACE, class COMPONENT >
318 bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query )
320 reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _query ) );
321 return is();
324 template < class INTERFACE, class COMPONENT >
325 bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query )
327 reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _query ) );
328 return is();
331 } // namespace utl
333 #endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */