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 */ ComponentBase(::cppu::OBroadcastHelper & _rBHelper)42 ComponentBase( ::cppu::OBroadcastHelper& _rBHelper ) 43 :m_rBHelper( _rBHelper ) 44 ,m_bInitialized( false ) 45 { 46 } 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 */ ComponentBase(::cppu::OBroadcastHelper & _rBHelper,NoInitializationNeeded)55 ComponentBase( ::cppu::OBroadcastHelper& _rBHelper, NoInitializationNeeded ) 56 :m_rBHelper( _rBHelper ) 57 ,m_bInitialized( true ) 58 { 59 } 60 ~ComponentBase()61 ~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 */ setInitialized()67 void setInitialized() { m_bInitialized = true; } 68 69 public: 70 /// helper struct to grant access to selected public methods to the ComponentMethodGuard class GuardAccesscomphelper::ComponentBase::GuardAccess71 struct GuardAccess { friend class ComponentMethodGuard; private: GuardAccess() { } }; 72 73 /// retrieves the component's mutex getMutex(GuardAccess)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; 79 80 protected: 81 /// retrieves the component's broadcast helper getBroadcastHelper()82 ::cppu::OBroadcastHelper& getBroadcastHelper() { return m_rBHelper; } 83 /// retrieves the component's mutex getMutex()84 ::osl::Mutex& getMutex() { return m_rBHelper.rMutex; } 85 /// determines whether the instance is already disposed impl_isDisposed() const86 bool impl_isDisposed() const { return m_rBHelper.bDisposed; } 87 88 /// determines whether the component is already initialized 89 bool impl_isInitialized_nothrow() const90 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 ComponentMethodGuard(ComponentBase & _rComponent,const MethodType _eType=MethodType::Default)116 ComponentMethodGuard( ComponentBase& _rComponent, const MethodType _eType = MethodType::Default ) 117 :m_aMutexGuard( _rComponent.getMutex( ComponentBase::GuardAccess() ) ) 118 { 119 if ( _eType != MethodType::WithoutInit ) 120 _rComponent.checkInitialized( ComponentBase::GuardAccess() ); 121 _rComponent.checkDisposed( ComponentBase::GuardAccess() ); 122 } 123 clear()124 void clear() 125 { 126 m_aMutexGuard.clear(); 127 } 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: */ 140