LibreOffice
LibreOffice 25.8 SDK C/C++ API Reference
Loading...
Searching...
No Matches
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/*
21 * This file is part of LibreOffice published API.
22 */
23
24#ifndef INCLUDED_SALHELPER_SINGLETONREF_HXX
25#define INCLUDED_SALHELPER_SINGLETONREF_HXX
26
27#include "sal/config.h"
28
29#include <cstddef>
30
31#include "osl/mutex.hxx"
32#include "rtl/instance.hxx"
33#include "osl/diagnose.h"
35
36
37namespace salhelper{
38
39
72template< class SingletonClass >
74{
75
76 // member
77
78 private:
79
81 static SingletonClass* m_pInstance;
82
84 static sal_Int32 m_nRef;
85
86
87 // interface
88
89 public:
90
91
100 {
101 // GLOBAL SAFE ->
102 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
103
104 // must be increased before(!) the check is done.
105 // Otherwise this check can fail inside the same thread ...
106 ++m_nRef;
107 if (m_nRef == 1)
108 m_pInstance = new SingletonClass();
109
110 OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
111 // <- GLOBAL SAFE
112 }
113
114
123 {
124 // GLOBAL SAFE ->
125 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
126
127 // must be decreased before(!) the check is done.
128 // Otherwise this check can fail inside the same thread ...
129 --m_nRef;
130 if (m_nRef == 0)
131 {
132 delete m_pInstance;
133 m_pInstance = NULL;
134 }
135 // <- GLOBAL SAFE
136 }
137
138#if defined LIBO_INTERNAL_ONLY
139 SingletonRef & operator =(SingletonRef const &) = default;
140#endif
141
144 SingletonClass* operator->() const
145 {
146 // GLOBAL SAFE ->
147 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
148 return m_pInstance;
149 // <- GLOBAL SAFE
150 }
151
152
155 SingletonClass& operator*() const
156 {
157 // GLOBAL SAFE ->
158 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
159 return *m_pInstance;
160 // <- GLOBAL SAFE
161 }
162
163
164 // helper
165
166 private:
168
175 struct SingletonLockInit
176 {
177 ::osl::Mutex* operator()()
178 {
179 static ::osl::Mutex aInstance;
180 return &aInstance;
181 }
182 };
183
184 ::osl::Mutex& ownStaticLock() const
185 {
186 return *rtl_Instance< ::osl::Mutex,
187 SingletonLockInit,
189 ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
190 }
191};
192
193template< class SingletonClass >
194SingletonClass* SingletonRef< SingletonClass >::m_pInstance = NULL;
195
196template< class SingletonClass >
197sal_Int32 SingletonRef< SingletonClass >::m_nRef = 0;
198
199} // namespace salhelper
200
201#endif // INCLUDED_SALHELPER_SINGLETONREF_HXX
202
203/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition types.h:396
Provides simple diagnostic support.
#define OSL_ENSURE(c, m)
If cond is false, reports an error with message msg.
Definition diagnose.h:87
Guard< Mutex > MutexGuard
Definition mutex.hxx:273
Definition condition.hxx:34
template for implementing singleton classes.
Definition singletonref.hxx:74
SingletonClass * operator->() const
Allows rSingle->someBodyOp().
Definition singletonref.hxx:144
~SingletonRef()
standard dtor.
Definition singletonref.hxx:122
SingletonRef()
standard ctor.
Definition singletonref.hxx:99
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
Definition singletonref.hxx:155
A mutual exclusion synchronization object.
Definition mutex.hxx:35