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 _RTL_REF_HXX_
21 : #define _RTL_REF_HXX_
22 :
23 : #include <sal/types.h>
24 : #include <osl/diagnose.h>
25 : #include <osl/interlck.h>
26 :
27 : namespace rtl
28 : {
29 :
30 : /** Interface for a reference type.
31 : */
32 989395 : class IReference
33 : {
34 : public:
35 : /** @see osl_incrementInterlockedCount.
36 : */
37 : virtual oslInterlockedCount SAL_CALL acquire() = 0;
38 :
39 : /** @see osl_decrementInterlockedCount.
40 : */
41 : virtual oslInterlockedCount SAL_CALL release() = 0;
42 :
43 : #if !defined _MSC_VER // public -> protected changes mangled names there
44 : protected:
45 : #endif
46 987511 : ~IReference() {}
47 : // avoid warnings about virtual members and non-virtual dtor
48 : };
49 :
50 :
51 : /** Template reference class for reference type derived from IReference.
52 : */
53 : template <class reference_type>
54 : class Reference
55 : {
56 : /** The <b>reference_type</b> body pointer.
57 : */
58 : reference_type * m_pBody;
59 :
60 :
61 : public:
62 : /** Constructor...
63 : */
64 6376573 : inline Reference()
65 6376573 : : m_pBody (0)
66 6376573 : {}
67 :
68 :
69 : /** Constructor...
70 : */
71 7508516 : inline Reference (reference_type * pBody)
72 7508516 : : m_pBody (pBody)
73 : {
74 7508516 : if (m_pBody)
75 7448322 : m_pBody->acquire();
76 7508516 : }
77 :
78 :
79 : /** Copy constructor...
80 : */
81 34060973 : inline Reference (const Reference<reference_type> & handle)
82 34060973 : : m_pBody (handle.m_pBody)
83 : {
84 34060973 : if (m_pBody)
85 26765098 : m_pBody->acquire();
86 34060973 : }
87 :
88 :
89 : /** Destructor...
90 : */
91 47843579 : inline ~Reference()
92 : {
93 47843579 : if (m_pBody)
94 36370996 : m_pBody->release();
95 47843579 : }
96 :
97 : /** Set...
98 : Similar to assignment.
99 : */
100 : inline Reference<reference_type> &
101 16126770 : SAL_CALL set (reference_type * pBody)
102 : {
103 16126770 : if (pBody)
104 13802227 : pBody->acquire();
105 16126770 : reference_type * const pOld = m_pBody;
106 16126770 : m_pBody = pBody;
107 16126770 : if (pOld)
108 10884156 : pOld->release();
109 16126770 : return *this;
110 : }
111 :
112 : /** Assignment.
113 : Unbinds this instance from its body (if bound) and
114 : bind it to the body represented by the handle.
115 : */
116 : inline Reference<reference_type> &
117 15861685 : SAL_CALL operator= (const Reference<reference_type> & handle)
118 : {
119 15861685 : return set( handle.m_pBody );
120 : }
121 :
122 : /** Assignment...
123 : */
124 : inline Reference<reference_type> &
125 136207 : SAL_CALL operator= (reference_type * pBody)
126 : {
127 136207 : return set( pBody );
128 : }
129 :
130 : /** Unbind the body from this handle.
131 : Note that for a handle representing a large body,
132 : "handle.clear().set(new body());" _might_
133 : perform a little bit better than "handle.set(new body());",
134 : since in the second case two large objects exist in memory
135 : (the old body and the new body).
136 : */
137 728157 : inline Reference<reference_type> & SAL_CALL clear()
138 : {
139 728157 : if (m_pBody)
140 : {
141 663262 : reference_type * const pOld = m_pBody;
142 663262 : m_pBody = 0;
143 663262 : pOld->release();
144 : }
145 728157 : return *this;
146 : }
147 :
148 :
149 : /** Get the body. Can be used instead of operator->().
150 : I.e. handle->someBodyOp() and handle.get()->someBodyOp()
151 : are the same.
152 : */
153 3942206 : inline reference_type * SAL_CALL get() const
154 : {
155 3942206 : return m_pBody;
156 : }
157 :
158 :
159 : /** Probably most common used: handle->someBodyOp().
160 : */
161 33642931 : inline reference_type * SAL_CALL operator->() const
162 : {
163 : OSL_PRECOND(m_pBody, "Reference::operator->() : null body");
164 33642931 : return m_pBody;
165 : }
166 :
167 :
168 : /** Allows (*handle).someBodyOp().
169 : */
170 2371753 : inline reference_type & SAL_CALL operator*() const
171 : {
172 : OSL_PRECOND(m_pBody, "Reference::operator*() : null body");
173 2371753 : return *m_pBody;
174 : }
175 :
176 :
177 : /** Returns True if the handle does point to a valid body.
178 : */
179 30510616 : inline sal_Bool SAL_CALL is() const
180 : {
181 30510616 : return (m_pBody != 0);
182 : }
183 :
184 :
185 : /** Returns True if this points to pBody.
186 : */
187 30716 : inline sal_Bool SAL_CALL operator== (const reference_type * pBody) const
188 : {
189 30716 : return (m_pBody == pBody);
190 : }
191 :
192 :
193 : /** Returns True if handle points to the same body.
194 : */
195 : inline sal_Bool
196 1461 : SAL_CALL operator== (const Reference<reference_type> & handle) const
197 : {
198 1461 : return (m_pBody == handle.m_pBody);
199 : }
200 :
201 :
202 : /** Needed to place References into STL collection.
203 : */
204 : inline sal_Bool
205 125232 : SAL_CALL operator!= (const Reference<reference_type> & handle) const
206 : {
207 125232 : return (m_pBody != handle.m_pBody);
208 : }
209 :
210 :
211 : /** Needed to place References into STL collection.
212 : */
213 : inline sal_Bool
214 0 : SAL_CALL operator< (const Reference<reference_type> & handle) const
215 : {
216 0 : return (m_pBody < handle.m_pBody);
217 : }
218 :
219 :
220 : /** Needed to place References into STL collection.
221 : */
222 : inline sal_Bool
223 : SAL_CALL operator> (const Reference<reference_type> & handle) const
224 : {
225 : return (m_pBody > handle.m_pBody);
226 : }
227 : };
228 :
229 : /// @cond INTERNAL
230 : /** Enables boost::mem_fn and boost::bind to recognize Reference.
231 : */
232 : template <typename T>
233 0 : inline T * get_pointer( Reference<T> const& r )
234 : {
235 0 : return r.get();
236 : }
237 : /// @endcond
238 :
239 : } // namespace rtl
240 :
241 : #endif /* !_RTL_REF_HXX_ */
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|