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