AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
4 #ifndef INCLUDED_SALHELPER_SINGLETONREF_HXX
5 #define INCLUDED_SALHELPER_SINGLETONREF_HXX
6 
7 #include "sal/config.h"
8 
9 #include <cstddef>
10 
11 #include "osl/mutex.hxx"
12 #include "rtl/instance.hxx"
13 #include "osl/diagnose.h"
14 #include "osl/getglobalmutex.hxx"
15 
16 
17 namespace salhelper{
18 
19 
52 template< class SingletonClass >
54 {
55 
56  // member
57 
58  private:
59 
61  static SingletonClass* m_pInstance;
62 
64  static sal_Int32 m_nRef;
65 
66 
67  // interface
68 
69  public:
70 
71 
80  {
81  // GLOBAL SAFE ->
82  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
83 
84  // must be increased before(!) the check is done.
85  // Otherwise this check can fail inside the same thread ...
86  ++m_nRef;
87  if (m_nRef == 1)
88  m_pInstance = new SingletonClass();
89 
90  OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
91  // <- GLOBAL SAFE
92  }
93 
94 
103  {
104  // GLOBAL SAFE ->
105  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
106 
107  // must be decreased before(!) the check is done.
108  // Otherwise this check can fail inside the same thread ...
109  --m_nRef;
110  if (m_nRef == 0)
111  {
112  delete m_pInstance;
113  m_pInstance = NULL;
114  }
115  // <- GLOBAL SAFE
116  }
117 
118 #if defined LIBO_INTERNAL_ONLY
119  SingletonRef & operator =(SingletonRef const &) = default;
120 #endif
121 
124  SingletonClass* operator->() const
125  {
126  // GLOBAL SAFE ->
127  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
128  return m_pInstance;
129  // <- GLOBAL SAFE
130  }
131 
132 
135  SingletonClass& operator*() const
136  {
137  // GLOBAL SAFE ->
138  ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
139  return *m_pInstance;
140  // <- GLOBAL SAFE
141  }
142 
143 
144  // helper
145 
146  private:
148 
155  struct SingletonLockInit
156  {
157  ::osl::Mutex* operator()()
158  {
159  static ::osl::Mutex aInstance;
160  return &aInstance;
161  }
162  };
163 
164  ::osl::Mutex& ownStaticLock() const
165  {
166  return *rtl_Instance< ::osl::Mutex,
167  SingletonLockInit,
169  ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
170  }
171 };
172 
173 template< class SingletonClass >
174 SingletonClass* SingletonRef< SingletonClass >::m_pInstance = NULL;
175 
176 template< class SingletonClass >
177 sal_Int32 SingletonRef< SingletonClass >::m_nRef = 0;
178 
179 } // namespace salhelper
180 
181 #endif // INCLUDED_SALHELPER_SINGLETONREF_HXX
182 
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SingletonClass * operator->() const
Allows rSingle-&gt;someBodyOp().
Definition: singletonref.hxx:124
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:15
template for implementing singleton classes.
Definition: singletonref.hxx:53
Guard< Mutex > MutexGuard
Definition: mutex.hxx:235
#define OSL_ENSURE(c, m)
If cond is false, reports an error with message msg.
Definition: diagnose.h:67
A mutual exclusion synchronization object.
Definition: mutex.hxx:15
Provides simple diagnostic support.
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:103
SingletonClass & operator*() const
Allows (*rSingle).someBodyOp().
Definition: singletonref.hxx:135
~SingletonRef()
standard dtor.
Definition: singletonref.hxx:102
SingletonRef()
standard ctor.
Definition: singletonref.hxx:79
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:358