bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / cppuhelper / weak.hxx
blob97397cd09a37969d2653b2107327075f1d4360db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #ifndef INCLUDED_CPPUHELPER_WEAK_HXX
20 #define INCLUDED_CPPUHELPER_WEAK_HXX
22 #include <cassert>
23 #include <cstddef>
24 #include "osl/interlck.h"
25 #include "rtl/alloc.h"
26 #include "com/sun/star/uno/XWeak.hpp"
27 #include "cppuhelper/cppuhelperdllapi.h"
30 namespace cppu
33 class OWeakConnectionPoint;
35 /** Base class to implement a UNO object supporting weak references, i.e. the object can be held
36 weakly (by a css::uno::WeakReference).
37 This implementation copes with reference counting. Upon last release(), the virtual dtor
38 is called.
40 @derive
41 Inherit from this class and delegate acquire()/ release() calls.
43 class CPPUHELPER_DLLPUBLIC OWeakObject : public css::uno::XWeak
45 friend class OWeakConnectionPoint;
47 protected:
48 /** Virtual dtor.
50 @attention
51 Despite the fact that a RuntimeException is allowed to be thrown, you must not throw any
52 exception upon destruction!
54 virtual ~OWeakObject() COVERITY_NOEXCEPT_FALSE;
56 /** disposes and resets m_pWeakConnectionPoint
57 @pre
58 m_refCount equals 0
60 void disposeWeakConnectionPoint();
62 /** reference count.
64 @attention
65 Don't modify manually! Use acquire() and release().
67 oslInterlockedCount m_refCount;
69 /// @cond INTERNAL
71 /** Container of all weak reference listeners and the connection point from the weak reference.
73 OWeakConnectionPoint * m_pWeakConnectionPoint;
75 /** reserved for future use. do not use.
77 void * m_pReserved;
79 /// @endcond
81 public:
82 /// @cond INTERNAL
83 // these are here to force memory de/allocation to sal lib.
84 static void * SAL_CALL operator new( size_t nSize )
85 { return ::rtl_allocateMemory( nSize ); }
86 static void SAL_CALL operator delete( void * pMem )
87 { ::rtl_freeMemory( pMem ); }
88 static void * SAL_CALL operator new( size_t, void * pMem )
89 { return pMem; }
90 static void SAL_CALL operator delete( void *, void * )
92 /// @endcond
94 #ifdef _MSC_VER
95 /** Default Constructor. Sets the reference count to zero.
96 Accidentally occurs in msvc mapfile = > had to be outlined.
98 OWeakObject();
99 #else
100 /** Default Constructor. Sets the reference count to zero.
102 OWeakObject()
103 : m_refCount( 0 )
104 , m_pWeakConnectionPoint( NULL )
105 , m_pReserved(NULL)
107 #endif
108 /** Dummy copy constructor. Set the reference count to zero.
110 OWeakObject( const OWeakObject & )
111 : css::uno::XWeak()
112 , m_refCount( 0 )
113 , m_pWeakConnectionPoint( NULL )
114 , m_pReserved(NULL)
116 /** Dummy assignment operator. Does not affect reference count.
118 @return this OWeakObject
120 OWeakObject & SAL_CALL operator = ( const OWeakObject &)
121 { return *this; }
123 /** Basic queryInterface() implementation supporting com::sun::star::uno::XWeak and
124 com::sun::star::uno::XInterface.
126 @param rType demanded type
127 @return demanded type or empty any
129 virtual css::uno::Any SAL_CALL queryInterface(
130 const css::uno::Type & rType ) SAL_OVERRIDE;
131 /** increasing m_refCount
133 virtual void SAL_CALL acquire()
134 throw () SAL_OVERRIDE;
135 /** decreasing m_refCount
137 virtual void SAL_CALL release()
138 throw () SAL_OVERRIDE;
140 /** XWeak::queryAdapter() implementation
142 @return a com::sun::star::uno::XAdapter reference
144 virtual css::uno::Reference< css::uno::XAdapter > SAL_CALL queryAdapter() SAL_OVERRIDE;
146 /** Cast operator to XInterface reference.
148 @return XInterface reference
150 SAL_CALL operator css::uno::Reference< css::uno::XInterface > ()
151 { return this; }
154 /// @cond INTERNAL
155 /** Convenience function for constructor-based service implementations.
157 To be used like:
159 css::uno::XInterface * FOO_constructor_function(...) {
160 return cppu::acquire(new FOO(...));
163 @param instance
164 Newly created instance that should be acquired.
166 static inline css::uno::XInterface * acquire(OWeakObject * instance)
168 assert(instance != NULL);
169 instance->acquire();
170 return instance;
172 /// @endcond
176 #endif
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */