UDK 3.2.7 C/C++ API Reference
salhelper/refobj.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _SALHELPER_REFOBJ_HXX_
00030 #define _SALHELPER_REFOBJ_HXX_
00031 
00032 #include <sal/types.h>
00033 #include <rtl/alloc.h>
00034 #include <rtl/ref.hxx>
00035 #include <osl/diagnose.h>
00036 #include <osl/interlck.h>
00037 
00038 namespace salhelper
00039 {
00040 
00041 //----------------------------------------------------------------------------
00042 
00043 class ReferenceObject : public rtl::IReference
00044 {
00047     oslInterlockedCount m_nReferenceCount;
00048 
00051     ReferenceObject (const ReferenceObject&);
00052     ReferenceObject& operator= (const ReferenceObject&);
00053 
00054 public:
00057     static void* operator new (size_t n) SAL_THROW(())
00058     {
00059         return ::rtl_allocateMemory (n);
00060     }
00061     static void operator delete (void* p) SAL_THROW(())
00062     {
00063         ::rtl_freeMemory (p);
00064     }
00065     static void* operator new (size_t, void* p) SAL_THROW(())
00066     {
00067         return (p);
00068     }
00069     static void operator delete (void*, void*) SAL_THROW(())
00070     {}
00071 
00072 public:
00075     inline ReferenceObject() SAL_THROW(()) : m_nReferenceCount (0)
00076     {}
00077 
00078 
00081     virtual oslInterlockedCount SAL_CALL acquire() SAL_THROW(())
00082     {
00083         return ::osl_incrementInterlockedCount (&m_nReferenceCount);
00084     }
00085 
00086     virtual oslInterlockedCount SAL_CALL release() SAL_THROW(())
00087     {
00088         oslInterlockedCount result;
00089         result = ::osl_decrementInterlockedCount (&m_nReferenceCount);
00090         if (result == 0)
00091         {
00092             // Last reference released.
00093             delete this;
00094         }
00095         return (result);
00096     }
00097 
00098 protected:
00101     virtual ~ReferenceObject() SAL_THROW(())
00102     {
00103         OSL_ASSERT(m_nReferenceCount == 0);
00104     }
00105 };
00106 
00107 //----------------------------------------------------------------------------
00108 
00109 } // namespace salhelper
00110 
00111 #endif /* !_SALHELPER_REFOBJ_HXX_ */
00112 
00113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines