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 <rtl/ref.hxx>
25 #include <type_traits>
28 namespace vcl
{ namespace detail
{
30 // A mechanism to enable up-casts, used by the VclReference conversion constructor,
31 // heavily borrowed from boost::is_base_and_derived
32 // (which manages to avoid compilation problems with ambiguous bases and cites
33 // comp.lang.c++.moderated mail <http://groups.google.com/groups?
34 // selm=df893da6.0301280859.522081f7%40posting.google.com> "SuperSubclass
35 // (is_base_and_derived) complete implementation!" by Rani Sharoni and cites
36 // Aleksey Gurtovoy for the workaround for MSVC), to avoid including Boost
37 // headers in URE headers (could ultimately be based on C++11 std::is_base_of):
39 template< typename T1
, typename T2
> struct UpCast
{
41 template< bool, typename U1
, typename
> struct C
44 template< typename U1
, typename U2
> struct C
< false, U1
, U2
>
47 struct S
{ char c
[2]; };
50 static char f(T2
*, long);
51 static S
f(T1
* const &, int);
53 template< typename U
> static char f(T2
*, U
);
54 static S
f(T1
*, int);
58 H(); // avoid C2514 "class has no constructors" from MSVC 2008
60 operator T1
* const & () const;
62 operator T1
* () const;
68 typedef typename C
< sizeof (f(H(), 0)) == 1, void *, void >::t t
;
71 }; }; // namespace detail, namespace vcl
73 namespace vcl
{ class Window
; }
76 * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses.
78 * For more details on the design please see vcl/README.lifecycle
80 * @param reference_type must be a subclass of vcl::Window
82 template <class reference_type
>
85 ::rtl::Reference
<reference_type
> m_rInnerRef
;
96 inline VclPtr (reference_type
* pBody
)
100 /** Constructor... that doesn't take a ref.
102 inline VclPtr (reference_type
* pBody
, __sal_NoAcquire
)
103 : m_rInnerRef(pBody
, SAL_NO_ACQUIRE
)
106 /** Copy constructor...
108 inline VclPtr (const VclPtr
<reference_type
> & handle
)
109 : m_rInnerRef (handle
.m_rInnerRef
)
112 /** Up-casting conversion constructor: Copies interface reference.
114 Does not work for up-casts to ambiguous bases. For the special case of
115 up-casting to Reference< XInterface >, see the corresponding conversion
118 @param rRef another reference
120 template< class derived_type
>
122 const VclPtr
< derived_type
> & rRef
,
123 typename ::vcl::detail::UpCast
< reference_type
, derived_type
>::t
= 0 )
124 : m_rInnerRef( static_cast<reference_type
*>(rRef
) )
129 * A construction helper for VclPtr. Since VclPtr types are created
130 * with a reference-count of one - to help fit into the existing
131 * code-flow; this helps us to construct them easily.
133 * For more details on the design please see vcl/README.lifecycle
135 * @param reference_type must be a subclass of vcl::Window
137 template<typename
... Arg
> static VclPtr
< reference_type
> Create(Arg
&&... arg
)
139 return VclPtr
< reference_type
>( new reference_type(std::forward
<Arg
>(arg
)...), SAL_NO_ACQUIRE
);
142 /** Probably most common used: handle->someBodyOp().
144 inline reference_type
* operator->() const
146 return m_rInnerRef
.get();
149 /** Get the body. Can be used instead of operator->().
150 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
153 inline reference_type
* get() const
155 return m_rInnerRef
.get();
158 inline void set(reference_type
*pBody
)
160 m_rInnerRef
.set(pBody
);
163 inline void reset(reference_type
*pBody
)
165 m_rInnerRef
.set(pBody
);
168 /** Up-casting conversion constructor: Copies interface reference.
170 Does not work for up-casts to ambiguous bases. For the special case of
171 up-casting to Reference< XInterface >, see the corresponding conversion
174 @param rRef another reference
176 template< class derived_type
, class = typename
std::enable_if
< ::vcl::detail::UpCast
< reference_type
, derived_type
>::t
>::type
>
177 inline VclPtr
<reference_type
>& operator= (derived_type
* pBody
)
179 m_rInnerRef
.set(pBody
);
183 inline operator reference_type
* () const
185 return m_rInnerRef
.get();
188 inline explicit operator bool () const
190 return m_rInnerRef
.get() != NULL
;
203 inline void disposeAndClear()
205 // hold it alive for the lifetime of this method
206 ::rtl::Reference
<reference_type
> aTmp(m_rInnerRef
);
207 m_rInnerRef
.clear(); // we should use some 'swap' method ideally ;-)
213 /** Needed to place VclPtr's into STL collection.
215 inline bool operator< (const VclPtr
<reference_type
> & handle
) const
217 return (m_rInnerRef
< handle
.m_rInnerRef
);
220 /** Needed to place VclPtr's into STL collection.
222 inline bool operator> (const VclPtr
<reference_type
> & handle
) const
224 return (m_rInnerRef
> handle
.m_rInnerRef
);
228 template<typename T1
, typename T2
>
229 inline bool operator ==(VclPtr
<T1
> const & p1
, VclPtr
<T2
> const & p2
) {
230 return p1
.get() == p2
.get();
233 template<typename T
> inline bool operator ==(VclPtr
<T
> const & p1
, T
const * p2
)
235 return p1
.get() == p2
;
238 template<typename T
> inline bool operator ==(VclPtr
<T
> const & p1
, T
* p2
) {
239 return p1
.get() == p2
;
242 template<typename T
> inline bool operator ==(T
const * p1
, VclPtr
<T
> const & p2
)
244 return p1
== p2
.get();
247 template<typename T
> inline bool operator ==(T
* p1
, VclPtr
<T
> const & p2
) {
248 return p1
== p2
.get();
251 template<typename T1
, typename T2
>
252 inline bool operator !=(VclPtr
<T1
> const & p1
, VclPtr
<T2
> const & p2
) {
256 template<typename T
> inline bool operator !=(VclPtr
<T
> const & p1
, T
const * p2
)
261 template<typename T
> inline bool operator !=(VclPtr
<T
> const & p1
, T
* p2
) {
265 template<typename T
> inline bool operator !=(T
const * p1
, VclPtr
<T
> const & p2
)
270 template<typename T
> inline bool operator !=(T
* p1
, VclPtr
<T
> const & p2
) {
275 * A construction helper for a temporary VclPtr. Since VclPtr types
276 * are created with a reference-count of one - to help fit into
277 * the existing code-flow; this helps us to construct them easily.
278 * see also VclPtr::Create and ScopedVclPtr
280 * For more details on the design please see vcl/README.lifecycle
282 * @param reference_type must be a subclass of vcl::Window
284 template <class reference_type
>
285 class VclPtrInstance
: public VclPtr
<reference_type
>
288 template<typename
... Arg
> VclPtrInstance(Arg
&&... arg
)
289 : VclPtr
<reference_type
>( new reference_type(std::forward
<Arg
>(arg
)...), SAL_NO_ACQUIRE
)
294 template <class reference_type
>
295 class ScopedVclPtr
: public VclPtr
<reference_type
>
300 inline ScopedVclPtr()
301 : VclPtr
<reference_type
>()
306 inline ScopedVclPtr (reference_type
* pBody
)
307 : VclPtr
<reference_type
>(pBody
)
310 /** Copy constructor...
312 inline ScopedVclPtr (const VclPtr
<reference_type
> & handle
)
313 : VclPtr
<reference_type
>(handle
)
317 Assignment that releases the last reference.
319 inline ScopedVclPtr
<reference_type
>& operator= (reference_type
* pBody
)
321 VclPtr
<reference_type
>::disposeAndClear();
322 VclPtr
<reference_type
>::set(pBody
);
326 /** Up-casting conversion constructor: Copies interface reference.
328 Does not work for up-casts to ambiguous bases. For the special case of
329 up-casting to Reference< XInterface >, see the corresponding conversion
332 @param rRef another reference
334 template< class derived_type
>
336 const VclPtr
< derived_type
> & rRef
,
337 typename ::vcl::detail::UpCast
< reference_type
, derived_type
>::t
= 0 )
338 : VclPtr
<reference_type
>( rRef
)
344 VclPtr
<reference_type
>::disposeAndClear();
345 assert(VclPtr
<reference_type
>::get() == nullptr); // make sure there are no lingering references
349 // Most likely we don't want this default copy-construtor.
350 ScopedVclPtr (const ScopedVclPtr
<reference_type
> &) SAL_DELETED_FUNCTION
;
351 // And certainly we don't want a default assignment operator.
352 ScopedVclPtr
<reference_type
>& operator= (const ScopedVclPtr
<reference_type
> &) SAL_DELETED_FUNCTION
;
355 inline ScopedVclPtr (reference_type
* pBody
, __sal_NoAcquire
)
356 : VclPtr
<reference_type
>(pBody
, SAL_NO_ACQUIRE
)
361 * A construction helper for ScopedVclPtr. Since VclPtr types are created
362 * with a reference-count of one - to help fit into the existing
363 * code-flow; this helps us to construct them easily.
365 * For more details on the design please see vcl/README.lifecycle
367 * @param reference_type must be a subclass of vcl::Window
369 template <class reference_type
>
370 class ScopedVclPtrInstance
: public ScopedVclPtr
<reference_type
>
373 template<typename
... Arg
> ScopedVclPtrInstance(Arg
&&... arg
)
374 : ScopedVclPtr
<reference_type
>( new reference_type(std::forward
<Arg
>(arg
)...), SAL_NO_ACQUIRE
)
379 #endif // INCLUDED_VCL_PTR_HXX
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */