1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ref.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 #include <sal/types.h>
35 #include <osl/diagnose.h>
36 #include <osl/interlck.h>
41 /** Interface for a reference type.
46 /** @see osl_incrementInterlockedCount.
48 virtual oslInterlockedCount SAL_CALL
acquire() = 0;
50 /** @see osl_decrementInterlockedCount.
52 virtual oslInterlockedCount SAL_CALL
release() = 0;
56 /** Template reference class for reference type derived from IReference.
58 template <class reference_type
>
61 /** The <b>reference_type</b> body pointer.
63 reference_type
* m_pBody
;
76 inline Reference (reference_type
* pBody
)
84 /** Copy constructor...
86 inline Reference (const Reference
<reference_type
> & handle
)
87 : m_pBody (handle
.m_pBody
)
103 Similar to assignment.
105 inline Reference
<reference_type
> &
106 SAL_CALL
set (reference_type
* pBody
)
110 reference_type
* const pOld
= m_pBody
;
118 Unbinds this instance from its body (if bound) and
119 bind it to the body represented by the handle.
121 inline Reference
<reference_type
> &
122 SAL_CALL
operator= (const Reference
<reference_type
> & handle
)
124 return set( handle
.m_pBody
);
129 inline Reference
<reference_type
> &
130 SAL_CALL
operator= (reference_type
* pBody
)
135 /** Unbind the body from this handle.
136 Note that for a handle representing a large body,
137 "handle.clear().set(new body());" _might_
138 perform a little bit better than "handle.set(new body());",
139 since in the second case two large objects exist in memory
140 (the old body and the new body).
142 inline Reference
<reference_type
> & SAL_CALL
clear()
146 reference_type
* const pOld
= m_pBody
;
154 /** Get the body. Can be used instead of operator->().
155 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
158 inline reference_type
* SAL_CALL
get() const
164 /** Probably most common used: handle->someBodyOp().
166 inline reference_type
* SAL_CALL
operator->() const
168 OSL_PRECOND(m_pBody
, "Reference::operator->() : null body");
173 /** Allows (*handle).someBodyOp().
175 inline reference_type
& SAL_CALL
operator*() const
177 OSL_PRECOND(m_pBody
, "Reference::operator*() : null body");
182 /** Returns True if the handle does point to a valid body.
184 inline sal_Bool SAL_CALL
is() const
186 return (m_pBody
!= 0);
190 /** Returns True if this points to pBody.
192 inline sal_Bool SAL_CALL
operator== (const reference_type
* pBody
) const
194 return (m_pBody
== pBody
);
198 /** Returns True if handle points to the same body.
201 SAL_CALL
operator== (const Reference
<reference_type
> & handle
) const
203 return (m_pBody
== handle
.m_pBody
);
207 /** Needed to place References into STL collection.
210 SAL_CALL
operator!= (const Reference
<reference_type
> & handle
) const
212 return (m_pBody
!= handle
.m_pBody
);
216 /** Needed to place References into STL collection.
219 SAL_CALL
operator< (const Reference
<reference_type
> & handle
) const
221 return (m_pBody
< handle
.m_pBody
);
225 /** Needed to place References into STL collection.
228 SAL_CALL
operator> (const Reference
<reference_type
> & handle
) const
230 return (m_pBody
> handle
.m_pBody
);
235 Enables boost::mem_fn and boost::bind to recognize Reference.
237 template <typename T
>
238 inline T
* get_pointer( Reference
<T
> const& r
)
245 #endif /* !_RTL_REF_HXX_ */