Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/comphelper/componentbase.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_COMPHELPER_COMPONENTBASE_HXX
21
#define INCLUDED_COMPHELPER_COMPONENTBASE_HXX
22
23
#include <comphelper/comphelperdllapi.h>
24
#include <cppuhelper/interfacecontainer.h>
25
26
27
namespace comphelper
28
{
29
30
31
    //= ComponentBase
32
33
    class COMPHELPER_DLLPUBLIC ComponentBase
34
    {
35
    protected:
36
        /** creates a ComponentBase instance
37
38
            The instance is not initialized. As a consequence, every ComponentMethodGuard instantiated for
39
            this component will throw a css::lang::NotInitializedException,
40
            until ->setInitialized() is called.
41
        */
42
        ComponentBase( ::cppu::OBroadcastHelper& _rBHelper )
43
0
            :m_rBHelper( _rBHelper )
44
0
            ,m_bInitialized( false )
45
0
        {
46
0
        }
47
48
        struct NoInitializationNeeded { };
49
50
        /** creates a ComponentBase instance
51
52
            The instance is already initialized, so there's no need to call setInitialized later on. Use this
53
            constructor for component implementations which do not require explicit initialization.
54
        */
55
        ComponentBase( ::cppu::OBroadcastHelper& _rBHelper, NoInitializationNeeded )
56
0
            :m_rBHelper( _rBHelper )
57
0
            ,m_bInitialized( true )
58
0
        {
59
0
        }
60
61
0
        ~ComponentBase() COVERITY_NOEXCEPT_FALSE {}
62
63
        /** marks the instance as initialized
64
65
            Subsequent instantiations of a ComponentMethodGuard won't throw the NotInitializedException now.
66
        */
67
0
        void setInitialized()    { m_bInitialized = true; }
68
69
    public:
70
        /// helper struct to grant access to selected public methods to the ComponentMethodGuard class
71
0
        struct GuardAccess { friend class ComponentMethodGuard; private: GuardAccess() { } };
72
73
        /// retrieves the component's mutex
74
0
        ::osl::Mutex&   getMutex( GuardAccess )                 { return getMutex(); }
75
        /// checks whether the component is already disposed, throws a DisposedException if so.
76
        void                    checkDisposed( GuardAccess ) const;
77
        /// checks whether the component is already initialized, throws a NotInitializedException if not.
78
        void                    checkInitialized( GuardAccess ) const;
79
80
    protected:
81
        /// retrieves the component's broadcast helper
82
0
        ::cppu::OBroadcastHelper&   getBroadcastHelper()    { return m_rBHelper; }
83
        /// retrieves the component's mutex
84
0
        ::osl::Mutex&               getMutex()              { return m_rBHelper.rMutex; }
85
        /// determines whether the instance is already disposed
86
0
        bool                        impl_isDisposed() const { return m_rBHelper.bDisposed; }
87
88
        /// determines whether the component is already initialized
89
        bool
90
0
                impl_isInitialized_nothrow() const { return m_bInitialized; }
91
92
        /** returns the context to be used when throwing exceptions
93
94
            The default implementation returns <NULL/>.
95
        */
96
        static css::uno::Reference< css::uno::XInterface >
97
                getComponent();
98
99
    private:
100
        ::cppu::OBroadcastHelper&   m_rBHelper;
101
        bool                        m_bInitialized;
102
    };
103
104
    class ComponentMethodGuard
105
    {
106
    public:
107
        enum class MethodType
108
        {
109
            /// allow the method to be called only when being initialized and not being disposed
110
            Default,
111
            /// allow the method to be called without being initialized
112
            WithoutInit
113
114
        };
115
116
        ComponentMethodGuard( ComponentBase& _rComponent, const MethodType _eType = MethodType::Default )
117
0
            :m_aMutexGuard( _rComponent.getMutex( ComponentBase::GuardAccess() ) )
118
0
        {
119
0
            if ( _eType != MethodType::WithoutInit )
120
0
                _rComponent.checkInitialized( ComponentBase::GuardAccess() );
121
0
            _rComponent.checkDisposed( ComponentBase::GuardAccess() );
122
0
        }
123
124
        void clear()
125
0
        {
126
0
            m_aMutexGuard.clear();
127
0
        }
128
129
    private:
130
        osl::ClearableMutexGuard m_aMutexGuard;
131
    };
132
133
134
} // namespace ComponentBase
135
136
137
#endif // INCLUDED_COMPHELPER_COMPONENTBASE_HXX
138
139
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */