1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 * This file is part of LibreOffice published API.
23 #ifndef INCLUDED_CPPUHELPER_WEAK_HXX
24 #define INCLUDED_CPPUHELPER_WEAK_HXX
28 #include "osl/interlck.h"
29 #include "rtl/alloc.h"
30 #include "com/sun/star/uno/XWeak.hpp"
31 #include "cppuhelper/cppuhelperdllapi.h"
37 class OWeakConnectionPoint
;
39 /** Base class to implement a UNO object supporting weak references, i.e. the object can be held
40 weakly (by a css::uno::WeakReference).
41 This implementation copes with reference counting. Upon last release(), the virtual dtor
45 Inherit from this class and delegate acquire()/ release() calls.
47 class CPPUHELPER_DLLPUBLIC OWeakObject
: public css::uno::XWeak
49 friend class OWeakConnectionPoint
;
55 Despite the fact that a RuntimeException is allowed to be thrown, you must not throw any
56 exception upon destruction!
58 virtual ~OWeakObject() COVERITY_NOEXCEPT_FALSE
;
60 /** disposes and resets m_pWeakConnectionPoint
64 void disposeWeakConnectionPoint();
69 Don't modify manually! Use acquire() and release().
71 oslInterlockedCount m_refCount
;
75 /** Container of all weak reference listeners and the connection point from the weak reference.
77 OWeakConnectionPoint
* m_pWeakConnectionPoint
;
79 /** reserved for future use. do not use.
87 // these are here to force memory de/allocation to sal lib.
88 static void * SAL_CALL
operator new( size_t nSize
)
89 { return ::rtl_allocateMemory( nSize
); }
90 static void SAL_CALL
operator delete( void * pMem
)
91 { ::rtl_freeMemory( pMem
); }
92 static void * SAL_CALL
operator new( size_t, void * pMem
)
94 static void SAL_CALL
operator delete( void *, void * )
98 /** Default Constructor. Sets the reference count to zero.
102 , m_pWeakConnectionPoint( NULL
)
106 /** Dummy copy constructor. Set the reference count to zero.
108 OWeakObject( const OWeakObject
& )
111 , m_pWeakConnectionPoint( NULL
)
114 /** Dummy assignment operator. Does not affect reference count.
116 @return this OWeakObject
118 OWeakObject
& SAL_CALL
operator = ( const OWeakObject
&)
121 /** Basic queryInterface() implementation supporting com::sun::star::uno::XWeak and
122 com::sun::star::uno::XInterface.
124 @param rType demanded type
125 @return demanded type or empty any
127 virtual css::uno::Any SAL_CALL
queryInterface(
128 const css::uno::Type
& rType
) SAL_OVERRIDE
;
129 /** increasing m_refCount
131 virtual void SAL_CALL
acquire()
132 SAL_NOEXCEPT SAL_OVERRIDE
;
133 /** decreasing m_refCount
135 virtual void SAL_CALL
release()
136 SAL_NOEXCEPT SAL_OVERRIDE
;
138 /** XWeak::queryAdapter() implementation
140 @return a com::sun::star::uno::XAdapter reference
142 virtual css::uno::Reference
< css::uno::XAdapter
> SAL_CALL
queryAdapter() SAL_OVERRIDE
;
144 /** Cast operator to XInterface reference.
146 @return XInterface reference
148 SAL_CALL
operator css::uno::Reference
< css::uno::XInterface
> ()
151 #if defined LIBO_INTERNAL_ONLY
152 css::uno::XWeak
* getXWeak() { return this; }
157 /** Convenience function for constructor-based service implementations.
161 css::uno::XInterface * FOO_constructor_function(...) {
162 return cppu::acquire(new FOO(...));
166 Newly created instance that should be acquired.
168 static inline css::uno::XInterface
* acquire(OWeakObject
* instance
)
170 assert(instance
!= NULL
);
175 #if defined LIBO_INTERNAL_ONLY
176 static inline css::uno::XWeak
* getXWeak(OWeakObject
* instance
) { return instance
; }
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */