UDK 3.2.7 C/C++ API Reference

salhelper/singletonref.hxx

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*
00003  * This file is part of the LibreOffice project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * This file incorporates work covered by the following license notice:
00010  *
00011  *   Licensed to the Apache Software Foundation (ASF) under one or more
00012  *   contributor license agreements. See the NOTICE file distributed
00013  *   with this work for additional information regarding copyright
00014  *   ownership. The ASF licenses this file to you under the Apache
00015  *   License, Version 2.0 (the "License"); you may not use this file
00016  *   except in compliance with the License. You may obtain a copy of
00017  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
00018  */
00019 
00020 #ifndef _SALHELPER_SINGLETONREF_HXX_
00021 #define _SALHELPER_SINGLETONREF_HXX_
00022 
00023 #include <osl/mutex.hxx>
00024 #include "rtl/instance.hxx"
00025 #include "osl/diagnose.h"
00026 #include "osl/getglobalmutex.hxx"
00027 
00028 
00029 namespace salhelper{
00030 
00031 
00064 template< class SingletonClass >
00065 class SingletonRef
00066 {
00067     //-------------------------------------------
00068     // member
00069 
00070     private :
00071 
00073         static SingletonClass* m_pInstance;
00074 
00076         static sal_Int32 m_nRef;
00077 
00078     //-------------------------------------------
00079     // interface
00080 
00081     public :
00082 
00083         //---------------------------------------
00084 
00092         SingletonRef()
00093         {
00094             // GLOBAL SAFE ->
00095             ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
00096 
00097             // must be increased before(!) the check is done.
00098             // Otherwhise this check can fail inside the same thread ...
00099             ++m_nRef;
00100             if (m_nRef == 1)
00101                 m_pInstance = new SingletonClass();
00102 
00103             OSL_ENSURE(m_nRef>0 && m_pInstance, "Race? Ref count of singleton >0, but instance is NULL!");
00104             // <- GLOBAL SAFE
00105         }
00106 
00107         //---------------------------------------
00108 
00116         ~SingletonRef()
00117         {
00118             // GLOBAL SAFE ->
00119             ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
00120 
00121             // must be decreased before(!) the check is done.
00122             // Otherwhise this check can fail inside the same thread ...
00123             --m_nRef;
00124             if (m_nRef == 0)
00125             {
00126                 delete m_pInstance;
00127                 m_pInstance = 0;
00128             }
00129             // <- GLOBAL SAFE
00130         }
00131 
00132         //---------------------------------------
00133 
00136         SingletonClass* operator->() const
00137         {
00138             // GLOBAL SAFE ->
00139             ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
00140             return m_pInstance;
00141             // <- GLOBAL SAFE
00142         }
00143 
00144         //---------------------------------------
00145 
00148         SingletonClass& operator*() const
00149         {
00150             // GLOBAL SAFE ->
00151             ::osl::MutexGuard aLock(SingletonRef::ownStaticLock());
00152             return *m_pInstance;
00153             // <- GLOBAL SAFE
00154         }
00155 
00156     //-------------------------------------------
00157     // helper
00158 
00159     private :
00160 
00161         //---------------------------------------
00162 
00169         struct SingletonLockInit
00170         {
00171             ::osl::Mutex* operator()()
00172             {
00173                 static ::osl::Mutex aInstance;
00174                 return &aInstance;
00175             }
00176         };
00177 
00178         ::osl::Mutex& ownStaticLock() const
00179         {
00180             return *rtl_Instance< ::osl::Mutex,
00181                                   SingletonLockInit,
00182                                   ::osl::MutexGuard,
00183                                   ::osl::GetGlobalMutex >::create(SingletonLockInit(), ::osl::GetGlobalMutex());
00184         }
00185 };
00186 
00187 template< class SingletonClass >
00188 SingletonClass* SingletonRef< SingletonClass >::m_pInstance = 0;
00189 
00190 template< class SingletonClass >
00191 sal_Int32 SingletonRef< SingletonClass >::m_nRef = 0;
00192 
00193 } // namespace salhelper
00194 
00195 #endif // _SALHELPER_SINGLETONREF_HXX_
00196 
00197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines