bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / salhelper / singletonref.hxx
blob9d9747ee70ae8328d1c0b98fcada3ad57fdb9e38
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SALHELPER_SINGLETONREF_HXX
21 #define INCLUDED_SALHELPER_SINGLETONREF_HXX
23 #include "sal/config.h"
25 #include <cstddef>
27 #include "osl/mutex.hxx"
28 #include "rtl/instance.hxx"
29 #include "osl/diagnose.h"
30 #include "osl/getglobalmutex.hxx"
33 namespace salhelper{
36 /** @short template for implementing singleton classes.
38 Such classes can be instantiated every time they
39 are needed. But the internal wrapped object will
40 be created one times only. Of course its used
41 resources are referenced one times only too.
42 This template hold it alive till the last
43 reference is gone. Further all operations
44 on this reference are threadsafe. Only
45 calls directly to the internal object (which modify
46 its state) must be made threadsafe by the object itself
47 or from outside.
49 @attention To prevent the code against race conditions, it's not
50 allowed to start operations inside the ctor
51 of the internal wrapped object - especially operations
52 which needs a reference to the same singleton too.
54 The only chance to suppress such strange constellations
55 is a lazy-init mechanism.
57 <ul>
58 <li>a) The singleton class can provide a special init()
59 method, which must be called as first after creation.</li>
60 <li>b) The singleton class can call a special impl_init()
61 method implicit for every called interface method.</li>
62 </ul>
64 Note further that this singleton pattern can work only, if
65 all user of such singleton are located inside the same library!
66 Because static values can't be exported - e.g. from windows libraries.
68 template< class SingletonClass >
69 class SingletonRef
72 // member
74 private:
76 /** @short pointer to the internal wrapped singleton. */
77 static SingletonClass* m_pInstance;
79 /** @short ref count, which regulate creation and removing of m_pInstance. */
80 static sal_Int32 m_nRef;
83 // interface
85 public:
88 /** @short standard ctor.
90 The internal wrapped object is created only,
91 if its ref count was 0. Otherwise this method
92 does nothing ... except increasing of the internal
93 ref count!
95 SingletonRef()
97 // GLOBAL SAFE ->
98 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
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();
106 OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
107 // <- GLOBAL SAFE
111 /** @short standard dtor.
113 The internal wrapped object is removed only,
114 if its ref count will be 0. Otherwise this method
115 does nothing ... except decreasing of the internal
116 ref count!
118 ~SingletonRef()
120 // GLOBAL SAFE ->
121 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
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)
128 delete m_pInstance;
129 m_pInstance = NULL;
131 // <- GLOBAL SAFE
134 #if defined LIBO_INTERNAL_ONLY
135 SingletonRef & operator =(SingletonRef const &) = default;
136 #endif
138 /** @short Allows rSingle->someBodyOp().
140 SingletonClass* operator->() const
142 // GLOBAL SAFE ->
143 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
144 return m_pInstance;
145 // <- GLOBAL SAFE
149 /** @short Allows (*rSingle).someBodyOp().
151 SingletonClass& operator*() const
153 // GLOBAL SAFE ->
154 ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
155 return *m_pInstance;
156 // <- GLOBAL SAFE
160 // helper
162 private:
163 SingletonRef(SingletonRef &) SAL_DELETED_FUNCTION;
165 /** @short creates an own mutex for guarding static contents.
167 The global mutex the osl library is used one times
168 only to create an own static mutex, which can be used
169 next time to guard own static member operations.
171 struct SingletonLockInit
173 ::osl::Mutex* operator()()
175 static ::osl::Mutex aInstance;
176 return &aInstance;
180 ::osl::Mutex& ownStaticLock() const
182 return *rtl_Instance< ::osl::Mutex,
183 SingletonLockInit,
184 ::osl::MutexGuard,
185 ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
189 template< class SingletonClass >
190 SingletonClass* SingletonRef< SingletonClass >::m_pInstance = NULL;
192 template< class SingletonClass >
193 sal_Int32 SingletonRef< SingletonClass >::m_nRef = 0;
195 } // namespace salhelper
197 #endif // INCLUDED_SALHELPER_SINGLETONREF_HXX
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */