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 INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
21 #define INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
23 #include <config_options.h>
24 #include <unotools/unotoolsdllapi.h>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <rtl/ref.hxx>
29 namespace com::sun::star
{
38 //= DisposableComponent
40 /** is a class which controls lifetime of a UNO component via ->XComponent::dispose
42 You'll usually never use this class directly, but only as parameter for a
43 ->SharedUNOComponent class.
45 class UNOTOOLS_DLLPUBLIC DisposableComponent
47 css::uno::Reference
< css::lang::XComponent
> m_xComponent
;
50 /** constructs a ->DisposableComponent instance
53 the component whose life time should be controlled by the instance. Must not be <NULL/>.
55 DisposableComponent( const css::uno::Reference
< css::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();
66 DisposableComponent( const DisposableComponent
& ) = delete;
67 DisposableComponent
& operator=( const DisposableComponent
& ) = delete;
70 //= CloseableComponent
72 class CloseableComponentImpl
;
73 /** is a class which controls lifetime of a 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 UNLESS_MERGELIBS_MORE(UNOTOOLS_DLLPUBLIC
) CloseableComponent
83 ::rtl::Reference
< CloseableComponentImpl
> m_pImpl
;
86 /** constructs a ->CloseableComponent instance
89 the component whose life time should be controlled by the instance. Must not be <NULL/>.
91 CloseableComponent( const css::uno::Reference
< css::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();
106 CloseableComponent( const CloseableComponent
& ) = delete;
107 CloseableComponent
& operator=( const CloseableComponent
& ) = delete;
110 //= SharedUNOComponent
112 /** is a helper class for sharing ownership of a UNO component
114 If you need to share a 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< XFoo ></code>, and bothering
120 with ownership and disposal, you just use a <code>SharedUNOComponent< XFoo ></code>.
121 This instance can be passed around, including copying, and in nearly all respects behaves
122 like the original <code>Reference< XFoo ></code>. However, when the last
123 ->SharedUNOComponent referencing a certain <code>Reference< XFoo ></code> dies, it
124 will automatically get rid of the object held by this reference.
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< INTERFACE ></code>, and destruction. Upon destruction,
133 the class must dispose (by any suitable means) the component instance it was
136 template < class INTERFACE
, class COMPONENT
= DisposableComponent
>
137 class SharedUNOComponent
140 typedef COMPONENT Component
;
143 std::shared_ptr
<Component
> m_xComponent
;
144 css::uno::Reference
< INTERFACE
> m_xTypedComponent
;
158 explicit SharedUNOComponent( const css::uno::Reference
< INTERFACE
>& _rxComponent
, AssignmentMode eMode
= TakeOwnership
)
160 reset( _rxComponent
, eMode
);
163 SharedUNOComponent( const css::uno::BaseReference
& _rRef
, css::uno::UnoReference_QueryThrow _queryThrow
)
165 set( _rRef
, _queryThrow
);
168 // SharedUNOComponent& operator=( const css::uno::Reference< INTERFACE >& _rxComponent );
169 // This operator is intentionally not implemented. There is no canonic ownership after this operator
170 // would have been applied: Should the SharedUNOComponent have the ownership of the component,
171 // or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
172 // instead of tempting clients of this class to use such a dangerous operator, we do
173 // not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
174 // use the ->reset method.
176 /** assigns a new component, and releases the old one
178 void reset( const css::uno::Reference
< INTERFACE
>& _rxComponent
, AssignmentMode _eMode
= TakeOwnership
);
180 inline bool set( const css::uno::BaseReference
& _rRef
, css::uno::UnoReference_Query _query
);
182 inline void set( const css::uno::BaseReference
& _rRef
, css::uno::UnoReference_QueryThrow _queryThrow
);
184 inline void set( const css::uno::Reference
< INTERFACE
>& _rRef
, css::uno::UnoReference_SetThrow _setThrow
);
185 inline void set( const SharedUNOComponent
& _rComp
, css::uno::UnoReference_SetThrow _setThrow
);
187 INTERFACE
* SAL_CALL
operator->() const;
189 operator const css::uno::Reference
< INTERFACE
>&() const
191 return m_xTypedComponent
;
194 const css::uno::Reference
< INTERFACE
>& getTyped() const
196 return m_xTypedComponent
;
201 return m_xTypedComponent
.is();
206 m_xComponent
.reset();
207 m_xTypedComponent
.clear();
211 template < class INTERFACE
, class COMPONENT
>
212 INTERFACE
* SAL_CALL SharedUNOComponent
< INTERFACE
, COMPONENT
>::operator->() const
214 return m_xTypedComponent
.operator->();
218 template < class INTERFACE
, class COMPONENT
>
219 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::reset( const css::uno::Reference
< INTERFACE
>& _rxComponent
, AssignmentMode _eMode
)
221 m_xComponent
.reset(_eMode
== TakeOwnership
? new COMPONENT( _rxComponent
) : nullptr);
222 m_xTypedComponent
= _rxComponent
;
225 // comparison operators
227 template < class INTERFACE
, class COMPONENT
>
228 bool operator==( const SharedUNOComponent
< INTERFACE
, COMPONENT
>& _rLHS
, const css::uno::Reference
< INTERFACE
>& _rRHS
)
230 return _rLHS
.getTyped() == _rRHS
;
233 template < class INTERFACE
, class COMPONENT
>
234 inline css::uno::Any SAL_CALL
makeAny( const SharedUNOComponent
< INTERFACE
, COMPONENT
>& value
)
236 return css::uno::Any( value
.getTyped() );
239 template < class INTERFACE
, class COMPONENT
>
240 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const css::uno::BaseReference
& _rRef
, css::uno::UnoReference_QueryThrow _queryThrow
)
242 reset( css::uno::Reference
< INTERFACE
>( _rRef
, _queryThrow
), TakeOwnership
);
245 template < class INTERFACE
, class COMPONENT
>
246 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const css::uno::Reference
< INTERFACE
>& _rRef
, css::uno::UnoReference_SetThrow _setThrow
)
248 reset( css::uno::Reference
< INTERFACE
>( _rRef
, _setThrow
), TakeOwnership
);
251 template < class INTERFACE
, class COMPONENT
>
252 void SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const SharedUNOComponent
& _rComp
, css::uno::UnoReference_SetThrow _setThrow
)
255 // provoke an exception in case the component is NULL
256 m_xTypedComponent
.set( m_xTypedComponent
, _setThrow
);
259 template < class INTERFACE
, class COMPONENT
>
260 bool SharedUNOComponent
< INTERFACE
, COMPONENT
>::set( const css::uno::BaseReference
& _rRef
, css::uno::UnoReference_Query _query
)
262 reset( css::uno::Reference
< INTERFACE
>( _rRef
, _query
) );
268 #endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */