LibreOffice
LibreOffice 5.1 SDK C/C++ API Reference
singletonref.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_SALHELPER_SINGLETONREF_HXX
21 #define INCLUDED_SALHELPER_SINGLETONREF_HXX
22 
23 #include <sal/config.h>
24 
25 #include <cstddef>
26 
27 #include <osl/mutex.hxx>
28 #include <rtl/instance.hxx>
29 #include <osl/diagnose.h>
30 #include <osl/getglobalmutex.hxx>
31 
32 
33 namespace salhelper{
34 
35 
68 template< class SingletonClass >
70 {
71 
72  // member
73 
74  private:
75 
77  static SingletonClass* m_pInstance;
78 
80  static sal_Int32 m_nRef;
81 
82 
83  // interface
84 
85  public:
86 
87 
88 
97  {
98  // GLOBAL SAFE ->
99  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
100 
101  // must be increased before(!) the check is done.
102  // Otherwise this check can fail inside the same thread ...
103  ++m_nRef;
104  if (m_nRef == 1)
105  m_pInstance = new SingletonClass();
106 
107  OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
108  // <- GLOBAL SAFE
109  }
110 
111 
112 
121  {
122  // GLOBAL SAFE ->
123  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
124 
125  // must be decreased before(!) the check is done.
126  // Otherwise this check can fail inside the same thread ...
127  --m_nRef;
128  if (m_nRef == 0)
129  {
130  delete m_pInstance;
131  m_pInstance = 0;
132  }
133  // <- GLOBAL SAFE
134  }
135 
136 
137 
140  SingletonClass* operator->() const
141  {
142  // GLOBAL SAFE ->
143  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
144  return m_pInstance;
145  // <- GLOBAL SAFE
146  }
147 
148 
149 
152  SingletonClass& operator*() const
153  {
154  // GLOBAL SAFE ->
155  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
156  return *m_pInstance;
157  // <- GLOBAL SAFE
158  }
159 
160 
161  // helper
162 
163  private:
164 
165 
166 
173  struct SingletonLockInit
174  {
175  ::osl::Mutex* operator()()
176  {
177  static ::osl::Mutex aInstance;
178  return &aInstance;
179  }
180  };
181 
182  ::osl::Mutex& ownStaticLock() const
183  {
184  return *rtl_Instance< ::osl::Mutex,
185  SingletonLockInit,
187  ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
188  }
189 };
190 
191 template< class SingletonClass >
192 SingletonClass* SingletonRef< SingletonClass >::m_pInstance = NULL;
193 
194 template< class SingletonClass >
196 
197 } // namespace salhelper
198 
199 #endif // INCLUDED_SALHELPER_SINGLETONREF_HXX
200 
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:31
Definition: condition.hxx:29
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
SingletonRef()
standard ctor.
Definition: singletonref.hxx:96
template for implementing singleton classes.
Definition: singletonref.hxx:69
Guard< Mutex > MutexGuard
Definition: mutex.hxx:224
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
Definition: singletonref.hxx:152
~SingletonRef()
standard dtor.
Definition: singletonref.hxx:120
#define OSL_ENSURE(c, m)
Definition: diagnose.h:101
SingletonClass * operator->() const
Allows rSingle->someBodyOp().
Definition: singletonref.hxx:140
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:108