LibreOffice
LibreOffice 5.0 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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:
45  inline Reference()
46  : m_pBody (0)
47  {}
48 
49 
52  inline Reference (reference_type * pBody, __sal_NoAcquire)
53  : m_pBody (pBody)
54  {
55  }
56 
59  inline Reference (reference_type * pBody)
60  : m_pBody (pBody)
61  {
62  if (m_pBody)
63  m_pBody->acquire();
64  }
65 
68  inline Reference (const Reference<reference_type> & handle)
69  : m_pBody (handle.m_pBody)
70  {
71  if (m_pBody)
72  m_pBody->acquire();
73  }
74 
75 
78  inline ~Reference()
79  {
80  if (m_pBody)
81  m_pBody->release();
82  }
83 
88  SAL_CALL set (reference_type * pBody)
89  {
90  if (pBody)
91  pBody->acquire();
92  reference_type * const pOld = m_pBody;
93  m_pBody = pBody;
94  if (pOld)
95  pOld->release();
96  return *this;
97  }
98 
104  SAL_CALL operator= (const Reference<reference_type> & handle)
105  {
106  return set( handle.m_pBody );
107  }
108 
112  SAL_CALL operator= (reference_type * pBody)
113  {
114  return set( pBody );
115  }
116 
124  inline Reference<reference_type> & SAL_CALL clear()
125  {
126  if (m_pBody)
127  {
128  reference_type * const pOld = m_pBody;
129  m_pBody = 0;
130  pOld->release();
131  }
132  return *this;
133  }
134 
135 
140  inline reference_type * SAL_CALL get() const
141  {
142  return m_pBody;
143  }
144 
145 
148  inline reference_type * SAL_CALL operator->() const
149  {
150  assert(m_pBody != 0);
151  return m_pBody;
152  }
153 
154 
157  inline reference_type & SAL_CALL operator*() const
158  {
159  assert(m_pBody != 0);
160  return *m_pBody;
161  }
162 
163 
166  inline bool SAL_CALL is() const
167  {
168  return (m_pBody != 0);
169  }
170 
171 
174  inline bool SAL_CALL operator== (const reference_type * pBody) const
175  {
176  return (m_pBody == pBody);
177  }
178 
179 
182  inline bool
183  SAL_CALL operator== (const Reference<reference_type> & handle) const
184  {
185  return (m_pBody == handle.m_pBody);
186  }
187 
188 
191  inline bool
192  SAL_CALL operator!= (const Reference<reference_type> & handle) const
193  {
194  return (m_pBody != handle.m_pBody);
195  }
196 
197 
200  inline bool
201  SAL_CALL operator< (const Reference<reference_type> & handle) const
202  {
203  return (m_pBody < handle.m_pBody);
204  }
205 
206 
209  inline bool
210  SAL_CALL operator> (const Reference<reference_type> & handle) const
211  {
212  return (m_pBody > handle.m_pBody);
213  }
214 };
215 
217 
219 template <typename T>
220 inline T * get_pointer( Reference<T> const& r )
221 {
222  return r.get();
223 }
225 
226 } // namespace rtl
227 
228 #endif /* ! INCLUDED_RTL_REF_HXX */
229 
230 /* 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:148
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:174
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:104
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:59
__sal_NoAcquire
Definition: types.h:378
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:124
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:157
bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:166
Reference(reference_type *pBody, __sal_NoAcquire)
Constructor...
Definition: ref.hxx:52
bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:210
~Reference()
Destructor...
Definition: ref.hxx:78
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:68
Reference< reference_type > & set(reference_type *pBody)
Set...
Definition: ref.hxx:88
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:192
Definition: bootstrap.hxx:24
Template reference class for reference type.
Definition: ref.hxx:35