Line data Source code
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.hxx>
25 :
26 :
27 : namespace comphelper
28 : {
29 :
30 :
31 :
32 : //= ComponentBase
33 :
34 : class COMPHELPER_DLLPUBLIC ComponentBase
35 : {
36 : protected:
37 : /** creates a ComponentBase instance
38 :
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.
42 : */
43 34 : ComponentBase( ::cppu::OBroadcastHelper& _rBHelper )
44 : :m_rBHelper( _rBHelper )
45 34 : ,m_bInitialized( false )
46 : {
47 34 : }
48 :
49 : struct NoInitializationNeeded { };
50 :
51 : /** creates a ComponentBase instance
52 :
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.
55 : */
56 6 : ComponentBase( ::cppu::OBroadcastHelper& _rBHelper, NoInitializationNeeded )
57 : :m_rBHelper( _rBHelper )
58 6 : ,m_bInitialized( true )
59 : {
60 6 : }
61 :
62 40 : ~ComponentBase() {}
63 :
64 : /** marks the instance as initialized
65 :
66 : Subsequent instantiations of a ComponentMethodGuard won't throw the NotInitializedException now.
67 : */
68 34 : inline void setInitialized() { m_bInitialized = true; }
69 :
70 : public:
71 : /// helper struct to grant access to selected public methods to the ComponentMethodGuard class
72 1640 : struct GuardAccess { friend class ComponentMethodGuard; private: GuardAccess() { } };
73 :
74 : /// retrieves the component's mutex
75 558 : inline ::osl::Mutex& getMutex( GuardAccess ) { return getMutex(); }
76 : /// checks whether the component is already disposed, throws a DisposedException if so.
77 558 : inline void checkDisposed( GuardAccess ) const { impl_checkDisposed_throw(); }
78 : /// checks whether the component is already initialized, throws a NotInitializedException if not.
79 524 : inline void checkInitialized( GuardAccess ) const { impl_checkInitialized_throw(); }
80 :
81 : protected:
82 : /// retrieves the component's broadcast helper
83 6 : inline ::cppu::OBroadcastHelper& getBroadcastHelper() { return m_rBHelper; }
84 : /// retrieves the component's mutex
85 564 : inline ::osl::Mutex& getMutex() { return m_rBHelper.rMutex; }
86 : /// determines whether the instance is already disposed
87 34 : inline bool impl_isDisposed() const { return m_rBHelper.bDisposed; }
88 :
89 : /// checks whether the component is already disposed. Throws a DisposedException if so.
90 : void impl_checkDisposed_throw() const;
91 :
92 : /// checks whether the component is already initialized. Throws a NotInitializedException if not.
93 : void impl_checkInitialized_throw() const;
94 :
95 : /// determines whether the component is already initialized
96 : inline bool
97 34 : impl_isInitialized_nothrow() const { return m_bInitialized; }
98 :
99 : /** returns the context to be used when throwing exceptions
100 :
101 : The default implementation returns <NULL/>.
102 : */
103 : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
104 : getComponent();
105 :
106 : private:
107 : ::cppu::OBroadcastHelper& m_rBHelper;
108 : bool m_bInitialized;
109 : };
110 :
111 : class ComponentMethodGuard
112 : {
113 : public:
114 : enum MethodType
115 : {
116 : /// allow the method to be called only when being initialized and not being disposed
117 : Default,
118 : /// allow the method to be called without being initialized
119 : WithoutInit
120 :
121 : };
122 :
123 558 : ComponentMethodGuard( ComponentBase& _rComponent, const MethodType _eType = Default )
124 558 : :m_aMutexGuard( _rComponent.getMutex( ComponentBase::GuardAccess() ) )
125 : {
126 558 : if ( _eType != WithoutInit )
127 524 : _rComponent.checkInitialized( ComponentBase::GuardAccess() );
128 558 : _rComponent.checkDisposed( ComponentBase::GuardAccess() );
129 558 : }
130 :
131 558 : ~ComponentMethodGuard()
132 558 : {
133 558 : }
134 :
135 0 : inline void clear()
136 : {
137 0 : m_aMutexGuard.clear();
138 0 : }
139 : inline void reset()
140 : {
141 : m_aMutexGuard.reset();
142 : }
143 :
144 : private:
145 : ::osl::ResettableMutexGuard m_aMutexGuard;
146 : };
147 :
148 :
149 : } // namespace ComponentBase
150 :
151 :
152 : #endif // INCLUDED_COMPHELPER_COMPONENTBASE_HXX
153 :
154 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|