xref: /aoo42x/main/cppuhelper/inc/cppuhelper/weak.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 #ifndef _CPPUHELPER_WEAK_HXX_
28 #define _CPPUHELPER_WEAK_HXX_
29 
30 #include <osl/interlck.h>
31 #include <rtl/alloc.h>
32 #include <cppuhelper/weakref.hxx>
33 #include <cppuhelper/queryinterface.hxx>
34 #ifndef _COM_SUN_STAR_UNO_WEAK_HPP_
35 #include <com/sun/star/uno/XWeak.hpp>
36 #endif
37 
38 
39 namespace cppu
40 {
41 
42 /** @internal */
43 class OWeakConnectionPoint;
44 
45 /** Base class to implement an UNO object supporting weak references, i.e. the object can be held
46     weakly (by a ::com::sun::star::uno::WeakReference).
47     This implementation copes with reference counting.  Upon last release(), the virtual dtor
48     is called.
49 
50     @derive
51     Inherit from this class and delegate acquire()/ release() calls.
52 */
53 class OWeakObject : public ::com::sun::star::uno::XWeak
54 {
55     /** @internal */
56 	friend class OWeakConnectionPoint;
57 
58 protected:
59 	/** Virtual dtor.
60 
61         @attention
62         Despite the fact that a RuntimeException is allowed to be thrown, you must not throw any
63         exception upon destruction!
64 	*/
65     virtual ~OWeakObject() SAL_THROW( (::com::sun::star::uno::RuntimeException) );
66 
67     /** disposes and resets m_pWeakConnectionPoint
68         @precond
69             m_refCount equals 0
70     */
71     void    disposeWeakConnectionPoint();
72 
73 	/** reference count.
74 
75         @attention
76         Don't modify manually!  Use acquire() and release().
77     */
78     oslInterlockedCount m_refCount;
79 
80 	/** Container of all weak reference listeners and the connection point from the weak reference.
81         @internal
82     */
83 	OWeakConnectionPoint * m_pWeakConnectionPoint;
84 
85     /** reserved for future use. do not use.
86         @internal
87     */
88     void * m_pReserved;
89 
90 public:
91 	// these are here to force memory de/allocation to sal lib.
92     /** @internal */
93 	inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
94 		{ return ::rtl_allocateMemory( nSize ); }
95     /** @internal */
96 	inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
97 		{ ::rtl_freeMemory( pMem ); }
98     /** @internal */
99 	inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW( () )
100 		{ return pMem; }
101     /** @internal */
102 	inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW( () )
103 		{}
104 
105 #ifdef _MSC_VER
106 	/** Default Constructor.  Sets the reference count to zero.
107         Accidentally occurs in msvc mapfile = > had to be outlined.
108     */
109 	OWeakObject() SAL_THROW( () );
110 #else
111 	/** Default Constructor.  Sets the reference count to zero.
112     */
113 	inline OWeakObject() SAL_THROW( () )
114 		: m_refCount( 0 )
115 		, m_pWeakConnectionPoint( 0 )
116 		{}
117 #endif
118 	/** Dummy copy constructor.  Set the reference count to zero.
119 
120         @param rObj dummy param
121     */
122     inline OWeakObject( const OWeakObject & ) SAL_THROW( () )
123 		: com::sun::star::uno::XWeak()
124         , m_refCount( 0 )
125 		, m_pWeakConnectionPoint( 0 )
126 		{}
127 	/** Dummy assignment operator. Does not affect reference count.
128 
129         @return this OWeakObject
130     */
131     inline OWeakObject & SAL_CALL operator = ( const OWeakObject &)
132         SAL_THROW( () )
133     	{ return *this; }
134 
135 	/** Basic queryInterface() implementation supporting ::com::sun::star::uno::XWeak and
136         ::com::sun::star::uno::XInterface.
137 
138         @param rType demanded type
139         @return demanded type or empty any
140     */
141     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface(
142 		const ::com::sun::star::uno::Type & rType )
143 		throw (::com::sun::star::uno::RuntimeException);
144     /** increasing m_refCount
145     */
146     virtual void SAL_CALL acquire()
147 		throw ();
148     /** decreasing m_refCount
149     */
150     virtual void SAL_CALL release()
151 		throw ();
152 
153 	/** XWeak::queryAdapter() implementation
154 
155         @return a ::com::sun::star::uno::XAdapter reference
156     */
157     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAdapter > SAL_CALL queryAdapter()
158 		throw (::com::sun::star::uno::RuntimeException);
159 
160 	/** Cast operator to XInterface reference.
161 
162         @return XInterface reference
163     */
164 	inline SAL_CALL operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > () SAL_THROW( () )
165 		{ return this; }
166 };
167 
168 }
169 
170 #endif
171 
172 
173