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