1*ebfcd9afSAndrew Rist /**************************************************************
2*ebfcd9afSAndrew Rist  *
3*ebfcd9afSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ebfcd9afSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ebfcd9afSAndrew Rist  * distributed with this work for additional information
6*ebfcd9afSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ebfcd9afSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ebfcd9afSAndrew Rist  * "License"); you may not use this file except in compliance
9*ebfcd9afSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ebfcd9afSAndrew Rist  *
11*ebfcd9afSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ebfcd9afSAndrew Rist  *
13*ebfcd9afSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ebfcd9afSAndrew Rist  * software distributed under the License is distributed on an
15*ebfcd9afSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ebfcd9afSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ebfcd9afSAndrew Rist  * specific language governing permissions and limitations
18*ebfcd9afSAndrew Rist  * under the License.
19*ebfcd9afSAndrew Rist  *
20*ebfcd9afSAndrew Rist  *************************************************************/
21*ebfcd9afSAndrew Rist 
22*ebfcd9afSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef VBAHELPER_WEAKREFERENCE_HXX
25cdf0e10cSrcweir #define VBAHELPER_WEAKREFERENCE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
28cdf0e10cSrcweir #include <rtl/ref.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace vbahelper {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /** A weak reference holding any UNO implementation object.
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     The held object must implement the ::com::sun::star::uno::XWeak interface.
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     In difference to the ::com::sun::star::uno::WeakReference<> implementation
39cdf0e10cSrcweir     from cppuhelper/weakref.hxx, the class type of this weak reference is not
40cdf0e10cSrcweir     restricted to UNO interface types, but can be used for any C++ class type
41cdf0e10cSrcweir     implementing the XWeak interface somehow (e.g. ::cppu::WeakImplHelperN<>,
42cdf0e10cSrcweir     ::cppu::ImplInheritanceHelperN<>, etc.).
43cdf0e10cSrcweir  */
44cdf0e10cSrcweir template< typename ObjectType >
45cdf0e10cSrcweir class WeakReference
46cdf0e10cSrcweir {
47cdf0e10cSrcweir public:
48cdf0e10cSrcweir     /** Default constructor. Creates an empty weak reference.
49cdf0e10cSrcweir      */
50cdf0e10cSrcweir     inline explicit WeakReference() SAL_THROW( () ) : mpObject( 0 ) {}
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     /** Initializes this weak reference with the passed reference to an object.
53cdf0e10cSrcweir      */
54cdf0e10cSrcweir     inline explicit WeakReference( const ::rtl::Reference< ObjectType >& rxObject ) SAL_THROW( () ) :
55cdf0e10cSrcweir         mxWeakRef( rxObject.get() ), mpObject( rxObject.get() ) {}
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /** Releases this weak reference and takes over the passed reference.
58cdf0e10cSrcweir      */
operator =(const::rtl::Reference<ObjectType> & rxObject)59cdf0e10cSrcweir     inline WeakReference& SAL_CALL operator=( const ::rtl::Reference< ObjectType >& rxObject ) SAL_THROW( () )
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         mxWeakRef = ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak >( rxObject.get() );
62cdf0e10cSrcweir         mpObject = rxObject.get();
63cdf0e10cSrcweir         return *this;
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     /** Gets an RTL reference to the referenced object.
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         @return  Reference or null, if the weakly referenced object is gone.
69cdf0e10cSrcweir      */
operator ::rtl::Reference<ObjectType>()70cdf0e10cSrcweir     inline SAL_CALL operator ::rtl::Reference< ObjectType >() SAL_THROW( () )
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak > xRef = mxWeakRef;
73cdf0e10cSrcweir         ::rtl::Reference< ObjectType > xObject;
74cdf0e10cSrcweir         if( xRef.is() )
75cdf0e10cSrcweir             xObject = mpObject;
76cdf0e10cSrcweir         else
77cdf0e10cSrcweir             mpObject = 0;
78cdf0e10cSrcweir         return xObject;
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir private:
82cdf0e10cSrcweir     ::com::sun::star::uno::WeakReference< ::com::sun::star::uno::XWeak > mxWeakRef;
83cdf0e10cSrcweir     ObjectType* mpObject;
84cdf0e10cSrcweir };
85cdf0e10cSrcweir 
86cdf0e10cSrcweir // ============================================================================
87cdf0e10cSrcweir 
88cdf0e10cSrcweir } // namespace vbahelper
89cdf0e10cSrcweir 
90cdf0e10cSrcweir #endif
91