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 .
20 #ifndef INCLUDED_VCL_PTR_HXX
21 #define INCLUDED_VCL_PTR_HXX
23 #include <sal/config.h>
25 #include <rtl/ref.hxx>
28 #include <type_traits>
32 #include <vcl/vclmain.hxx>
36 class VclReferenceBase
;
38 namespace vcl::detail
{
41 constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
43 template<typename T
> constexpr bool isIncompleteOrDerivedFromVclReferenceBase(
45 { return std::is_base_of
<VclReferenceBase
, T
>::value
; }
47 } // namespace vcl::detail
50 * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses.
52 * For more details on the design please see vcl/README.lifecycle
54 * @param reference_type must be a subclass of vcl::Window
56 template <class reference_type
>
60 vcl::detail::isIncompleteOrDerivedFromVclReferenceBase
<reference_type
>(
62 "template argument type must be derived from VclReferenceBase");
64 ::rtl::Reference
<reference_type
> m_rInnerRef
;
75 VclPtr (reference_type
* pBody
)
79 /** Constructor... that doesn't take a ref.
81 VclPtr (reference_type
* pBody
, __sal_NoAcquire
)
82 : m_rInnerRef(pBody
, SAL_NO_ACQUIRE
)
85 /** Up-casting conversion constructor: Copies interface reference.
87 Does not work for up-casts to ambiguous bases. For the special case of
88 up-casting to Reference< XInterface >, see the corresponding conversion
91 @param rRef another reference
93 template< class derived_type
>
95 const VclPtr
< derived_type
> & rRef
,
96 typename
std::enable_if
<
97 std::is_base_of
<reference_type
, derived_type
>::value
, int>::type
99 : m_rInnerRef( static_cast<reference_type
*>(rRef
) )
103 #if defined(DBG_UTIL) && !defined(_WIN32)
106 assert(m_rInnerRef
.get() == nullptr || vclmain::isAlive());
107 // We can be one of the intermediate counts, but if we are the last
108 // VclPtr keeping this object alive, then something forgot to call dispose().
109 assert((!m_rInnerRef
.get() || m_rInnerRef
->isDisposed() || m_rInnerRef
->getRefCount() > 1)
110 && "someone forgot to call dispose()");
112 VclPtr(VclPtr
const &) = default;
113 VclPtr(VclPtr
&&) = default;
114 VclPtr
& operator =(VclPtr
const &) = default;
115 VclPtr
& operator =(VclPtr
&&) = default;
119 * A construction helper for VclPtr. Since VclPtr types are created
120 * with a reference-count of one - to help fit into the existing
121 * code-flow; this helps us to construct them easily.
123 * For more details on the design please see vcl/README.lifecycle
125 * @tparam reference_type must be a subclass of vcl::Window
127 template<typename
... Arg
> [[nodiscard
]] static VclPtr
< reference_type
> Create(Arg
&&... arg
)
129 return VclPtr
< reference_type
>( new reference_type(std::forward
<Arg
>(arg
)...), SAL_NO_ACQUIRE
);
132 /** Probably most common used: handle->someBodyOp().
134 reference_type
* operator->() const
136 return m_rInnerRef
.get();
139 /** Get the body. Can be used instead of operator->().
140 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
143 reference_type
* get() const
145 return m_rInnerRef
.get();
148 void set(reference_type
*pBody
)
150 m_rInnerRef
.set(pBody
);
153 void reset(reference_type
*pBody
)
155 m_rInnerRef
.set(pBody
);
158 /** Up-casting copy assignment operator.
160 Does not work for up-casts to ambiguous bases.
162 @param rRef another reference
164 template<typename derived_type
>
165 typename
std::enable_if
<
166 std::is_base_of
<reference_type
, derived_type
>::value
,
168 operator =(VclPtr
<derived_type
> const & rRef
)
170 m_rInnerRef
.set(rRef
.get());
174 VclPtr
& operator =(reference_type
* pBody
)
176 m_rInnerRef
.set(pBody
);
180 operator reference_type
* () const
182 return m_rInnerRef
.get();
185 explicit operator bool () const
187 return m_rInnerRef
.get() != nullptr;
200 void disposeAndClear()
202 // hold it alive for the lifetime of this method
203 ::rtl::Reference
<reference_type
> aTmp(std::move(m_rInnerRef
));
204 // coverity[use_after_move : SUPPRESS] - the move ctor above must take care of it
205 assert(!m_rInnerRef
);
211 /** Needed to place VclPtr's into STL collection.
213 bool operator< (const VclPtr
<reference_type
> & handle
) const
215 return (m_rInnerRef
< handle
.m_rInnerRef
);
219 template<typename T1
, typename T2
>
220 inline bool operator ==(VclPtr
<T1
> const & p1
, VclPtr
<T2
> const & p2
) {
221 return p1
.get() == p2
.get();
224 template<typename T
> inline bool operator ==(VclPtr
<T
> const & p1
, T
const * p2
)
226 return p1
.get() == p2
;
229 template<typename T
> inline bool operator ==(VclPtr
<T
> const & p1
, T
* p2
) {
230 return p1
.get() == p2
;
233 template<typename T
> inline bool operator ==(T
const * p1
, VclPtr
<T
> const & p2
)
235 return p1
== p2
.get();
238 template<typename T
> inline bool operator ==(T
* p1
, VclPtr
<T
> const & p2
) {
239 return p1
== p2
.get();
242 template<typename T1
, typename T2
>
243 inline bool operator !=(VclPtr
<T1
> const & p1
, VclPtr
<T2
> const & p2
) {
247 template<typename T
> inline bool operator !=(VclPtr
<T
> const & p1
, T
const * p2
)
252 template<typename T
> inline bool operator !=(VclPtr
<T
> const & p1
, T
* p2
) {
256 template<typename T
> inline bool operator !=(T
const * p1
, VclPtr
<T
> const & p2
)
261 template<typename T
> inline bool operator !=(T
* p1
, VclPtr
<T
> const & p2
) {
266 * A construction helper for a temporary VclPtr. Since VclPtr types
267 * are created with a reference-count of one - to help fit into
268 * the existing code-flow; this helps us to construct them easily.
269 * see also VclPtr::Create and ScopedVclPtr
271 * For more details on the design please see vcl/README.lifecycle
273 * @param reference_type must be a subclass of vcl::Window
275 template <class reference_type
>
276 class SAL_WARN_UNUSED VclPtrInstance final
: public VclPtr
<reference_type
>
279 template<typename
... Arg
> VclPtrInstance(Arg
&&... arg
)
280 : VclPtr
<reference_type
>( new reference_type(std::forward
<Arg
>(arg
)...), SAL_NO_ACQUIRE
)
285 * Override and disallow this, to prevent people accidentally calling it and actually
286 * getting VclPtr::Create and getting a naked VclPtr<> instance
288 template<typename
... Arg
> static VclPtrInstance
< reference_type
> Create(Arg
&&... ) = delete;
291 template <class reference_type
>
292 class ScopedVclPtr
: public VclPtr
<reference_type
>
298 : VclPtr
<reference_type
>()
303 ScopedVclPtr (reference_type
* pBody
)
304 : VclPtr
<reference_type
>(pBody
)
307 /** Copy constructor...
309 ScopedVclPtr (const VclPtr
<reference_type
> & handle
)
310 : VclPtr
<reference_type
>(handle
)
314 Assignment that releases the last reference.
316 void disposeAndReset(reference_type
*pBody
)
318 if (pBody
!= this->get()) {
319 VclPtr
<reference_type
>::disposeAndClear();
320 VclPtr
<reference_type
>::set(pBody
);
325 Assignment that releases the last reference.
327 ScopedVclPtr
<reference_type
>& operator = (reference_type
* pBody
)
329 disposeAndReset(pBody
);
333 /** Up-casting conversion constructor: Copies interface reference.
335 Does not work for up-casts to ambiguous bases. For the special case of
336 up-casting to Reference< XInterface >, see the corresponding conversion
339 @param rRef another reference
341 template< class derived_type
>
343 const VclPtr
< derived_type
> & rRef
,
344 typename
std::enable_if
<
345 std::is_base_of
<reference_type
, derived_type
>::value
, int>::type
347 : VclPtr
<reference_type
>( rRef
)
351 /** Up-casting assignment operator.
353 Does not work for up-casts to ambiguous bases.
355 @param rRef another VclPtr
357 template<typename derived_type
>
358 typename
std::enable_if
<
359 std::is_base_of
<reference_type
, derived_type
>::value
,
360 ScopedVclPtr
&>::type
361 operator =(VclPtr
<derived_type
> const & rRef
)
363 disposeAndReset(rRef
.get());
368 * Override and disallow this, to prevent people accidentally calling it and actually
369 * getting VclPtr::Create and getting a naked VclPtr<> instance
371 template<typename
... Arg
> static ScopedVclPtr
< reference_type
> Create(Arg
&&... ) = delete;
375 VclPtr
<reference_type
>::disposeAndClear();
376 assert(VclPtr
<reference_type
>::get() == nullptr); // make sure there are no lingering references
380 // Most likely we don't want this default copy-constructor.
381 ScopedVclPtr (const ScopedVclPtr
<reference_type
> &) = delete;
382 // And certainly we don't want a default assignment operator.
383 ScopedVclPtr
<reference_type
>& operator = (const ScopedVclPtr
<reference_type
> &) = delete;
384 // And disallow reset as that doesn't call disposeAndClear on the original reference
385 void reset() = delete;
386 void reset(reference_type
*pBody
) = delete;
389 ScopedVclPtr (reference_type
* pBody
, __sal_NoAcquire
)
390 : VclPtr
<reference_type
>(pBody
, SAL_NO_ACQUIRE
)
395 * A construction helper for ScopedVclPtr. Since VclPtr types are created
396 * with a reference-count of one - to help fit into the existing
397 * code-flow; this helps us to construct them easily.
399 * For more details on the design please see vcl/README.lifecycle
401 * @param reference_type must be a subclass of vcl::Window
404 #pragma warning(push)
405 #pragma warning(disable: 4521) // " multiple copy constructors specified"
407 template <class reference_type
>
408 class SAL_WARN_UNUSED ScopedVclPtrInstance final
: public ScopedVclPtr
<reference_type
>
411 template<typename
... Arg
> ScopedVclPtrInstance(Arg
&&... arg
)
412 : ScopedVclPtr
<reference_type
>( new reference_type(std::forward
<Arg
>(arg
)...), SAL_NO_ACQUIRE
)
417 * Override and disallow this, to prevent people accidentally calling it and actually
418 * getting VclPtr::Create and getting a naked VclPtr<> instance
420 template<typename
... Arg
> static ScopedVclPtrInstance
< reference_type
> Create(Arg
&&...) = delete;
423 // Prevent the above perfect forwarding ctor from hijacking (accidental)
424 // attempts at ScopedVclPtrInstance copy construction (where the hijacking
425 // would typically lead to somewhat obscure error messages); both non-const
426 // and const variants are needed here, as the ScopedVclPtr base class has a
427 // const--variant copy ctor, so the implicitly declared copy ctor for
428 // ScopedVclPtrInstance would also be the const variant, so non-const copy
429 // construction attempts would be hijacked by the perfect forwarding ctor;
430 // but if we only declared a non-const variant here, the const variant would
431 // no longer be implicitly declared (as there would already be an explicitly
432 // declared copy ctor), so const copy construction attempts would then be
433 // hijacked by the perfect forwarding ctor:
434 ScopedVclPtrInstance(ScopedVclPtrInstance
&) = delete;
435 ScopedVclPtrInstance(ScopedVclPtrInstance
const &) = delete;
441 #endif // INCLUDED_VCL_PTR_HXX
443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */