1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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_COMPHELPER_COMPONENTBASE_HXX
21 #define INCLUDED_COMPHELPER_COMPONENTBASE_HXX
23 #include <comphelper/comphelperdllapi.h>
24 #include <cppuhelper/interfacecontainer.hxx>
33 class COMPHELPER_DLLPUBLIC ComponentBase
36 /** creates a ComponentBase instance
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.
42 ComponentBase( ::cppu::OBroadcastHelper
& _rBHelper
)
43 :m_rBHelper( _rBHelper
)
44 ,m_bInitialized( false )
48 struct NoInitializationNeeded
{ };
50 /** creates a ComponentBase instance
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.
55 ComponentBase( ::cppu::OBroadcastHelper
& _rBHelper
, NoInitializationNeeded
)
56 :m_rBHelper( _rBHelper
)
57 ,m_bInitialized( true )
63 /** marks the instance as initialized
65 Subsequent instantiations of a ComponentMethodGuard won't throw the NotInitializedException now.
67 void setInitialized() { m_bInitialized
= true; }
70 /// helper struct to grant access to selected public methods to the ComponentMethodGuard class
71 struct GuardAccess
{ friend class ComponentMethodGuard
; private: GuardAccess() { } };
73 /// retrieves the component's mutex
74 ::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;
81 /// retrieves the component's broadcast helper
82 ::cppu::OBroadcastHelper
& getBroadcastHelper() { return m_rBHelper
; }
83 /// retrieves the component's mutex
84 ::osl::Mutex
& getMutex() { return m_rBHelper
.rMutex
; }
85 /// determines whether the instance is already disposed
86 bool impl_isDisposed() const { return m_rBHelper
.bDisposed
; }
88 /// determines whether the component is already initialized
90 impl_isInitialized_nothrow() const { return m_bInitialized
; }
92 /** returns the context to be used when throwing exceptions
94 The default implementation returns <NULL/>.
96 static css::uno::Reference
< css::uno::XInterface
>
100 ::cppu::OBroadcastHelper
& m_rBHelper
;
104 class ComponentMethodGuard
107 enum class MethodType
109 /// allow the method to be called only when being initialized and not being disposed
111 /// allow the method to be called without being initialized
116 ComponentMethodGuard( ComponentBase
& _rComponent
, const MethodType _eType
= MethodType::Default
)
117 :m_aMutexGuard( _rComponent
.getMutex( ComponentBase::GuardAccess() ) )
119 if ( _eType
!= MethodType::WithoutInit
)
120 _rComponent
.checkInitialized( ComponentBase::GuardAccess() );
121 _rComponent
.checkDisposed( ComponentBase::GuardAccess() );
126 m_aMutexGuard
.clear();
130 ::osl::ResettableMutexGuard m_aMutexGuard
;
134 } // namespace ComponentBase
137 #endif // INCLUDED_COMPHELPER_COMPONENTBASE_HXX
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */