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 #if !defined(__COVERITY__) // suppress COPY_INSTEAD_OF_MOVE suggestions
87 /** Move constructor...
89 Reference (Reference
<reference_type
> && handle
) noexcept
90 : m_pBody (handle
.m_pBody
)
92 handle
.m_pBody
= nullptr;
97 #if defined LIBO_INTERNAL_ONLY
98 /** Up-casting conversion operator
100 Does not work for up-casts to ambiguous bases.
102 template <class super_type
,
103 std::enable_if_t
<std::is_base_of_v
<super_type
, reference_type
>, int> = 0>
104 inline operator Reference
<super_type
>() const
106 return Reference
<super_type
>(m_pBody
);
109 /** Up-casting conversion operator to convert to css::uno::Interface
111 Does not work for up-casts to ambiguous bases.
113 template< class super_type
,
114 std::enable_if_t
<std::is_base_of_v
<super_type
, reference_type
>, int> = 0 >
115 inline operator css::uno::Reference
<super_type
>() const
117 return css::uno::Reference
<super_type
>(m_pBody
);
123 ~Reference() COVERITY_NOEXCEPT_FALSE
130 Similar to assignment.
132 Reference
<reference_type
> &
133 SAL_CALL
set (reference_type
* pBody
)
137 reference_type
* const pOld
= m_pBody
;
145 Unbinds this instance from its body (if bound) and
146 bind it to the body represented by the handle.
148 Reference
<reference_type
> &
149 SAL_CALL
operator= (const Reference
<reference_type
> & handle
)
151 return set( handle
.m_pBody
);
154 #ifdef LIBO_INTERNAL_ONLY
156 * Unbinds this instance from its body (if bound),
157 * bind it to the body represented by the handle, and
158 * set the body represented by the handle to nullptr.
160 Reference
<reference_type
> &
161 operator= (Reference
<reference_type
> && handle
)
163 // self-movement guts ourself
166 m_pBody
= handle
.m_pBody
;
167 handle
.m_pBody
= nullptr;
174 Reference
<reference_type
> &
175 SAL_CALL
operator= (reference_type
* pBody
)
180 /** Unbind the body from this handle.
181 Note that for a handle representing a large body,
182 "handle.clear().set(new body());" _might_
183 perform a little bit better than "handle.set(new body());",
184 since in the second case two large objects exist in memory
185 (the old body and the new body).
187 Reference
<reference_type
> & SAL_CALL
clear()
191 reference_type
* const pOld
= m_pBody
;
199 /** Get the body. Can be used instead of operator->().
200 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
203 reference_type
* SAL_CALL
get() const
209 /** Probably most common used: handle->someBodyOp().
211 reference_type
* SAL_CALL
operator->() const
213 assert(m_pBody
!= NULL
);
218 /** Allows (*handle).someBodyOp().
220 reference_type
& SAL_CALL
operator*() const
222 assert(m_pBody
!= NULL
);
227 /** Returns True if the handle does point to a valid body.
229 bool SAL_CALL
is() const
231 return (m_pBody
!= NULL
);
234 #if defined LIBO_INTERNAL_ONLY
235 /** Returns True if the handle does point to a valid body.
237 explicit operator bool() const
243 /** Returns True if this points to pBody.
245 bool SAL_CALL
operator== (const reference_type
* pBody
) const
247 return (m_pBody
== pBody
);
251 /** Returns True if handle points to the same body.
254 SAL_CALL
operator== (const Reference
<reference_type
> & handle
) const
256 return (m_pBody
== handle
.m_pBody
);
260 /** Needed to place References into STL collection.
263 SAL_CALL
operator!= (const Reference
<reference_type
> & handle
) const
265 return (m_pBody
!= handle
.m_pBody
);
269 /** Needed to place References into STL collection.
272 SAL_CALL
operator< (const Reference
<reference_type
> & handle
) const
274 return (m_pBody
< handle
.m_pBody
);
278 /** Needed to place References into STL collection.
281 SAL_CALL
operator> (const Reference
<reference_type
> & handle
) const
283 return (m_pBody
> handle
.m_pBody
);
289 #if defined LIBO_INTERNAL_ONLY
295 Make rtl::Reference hashable by default for use in STL containers.
297 @since LibreOffice 6.3
300 struct hash
<::rtl::Reference
<T
>>
302 std::size_t operator()(::rtl::Reference
<T
> const & s
) const
303 { return std::size_t(s
.get()); }
311 #endif /* ! INCLUDED_RTL_REF_HXX */
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */