Line data Source code
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 : #include <osl/interlck.h>
29 :
30 : namespace rtl
31 : {
32 :
33 : /** Template reference class for reference type.
34 : */
35 : template <class reference_type>
36 : class Reference
37 : {
38 : /** The <b>reference_type</b> body pointer.
39 : */
40 : reference_type * m_pBody;
41 :
42 :
43 : public:
44 : /** Constructor...
45 : */
46 39745980 : inline Reference()
47 39745980 : : m_pBody (0)
48 39745980 : {}
49 :
50 :
51 : /** Constructor...
52 : */
53 80907023 : inline Reference (reference_type * pBody)
54 80907023 : : m_pBody (pBody)
55 : {
56 80907023 : if (m_pBody)
57 77606055 : m_pBody->acquire();
58 80907023 : }
59 :
60 :
61 : /** Copy constructor...
62 : */
63 211905038 : inline Reference (const Reference<reference_type> & handle)
64 211905038 : : m_pBody (handle.m_pBody)
65 : {
66 211905038 : if (m_pBody)
67 211291178 : m_pBody->acquire();
68 211905039 : }
69 :
70 :
71 : /** Destructor...
72 : */
73 332409514 : inline ~Reference()
74 : {
75 332409514 : if (m_pBody)
76 309311516 : m_pBody->release();
77 332409466 : }
78 :
79 : /** Set...
80 : Similar to assignment.
81 : */
82 : inline Reference<reference_type> &
83 48553258 : SAL_CALL set (reference_type * pBody)
84 : {
85 48553258 : if (pBody)
86 47702637 : pBody->acquire();
87 48553258 : reference_type * const pOld = m_pBody;
88 48553258 : m_pBody = pBody;
89 48553258 : if (pOld)
90 19546187 : pOld->release();
91 48553258 : return *this;
92 : }
93 :
94 : /** Assignment.
95 : Unbinds this instance from its body (if bound) and
96 : bind it to the body represented by the handle.
97 : */
98 : inline Reference<reference_type> &
99 34470293 : SAL_CALL operator= (const Reference<reference_type> & handle)
100 : {
101 34470293 : return set( handle.m_pBody );
102 : }
103 :
104 : /** Assignment...
105 : */
106 : inline Reference<reference_type> &
107 8713335 : SAL_CALL operator= (reference_type * pBody)
108 : {
109 8713335 : return set( pBody );
110 : }
111 :
112 : /** Unbind the body from this handle.
113 : Note that for a handle representing a large body,
114 : "handle.clear().set(new body());" _might_
115 : perform a little bit better than "handle.set(new body());",
116 : since in the second case two large objects exist in memory
117 : (the old body and the new body).
118 : */
119 7666876 : inline Reference<reference_type> & SAL_CALL clear()
120 : {
121 7666876 : if (m_pBody)
122 : {
123 7595294 : reference_type * const pOld = m_pBody;
124 7595294 : m_pBody = 0;
125 7595294 : pOld->release();
126 : }
127 7666876 : return *this;
128 : }
129 :
130 :
131 : /** Get the body. Can be used instead of operator->().
132 : I.e. handle->someBodyOp() and handle.get()->someBodyOp()
133 : are the same.
134 : */
135 71036920 : inline reference_type * SAL_CALL get() const
136 : {
137 71036920 : return m_pBody;
138 : }
139 :
140 :
141 : /** Probably most common used: handle->someBodyOp().
142 : */
143 667616322 : inline reference_type * SAL_CALL operator->() const
144 : {
145 : assert(m_pBody != 0);
146 667616322 : return m_pBody;
147 : }
148 :
149 :
150 : /** Allows (*handle).someBodyOp().
151 : */
152 6157765 : inline reference_type & SAL_CALL operator*() const
153 : {
154 : assert(m_pBody != 0);
155 6157765 : return *m_pBody;
156 : }
157 :
158 :
159 : /** Returns True if the handle does point to a valid body.
160 : */
161 320754167 : inline bool SAL_CALL is() const
162 : {
163 320754167 : return (m_pBody != 0);
164 : }
165 :
166 :
167 : /** Returns True if this points to pBody.
168 : */
169 1142298 : inline bool SAL_CALL operator== (const reference_type * pBody) const
170 : {
171 1142298 : return (m_pBody == pBody);
172 : }
173 :
174 :
175 : /** Returns True if handle points to the same body.
176 : */
177 : inline bool
178 88678 : SAL_CALL operator== (const Reference<reference_type> & handle) const
179 : {
180 88678 : return (m_pBody == handle.m_pBody);
181 : }
182 :
183 :
184 : /** Needed to place References into STL collection.
185 : */
186 : inline bool
187 6526622 : SAL_CALL operator!= (const Reference<reference_type> & handle) const
188 : {
189 6526622 : return (m_pBody != handle.m_pBody);
190 : }
191 :
192 :
193 : /** Needed to place References into STL collection.
194 : */
195 : inline bool
196 0 : SAL_CALL operator< (const Reference<reference_type> & handle) const
197 : {
198 0 : return (m_pBody < handle.m_pBody);
199 : }
200 :
201 :
202 : /** Needed to place References into STL collection.
203 : */
204 : inline bool
205 : SAL_CALL operator> (const Reference<reference_type> & handle) const
206 : {
207 : return (m_pBody > handle.m_pBody);
208 : }
209 : };
210 :
211 : /// @cond INTERNAL
212 : /** Enables boost::mem_fn and boost::bind to recognize Reference.
213 : */
214 : template <typename T>
215 90 : inline T * get_pointer( Reference<T> const& r )
216 : {
217 90 : return r.get();
218 : }
219 : /// @endcond
220 :
221 : } // namespace rtl
222 :
223 : #endif /* ! INCLUDED_RTL_REF_HXX */
224 :
225 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|