LibreOffice
LibreOffice 4.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 
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)
53  : m_pBody (pBody)
54  {
55  if (m_pBody)
56  m_pBody->acquire();
57  }
58 
59 
62  inline Reference (const Reference<reference_type> & handle)
63  : m_pBody (handle.m_pBody)
64  {
65  if (m_pBody)
66  m_pBody->acquire();
67  }
68 
69 
72  inline ~Reference()
73  {
74  if (m_pBody)
75  m_pBody->release();
76  }
77 
82  SAL_CALL set (reference_type * pBody)
83  {
84  if (pBody)
85  pBody->acquire();
86  reference_type * const pOld = m_pBody;
87  m_pBody = pBody;
88  if (pOld)
89  pOld->release();
90  return *this;
91  }
92 
98  SAL_CALL operator= (const Reference<reference_type> & handle)
99  {
100  return set( handle.m_pBody );
101  }
102 
106  SAL_CALL operator= (reference_type * pBody)
107  {
108  return set( pBody );
109  }
110 
118  inline Reference<reference_type> & SAL_CALL clear()
119  {
120  if (m_pBody)
121  {
122  reference_type * const pOld = m_pBody;
123  m_pBody = 0;
124  pOld->release();
125  }
126  return *this;
127  }
128 
129 
134  inline reference_type * SAL_CALL get() const
135  {
136  return m_pBody;
137  }
138 
139 
142  inline reference_type * SAL_CALL operator->() const
143  {
144  assert(m_pBody != 0);
145  return m_pBody;
146  }
147 
148 
151  inline reference_type & SAL_CALL operator*() const
152  {
153  assert(m_pBody != 0);
154  return *m_pBody;
155  }
156 
157 
160  inline bool SAL_CALL is() const
161  {
162  return (m_pBody != 0);
163  }
164 
165 
168  inline bool SAL_CALL operator== (const reference_type * pBody) const
169  {
170  return (m_pBody == pBody);
171  }
172 
173 
176  inline bool
177  SAL_CALL operator== (const Reference<reference_type> & handle) const
178  {
179  return (m_pBody == handle.m_pBody);
180  }
181 
182 
185  inline bool
186  SAL_CALL operator!= (const Reference<reference_type> & handle) const
187  {
188  return (m_pBody != handle.m_pBody);
189  }
190 
191 
194  inline bool
195  SAL_CALL operator< (const Reference<reference_type> & handle) const
196  {
197  return (m_pBody < handle.m_pBody);
198  }
199 
200 
203  inline bool
204  SAL_CALL operator> (const Reference<reference_type> & handle) const
205  {
206  return (m_pBody > handle.m_pBody);
207  }
208 };
209 
211 
213 template <typename T>
214 inline T * get_pointer( Reference<T> const& r )
215 {
216  return r.get();
217 }
219 
220 } // namespace rtl
221 
222 #endif /* ! INCLUDED_RTL_REF_HXX */
223 
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:142
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:98
bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:204
Definition: bootstrap.hxx:24
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:52
bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:160
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:118
Reference< reference_type > & set(reference_type *pBody)
Set...
Definition: ref.hxx:82
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:62
bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:186
Template reference class for reference type.
Definition: ref.hxx:35
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:151
~Reference()
Destructor...
Definition: ref.hxx:72
Reference()
Constructor...
Definition: ref.hxx:45
bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:168