LibreOffice Module unotools (master) 1
sharedunocomponent.hxx
Go to the documentation of this file.
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
24#include <com/sun/star/uno/Reference.hxx>
25#include <rtl/ref.hxx>
26#include <memory>
27
28namespace com::sun::star {
29 namespace lang {
30 class XComponent;
31 }
32}
33
34namespace utl
35{
36
37 //= DisposableComponent
38
45 {
46 css::uno::Reference< css::lang::XComponent > m_xComponent;
47
48 public:
54 DisposableComponent( const css::uno::Reference< css::uno::XInterface >& _rxComponent );
55
63
64 private:
67 };
68
69 //= CloseableComponent
70
78 {
79 private:
83
84 public:
90 CloseableComponent( const css::uno::Reference< css::uno::XInterface >& _rxComponent );
91
103
104 private:
107 };
108
109 //= SharedUNOComponent
110
135 template < class INTERFACE, class COMPONENT = DisposableComponent >
137 {
138 private:
139 typedef COMPONENT Component;
140
141 private:
142 std::shared_ptr<Component> m_xComponent;
143 css::uno::Reference< INTERFACE > m_xTypedComponent;
144
145 public:
147 {
150 };
151
152 public:
154 {
155 }
156
157 explicit SharedUNOComponent( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
158 {
159 reset( _rxComponent, eMode );
160 }
161
162 SharedUNOComponent( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow )
163 {
164 set( _rRef, _queryThrow );
165 }
166
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.
174
177 void reset( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
178
179 inline bool set( const css::uno::BaseReference& _rRef, css::uno::UnoReference_Query _query );
180
181 inline void set( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow );
182
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 );
185
186 INTERFACE* SAL_CALL operator->() const;
187
188 operator const css::uno::Reference< INTERFACE >&() const
189 {
190 return m_xTypedComponent;
191 }
192
193 const css::uno::Reference< INTERFACE >& getTyped() const
194 {
195 return m_xTypedComponent;
196 }
197
198 bool is() const
199 {
200 return m_xTypedComponent.is();
201 }
202
203 void clear()
204 {
205 m_xComponent.reset();
206 m_xTypedComponent.clear();
207 }
208 };
209
210 template < class INTERFACE, class COMPONENT >
212 {
213 return m_xTypedComponent.operator->();
214 }
215
216 // assignments
217 template < class INTERFACE, class COMPONENT >
218 void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
219 {
220 m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : nullptr);
221 m_xTypedComponent = _rxComponent;
222 }
223
224 // comparison operators
225
226 template < class INTERFACE, class COMPONENT >
227 bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const css::uno::Reference< INTERFACE >& _rRHS )
228 {
229 return _rLHS.getTyped() == _rRHS;
230 }
231
232 template < class INTERFACE, class COMPONENT >
233 inline css::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value )
234 {
235 return css::uno::Any( value.getTyped() );
236 }
237
238 template < class INTERFACE, class COMPONENT >
239 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow )
240 {
241 reset( css::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
242 }
243
244 template < class INTERFACE, class COMPONENT >
245 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::Reference< INTERFACE >& _rRef, css::uno::UnoReference_SetThrow _setThrow )
246 {
247 reset( css::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
248 }
249
250 template < class INTERFACE, class COMPONENT >
251 void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, css::uno::UnoReference_SetThrow _setThrow )
252 {
253 *this = _rComp;
254 // provoke an exception in case the component is NULL
255 m_xTypedComponent.set( m_xTypedComponent, _setThrow );
256 }
257
258 template < class INTERFACE, class COMPONENT >
259 bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::BaseReference& _rRef, css::uno::UnoReference_Query _query )
260 {
261 reset( css::uno::Reference< INTERFACE >( _rRef, _query ) );
262 return is();
263 }
264
265} // namespace utl
266
267#endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
268
269/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
is a class which controls lifetime of a UNO component via ->XCloseable::close
CloseableComponent(const CloseableComponent &)=delete
CloseableComponent(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
constructs a ->CloseableComponent instance
::rtl::Reference< CloseableComponentImpl > m_pImpl
Our IMPL class.
CloseableComponent & operator=(const CloseableComponent &)=delete
is a class which controls lifetime of a UNO component via ->XComponent::dispose
DisposableComponent & operator=(const DisposableComponent &)=delete
DisposableComponent(const DisposableComponent &)=delete
DisposableComponent(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
constructs a ->DisposableComponent instance
css::uno::Reference< css::lang::XComponent > m_xComponent
is a helper class for sharing ownership of a UNO component
const css::uno::Reference< INTERFACE > & getTyped() const
css::uno::Reference< INTERFACE > m_xTypedComponent
void reset(const css::uno::Reference< INTERFACE > &_rxComponent, AssignmentMode _eMode=TakeOwnership)
assigns a new component, and releases the old one
INTERFACE *SAL_CALL operator->() const
void set(const css::uno::Reference< INTERFACE > &_rRef, css::uno::UnoReference_SetThrow _setThrow)
SharedUNOComponent(const css::uno::Reference< INTERFACE > &_rxComponent, AssignmentMode eMode=TakeOwnership)
void set(const SharedUNOComponent &_rComp, css::uno::UnoReference_SetThrow _setThrow)
SharedUNOComponent(const css::uno::BaseReference &_rRef, css::uno::UnoReference_QueryThrow _queryThrow)
void set(const css::uno::BaseReference &_rRef, css::uno::UnoReference_QueryThrow _queryThrow)
std::shared_ptr< Component > m_xComponent
bool set(const css::uno::BaseReference &_rRef, css::uno::UnoReference_Query _query)
Any value
Mode eMode
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)
bool operator==(const SharedUNOComponent< INTERFACE, COMPONENT > &_rLHS, const css::uno::Reference< INTERFACE > &_rRHS)
#define UNOTOOLS_DLLPUBLIC