bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / unotools / sharedunocomponent.hxx
blob81e2a253c6de4c8892f533834b0e94644edebd09
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 a 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 css::uno::Reference< css::lang::XComponent > m_xComponent;
48 public:
49 /** constructs a ->DisposableComponent instance
51 @param _rxComponent
52 the component whose life time should be controlled by the instance. Must not be <NULL/>.
54 DisposableComponent( const css::uno::Reference< css::uno::XInterface >& _rxComponent );
56 /** disposes the component represented by the instance
58 The component is queried for ->XComponent(which <em>must</em> be supported),
59 and ->XComponent::dispose is invoked. A failure of this invocation (e.g. a thrown
60 exception) is silenced in release builds, and reported in debug builds.
62 ~DisposableComponent();
64 private:
65 DisposableComponent( const DisposableComponent& ) = delete;
66 DisposableComponent& operator=( const DisposableComponent& ) = delete;
69 //= CloseableComponent
71 class CloseableComponentImpl;
72 /** is a class which controls lifetime of a UNO component via ->XCloseable::close
74 You'll usually never use this class directly, but only as parameter for a
75 ->SharedUNOComponent class.
77 class UNOTOOLS_DLLPUBLIC CloseableComponent
79 private:
80 /** Our IMPL class.
82 ::rtl::Reference< CloseableComponentImpl > m_pImpl;
84 public:
85 /** constructs a ->CloseableComponent instance
87 @param _rxComponent
88 the component whose life time should be controlled by the instance. Must not be <NULL/>.
90 CloseableComponent( const css::uno::Reference< css::uno::XInterface >& _rxComponent );
92 /** destroys resources associated with this instance, and disposes the component
94 The component is queried for ->XCloseable (which <em>must</em> be supported),
95 and ->XCloseable::close is invoked, with delivering the ownership.
96 If the invocation fails with a ->CloseVetoException, this is ignored, since in
97 this case the vetoing instance took the ownership.
99 Any other failure will be reported in a debug version via assertion mechanisms,
100 and silenced in release builds.
102 ~CloseableComponent();
104 private:
105 CloseableComponent( const CloseableComponent& ) = delete;
106 CloseableComponent& operator=( const CloseableComponent& ) = delete;
109 //= SharedUNOComponent
111 /** is a helper class for sharing ownership of a UNO component
113 If you need to share a UNO component, which normally needs a dedicated owner,
114 and is lifetime controlled by an explicit disposal action (not necessarily ->XComponent::dispose,
115 but <em>any</em> explicit method call, after which the object is considered
116 to be disposed), between different classes, ->SharedUNOComponent is what you need.
118 Instead of passing around a <code>Reference&lt; XFoo &gt;</code>, and bothering
119 with ownership and disposal, you just use a <code>SharedUNOComponent&lt; XFoo &gt;</code>.
120 This instance can be passed around, including copying, and in nearly all respects behaves
121 like the original <code>Reference&lt; XFoo &gt;</code>. However, when the last
122 ->SharedUNOComponent referencing a certain <code>Reference&lt; XFoo &gt;</code> dies, it
123 will automatically get rid of the object held by this reference.
125 @param INTERFACE
126 the UNO interface type as which the component should be held
128 @param COMPONENT_HOLDER
129 a class which can be used to represent and dispose a UNO component.
130 The class must support (maybe explicit only) construction from a
131 <code>Reference&lt; INTERFACE &gt;</code>, and destruction. Upon destruction,
132 the class must dispose (by any suitable means) the component instance it was
133 constructed with.
135 template < class INTERFACE, class COMPONENT = DisposableComponent >
136 class SharedUNOComponent
138 private:
139 typedef COMPONENT Component;
141 private:
142 std::shared_ptr<Component> m_xComponent;
143 css::uno::Reference< INTERFACE > m_xTypedComponent;
145 public:
146 enum AssignmentMode
148 TakeOwnership,
149 NoTakeOwnership
152 public:
153 SharedUNOComponent()
157 explicit SharedUNOComponent( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
159 reset( _rxComponent, eMode );
162 SharedUNOComponent( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow )
164 set( _rRef, _queryThrow );
167 // SharedUNOComponent& operator=( const css::uno::Reference< INTERFACE >& _rxComponent );
168 // This operator is intentionally not implemented. There is no canonic ownership after this operator
169 // would have been applied: Should the SharedUNOComponent have the ownership of the component,
170 // or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
171 // instead of tempting clients of this class to use such a dangerous operator, we do
172 // not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
173 // use the ->reset method.
175 /** assigns a new component, and releases the old one
177 void reset( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
179 inline bool set( const css::uno::BaseReference& _rRef, css::uno::UnoReference_Query _query );
181 inline void set( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow );
183 inline void set( const css::uno::Reference< INTERFACE >& _rRef, css::uno::UnoReference_SetThrow _setThrow );
184 inline void set( const SharedUNOComponent& _rComp, css::uno::UnoReference_SetThrow _setThrow );
186 INTERFACE* SAL_CALL operator->() const;
188 operator const css::uno::Reference< INTERFACE >&() const
190 return m_xTypedComponent;
193 const css::uno::Reference< INTERFACE >& getTyped() const
195 return m_xTypedComponent;
198 bool is() const
200 return m_xTypedComponent.is();
203 void clear()
205 m_xComponent.reset();
206 m_xTypedComponent.clear();
210 template < class INTERFACE, class COMPONENT >
211 INTERFACE* SAL_CALL SharedUNOComponent< INTERFACE, COMPONENT >::operator->() const
213 return m_xTypedComponent.operator->();
216 // assignments
217 template < class INTERFACE, class COMPONENT >
218 void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
220 m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : nullptr);
221 m_xTypedComponent = _rxComponent;
224 // comparison operators
226 template < class INTERFACE, class COMPONENT >
227 bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const css::uno::Reference< INTERFACE >& _rRHS )
229 return _rLHS.getTyped() == _rRHS;
232 template < class INTERFACE, class COMPONENT >
233 inline css::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value )
235 return makeAny( value.getTyped() );
238 template < class INTERFACE, class COMPONENT >
239 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow )
241 reset( css::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
244 template < class INTERFACE, class COMPONENT >
245 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::Reference< INTERFACE >& _rRef, css::uno::UnoReference_SetThrow _setThrow )
247 reset( css::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
250 template < class INTERFACE, class COMPONENT >
251 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, css::uno::UnoReference_SetThrow _setThrow )
253 *this = _rComp;
254 // provoke an exception in case the component is NULL
255 m_xTypedComponent.set( m_xTypedComponent, _setThrow );
258 template < class INTERFACE, class COMPONENT >
259 bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::BaseReference& _rRef, css::uno::UnoReference_Query _query )
261 reset( css::uno::Reference< INTERFACE >( _rRef, _query ) );
262 return is();
265 } // namespace utl
267 #endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */