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>
34 class COMPHELPER_DLLPUBLIC ComponentBase
37 /** creates a ComponentBase instance
39 The instance is not initialized. As a consequence, every ComponentMethodGuard instantiated for
40 this component will throw a com::sun::star::lang::NotInitializedException,
41 until ->setInitialized() is called.
43 ComponentBase( ::cppu::OBroadcastHelper
& _rBHelper
)
44 :m_rBHelper( _rBHelper
)
45 ,m_bInitialized( false )
49 struct NoInitializationNeeded
{ };
51 /** creates a ComponentBase instance
53 The instance is already initialized, so there's no need to call setInitialized later on. Use this
54 constructor for component implementations which do not require explicit initialization.
56 ComponentBase( ::cppu::OBroadcastHelper
& _rBHelper
, NoInitializationNeeded
)
57 :m_rBHelper( _rBHelper
)
58 ,m_bInitialized( true )
64 /** marks the instance as initialized
66 Subsequent instantiations of a ComponentMethodGuard won't throw the NotInitializedException now.
68 inline void setInitialized() { m_bInitialized
= true; }
71 /// helper struct to grant access to selected public methods to the ComponentMethodGuard class
72 struct GuardAccess
{ friend class ComponentMethodGuard
; private: GuardAccess() { } };
74 /// retrieves the component's mutex
75 inline ::osl::Mutex
& getMutex( GuardAccess
) { return getMutex(); }
76 /// checks whether the component is already disposed, throws a DisposedException if so.
77 inline void checkDisposed( GuardAccess
) const { impl_checkDisposed_throw(); }
78 /// checks whether the component is already initialized, throws a NotInitializedException if not.
79 inline void checkInitialized( GuardAccess
) const { impl_checkInitialized_throw(); }
82 /// retrieves the component's broadcast helper
83 inline ::cppu::OBroadcastHelper
& getBroadcastHelper() { return m_rBHelper
; }
84 /// retrieves the component's mutex
85 inline ::osl::Mutex
& getMutex() { return m_rBHelper
.rMutex
; }
86 /// determines whether the instance is already disposed
87 inline bool impl_isDisposed() const { return m_rBHelper
.bDisposed
; }
89 /// checks whether the component is already disposed. Throws a DisposedException if so.
90 void impl_checkDisposed_throw() const;
92 /// checks whether the component is already initialized. Throws a NotInitializedException if not.
93 void impl_checkInitialized_throw() const;
95 /// determines whether the component is already initialized
97 impl_isInitialized_nothrow() const { return m_bInitialized
; }
99 /** returns the context to be used when throwing exceptions
101 The default implementation returns <NULL/>.
103 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
107 ::cppu::OBroadcastHelper
& m_rBHelper
;
111 class ComponentMethodGuard
116 /// allow the method to be called only when being initialized and not being disposed
118 /// allow the method to be called without being initialized
123 ComponentMethodGuard( ComponentBase
& _rComponent
, const MethodType _eType
= Default
)
124 :m_aMutexGuard( _rComponent
.getMutex( ComponentBase::GuardAccess() ) )
126 if ( _eType
!= WithoutInit
)
127 _rComponent
.checkInitialized( ComponentBase::GuardAccess() );
128 _rComponent
.checkDisposed( ComponentBase::GuardAccess() );
131 ~ComponentMethodGuard()
137 m_aMutexGuard
.clear();
141 m_aMutexGuard
.reset();
145 ::osl::ResettableMutexGuard m_aMutexGuard
;
149 } // namespace ComponentBase
152 #endif // INCLUDED_COMPHELPER_COMPONENTBASE_HXX
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */