xref: /trunk/main/cppuhelper/inc/cppuhelper/weak.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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         You must not throw any exception upon destruction!
60     */
61     virtual ~OWeakObject();
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     /** increasing m_refCount
140     */
141     virtual void SAL_CALL acquire()
142         throw ();
143     /** decreasing m_refCount
144     */
145     virtual void SAL_CALL release()
146         throw ();
147 
148     /** XWeak::queryAdapter() implementation
149 
150         @return a ::com::sun::star::uno::XAdapter reference
151     */
152     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAdapter > SAL_CALL queryAdapter();
153 
154     /** Cast operator to XInterface reference.
155 
156         @return XInterface reference
157     */
operator ::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface>()158     inline SAL_CALL operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > () SAL_THROW( () )
159         { return this; }
160 };
161 
162 }
163 
164 #endif
165