1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _CPPUHELPER_WEAK_HXX_
24 #define _CPPUHELPER_WEAK_HXX_
25 
26 #include <osl/interlck.h>
27 #include <rtl/alloc.h>
28 #include <cppuhelper/weakref.hxx>
29 #include <cppuhelper/queryinterface.hxx>
30 #ifndef _COM_SUN_STAR_UNO_WEAK_HPP_
31 #include <com/sun/star/uno/XWeak.hpp>
32 #endif
33 
34 
35 namespace cppu
36 {
37 
38 /** @internal */
39 class OWeakConnectionPoint;
40 
41 /** Base class to implement an UNO object supporting weak references, i.e. the object can be held
42     weakly (by a ::com::sun::star::uno::WeakReference).
43     This implementation copes with reference counting.  Upon last release(), the virtual dtor
44     is called.
45 
46     @derive
47     Inherit from this class and delegate acquire()/ release() calls.
48 */
49 class OWeakObject : public ::com::sun::star::uno::XWeak
50 {
51     /** @internal */
52 	friend class OWeakConnectionPoint;
53 
54 protected:
55 	/** Virtual dtor.
56 
57         @attention
58         Despite the fact that a RuntimeException is allowed to be thrown, you must not throw any
59         exception upon destruction!
60 	*/
61     virtual ~OWeakObject() SAL_THROW( (::com::sun::star::uno::RuntimeException) );
62 
63     /** disposes and resets m_pWeakConnectionPoint
64         @precond
65             m_refCount equals 0
66     */
67     void    disposeWeakConnectionPoint();
68 
69 	/** reference count.
70 
71         @attention
72         Don't modify manually!  Use acquire() and release().
73     */
74     oslInterlockedCount m_refCount;
75 
76 	/** Container of all weak reference listeners and the connection point from the weak reference.
77         @internal
78     */
79 	OWeakConnectionPoint * m_pWeakConnectionPoint;
80 
81     /** reserved for future use. do not use.
82         @internal
83     */
84     void * m_pReserved;
85 
86 public:
87 	// these are here to force memory de/allocation to sal lib.
88     /** @internal */
operator new(size_t nSize)89 	inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
90 		{ return ::rtl_allocateMemory( nSize ); }
91     /** @internal */
operator delete(void * pMem)92 	inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
93 		{ ::rtl_freeMemory( pMem ); }
94     /** @internal */
operator new(size_t,void * pMem)95 	inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW( () )
96 		{ return pMem; }
97     /** @internal */
operator delete(void *,void *)98 	inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW( () )
99 		{}
100 
101 #ifdef _MSC_VER
102 	/** Default Constructor.  Sets the reference count to zero.
103         Accidentally occurs in msvc mapfile = > had to be outlined.
104     */
105 	OWeakObject() SAL_THROW( () );
106 #else
107 	/** Default Constructor.  Sets the reference count to zero.
108     */
109 	inline OWeakObject() SAL_THROW( () )
110 		: m_refCount( 0 )
111 		, m_pWeakConnectionPoint( 0 )
112 		{}
113 #endif
114 	/** Dummy copy constructor.  Set the reference count to zero.
115 
116         @param rObj dummy param
117     */
118     inline OWeakObject( const OWeakObject & ) SAL_THROW( () )
119 		: com::sun::star::uno::XWeak()
120         , m_refCount( 0 )
121 		, m_pWeakConnectionPoint( 0 )
122 		{}
123 	/** Dummy assignment operator. Does not affect reference count.
124 
125         @return this OWeakObject
126     */
operator =(const OWeakObject &)127     inline OWeakObject & SAL_CALL operator = ( const OWeakObject &)
128         SAL_THROW( () )
129     	{ return *this; }
130 
131 	/** Basic queryInterface() implementation supporting ::com::sun::star::uno::XWeak and
132         ::com::sun::star::uno::XInterface.
133 
134         @param rType demanded type
135         @return demanded type or empty any
136     */
137     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface(
138 		const ::com::sun::star::uno::Type & rType )
139 		throw (::com::sun::star::uno::RuntimeException);
140     /** increasing m_refCount
141     */
142     virtual void SAL_CALL acquire()
143 		throw ();
144     /** decreasing m_refCount
145     */
146     virtual void SAL_CALL release()
147 		throw ();
148 
149 	/** XWeak::queryAdapter() implementation
150 
151         @return a ::com::sun::star::uno::XAdapter reference
152     */
153     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAdapter > SAL_CALL queryAdapter()
154 		throw (::com::sun::star::uno::RuntimeException);
155 
156 	/** Cast operator to XInterface reference.
157 
158         @return XInterface reference
159     */
operator ::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface>()160 	inline SAL_CALL operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > () SAL_THROW( () )
161 		{ return this; }
162 };
163 
164 }
165 
166 #endif
167 
168 
169