Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / include / cppuhelper / weak.hxx
bloba1a920ce4193f0cb777fef991662fa52f4496a47
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 .
21 * This file is part of LibreOffice published API.
23 #ifndef INCLUDED_CPPUHELPER_WEAK_HXX
24 #define INCLUDED_CPPUHELPER_WEAK_HXX
26 #include <cassert>
27 #include <cstddef>
28 #include "osl/interlck.h"
29 #include "rtl/alloc.h"
30 #include "com/sun/star/uno/XWeak.hpp"
31 #include "cppuhelper/cppuhelperdllapi.h"
34 namespace cppu
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
42 is called.
44 @derive
45 Inherit from this class and delegate acquire()/ release() calls.
47 class CPPUHELPER_DLLPUBLIC OWeakObject : public css::uno::XWeak
49 friend class OWeakConnectionPoint;
51 protected:
52 /** Virtual dtor.
54 @attention
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
61 @pre
62 m_refCount equals 0
64 void disposeWeakConnectionPoint();
66 /** reference count.
68 @attention
69 Don't modify manually! Use acquire() and release().
71 oslInterlockedCount m_refCount;
73 /// @cond INTERNAL
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.
81 void * m_pReserved;
83 /// @endcond
85 public:
86 /// @cond INTERNAL
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 )
93 { return pMem; }
94 static void SAL_CALL operator delete( void *, void * )
96 /// @endcond
98 /** Default Constructor. Sets the reference count to zero.
100 OWeakObject()
101 : m_refCount( 0 )
102 , m_pWeakConnectionPoint( NULL )
103 , m_pReserved(NULL)
106 /** Dummy copy constructor. Set the reference count to zero.
108 OWeakObject( const OWeakObject & )
109 : css::uno::XWeak()
110 , m_refCount( 0 )
111 , m_pWeakConnectionPoint( NULL )
112 , m_pReserved(NULL)
114 /** Dummy assignment operator. Does not affect reference count.
116 @return this OWeakObject
118 OWeakObject & SAL_CALL operator = ( const OWeakObject &)
119 { return *this; }
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 > ()
149 { return this; }
151 #if defined LIBO_INTERNAL_ONLY
152 css::uno::XWeak* getXWeak() { return this; }
153 #endif
156 /// @cond INTERNAL
157 /** Convenience function for constructor-based service implementations.
159 To be used like:
161 css::uno::XInterface * FOO_constructor_function(...) {
162 return cppu::acquire(new FOO(...));
165 @param instance
166 Newly created instance that should be acquired.
168 static inline css::uno::XInterface * acquire(OWeakObject * instance)
170 assert(instance != NULL);
171 instance->acquire();
172 return instance;
175 #if defined LIBO_INTERNAL_ONLY
176 static inline css::uno::XWeak* getXWeak(OWeakObject* instance) { return instance; }
177 #endif
178 /// @endcond
182 #endif
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */