1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
#define INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
#include <unotools/unotoolsdllapi.h>
#include <boost/shared_ptr.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <rtl/ref.hxx>
namespace com { namespace sun { namespace star {
namespace lang {
class XComponent;
}
} } }
namespace utl
{
//= DisposableComponent
/** is a class which controls lifetime of an UNO component via ->XComponent::dispose
You'll usually never use this class directly, but only as parameter for a
->SharedUNOComponent class.
*/
class UNOTOOLS_DLLPUBLIC DisposableComponent
{
::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
m_xComponent;
public:
/** constructs a ->DisposableComponent instance
@param _rxComponent
the component whose life time should be controlled by the instance. Must not be <NULL/>.
*/
DisposableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
/** disposes the component represented by the instance
The component is queried for ->XComponent(which <em>must</em> be supported),
and ->XComponent::dispose is invoked. A failure of this invocation (e.g. a thrown
exception) is silenced in release builds, and reported in debug builds.
*/
~DisposableComponent();
private:
DisposableComponent(); // never implemented
DisposableComponent( const DisposableComponent& ); // never implemented
DisposableComponent& operator=( const DisposableComponent& ); // never implemented
};
//= CloseableComponent
class CloseableComponentImpl;
/** is a class which controls lifetime of an UNO component via ->XCloseable::close
You'll usually never use this class directly, but only as parameter for a
->SharedUNOComponent class.
*/
class UNOTOOLS_DLLPUBLIC CloseableComponent
{
private:
/** Our IMPL class.
*/
::rtl::Reference< CloseableComponentImpl > m_pImpl;
public:
/** constructs a ->CloseableComponent instance
@param _rxComponent
the component whose life time should be controlled by the instance. Must not be <NULL/>.
*/
CloseableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
/** destroys resources associated with this instance, and disposes the component
The component is queried for ->XCloseable (which <em>must</em> be supported),
and ->XCloseable::close is invoked, with delivering the ownership.
If the invocation fails with a ->CloseVetoException, this is ignored, since in
this case the vetoing instance took the ownership.
Any other failure will be reported in a debug version via assertion mechanisms,
and silenced in release builds.
*/
~CloseableComponent();
private:
CloseableComponent(); // never implemented
CloseableComponent( const CloseableComponent& ); // never implemented
CloseableComponent& operator=( const CloseableComponent& ); // never implemented
};
//= SharedUNOComponent
/** is a helper class for sharing ownership of a UNO component
If you need to share an UNO component, which normally needs a dedicated owner,
and is lifetime controlled by an explicit disposal action (not necessarily ->XComponent::dispose,
but <em>any</em> explicit method call, after which the object is considered
to be disposed), between different classes, ->SharedUNOComponent is what you need.
Instead of passing around a <code>Reference< XFoo ></code>, and bothering
with ownership and disposal, you just use a <code>SharedUNOComponent< XFoo ></code>.
This instance can be passed around, including copying, and in nearly all respects behaves
like the original <code>Reference< XFoo ></code>. However, when the last
->SharedUNOComponent referencing a certain <code>Reference< XFoo ></code> dies, it
will automatically get rid of the object held by this reference.
@param INTERFACE
the UNO interface type as which the component should be held
@param COMPONENT_HOLDER
a class which can be used to represent and dispose a UNO component.
The class must support (maybe explicit only) construction from a
<code>Reference< INTERFACE ></code>, and destruction. Upon destruction,
the class must dispose (by any suitable means) the component instance it was
constructed with.
*/
template < class INTERFACE, class COMPONENT = DisposableComponent >
class SharedUNOComponent
{
private:
typedef COMPONENT Component;
typedef ::boost::shared_ptr< Component > ComponentPointer;
private:
ComponentPointer m_pComponent;
::com::sun::star::uno::Reference< INTERFACE > m_xTypedComponent;
public:
enum AssignmentMode
{
TakeOwnership,
NoTakeOwnership
};
public:
inline SharedUNOComponent()
{
}
explicit inline SharedUNOComponent( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
{
reset( _rxComponent, eMode );
}
inline SharedUNOComponent( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
{
set( _pInterface, _queryThrow );
}
inline SharedUNOComponent( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
{
set( _rRef, _queryThrow );
}
inline SharedUNOComponent( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
{
set( _rAny, _queryThrow );
}
inline SharedUNOComponent( const SharedUNOComponent& _rxComponent, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
{
set( _rxComponent, _setThrow );
}
// SharedUNOComponent& operator=( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent );
// This operator is intentionally not implemented. There is no canonic ownership after this operator
// would have been applied: Should the SharedUNOComponent have the ownership of the component,
// or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
// instead of tempting clients of this class to use such a dangerous operator, we do
// not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
// use the ->reset method.
/** assigns a new component, and releases the old one
*/
void reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
inline bool set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query );
inline bool set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query );
inline bool set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query );
inline void set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
inline void set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
inline void set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
inline void set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
inline void set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
inline void set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
INTERFACE* SAL_CALL operator->() const;
inline operator const ::com::sun::star::uno::Reference< INTERFACE >&() const
{
return m_xTypedComponent;
}
inline const ::com::sun::star::uno::Reference< INTERFACE >& getTyped() const
{
return m_xTypedComponent;
}
inline bool is() const
{
return m_xTypedComponent.is();
}
inline void clear()
{
m_pComponent.reset();
m_xTypedComponent.clear();
}
};
template < class INTERFACE, class COMPONENT >
INTERFACE* SAL_CALL SharedUNOComponent< INTERFACE, COMPONENT >::operator->() const
{
return m_xTypedComponent.operator->();
}
// assignments
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
{
m_pComponent.reset( _eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : NULL );
m_xTypedComponent = _rxComponent;
}
// comparison operators
template < class INTERFACE, class COMPONENT >
bool operator==( const ::com::sun::star::uno::Reference< INTERFACE >& _rLHS, const SharedUNOComponent< INTERFACE, COMPONENT >& _rRHS )
{
return _rLHS == _rRHS.getTyped();
}
template < class INTERFACE, class COMPONENT >
bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const ::com::sun::star::uno::Reference< INTERFACE >& _rRHS )
{
return _rLHS.getTyped() == _rRHS;
}
// conversion to Any
template < class INTERFACE, class COMPONENT >
inline void SAL_CALL operator <<= ( ::com::sun::star::uno::Any & rAny, const SharedUNOComponent< INTERFACE, COMPONENT >& value ) SAL_THROW(())
{
rAny <<= value.getTyped();
}
template < class INTERFACE, class COMPONENT >
inline ::com::sun::star::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value ) SAL_THROW(())
{
return makeAny( value.getTyped() );
}
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _queryThrow ), TakeOwnership );
}
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
}
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _queryThrow ), TakeOwnership );
}
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _setThrow ), TakeOwnership );
}
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
}
template < class INTERFACE, class COMPONENT >
void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
{
*this = _rComp;
// provoke an exception in case the component is NULL
m_xTypedComponent.set( m_xTypedComponent, _setThrow );
}
template < class INTERFACE, class COMPONENT >
bool SharedUNOComponent< INTERFACE, COMPONENT >::set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _query ) );
return is();
}
template < class INTERFACE, class COMPONENT >
bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _query ) );
return is();
}
template < class INTERFACE, class COMPONENT >
bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query )
{
reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _query ) );
return is();
}
} // namespace utl
#endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|