1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #ifndef _SALHELPER_SIMPLEREFERENCECOMPONENT_HXX_
32 #include "helper/simplereferencecomponent.hxx"
33 #endif
34 
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include "osl/diagnose.h"
37 
38 #ifndef INCLUDED_NEW
39 #include <new>
40 #define INCLUDED_NEW
41 #endif
42 
43 using com::sun::star::uno::RuntimeException;
44 using sd::SimpleReferenceComponent;
45 
46 SimpleReferenceComponent::SimpleReferenceComponent()
47 : m_nCount(0)
48 , mbDisposed(false)
49 {
50 }
51 
52 SimpleReferenceComponent::~SimpleReferenceComponent()
53 {
54     OSL_ASSERT(m_nCount == 0);
55     OSL_ASSERT(mbDisposed);
56 }
57 
58 void SimpleReferenceComponent::acquire()
59 {
60 	osl_incrementInterlockedCount(&m_nCount);
61 }
62 
63 void SimpleReferenceComponent::release()
64 {
65     if((1 == m_nCount) && !mbDisposed)
66     {
67         try
68         {
69 			Dispose();
70         }
71         catch (RuntimeException &
72 #if OSL_DEBUG_LEVEL > 0
73 			exc
74 #endif
75 			) // don't break throw ()
76         {
77 #if OSL_DEBUG_LEVEL > 0
78 			rtl::OString msg( rtl::OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
79             OSL_ENSURE( 0, msg.getStr() );
80 #endif
81         }
82     }
83 
84 	if(osl_decrementInterlockedCount(&m_nCount) == 0) delete this;
85 }
86 
87 void SimpleReferenceComponent::Dispose()
88 {
89 	if( !mbDisposed )
90 	{
91 		mbDisposed = true;
92 		disposing();
93 	}
94 }
95 
96 void SimpleReferenceComponent::disposing()
97 {
98 }
99 
100 void * SimpleReferenceComponent::operator new(std::size_t nSize)
101     SAL_THROW((std::bad_alloc))
102 {
103     return ::operator new(nSize);
104 }
105 
106 void * SimpleReferenceComponent::operator new(std::size_t nSize,
107                                            std::nothrow_t const &
108 #ifndef WNT
109 										   rNothrow
110 #endif
111 										   )
112     SAL_THROW(())
113 {
114 #if defined WNT
115     return ::operator new(nSize);
116         // WNT lacks a global nothrow operator new...
117 #else // WNT
118     return ::operator new(nSize, rNothrow);
119 #endif // WNT
120 }
121 
122 void SimpleReferenceComponent::operator delete(void * pPtr) SAL_THROW(())
123 {
124     ::operator delete(pPtr);
125 }
126 
127 void SimpleReferenceComponent::operator delete(void * pPtr,
128                                             std::nothrow_t const &
129 #ifndef WNT
130 											rNothrow
131 #endif
132 )
133     SAL_THROW(())
134 {
135 #if defined WNT
136     ::operator delete(pPtr); // WNT lacks a global nothrow operator delete...
137 #else // WNT
138     ::operator delete(pPtr, rNothrow);
139 #endif // WNT
140 }
141