Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_CPPUHELPER_WEAK_HXX
20 : #define INCLUDED_CPPUHELPER_WEAK_HXX
21 :
22 : #include <osl/interlck.h>
23 : #include <rtl/alloc.h>
24 : #include <cppuhelper/weakref.hxx>
25 : #include <cppuhelper/queryinterface.hxx>
26 : #include <com/sun/star/uno/XWeak.hpp>
27 : #include <cppuhelper/cppuhelperdllapi.h>
28 :
29 :
30 : namespace cppu
31 : {
32 :
33 : class OWeakConnectionPoint;
34 :
35 : /** Base class to implement an UNO object supporting weak references, i.e. the object can be held
36 : weakly (by a ::com::sun::star::uno::WeakReference).
37 : This implementation copes with reference counting. Upon last release(), the virtual dtor
38 : is called.
39 :
40 : @derive
41 : Inherit from this class and delegate acquire()/ release() calls.
42 : */
43 : class CPPUHELPER_DLLPUBLIC OWeakObject : public ::com::sun::star::uno::XWeak
44 : {
45 : friend class OWeakConnectionPoint;
46 :
47 : protected:
48 : /** Virtual dtor.
49 :
50 : @attention
51 : Despite the fact that a RuntimeException is allowed to be thrown, you must not throw any
52 : exception upon destruction!
53 : */
54 : virtual ~OWeakObject() SAL_THROW( (::com::sun::star::uno::RuntimeException) );
55 :
56 : /** disposes and resets m_pWeakConnectionPoint
57 : @pre
58 : m_refCount equals 0
59 : */
60 : void disposeWeakConnectionPoint();
61 :
62 : /** reference count.
63 :
64 : @attention
65 : Don't modify manually! Use acquire() and release().
66 : */
67 : oslInterlockedCount m_refCount;
68 :
69 : /// @cond INTERNAL
70 :
71 : /** Container of all weak reference listeners and the connection point from the weak reference.
72 : */
73 : OWeakConnectionPoint * m_pWeakConnectionPoint;
74 :
75 : /** reserved for future use. do not use.
76 : */
77 : void * m_pReserved;
78 :
79 : /// @endcond
80 :
81 : public:
82 : /// @cond INTERNAL
83 : // these are here to force memory de/allocation to sal lib.
84 65 : inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
85 65 : { return ::rtl_allocateMemory( nSize ); }
86 64 : inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
87 64 : { ::rtl_freeMemory( pMem ); }
88 : inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
89 : { return pMem; }
90 : inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
91 : {}
92 : /// @endcond
93 :
94 : #ifdef _MSC_VER
95 : /** Default Constructor. Sets the reference count to zero.
96 : Accidentally occurs in msvc mapfile = > had to be outlined.
97 : */
98 : OWeakObject() SAL_THROW(());
99 : #else
100 : /** Default Constructor. Sets the reference count to zero.
101 : */
102 69 : inline OWeakObject() SAL_THROW(())
103 : : m_refCount( 0 )
104 69 : , m_pWeakConnectionPoint( 0 )
105 69 : {}
106 : #endif
107 : /** Dummy copy constructor. Set the reference count to zero.
108 :
109 : @param rObj dummy param
110 : */
111 0 : inline OWeakObject( const OWeakObject & rObj ) SAL_THROW(())
112 : : com::sun::star::uno::XWeak()
113 : , m_refCount( 0 )
114 : , m_pWeakConnectionPoint( 0 )
115 0 : , m_pReserved(0)
116 : {
117 : (void) rObj;
118 0 : }
119 : /** Dummy assignment operator. Does not affect reference count.
120 :
121 : @return this OWeakObject
122 : */
123 : inline OWeakObject & SAL_CALL operator = ( const OWeakObject &)
124 : SAL_THROW(())
125 : { return *this; }
126 :
127 : /** Basic queryInterface() implementation supporting \::com::sun::star::uno::XWeak and
128 : \::com::sun::star::uno::XInterface.
129 :
130 : @param rType demanded type
131 : @return demanded type or empty any
132 : */
133 : virtual ::com::sun::star::uno::Any SAL_CALL queryInterface(
134 : const ::com::sun::star::uno::Type & rType )
135 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
136 : /** increasing m_refCount
137 : */
138 : virtual void SAL_CALL acquire()
139 : throw () SAL_OVERRIDE;
140 : /** decreasing m_refCount
141 : */
142 : virtual void SAL_CALL release()
143 : throw () SAL_OVERRIDE;
144 :
145 : /** XWeak::queryAdapter() implementation
146 :
147 : @return a \::com::sun::star::uno::XAdapter reference
148 : */
149 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAdapter > SAL_CALL queryAdapter()
150 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
151 :
152 : /** Cast operator to XInterface reference.
153 :
154 : @return XInterface reference
155 : */
156 4 : inline SAL_CALL operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > () SAL_THROW(())
157 4 : { return this; }
158 : };
159 :
160 : /// @cond INTERNAL
161 : /** Convenience function for constructor-based service implementations.
162 :
163 : To be used like:
164 :
165 : css::uno::XInterface * FOO_constructor_function(...) {
166 : return cppu::acquire(new FOO(...));
167 : }
168 :
169 : @param instance
170 : Newly created instance that should be acquired.
171 : */
172 0 : static inline ::com::sun::star::uno::XInterface * acquire(OWeakObject * instance)
173 : {
174 : assert(instance != 0);
175 0 : instance->acquire();
176 0 : return instance;
177 : }
178 : /// @endcond
179 :
180 : }
181 :
182 : #endif
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|