LibreOffice
LibreOffice 7.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"
31
32
33namespace salhelper{
34
35
68template< 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
96 {
97 // GLOBAL SAFE ->
98 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
99
100 // must be increased before(!) the check is done.
101 // Otherwise this check can fail inside the same thread ...
102 ++m_nRef;
103 if (m_nRef == 1)
104 m_pInstance = new SingletonClass();
105
106 OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
107 // <- GLOBAL SAFE
108 }
109
110
119 {
120 // GLOBAL SAFE ->
121 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
122
123 // must be decreased before(!) the check is done.
124 // Otherwise this check can fail inside the same thread ...
125 --m_nRef;
126 if (m_nRef == 0)
127 {
128 delete m_pInstance;
129 m_pInstance = NULL;
130 }
131 // <- GLOBAL SAFE
132 }
133
134#if defined LIBO_INTERNAL_ONLY
135 SingletonRef & operator =(SingletonRef const &) = default;
136#endif
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
151 SingletonClass& operator*() const
152 {
153 // GLOBAL SAFE ->
154 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
155 return *m_pInstance;
156 // <- GLOBAL SAFE
157 }
158
159
160 // helper
161
162 private:
164
171 struct SingletonLockInit
172 {
173 ::osl::Mutex* operator()()
174 {
175 static ::osl::Mutex aInstance;
176 return &aInstance;
177 }
178 };
179
180 ::osl::Mutex& ownStaticLock() const
181 {
182 return *rtl_Instance< ::osl::Mutex,
183 SingletonLockInit,
185 ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
186 }
187};
188
189template< class SingletonClass >
190SingletonClass* SingletonRef< SingletonClass >::m_pInstance = NULL;
191
192template< class SingletonClass >
193sal_Int32 SingletonRef< SingletonClass >::m_nRef = 0;
194
195} // namespace salhelper
196
197#endif // INCLUDED_SALHELPER_SINGLETONREF_HXX
198
199/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:374
Provides simple diagnostic support.
#define OSL_ENSURE(c, m)
If cond is false, reports an error with message msg.
Definition: diagnose.h:84
Guard< Mutex > MutexGuard
Definition: mutex.hxx:246
Definition: condition.hxx:31
template for implementing singleton classes.
Definition: singletonref.hxx:70
SingletonClass * operator->() const
Allows rSingle->someBodyOp().
Definition: singletonref.hxx:140
~SingletonRef()
standard dtor.
Definition: singletonref.hxx:118
SingletonRef()
standard ctor.
Definition: singletonref.hxx:95
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
Definition: singletonref.hxx:151
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:32
A mutual exclusion synchronization object.
Definition: mutex.hxx:31
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:115