LibreOffice
LibreOffice 24.2 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 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_RTL_REF_HXX
25 #define INCLUDED_RTL_REF_HXX
26 
27 #include "sal/config.h"
28 
29 #include <cassert>
30 #include <cstddef>
31 #include <functional>
32 #ifdef LIBO_INTERNAL_ONLY
33 #include <type_traits>
35 #endif
36 
37 #include "sal/types.h"
38 
39 namespace rtl
40 {
41 
44 template <class reference_type>
45 class Reference
46 {
49  reference_type * m_pBody;
50 
51 
52 public:
56  : m_pBody (NULL)
57  {}
58 
59 
62  Reference (reference_type * pBody, __sal_NoAcquire)
63  : m_pBody (pBody)
64  {
65  }
66 
69  Reference (reference_type * pBody)
70  : m_pBody (pBody)
71  {
72  if (m_pBody)
73  m_pBody->acquire();
74  }
75 
79  : m_pBody (handle.m_pBody)
80  {
81  if (m_pBody)
82  m_pBody->acquire();
83  }
84 
85 #ifdef LIBO_INTERNAL_ONLY
86 
88  Reference (Reference<reference_type> && handle) noexcept
89  : m_pBody (handle.m_pBody)
90  {
91  handle.m_pBody = nullptr;
92  }
93 #endif
94 
95 #if defined LIBO_INTERNAL_ONLY
96 
102  template< class derived_type >
103  inline Reference(
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())
107  {
108  if (m_pBody)
109  m_pBody->acquire();
110  }
111 
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
119  {
120  return css::uno::Reference<super_type>(m_pBody);
121  }
122 #endif
123 
127  {
128  if (m_pBody)
129  m_pBody->release();
130  }
131 
136  SAL_CALL set (reference_type * pBody)
137  {
138  if (pBody)
139  pBody->acquire();
140  reference_type * const pOld = m_pBody;
141  m_pBody = pBody;
142  if (pOld)
143  pOld->release();
144  return *this;
145  }
146 
152  SAL_CALL operator= (const Reference<reference_type> & handle)
153  {
154  return set( handle.m_pBody );
155  }
156 
157 #ifdef LIBO_INTERNAL_ONLY
158 
165  {
166  // self-movement guts ourself
167  if (m_pBody)
168  m_pBody->release();
169  m_pBody = handle.m_pBody;
170  handle.m_pBody = nullptr;
171  return *this;
172  }
173 #endif
174 
177  Reference<reference_type> &
178  SAL_CALL operator= (reference_type * pBody)
179  {
180  return set( pBody );
181  }
182 
191  {
192  if (m_pBody)
193  {
194  reference_type * const pOld = m_pBody;
195  m_pBody = NULL;
196  pOld->release();
197  }
198  return *this;
199  }
200 
201 
206  reference_type * SAL_CALL get() const
207  {
208  return m_pBody;
209  }
210 
211 
214  reference_type * SAL_CALL operator->() const
215  {
216  assert(m_pBody != NULL);
217  return m_pBody;
218  }
219 
220 
223  reference_type & SAL_CALL operator*() const
224  {
225  assert(m_pBody != NULL);
226  return *m_pBody;
227  }
228 
229 
232  bool SAL_CALL is() const
233  {
234  return (m_pBody != NULL);
235  }
236 
237 #if defined LIBO_INTERNAL_ONLY
238 
240  explicit operator bool() const
241  {
242  return is();
243  }
244 #endif
245 
248  bool SAL_CALL operator== (const reference_type * pBody) const
249  {
250  return (m_pBody == pBody);
251  }
252 
253 
256  bool
257  SAL_CALL operator== (const Reference<reference_type> & handle) const
258  {
259  return (m_pBody == handle.m_pBody);
260  }
261 
262 
265  bool
266  SAL_CALL operator!= (const Reference<reference_type> & handle) const
267  {
268  return (m_pBody != handle.m_pBody);
269  }
270 
271 
274  bool
275  SAL_CALL operator< (const Reference<reference_type> & handle) const
276  {
277  return (m_pBody < handle.m_pBody);
278  }
279 
280 
283  bool
284  SAL_CALL operator> (const Reference<reference_type> & handle) const
285  {
286  return (m_pBody > handle.m_pBody);
287  }
288 };
289 
290 } // namespace rtl
291 
292 #if defined LIBO_INTERNAL_ONLY
293 namespace std
294 {
295 
297 
302 template<typename T>
303 struct hash<::rtl::Reference<T>>
304 {
305  std::size_t operator()(::rtl::Reference<T> const & s) const
306  { return std::size_t(s.get()); }
307 };
309 
310 }
311 
312 #endif
313 
314 #endif /* ! INCLUDED_RTL_REF_HXX */
315 
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:214
Reference(reference_type *pBody, __sal_NoAcquire)
Constructor...
Definition: ref.hxx:62
bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:284
Reference()
Constructor...
Definition: ref.hxx:55
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:152
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:349
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:248
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:223
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:266
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:190
__sal_NoAcquire
Definition: types.h:352
Template reference class for reference type.
Definition: ref.hxx:45
Definition: bootstrap.hxx:33
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor...
Definition: ref.hxx:126
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:69
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:78
bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:232
reference_type * get() const
Get the body.
Definition: ref.hxx:206