LibreOffice
LibreOffice 24.8 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 #if !defined(__COVERITY__) // suppress COPY_INSTEAD_OF_MOVE suggestions
87 
89  Reference (Reference<reference_type> && handle) noexcept
90  : m_pBody (handle.m_pBody)
91  {
92  handle.m_pBody = nullptr;
93  }
94 #endif
95 #endif
96 
97 #if defined LIBO_INTERNAL_ONLY
98 
104  template< class derived_type >
105  inline Reference(
106  const Reference< derived_type > & rRef,
107  std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
108  : m_pBody (rRef.get())
109  {
110  if (m_pBody)
111  m_pBody->acquire();
112  }
113 
118  template< class super_type,
119  std::enable_if_t<std::is_base_of_v<super_type, reference_type>, int> = 0 >
120  inline operator css::uno::Reference<super_type>() const
121  {
122  return css::uno::Reference<super_type>(m_pBody);
123  }
124 #endif
125 
129  {
130  if (m_pBody)
131  m_pBody->release();
132  }
133 
138  SAL_CALL set (reference_type * pBody)
139  {
140  if (pBody)
141  pBody->acquire();
142  reference_type * const pOld = m_pBody;
143  m_pBody = pBody;
144  if (pOld)
145  pOld->release();
146  return *this;
147  }
148 
154  SAL_CALL operator= (const Reference<reference_type> & handle)
155  {
156  return set( handle.m_pBody );
157  }
158 
159 #ifdef LIBO_INTERNAL_ONLY
160 
167  {
168  // self-movement guts ourself
169  if (m_pBody)
170  m_pBody->release();
171  m_pBody = handle.m_pBody;
172  handle.m_pBody = nullptr;
173  return *this;
174  }
175 #endif
176 
179  Reference<reference_type> &
180  SAL_CALL operator= (reference_type * pBody)
181  {
182  return set( pBody );
183  }
184 
193  {
194  if (m_pBody)
195  {
196  reference_type * const pOld = m_pBody;
197  m_pBody = NULL;
198  pOld->release();
199  }
200  return *this;
201  }
202 
203 
208  reference_type * SAL_CALL get() const
209  {
210  return m_pBody;
211  }
212 
213 
216  reference_type * SAL_CALL operator->() const
217  {
218  assert(m_pBody != NULL);
219  return m_pBody;
220  }
221 
222 
225  reference_type & SAL_CALL operator*() const
226  {
227  assert(m_pBody != NULL);
228  return *m_pBody;
229  }
230 
231 
234  bool SAL_CALL is() const
235  {
236  return (m_pBody != NULL);
237  }
238 
239 #if defined LIBO_INTERNAL_ONLY
240 
242  explicit operator bool() const
243  {
244  return is();
245  }
246 #endif
247 
250  bool SAL_CALL operator== (const reference_type * pBody) const
251  {
252  return (m_pBody == 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 
276  bool
277  SAL_CALL operator< (const Reference<reference_type> & handle) const
278  {
279  return (m_pBody < handle.m_pBody);
280  }
281 
282 
285  bool
286  SAL_CALL operator> (const Reference<reference_type> & handle) const
287  {
288  return (m_pBody > handle.m_pBody);
289  }
290 };
291 
292 } // namespace rtl
293 
294 #if defined LIBO_INTERNAL_ONLY
295 namespace std
296 {
297 
299 
304 template<typename T>
305 struct hash<::rtl::Reference<T>>
306 {
307  std::size_t operator()(::rtl::Reference<T> const & s) const
308  { return std::size_t(s.get()); }
309 };
311 
312 }
313 
314 #endif
315 
316 #endif /* ! INCLUDED_RTL_REF_HXX */
317 
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:216
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:286
Reference()
Constructor...
Definition: ref.hxx:55
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:154
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:367
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:250
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:225
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:268
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:192
__sal_NoAcquire
Definition: types.h:370
Template reference class for reference type.
Definition: ref.hxx:45
Definition: bootstrap.hxx:33
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor...
Definition: ref.hxx:128
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:234
reference_type * get() const
Get the body.
Definition: ref.hxx:208