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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_REF_HXX
25 #define INCLUDED_RTL_REF_HXX
27 #include "sal/config.h"
32 #ifdef LIBO_INTERNAL_ONLY
33 #include <type_traits>
34 #include "com/sun/star/uno/Reference.h"
37 #include "sal/types.h"
42 /** Template reference class for reference type.
44 template <class reference_type
>
47 /** The <b>reference_type</b> body pointer.
49 reference_type
* m_pBody
;
62 Reference (reference_type
* pBody
, __sal_NoAcquire
)
69 Reference (reference_type
* pBody
)
76 /** Copy constructor...
78 Reference (const Reference
<reference_type
> & handle
)
79 : m_pBody (handle
.m_pBody
)
85 #ifdef LIBO_INTERNAL_ONLY
86 /** Move constructor...
88 Reference (Reference
<reference_type
> && handle
) noexcept
89 : m_pBody (handle
.m_pBody
)
91 handle
.m_pBody
= nullptr;
95 #if defined LIBO_INTERNAL_ONLY
96 /** Up-casting conversion constructor: Copies interface reference.
98 Does not work for up-casts to ambiguous bases.
100 @param rRef another reference
102 template< class derived_type
>
104 const Reference
< derived_type
> & rRef
,
105 std::enable_if_t
<std::is_base_of_v
<reference_type
, derived_type
>, int> = 0 )
106 : m_pBody (rRef
.get())
112 /** Up-casting conversion operator to convert to css::uno::Interface
114 Does not work for up-casts to ambiguous bases.
116 template< class super_type
,
117 std::enable_if_t
<std::is_base_of_v
<super_type
, reference_type
>, int> = 0 >
118 inline operator css::uno::Reference
<super_type
>() const
120 return css::uno::Reference
<super_type
>(m_pBody
);
126 ~Reference() COVERITY_NOEXCEPT_FALSE
133 Similar to assignment.
135 Reference
<reference_type
> &
136 SAL_CALL
set (reference_type
* pBody
)
140 reference_type
* const pOld
= m_pBody
;
148 Unbinds this instance from its body (if bound) and
149 bind it to the body represented by the handle.
151 Reference
<reference_type
> &
152 SAL_CALL
operator= (const Reference
<reference_type
> & handle
)
154 return set( handle
.m_pBody
);
157 #ifdef LIBO_INTERNAL_ONLY
159 * Unbinds this instance from its body (if bound),
160 * bind it to the body represented by the handle, and
161 * set the body represented by the handle to nullptr.
163 Reference
<reference_type
> &
164 operator= (Reference
<reference_type
> && handle
)
166 // self-movement guts ourself
169 m_pBody
= handle
.m_pBody
;
170 handle
.m_pBody
= nullptr;
177 Reference
<reference_type
> &
178 SAL_CALL
operator= (reference_type
* pBody
)
183 /** Unbind the body from this handle.
184 Note that for a handle representing a large body,
185 "handle.clear().set(new body());" _might_
186 perform a little bit better than "handle.set(new body());",
187 since in the second case two large objects exist in memory
188 (the old body and the new body).
190 Reference
<reference_type
> & SAL_CALL
clear()
194 reference_type
* const pOld
= m_pBody
;
202 /** Get the body. Can be used instead of operator->().
203 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
206 reference_type
* SAL_CALL
get() const
212 /** Probably most common used: handle->someBodyOp().
214 reference_type
* SAL_CALL
operator->() const
216 assert(m_pBody
!= NULL
);
221 /** Allows (*handle).someBodyOp().
223 reference_type
& SAL_CALL
operator*() const
225 assert(m_pBody
!= NULL
);
230 /** Returns True if the handle does point to a valid body.
232 bool SAL_CALL
is() const
234 return (m_pBody
!= NULL
);
237 #if defined LIBO_INTERNAL_ONLY
238 /** Returns True if the handle does point to a valid body.
240 explicit operator bool() const
246 /** Returns True if this points to pBody.
248 bool SAL_CALL
operator== (const reference_type
* pBody
) const
250 return (m_pBody
== pBody
);
254 /** Returns True if handle points to the same body.
257 SAL_CALL
operator== (const Reference
<reference_type
> & handle
) const
259 return (m_pBody
== handle
.m_pBody
);
263 /** Needed to place References into STL collection.
266 SAL_CALL
operator!= (const Reference
<reference_type
> & handle
) const
268 return (m_pBody
!= handle
.m_pBody
);
272 /** Needed to place References into STL collection.
275 SAL_CALL
operator< (const Reference
<reference_type
> & handle
) const
277 return (m_pBody
< handle
.m_pBody
);
281 /** Needed to place References into STL collection.
284 SAL_CALL
operator> (const Reference
<reference_type
> & handle
) const
286 return (m_pBody
> handle
.m_pBody
);
292 #if defined LIBO_INTERNAL_ONLY
298 Make rtl::Reference hashable by default for use in STL containers.
300 @since LibreOffice 6.3
303 struct hash
<::rtl::Reference
<T
>>
305 std::size_t operator()(::rtl::Reference
<T
> const & s
) const
306 { return std::size_t(s
.get()); }
314 #endif /* ! INCLUDED_RTL_REF_HXX */
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */