LibreOffice
LibreOffice 6.0 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 
27 #include <sal/types.h>
28 
29 namespace rtl
30 {
31 
34 template <class reference_type>
35 class Reference
36 {
39  reference_type * m_pBody;
40 
41 
42 public:
46  : m_pBody (NULL)
47  {}
48 
49 
52  Reference (reference_type * pBody, __sal_NoAcquire)
53  : m_pBody (pBody)
54  {
55  }
56 
59  Reference (reference_type * pBody)
60  : m_pBody (pBody)
61  {
62  if (m_pBody)
63  m_pBody->acquire();
64  }
65 
69  : m_pBody (handle.m_pBody)
70  {
71  if (m_pBody)
72  m_pBody->acquire();
73  }
74 
75 #ifdef LIBO_INTERNAL_ONLY
76 
79  : m_pBody (handle.m_pBody)
80  {
81  handle.m_pBody = nullptr;
82  }
83 #endif
84 
88  {
89  if (m_pBody)
90  m_pBody->release();
91  }
92 
97  SAL_CALL set (reference_type * pBody)
98  {
99  if (pBody)
100  pBody->acquire();
101  reference_type * const pOld = m_pBody;
102  m_pBody = pBody;
103  if (pOld)
104  pOld->release();
105  return *this;
106  }
107 
113  SAL_CALL operator= (const Reference<reference_type> & handle)
114  {
115  return set( handle.m_pBody );
116  }
117 
118 #ifdef LIBO_INTERNAL_ONLY
119 
126  {
127  // self-movement guts ourself
128  if (m_pBody)
129  m_pBody->release();
130  m_pBody = handle.m_pBody;
131  handle.m_pBody = nullptr;
132  return *this;
133  }
134 #endif
135 
139  SAL_CALL operator= (reference_type * pBody)
140  {
141  return set( pBody );
142  }
143 
152  {
153  if (m_pBody)
154  {
155  reference_type * const pOld = m_pBody;
156  m_pBody = NULL;
157  pOld->release();
158  }
159  return *this;
160  }
161 
162 
167  reference_type * SAL_CALL get() const
168  {
169  return m_pBody;
170  }
171 
172 
175  reference_type * SAL_CALL operator->() const
176  {
177  assert(m_pBody != NULL);
178  return m_pBody;
179  }
180 
181 
184  reference_type & SAL_CALL operator*() const
185  {
186  assert(m_pBody != NULL);
187  return *m_pBody;
188  }
189 
190 
193  bool SAL_CALL is() const
194  {
195  return (m_pBody != NULL);
196  }
197 
198 #if defined LIBO_INTERNAL_ONLY
199 
201  explicit operator bool() const
202  {
203  return is();
204  }
205 #endif
206 
209  bool SAL_CALL operator== (const reference_type * pBody) const
210  {
211  return (m_pBody == pBody);
212  }
213 
214 
217  bool
218  SAL_CALL operator== (const Reference<reference_type> & handle) const
219  {
220  return (m_pBody == handle.m_pBody);
221  }
222 
223 
226  bool
227  SAL_CALL operator!= (const Reference<reference_type> & handle) const
228  {
229  return (m_pBody != handle.m_pBody);
230  }
231 
232 
235  bool
236  SAL_CALL operator< (const Reference<reference_type> & handle) const
237  {
238  return (m_pBody < handle.m_pBody);
239  }
240 
241 
244  bool
245  SAL_CALL operator> (const Reference<reference_type> & handle) const
246  {
247  return (m_pBody > handle.m_pBody);
248  }
249 };
250 
251 } // namespace rtl
252 
253 #endif /* ! INCLUDED_RTL_REF_HXX */
254 
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference()
Constructor...
Definition: ref.hxx:45
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:175
#define COVERITY_NOEXCEPT_FALSE
To markup destructors that coverity warns might throw exceptions which won&#39;t throw in practice...
Definition: types.h:361
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:209
__sal_NoAcquire
Definition: types.h:376
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:113
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:59
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:151
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:184
bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:193
Reference(reference_type *pBody, __sal_NoAcquire)
Constructor...
Definition: ref.hxx:52
Definition: bootstrap.hxx:29
bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:245
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:68
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:227
~Reference() COVERITY_NOEXCEPT_FALSE
Destructor...
Definition: ref.hxx:87
Template reference class for reference type.
Definition: ref.hxx:35