LibreOffice
LibreOffice 4.2 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <osl/mutex.hxx>
24 #include <rtl/instance.hxx>
25 #include <osl/diagnose.h>
26 #include <osl/getglobalmutex.hxx>
27 
28 
29 namespace salhelper{
30 
31 
64 template< class SingletonClass >
66 {
67  //-------------------------------------------
68  // member
69 
70  private :
71 
73  static SingletonClass* m_pInstance;
74 
76  static sal_Int32 m_nRef;
77 
78  //-------------------------------------------
79  // interface
80 
81  public :
82 
83  //---------------------------------------
84 
93  {
94  // GLOBAL SAFE ->
95  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
96 
97  // must be increased before(!) the check is done.
98  // Otherwise this check can fail inside the same thread ...
99  ++m_nRef;
100  if (m_nRef == 1)
101  m_pInstance = new SingletonClass();
102 
103  OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
104  // <- GLOBAL SAFE
105  }
106 
107  //---------------------------------------
108 
117  {
118  // GLOBAL SAFE ->
119  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
120 
121  // must be decreased before(!) the check is done.
122  // Otherwise this check can fail inside the same thread ...
123  --m_nRef;
124  if (m_nRef == 0)
125  {
126  delete m_pInstance;
127  m_pInstance = 0;
128  }
129  // <- GLOBAL SAFE
130  }
131 
132  //---------------------------------------
133 
136  SingletonClass* operator->() const
137  {
138  // GLOBAL SAFE ->
139  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
140  return m_pInstance;
141  // <- GLOBAL SAFE
142  }
143 
144  //---------------------------------------
145 
148  SingletonClass& operator*() const
149  {
150  // GLOBAL SAFE ->
151  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
152  return *m_pInstance;
153  // <- GLOBAL SAFE
154  }
155 
156  //-------------------------------------------
157  // helper
158 
159  private :
160 
161  //---------------------------------------
162 
169  struct SingletonLockInit
170  {
171  ::osl::Mutex* operator()()
172  {
173  static ::osl::Mutex aInstance;
174  return &aInstance;
175  }
176  };
177 
178  ::osl::Mutex& ownStaticLock() const
179  {
180  return *rtl_Instance< ::osl::Mutex,
181  SingletonLockInit,
183  ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
184  }
185 };
186 
187 template< class SingletonClass >
188 SingletonClass* SingletonRef< SingletonClass >::m_pInstance = 0;
189 
190 template< class SingletonClass >
191 sal_Int32 SingletonRef< SingletonClass >::m_nRef = 0;
192 
193 } // namespace salhelper
194 
195 #endif // INCLUDED_SALHELPER_SINGLETONREF_HXX
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
Definition: singletonref.hxx:148
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:123
SingletonRef()
standard ctor.
Definition: singletonref.hxx:92
#define OSL_ENSURE(c, m)
Definition: diagnose.h:154
template for implementing singleton classes.
Definition: singletonref.hxx:65
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:31
A mutual exclusion synchronization object.
Definition: mutex.hxx:32
~SingletonRef()
standard dtor.
Definition: singletonref.hxx:116
SingletonClass * operator->() const
Allows rSingle->someBodyOp().
Definition: singletonref.hxx:136
Guard< Mutex > MutexGuard
Definition: mutex.hxx:238