LibreOffice Module comphelper (master)  1
componentbase.hxx
Go to the documentation of this file.
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 
25 
26 
27 namespace comphelper
28 {
29 
30 
31  //= ComponentBase
32 
34  {
35  protected:
43  :m_rBHelper( _rBHelper )
44  ,m_bInitialized( false )
45  {
46  }
47 
49 
56  :m_rBHelper( _rBHelper )
57  ,m_bInitialized( true )
58  {
59  }
60 
61  ~ComponentBase() COVERITY_NOEXCEPT_FALSE {}
62 
67  void setInitialized() { m_bInitialized = true; }
68 
69  public:
71  struct GuardAccess { friend class ComponentMethodGuard; private: GuardAccess() { } };
72 
74  ::osl::Mutex& getMutex( GuardAccess ) { return getMutex(); }
76  void checkDisposed( GuardAccess ) const;
78  void checkInitialized( GuardAccess ) const;
79 
80  protected:
82  ::cppu::OBroadcastHelper& getBroadcastHelper() { return m_rBHelper; }
84  ::osl::Mutex& getMutex() { return m_rBHelper.rMutex; }
86  bool impl_isDisposed() const { return m_rBHelper.bDisposed; }
87 
89  bool
90  impl_isInitialized_nothrow() const { return m_bInitialized; }
91 
96  static css::uno::Reference< css::uno::XInterface >
97  getComponent();
98 
99  private:
102  };
103 
105  {
106  public:
107  enum class MethodType
108  {
110  Default,
113 
114  };
115 
117  :m_aMutexGuard( _rComponent.getMutex( ComponentBase::GuardAccess() ) )
118  {
119  if ( _eType != MethodType::WithoutInit )
121  _rComponent.checkDisposed( ComponentBase::GuardAccess() );
122  }
123 
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: */
::cppu::OBroadcastHelper & getBroadcastHelper()
retrieves the component's broadcast helper
osl::ClearableMutexGuard m_aMutexGuard
::cppu::OBroadcastHelper & m_rBHelper
::osl::Mutex & getMutex(GuardAccess)
retrieves the component's mutex
void checkDisposed(GuardAccess) const
checks whether the component is already disposed, throws a DisposedException if so.
allow the method to be called only when being initialized and not being disposed
ComponentBase(::cppu::OBroadcastHelper &_rBHelper, NoInitializationNeeded)
creates a ComponentBase instance
bool impl_isDisposed() const
determines whether the instance is already disposed
ComponentBase(::cppu::OBroadcastHelper &_rBHelper)
creates a ComponentBase instance
allow the method to be called without being initialized
void setInitialized()
marks the instance as initialized
helper struct to grant access to selected public methods to the ComponentMethodGuard class ...
ComponentMethodGuard(ComponentBase &_rComponent, const MethodType _eType=MethodType::Default)
void checkDisposed(bool _bThrow)
void checkInitialized(GuardAccess) const
checks whether the component is already initialized, throws a NotInitializedException if not...
::osl::Mutex & getMutex()
retrieves the component's mutex
~ComponentBase() COVERITY_NOEXCEPT_FALSE
#define COMPHELPER_DLLPUBLIC
bool impl_isInitialized_nothrow() const
determines whether the component is already initialized
::osl::Mutex & getMutex()