Branch libreoffice-5-0-4
[LibreOffice.git] / include / cppuhelper / weak.hxx
blob9719db1a2e62836de746921ef06934480c00ffcb
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 <osl/interlck.h>
24 #include <rtl/alloc.h>
25 #include <com/sun/star/uno/XWeak.hpp>
26 #include <cppuhelper/cppuhelperdllapi.h>
29 namespace cppu
32 class OWeakConnectionPoint;
34 /** Base class to implement an UNO object supporting weak references, i.e. the object can be held
35 weakly (by a ::com::sun::star::uno::WeakReference).
36 This implementation copes with reference counting. Upon last release(), the virtual dtor
37 is called.
39 @derive
40 Inherit from this class and delegate acquire()/ release() calls.
42 class CPPUHELPER_DLLPUBLIC OWeakObject : public ::com::sun::star::uno::XWeak
44 friend class OWeakConnectionPoint;
46 protected:
47 /** Virtual dtor.
49 @attention
50 Despite the fact that a RuntimeException is allowed to be thrown, you must not throw any
51 exception upon destruction!
53 virtual ~OWeakObject();
55 /** disposes and resets m_pWeakConnectionPoint
56 @pre
57 m_refCount equals 0
59 void disposeWeakConnectionPoint();
61 /** reference count.
63 @attention
64 Don't modify manually! Use acquire() and release().
66 oslInterlockedCount m_refCount;
68 /// @cond INTERNAL
70 /** Container of all weak reference listeners and the connection point from the weak reference.
72 OWeakConnectionPoint * m_pWeakConnectionPoint;
74 /** reserved for future use. do not use.
76 void * m_pReserved;
78 /// @endcond
80 public:
81 /// @cond INTERNAL
82 // these are here to force memory de/allocation to sal lib.
83 inline static void * SAL_CALL operator new( size_t nSize )
84 { return ::rtl_allocateMemory( nSize ); }
85 inline static void SAL_CALL operator delete( void * pMem )
86 { ::rtl_freeMemory( pMem ); }
87 inline static void * SAL_CALL operator new( size_t, void * pMem )
88 { return pMem; }
89 inline static void SAL_CALL operator delete( void *, void * )
91 /// @endcond
93 #ifdef _MSC_VER
94 /** Default Constructor. Sets the reference count to zero.
95 Accidentally occurs in msvc mapfile = > had to be outlined.
97 OWeakObject();
98 #else
99 /** Default Constructor. Sets the reference count to zero.
101 inline OWeakObject()
102 : m_refCount( 0 )
103 , m_pWeakConnectionPoint( 0 )
104 , m_pReserved(0)
106 #endif
107 /** Dummy copy constructor. Set the reference count to zero.
109 @param rObj dummy param
111 inline OWeakObject( const OWeakObject & rObj )
112 : com::sun::star::uno::XWeak()
113 , m_refCount( 0 )
114 , m_pWeakConnectionPoint( 0 )
115 , m_pReserved(0)
117 (void) rObj;
119 /** Dummy assignment operator. Does not affect reference count.
121 @return this OWeakObject
123 inline OWeakObject & SAL_CALL operator = ( const OWeakObject &)
124 { return *this; }
126 /** Basic queryInterface() implementation supporting \::com::sun::star::uno::XWeak and
127 \::com::sun::star::uno::XInterface.
129 @param rType demanded type
130 @return demanded type or empty any
132 virtual ::com::sun::star::uno::Any SAL_CALL queryInterface(
133 const ::com::sun::star::uno::Type & rType )
134 throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
135 /** increasing m_refCount
137 virtual void SAL_CALL acquire()
138 throw () SAL_OVERRIDE;
139 /** decreasing m_refCount
141 virtual void SAL_CALL release()
142 throw () SAL_OVERRIDE;
144 /** XWeak::queryAdapter() implementation
146 @return a \::com::sun::star::uno::XAdapter reference
148 virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAdapter > SAL_CALL queryAdapter()
149 throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
151 /** Cast operator to XInterface reference.
153 @return XInterface reference
155 inline SAL_CALL operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > ()
156 { return this; }
159 /// @cond INTERNAL
160 /** Convenience function for constructor-based service implementations.
162 To be used like:
164 css::uno::XInterface * FOO_constructor_function(...) {
165 return cppu::acquire(new FOO(...));
168 @param instance
169 Newly created instance that should be acquired.
171 static inline ::com::sun::star::uno::XInterface * acquire(OWeakObject * instance)
173 assert(instance != 0);
174 instance->acquire();
175 return instance;
177 /// @endcond
181 #endif
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */