vcl: Drop local var pointing to self in WindowData ctor
[LibreOffice.git] / include / rtl / ref.hxx
blob88e67824816e3ed490b46e142cc8e9d6b0ffe4af
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.
24 #ifndef INCLUDED_RTL_REF_HXX
25 #define INCLUDED_RTL_REF_HXX
27 #include "sal/config.h"
29 #include <cassert>
30 #include <cstddef>
31 #include <functional>
32 #ifdef LIBO_INTERNAL_ONLY
33 #include <type_traits>
34 #include "com/sun/star/uno/Reference.h"
35 #endif
37 #include "sal/types.h"
39 namespace rtl
42 /** Template reference class for reference type.
44 template <class reference_type>
45 class Reference
47 /** The <b>reference_type</b> body pointer.
49 reference_type * m_pBody;
52 public:
53 /** Constructor...
55 Reference()
56 : m_pBody (NULL)
60 /** Constructor...
62 Reference (reference_type * pBody, __sal_NoAcquire)
63 : m_pBody (pBody)
67 /** Constructor...
69 Reference (reference_type * pBody)
70 : m_pBody (pBody)
72 if (m_pBody)
73 m_pBody->acquire();
76 /** Copy constructor...
78 Reference (const Reference<reference_type> & handle)
79 : m_pBody (handle.m_pBody)
81 if (m_pBody)
82 m_pBody->acquire();
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;
94 #endif
95 #endif
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);
119 #endif
121 /** Destructor...
123 ~Reference() COVERITY_NOEXCEPT_FALSE
125 if (m_pBody)
126 m_pBody->release();
129 /** Set...
130 Similar to assignment.
132 Reference<reference_type> &
133 SAL_CALL set (reference_type * pBody)
135 if (pBody)
136 pBody->acquire();
137 reference_type * const pOld = m_pBody;
138 m_pBody = pBody;
139 if (pOld)
140 pOld->release();
141 return *this;
144 /** Assignment.
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
155 /** Assignment.
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
164 if (m_pBody)
165 m_pBody->release();
166 m_pBody = handle.m_pBody;
167 handle.m_pBody = nullptr;
168 return *this;
170 #endif
172 /** Assignment...
174 Reference<reference_type> &
175 SAL_CALL operator= (reference_type * pBody)
177 return set( 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()
189 if (m_pBody)
191 reference_type * const pOld = m_pBody;
192 m_pBody = NULL;
193 pOld->release();
195 return *this;
199 /** Get the body. Can be used instead of operator->().
200 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
201 are the same.
203 reference_type * SAL_CALL get() const
205 return m_pBody;
209 /** Probably most common used: handle->someBodyOp().
211 reference_type * SAL_CALL operator->() const
213 assert(m_pBody != NULL);
214 return m_pBody;
218 /** Allows (*handle).someBodyOp().
220 reference_type & SAL_CALL operator*() const
222 assert(m_pBody != NULL);
223 return *m_pBody;
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
239 return is();
241 #endif
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.
253 bool
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.
262 bool
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.
271 bool
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.
280 bool
281 SAL_CALL operator> (const Reference<reference_type> & handle) const
283 return (m_pBody > handle.m_pBody);
287 } // namespace rtl
289 #if defined LIBO_INTERNAL_ONLY
290 namespace std
293 /// @cond INTERNAL
295 Make rtl::Reference hashable by default for use in STL containers.
297 @since LibreOffice 6.3
299 template<typename T>
300 struct hash<::rtl::Reference<T>>
302 std::size_t operator()(::rtl::Reference<T> const & s) const
303 { return std::size_t(s.get()); }
305 /// @endcond
309 #endif
311 #endif /* ! INCLUDED_RTL_REF_HXX */
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */