Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/unotools/sharedunocomponent.hxx
Line
Count
Source
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
23
#include <config_options.h>
24
#include <unotools/unotoolsdllapi.h>
25
#include <com/sun/star/uno/Reference.hxx>
26
#include <rtl/ref.hxx>
27
#include <memory>
28
29
namespace com::sun::star {
30
    namespace lang {
31
        class XComponent;
32
    }
33
}
34
35
namespace utl
36
{
37
38
    //= DisposableComponent
39
40
    /** is a class which controls lifetime of a UNO component via ->XComponent::dispose
41
42
        You'll usually never use this class directly, but only as parameter for a
43
        ->SharedUNOComponent class.
44
    */
45
    class UNOTOOLS_DLLPUBLIC DisposableComponent
46
    {
47
        css::uno::Reference< css::lang::XComponent >  m_xComponent;
48
49
    public:
50
        /** constructs a ->DisposableComponent instance
51
52
        @param _rxComponent
53
            the component whose life time should be controlled by the instance. Must not be <NULL/>.
54
        */
55
        DisposableComponent( const css::uno::Reference< css::uno::XInterface >& _rxComponent );
56
57
        /** disposes the component represented by the instance
58
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.
62
        */
63
        ~DisposableComponent();
64
65
    private:
66
        DisposableComponent( const DisposableComponent& ) = delete;
67
        DisposableComponent& operator=( const DisposableComponent& ) = delete;
68
    };
69
70
    //= CloseableComponent
71
72
    class CloseableComponentImpl;
73
    /** is a class which controls lifetime of a UNO component via ->XCloseable::close
74
75
        You'll usually never use this class directly, but only as parameter for a
76
        ->SharedUNOComponent class.
77
    */
78
    class UNLESS_MERGELIBS_MORE(UNOTOOLS_DLLPUBLIC) CloseableComponent
79
    {
80
    private:
81
        /** Our IMPL class.
82
        */
83
        ::rtl::Reference< CloseableComponentImpl >  m_pImpl;
84
85
    public:
86
        /** constructs a ->CloseableComponent instance
87
88
        @param _rxComponent
89
            the component whose life time should be controlled by the instance. Must not be <NULL/>.
90
        */
91
        CloseableComponent( const css::uno::Reference< css::uno::XInterface >& _rxComponent );
92
93
        /** destroys resources associated with this instance, and disposes the component
94
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.
99
100
            Any other failure will be reported in a debug version via assertion mechanisms,
101
            and silenced in release builds.
102
        */
103
        ~CloseableComponent();
104
105
    private:
106
        CloseableComponent( const CloseableComponent& ) = delete;
107
        CloseableComponent& operator=( const CloseableComponent& ) = delete;
108
    };
109
110
    //= SharedUNOComponent
111
112
    /** is a helper class for sharing ownership of a UNO component
113
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.
118
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.
125
126
    @param INTERFACE
127
        the UNO interface type as which the component should be held
128
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.
135
    */
136
    template < class INTERFACE, class COMPONENT = DisposableComponent >
137
    class SharedUNOComponent
138
    {
139
    private:
140
        typedef COMPONENT                           Component;
141
142
    private:
143
        std::shared_ptr<Component>                            m_xComponent;
144
        css::uno::Reference< INTERFACE >            m_xTypedComponent;
145
146
    public:
147
        enum AssignmentMode
148
        {
149
            TakeOwnership,
150
            NoTakeOwnership
151
        };
152
153
    public:
154
        SharedUNOComponent()
155
20.2k
        {
156
20.2k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::SharedUNOComponent()
utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::SharedUNOComponent()
Line
Count
Source
155
10.1k
        {
156
10.1k
        }
utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::SharedUNOComponent()
Line
Count
Source
155
10.1k
        {
156
10.1k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::embed::XStorage, utl::DisposableComponent>::SharedUNOComponent()
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::SharedUNOComponent()
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::SharedUNOComponent()
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::uno::XInterface, utl::DisposableComponent>::SharedUNOComponent()
157
158
        explicit SharedUNOComponent( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
159
20.2k
        {
160
20.2k
            reset( _rxComponent, eMode );
161
20.2k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::frame::XModel, utl::CloseableComponent>::SharedUNOComponent(com::sun::star::uno::Reference<com::sun::star::frame::XModel> const&, utl::SharedUNOComponent<com::sun::star::frame::XModel, utl::CloseableComponent>::AssignmentMode)
utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::SharedUNOComponent(com::sun::star::uno::Reference<com::sun::star::sdbc::XStatement> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::AssignmentMode)
Line
Count
Source
159
10.0k
        {
160
10.0k
            reset( _rxComponent, eMode );
161
10.0k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::SharedUNOComponent(com::sun::star::uno::Reference<com::sun::star::sdb::XSingleSelectQueryComposer> const&, utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::AssignmentMode)
utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::SharedUNOComponent(com::sun::star::uno::Reference<com::sun::star::sdbc::XResultSet> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::AssignmentMode)
Line
Count
Source
159
10.1k
        {
160
10.1k
            reset( _rxComponent, eMode );
161
10.1k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::SharedUNOComponent(com::sun::star::uno::Reference<com::sun::star::sdbc::XConnection> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::AssignmentMode)
162
163
        SharedUNOComponent( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow )
164
0
        {
165
0
            set( _rRef, _queryThrow );
166
0
        }
167
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.
175
176
        /** assigns a new component, and releases the old one
177
        */
178
        void reset( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
179
180
        inline bool set( const css::uno::BaseReference& _rRef, css::uno::UnoReference_Query _query );
181
182
        inline void set( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow );
183
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 );
186
187
        INTERFACE* SAL_CALL operator->() const;
188
189
        operator const css::uno::Reference< INTERFACE >&() const
190
30.3k
        {
191
30.3k
            return m_xTypedComponent;
192
30.3k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::operator com::sun::star::uno::Reference<com::sun::star::sdbc::XConnection> const&() const
utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::operator com::sun::star::uno::Reference<com::sun::star::sdbc::XPreparedStatement> const&() const
Line
Count
Source
190
10.0k
        {
191
10.0k
            return m_xTypedComponent;
192
10.0k
        }
utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::operator com::sun::star::uno::Reference<com::sun::star::sdbc::XStatement> const&() const
Line
Count
Source
190
10.1k
        {
191
10.1k
            return m_xTypedComponent;
192
10.1k
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::frame::XModel, utl::CloseableComponent>::operator com::sun::star::uno::Reference<com::sun::star::frame::XModel> const&() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::operator com::sun::star::uno::Reference<com::sun::star::sdb::XSingleSelectQueryComposer> const&() const
utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::operator com::sun::star::uno::Reference<com::sun::star::sdbc::XResultSet> const&() const
Line
Count
Source
190
10.1k
        {
191
10.1k
            return m_xTypedComponent;
192
10.1k
        }
193
194
        const css::uno::Reference< INTERFACE >& getTyped() const
195
0
        {
196
0
            return m_xTypedComponent;
197
0
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::getTyped() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::embed::XStorage, utl::DisposableComponent>::getTyped() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::getTyped() const
198
199
        bool is() const
200
43
        {
201
43
            return m_xTypedComponent.is();
202
43
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::is() const
utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::is() const
Line
Count
Source
200
43
        {
201
43
            return m_xTypedComponent.is();
202
43
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::frame::XModel, utl::CloseableComponent>::is() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::embed::XStorage, utl::DisposableComponent>::is() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::is() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::is() const
203
204
        void clear()
205
0
        {
206
0
            m_xComponent.reset();
207
0
            m_xTypedComponent.clear();
208
0
        }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::clear()
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::clear()
209
    };
210
211
    template < class INTERFACE, class COMPONENT >
212
    INTERFACE* SAL_CALL SharedUNOComponent< INTERFACE, COMPONENT >::operator->() const
213
10.1k
    {
214
10.1k
        return m_xTypedComponent.operator->();
215
10.1k
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::operator->() const
utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::operator->() const
Line
Count
Source
213
10.1k
    {
214
10.1k
        return m_xTypedComponent.operator->();
215
10.1k
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::beans::XPropertySet, utl::DisposableComponent>::operator->() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::operator->() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::operator->() const
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::operator->() const
216
217
    // assignments
218
    template < class INTERFACE, class COMPONENT >
219
    void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const css::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
220
30.3k
    {
221
30.3k
        m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : nullptr);
222
30.3k
        m_xTypedComponent = _rxComponent;
223
30.3k
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::sdbc::XConnection> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XConnection, utl::DisposableComponent>::AssignmentMode)
utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::sdbc::XPreparedStatement> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::AssignmentMode)
Line
Count
Source
220
10.0k
    {
221
10.0k
        m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : nullptr);
222
10.0k
        m_xTypedComponent = _rxComponent;
223
10.0k
    }
utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::sdbc::XStatement> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::AssignmentMode)
Line
Count
Source
220
10.1k
    {
221
10.1k
        m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : nullptr);
222
10.1k
        m_xTypedComponent = _rxComponent;
223
10.1k
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::frame::XModel, utl::CloseableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::frame::XModel> const&, utl::SharedUNOComponent<com::sun::star::frame::XModel, utl::CloseableComponent>::AssignmentMode)
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::beans::XPropertySet, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet> const&, utl::SharedUNOComponent<com::sun::star::beans::XPropertySet, utl::DisposableComponent>::AssignmentMode)
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::embed::XStorage, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::embed::XStorage> const&, utl::SharedUNOComponent<com::sun::star::embed::XStorage, utl::DisposableComponent>::AssignmentMode)
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::sdb::XSingleSelectQueryComposer> const&, utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::AssignmentMode)
utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::sdbc::XResultSet> const&, utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::AssignmentMode)
Line
Count
Source
220
10.1k
    {
221
10.1k
        m_xComponent.reset(_eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : nullptr);
222
10.1k
        m_xTypedComponent = _rxComponent;
223
10.1k
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::uno::XInterface, utl::DisposableComponent>::reset(com::sun::star::uno::Reference<com::sun::star::uno::XInterface> const&, utl::SharedUNOComponent<com::sun::star::uno::XInterface, utl::DisposableComponent>::AssignmentMode)
224
225
    // comparison operators
226
227
    template < class INTERFACE, class COMPONENT >
228
    bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const css::uno::Reference< INTERFACE >& _rRHS )
229
    {
230
        return _rLHS.getTyped() == _rRHS;
231
    }
232
233
    template < class INTERFACE, class COMPONENT >
234
    inline css::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value )
235
    {
236
        return css::uno::Any( value.getTyped() );
237
    }
238
239
    template < class INTERFACE, class COMPONENT >
240
    void SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::BaseReference & _rRef, css::uno::UnoReference_QueryThrow _queryThrow )
241
10.0k
    {
242
10.0k
        reset( css::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
243
10.0k
    }
utl::SharedUNOComponent<com::sun::star::sdbc::XPreparedStatement, utl::DisposableComponent>::set(com::sun::star::uno::BaseReference const&, com::sun::star::uno::UnoReference_QueryThrow)
Line
Count
Source
241
10.0k
    {
242
10.0k
        reset( css::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
243
10.0k
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::beans::XPropertySet, utl::DisposableComponent>::set(com::sun::star::uno::BaseReference const&, com::sun::star::uno::UnoReference_QueryThrow)
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdb::XSingleSelectQueryComposer, utl::DisposableComponent>::set(com::sun::star::uno::BaseReference const&, com::sun::star::uno::UnoReference_QueryThrow)
244
245
    template < class INTERFACE, class COMPONENT >
246
    void SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::Reference< INTERFACE >& _rRef, css::uno::UnoReference_SetThrow _setThrow )
247
0
    {
248
0
        reset( css::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
249
0
    }
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XStatement, utl::DisposableComponent>::set(com::sun::star::uno::Reference<com::sun::star::sdbc::XStatement> const&, com::sun::star::uno::UnoReference_SetThrow)
Unexecuted instantiation: utl::SharedUNOComponent<com::sun::star::sdbc::XResultSet, utl::DisposableComponent>::set(com::sun::star::uno::Reference<com::sun::star::sdbc::XResultSet> const&, com::sun::star::uno::UnoReference_SetThrow)
250
251
    template < class INTERFACE, class COMPONENT >
252
    void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, css::uno::UnoReference_SetThrow _setThrow )
253
    {
254
        *this = _rComp;
255
        // provoke an exception in case the component is NULL
256
        m_xTypedComponent.set( m_xTypedComponent, _setThrow );
257
    }
258
259
    template < class INTERFACE, class COMPONENT >
260
    bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const css::uno::BaseReference& _rRef, css::uno::UnoReference_Query _query )
261
    {
262
        reset( css::uno::Reference< INTERFACE >( _rRef, _query ) );
263
        return is();
264
    }
265
266
}   // namespace utl
267
268
#endif // INCLUDED_UNOTOOLS_SHAREDUNOCOMPONENT_HXX
269
270
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */