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_REFOBJ_HXX_ 00021 #define _SALHELPER_REFOBJ_HXX_ 00022 00023 #include <sal/types.h> 00024 #include <rtl/alloc.h> 00025 #include <rtl/ref.hxx> 00026 #include <osl/diagnose.h> 00027 #include <osl/interlck.h> 00028 00029 namespace salhelper 00030 { 00031 00032 //---------------------------------------------------------------------------- 00033 00034 class ReferenceObject : public rtl::IReference 00035 { 00038 oslInterlockedCount m_nReferenceCount; 00039 00042 ReferenceObject (const ReferenceObject&); 00043 ReferenceObject& operator= (const ReferenceObject&); 00044 00045 public: 00048 static void* operator new (size_t n) SAL_THROW(()) 00049 { 00050 return ::rtl_allocateMemory (n); 00051 } 00052 static void operator delete (void* p) SAL_THROW(()) 00053 { 00054 ::rtl_freeMemory (p); 00055 } 00056 static void* operator new (size_t, void* p) SAL_THROW(()) 00057 { 00058 return (p); 00059 } 00060 static void operator delete (void*, void*) SAL_THROW(()) 00061 {} 00062 00063 public: 00066 inline ReferenceObject() SAL_THROW(()) : m_nReferenceCount (0) 00067 {} 00068 00069 00072 virtual oslInterlockedCount SAL_CALL acquire() SAL_THROW(()) 00073 { 00074 return ::osl_incrementInterlockedCount (&m_nReferenceCount); 00075 } 00076 00077 virtual oslInterlockedCount SAL_CALL release() SAL_THROW(()) 00078 { 00079 oslInterlockedCount result; 00080 result = ::osl_decrementInterlockedCount (&m_nReferenceCount); 00081 if (result == 0) 00082 { 00083 // Last reference released. 00084 delete this; 00085 } 00086 return (result); 00087 } 00088 00089 protected: 00092 virtual ~ReferenceObject() SAL_THROW(()) 00093 { 00094 OSL_ASSERT(m_nReferenceCount == 0); 00095 } 00096 }; 00097 00098 //---------------------------------------------------------------------------- 00099 00100 } // namespace salhelper 00101 00102 #endif /* !_SALHELPER_REFOBJ_HXX_ */ 00103 00104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */