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 
24 #ifndef VBAHELPER_WEAKREFERENCE_HXX
25 #define VBAHELPER_WEAKREFERENCE_HXX
26 
27 #include <cppuhelper/weakref.hxx>
28 #include <rtl/ref.hxx>
29 
30 namespace vbahelper {
31 
32 // ============================================================================
33 
34 /** A weak reference holding any UNO implementation object.
35 
36     The held object must implement the ::com::sun::star::uno::XWeak interface.
37 
38     In difference to the ::com::sun::star::uno::WeakReference<> implementation
39     from cppuhelper/weakref.hxx, the class type of this weak reference is not
40     restricted to UNO interface types, but can be used for any C++ class type
41     implementing the XWeak interface somehow (e.g. ::cppu::WeakImplHelperN<>,
42     ::cppu::ImplInheritanceHelperN<>, etc.).
43  */
44 template< typename ObjectType >
45 class WeakReference
46 {
47 public:
48     /** Default constructor. Creates an empty weak reference.
49      */
50     inline explicit WeakReference() SAL_THROW( () ) : mpObject( 0 ) {}
51 
52     /** Initializes this weak reference with the passed reference to an object.
53      */
54     inline explicit WeakReference( const ::rtl::Reference< ObjectType >& rxObject ) SAL_THROW( () ) :
55         mxWeakRef( rxObject.get() ), mpObject( rxObject.get() ) {}
56 
57     /** Releases this weak reference and takes over the passed reference.
58      */
operator =(const::rtl::Reference<ObjectType> & rxObject)59     inline WeakReference& SAL_CALL operator=( const ::rtl::Reference< ObjectType >& rxObject ) SAL_THROW( () )
60     {
61         mxWeakRef = ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak >( rxObject.get() );
62         mpObject = rxObject.get();
63         return *this;
64     }
65 
66     /** Gets an RTL reference to the referenced object.
67 
68         @return  Reference or null, if the weakly referenced object is gone.
69      */
operator ::rtl::Reference<ObjectType>()70     inline SAL_CALL operator ::rtl::Reference< ObjectType >() SAL_THROW( () )
71     {
72         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XWeak > xRef = mxWeakRef;
73         ::rtl::Reference< ObjectType > xObject;
74         if( xRef.is() )
75             xObject = mpObject;
76         else
77             mpObject = 0;
78         return xObject;
79     }
80 
81 private:
82     ::com::sun::star::uno::WeakReference< ::com::sun::star::uno::XWeak > mxWeakRef;
83     ObjectType* mpObject;
84 };
85 
86 // ============================================================================
87 
88 } // namespace vbahelper
89 
90 #endif
91