LibreOffice Module comphelper (master) 1
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#pragma once
20
21#include <sal/config.h>
22#include <osl/diagnose.h>
23#include <cstddef>
24#include <mutex>
25
26namespace comphelper
27{
61template <class SingletonClass> class SingletonRef
62{
63 // member
64
65private:
67 static SingletonClass* m_pInstance;
68
70 static sal_Int32 m_nRef;
71
72 // interface
73
74public:
83 {
84 // GLOBAL SAFE ->
85 std::unique_lock aLock(SingletonRef::ownStaticLock());
86
87 // must be increased before(!) the check is done.
88 // Otherwise this check can fail inside the same thread ...
89 ++m_nRef;
90 if (m_nRef == 1)
91 m_pInstance = new SingletonClass();
92
93 OSL_ENSURE(m_nRef > 0 && m_pInstance,
94 "Race? Ref count of singleton >0, but instance is NULL!");
95 // <- GLOBAL SAFE
96 }
97
106 {
107 // GLOBAL SAFE ->
108 std::unique_lock aLock(SingletonRef::ownStaticLock());
109
110 // must be decreased before(!) the check is done.
111 // Otherwise this check can fail inside the same thread ...
112 --m_nRef;
113 if (m_nRef == 0)
114 {
115 delete m_pInstance;
116 m_pInstance = nullptr;
117 }
118 // <- GLOBAL SAFE
119 }
120
122
125 SingletonClass* operator->() const
126 {
127 // GLOBAL SAFE ->
128 return m_pInstance;
129 // <- GLOBAL SAFE
130 }
131
134 SingletonClass& operator*() const
135 {
136 // GLOBAL SAFE ->
137 return *m_pInstance;
138 // <- GLOBAL SAFE
139 }
140
141 // helper
142
143private:
145
147 {
148 static std::mutex aInstance;
149 return aInstance;
150 }
151};
152
153template <class SingletonClass> SingletonClass* SingletonRef<SingletonClass>::m_pInstance = nullptr;
154
155template <class SingletonClass> sal_Int32 SingletonRef<SingletonClass>::m_nRef = 0;
156
157} // namespace comphelper
158
159/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Template for implementing singleton classes.
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
~SingletonRef()
standard dtor.
SingletonRef(SingletonRef &)=delete
SingletonRef()
standard ctor.
SingletonRef & operator=(SingletonRef const &)=default
SingletonClass * operator->() const
Allows rSingle->someBodyOp().
static std::mutex & ownStaticLock()
static sal_Int32 m_nRef
ref count, which regulate creation and removing of m_pInstance.
static SingletonClass * m_pInstance
pointer to the internal wrapped singleton.
std::mutex mutex
Definition: random.cxx:41