xref: /aoo41x/main/salhelper/inc/salhelper/refobj.hxx (revision cdf0e10c)
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 #ifndef _SALHELPER_REFOBJ_HXX_
29 #define _SALHELPER_REFOBJ_HXX_
30 
31 #include <sal/types.h>
32 #include <rtl/alloc.h>
33 #include <rtl/ref.hxx>
34 #include <osl/diagnose.h>
35 #include <osl/interlck.h>
36 
37 namespace salhelper
38 {
39 
40 //----------------------------------------------------------------------------
41 
42 class ReferenceObject : public rtl::IReference
43 {
44 	/** Representation.
45 	 */
46 	oslInterlockedCount m_nReferenceCount;
47 
48 	/** Not implemented.
49 	 */
50 	ReferenceObject (const ReferenceObject&);
51 	ReferenceObject& operator= (const ReferenceObject&);
52 
53 public:
54 	/** Allocation.
55 	 */
56 	static void* operator new (size_t n) SAL_THROW(())
57 	{
58 		return ::rtl_allocateMemory (n);
59 	}
60 	static void operator delete (void* p) SAL_THROW(())
61 	{
62 		::rtl_freeMemory (p);
63 	}
64 	static void* operator new (size_t, void* p) SAL_THROW(())
65 	{
66 		return (p);
67 	}
68 	static void operator delete (void*, void*) SAL_THROW(())
69 	{}
70 
71 public:
72 	/** Construction.
73 	 */
74 	inline ReferenceObject() SAL_THROW(()) : m_nReferenceCount (0)
75 	{}
76 
77 
78 	/** IReference.
79 	 */
80 	virtual oslInterlockedCount SAL_CALL acquire() SAL_THROW(())
81 	{
82 		return ::osl_incrementInterlockedCount (&m_nReferenceCount);
83 	}
84 
85 	virtual oslInterlockedCount SAL_CALL release() SAL_THROW(())
86 	{
87 		oslInterlockedCount result;
88 		result = ::osl_decrementInterlockedCount (&m_nReferenceCount);
89 		if (result == 0)
90 		{
91 			// Last reference released.
92 			delete this;
93 		}
94 		return (result);
95 	}
96 
97 protected:
98 	/** Destruction.
99 	 */
100 	virtual ~ReferenceObject() SAL_THROW(())
101 	{
102 		OSL_ASSERT(m_nReferenceCount == 0);
103 	}
104 };
105 
106 //----------------------------------------------------------------------------
107 
108 } // namespace salhelper
109 
110 #endif /* !_SALHELPER_REFOBJ_HXX_ */
111