LibreOffice
LibreOffice 6.4 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 
29 #include "sal/types.h"
30 
31 namespace rtl
32 {
33 
36 template <class reference_type>
37 class Reference
38 {
41  reference_type * m_pBody;
42 
43 
44 public:
48  : m_pBody (NULL)
49  {}
50 
51 
54  Reference (reference_type * pBody, __sal_NoAcquire)
55  : m_pBody (pBody)
56  {
57  }
58 
61  Reference (reference_type * pBody)
62  : m_pBody (pBody)
63  {
64  if (m_pBody)
65  m_pBody->acquire();
66  }
67 
71  : m_pBody (handle.m_pBody)
72  {
73  if (m_pBody)
74  m_pBody->acquire();
75  }
76 
77 #ifdef LIBO_INTERNAL_ONLY
78 
80  Reference (Reference<reference_type> && handle) noexcept
81  : m_pBody (handle.m_pBody)
82  {
83  handle.m_pBody = nullptr;
84  }
85 #endif
86 
90  {
91  if (m_pBody)
92  m_pBody->release();
93  }
94 
99  SAL_CALL set (reference_type * pBody)
100  {
101  if (pBody)
102  pBody->acquire();
103  reference_type * const pOld = m_pBody;
104  m_pBody = pBody;
105  if (pOld)
106  pOld->release();
107  return *this;
108  }
109 
115  SAL_CALL operator= (const Reference<reference_type> & handle)
116  {
117  return set( handle.m_pBody );
118  }
119 
120 #ifdef LIBO_INTERNAL_ONLY
121 
128  {
129  // self-movement guts ourself
130  if (m_pBody)
131  m_pBody->release();
132  m_pBody = handle.m_pBody;
133  handle.m_pBody = nullptr;
134  return *this;
135  }
136 #endif
137 
140  Reference<reference_type> &
141  SAL_CALL operator= (reference_type * pBody)
142  {
143  return set( pBody );
144  }
145 
154  {
155  if (m_pBody)
156  {
157  reference_type * const pOld = m_pBody;
158  m_pBody = NULL;
159  pOld->release();
160  }
161  return *this;
162  }
163 
164 
169  reference_type * SAL_CALL get() const
170  {
171  return m_pBody;
172  }
173 
174 
177  reference_type * SAL_CALL operator->() const
178  {
179  assert(m_pBody != NULL);
180  return m_pBody;
181  }
182 
183 
186  reference_type & SAL_CALL operator*() const
187  {
188  assert(m_pBody != NULL);
189  return *m_pBody;
190  }
191 
192 
195  bool SAL_CALL is() const
196  {
197  return (m_pBody != NULL);
198  }
199 
200 #if defined LIBO_INTERNAL_ONLY
201 
203  explicit operator bool() const
204  {
205  return is();
206  }
207 #endif
208 
211  bool SAL_CALL operator== (const reference_type * pBody) const
212  {
213  return (m_pBody == pBody);
214  }
215 
216 
219  bool
220  SAL_CALL operator== (const Reference<reference_type> & handle) const
221  {
222  return (m_pBody == handle.m_pBody);
223  }
224 
225 
228  bool
229  SAL_CALL operator!= (const Reference<reference_type> & handle) const
230  {
231  return (m_pBody != handle.m_pBody);
232  }
233 
234 
237  bool
238  SAL_CALL operator< (const Reference<reference_type> & handle) const
239  {
240  return (m_pBody < handle.m_pBody);
241  }
242 
243 
246  bool
247  SAL_CALL operator> (const Reference<reference_type> & handle) const
248  {
249  return (m_pBody > handle.m_pBody);
250  }
251 };
252 
253 } // namespace rtl
254 
255 #if defined LIBO_INTERNAL_ONLY
256 namespace std
257 {
258 
260 
265 template<typename T>
266 struct hash<::rtl::Reference<T>>
267 {
268  std::size_t operator()(::rtl::Reference<T> const & s) const
269  { return std::size_t(s.get()); }
270 };
272 
273 }
274 
275 #endif
276 
277 #endif /* ! INCLUDED_RTL_REF_HXX */
278 
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:177
Reference(reference_type *pBody, __sal_NoAcquire)
Constructor...
Definition: ref.hxx:54
bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:247
Reference()
Constructor...
Definition: ref.hxx:47
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:115
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:355
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:211
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:186
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:229
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:153
__sal_NoAcquire
Definition: types.h:370
Template reference class for reference type.
Definition: ref.hxx:37
Definition: bootstrap.hxx:29
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor...
Definition: ref.hxx:89
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:61
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:70
bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:195
reference_type * get() const
Get the body.
Definition: ref.hxx:169