LibreOffice LibreOffice 7.2 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/*
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"
34#include "osl/getglobalmutex.hxx"
35
36
37 namespace salhelper{
38
39
72 template< class SingletonClass >
73 class SingletonRef
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
99 SingletonRef()
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
122 ~SingletonRef()
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:
167 SingletonRef(SingletonRef &) SAL_DELETED_FUNCTION;
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,
188 ::osl::MutexGuard,
189 ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
190 }
191};
192
193 template< class SingletonClass >
194SingletonClass* SingletonRef< SingletonClass >::m_pInstance = NULL;
195
196 template< 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: */
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
config.h
instance.hxx
diagnose.h
Provides simple diagnostic support.
getglobalmutex.hxx
mutex.hxx
OSL_ENSURE
#define OSL_ENSURE(c, m)
If cond is false, reports an error with message msg.
Definition: diagnose.h:87
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition: mutex.hxx:253
salhelper
Definition: condition.hxx:34
osl::GetGlobalMutex
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:36
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:35
osl::Guard
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:122
salhelper::SingletonRef
template for implementing singleton classes.
Definition: singletonref.hxx:74
salhelper::SingletonRef::operator->
SingletonClass * operator->() const
Allows rSingle->someBodyOp().
Definition: singletonref.hxx:144
salhelper::SingletonRef::~SingletonRef
~SingletonRef()
standard dtor.
Definition: singletonref.hxx:122
salhelper::SingletonRef::SingletonRef
SingletonRef()
standard ctor.
Definition: singletonref.hxx:99
salhelper::SingletonRef::operator*
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
Definition: singletonref.hxx:155