LibreOffice
LibreOffice 25.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 #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 
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
105  {
106  return Reference<super_type>(m_pBody);
107  }
108 
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
116  {
117  return css::uno::Reference<super_type>(m_pBody);
118  }
119 #endif
120 
124  {
125  if (m_pBody)
126  m_pBody->release();
127  }
128 
133  SAL_CALL set (reference_type * pBody)
134  {
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;
142  }
143 
149  SAL_CALL operator= (const Reference<reference_type> & handle)
150  {
151  return set( handle.m_pBody );
152  }
153 
154 #ifdef LIBO_INTERNAL_ONLY
155 
162  {
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;
169  }
170 #endif
171 
174  Reference<reference_type> &
175  SAL_CALL operator= (reference_type * pBody)
176  {
177  return set( pBody );
178  }
179 
188  {
189  if (m_pBody)
190  {
191  reference_type * const pOld = m_pBody;
192  m_pBody = NULL;
193  pOld->release();
194  }
195  return *this;
196  }
197 
198 
203  reference_type * SAL_CALL get() const
204  {
205  return m_pBody;
206  }
207 
208 
211  reference_type * SAL_CALL operator->() const
212  {
213  assert(m_pBody != NULL);
214  return m_pBody;
215  }
216 
217 
220  reference_type & SAL_CALL operator*() const
221  {
222  assert(m_pBody != NULL);
223  return *m_pBody;
224  }
225 
226 
229  bool SAL_CALL is() const
230  {
231  return (m_pBody != NULL);
232  }
233 
234 #if defined LIBO_INTERNAL_ONLY
235 
237  explicit operator bool() const
238  {
239  return is();
240  }
241 #endif
242 
245  bool SAL_CALL operator== (const reference_type * pBody) const
246  {
247  return (m_pBody == pBody);
248  }
249 
250 
253  bool
254  SAL_CALL operator== (const Reference<reference_type> & handle) const
255  {
256  return (m_pBody == handle.m_pBody);
257  }
258 
259 
262  bool
263  SAL_CALL operator!= (const Reference<reference_type> & handle) const
264  {
265  return (m_pBody != handle.m_pBody);
266  }
267 
268 
271  bool
272  SAL_CALL operator< (const Reference<reference_type> & handle) const
273  {
274  return (m_pBody < handle.m_pBody);
275  }
276 
277 
280  bool
281  SAL_CALL operator> (const Reference<reference_type> & handle) const
282  {
283  return (m_pBody > handle.m_pBody);
284  }
285 };
286 
287 } // namespace rtl
288 
289 #if defined LIBO_INTERNAL_ONLY
290 namespace std
291 {
292 
294 
299 template<typename T>
300 struct hash<::rtl::Reference<T>>
301 {
302  std::size_t operator()(::rtl::Reference<T> const & s) const
303  { return std::size_t(s.get()); }
304 };
306 
307 }
308 
309 #endif
310 
311 #endif /* ! INCLUDED_RTL_REF_HXX */
312 
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:211
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:281
Reference()
Constructor...
Definition: ref.hxx:55
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:149
#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:245
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:220
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:263
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:187
__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:123
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:229
reference_type * get() const
Get the body.
Definition: ref.hxx:203