Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / comphelper / componentbase.hxx
blobb5cce3d172929de6c78bc96af9eff2aecfd6207f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 namespace comphelper
31 //= ComponentBase
33 class COMPHELPER_DLLPUBLIC ComponentBase
35 protected:
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 )
61 ~ComponentBase() {}
63 /** marks the instance as initialized
65 Subsequent instantiations of a ComponentMethodGuard won't throw the NotInitializedException now.
67 inline void setInitialized() { m_bInitialized = true; }
69 public:
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 inline ::osl::Mutex& getMutex( GuardAccess ) { return getMutex(); }
75 /// checks whether the component is already disposed, throws a DisposedException if so.
76 inline void checkDisposed( GuardAccess ) const { impl_checkDisposed_throw(); }
77 /// checks whether the component is already initialized, throws a NotInitializedException if not.
78 inline void checkInitialized( GuardAccess ) const { impl_checkInitialized_throw(); }
80 protected:
81 /// retrieves the component's broadcast helper
82 inline ::cppu::OBroadcastHelper& getBroadcastHelper() { return m_rBHelper; }
83 /// retrieves the component's mutex
84 inline ::osl::Mutex& getMutex() { return m_rBHelper.rMutex; }
85 /// determines whether the instance is already disposed
86 inline bool impl_isDisposed() const { return m_rBHelper.bDisposed; }
88 /// checks whether the component is already disposed. Throws a DisposedException if so.
89 void impl_checkDisposed_throw() const;
91 /// checks whether the component is already initialized. Throws a NotInitializedException if not.
92 void impl_checkInitialized_throw() const;
94 /// determines whether the component is already initialized
95 inline bool
96 impl_isInitialized_nothrow() const { return m_bInitialized; }
98 /** returns the context to be used when throwing exceptions
100 The default implementation returns <NULL/>.
102 static css::uno::Reference< css::uno::XInterface >
103 getComponent();
105 private:
106 ::cppu::OBroadcastHelper& m_rBHelper;
107 bool m_bInitialized;
110 class ComponentMethodGuard
112 public:
113 enum class MethodType
115 /// allow the method to be called only when being initialized and not being disposed
116 Default,
117 /// allow the method to be called without being initialized
118 WithoutInit
122 ComponentMethodGuard( ComponentBase& _rComponent, const MethodType _eType = MethodType::Default )
123 :m_aMutexGuard( _rComponent.getMutex( ComponentBase::GuardAccess() ) )
125 if ( _eType != MethodType::WithoutInit )
126 _rComponent.checkInitialized( ComponentBase::GuardAccess() );
127 _rComponent.checkDisposed( ComponentBase::GuardAccess() );
130 ~ComponentMethodGuard()
134 inline void clear()
136 m_aMutexGuard.clear();
139 private:
140 ::osl::ResettableMutexGuard m_aMutexGuard;
144 } // namespace ComponentBase
147 #endif // INCLUDED_COMPHELPER_COMPONENTBASE_HXX
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */