LibreOffice: rtl/ref.hxx Source File

LibreOffice LibreOffice 7.1 SDK C/C++ API Reference

ref.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
39template <class reference_type>
40 class Reference
41{
44 reference_type * m_pBody;
45
46
47public:
50 Reference()
51 : m_pBody (NULL)
52 {}
53
54
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody)
113 m_pBody->release();
114 }
115
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld)
127 pOld->release();
128 return *this;
129 }
130
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
139 }
140
141#ifdef LIBO_INTERNAL_ONLY
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody)
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL;
180 pOld->release();
181 }
182 return *this;
183 }
184
185
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
193 }
194
195
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL);
201 return m_pBody;
202 }
203
204
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL);
210 return *m_pBody;
211 }
212
213
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL);
219 }
220
221#if defined LIBO_INTERNAL_ONLY
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY
277namespace std
278{
279
281
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
config.h
types.h
__sal_NoAcquire
__sal_NoAcquire
Definition: types.h:349
COVERITY_NOEXCEPT_FALSE
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won't throw in practice,...
Definition: types.h:333
rtl
Definition: unotype.hxx:43
rtl::Reference
Template reference class for reference type.
Definition: ref.hxx:41
rtl::Reference::operator!=
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:250
rtl::Reference::operator>
bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:268
rtl::Reference::clear
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:174
rtl::Reference::set
Reference< reference_type > & set(reference_type *pBody)
Set... Similar to assignment.
Definition: ref.hxx:120
rtl::Reference::operator==
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:232
rtl::Reference::get
reference_type * get() const
Get the body.
Definition: ref.hxx:190
rtl::Reference::is
bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:216
rtl::Reference::operator*
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:207
rtl::Reference::operator->
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:198
rtl::Reference::~Reference
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor...
Definition: ref.hxx:110
rtl::Reference::Reference
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:73
rtl::Reference::operator=
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:136
rtl::Reference::Reference
Reference()
Constructor...
Definition: ref.hxx:50
rtl::Reference::operator<
bool operator<(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:259
rtl::Reference::Reference
Reference(reference_type *pBody, __sal_NoAcquire)
Constructor...
Definition: ref.hxx:57
rtl::Reference::Reference
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:64