2001-03-12 11:31:39 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* $RCSfile: ref.hxx,v $
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-10-11 08:01:10 +00:00
|
|
|
* $Revision: 1.6 $
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-10-11 08:01:10 +00:00
|
|
|
* last change: $Author: obo $ $Date: 2005-10-11 09:01:10 $
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* The Contents of this file are made available subject to
|
|
|
|
* the terms of GNU Lesser General Public License Version 2.1.
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
2005-09-08 13:40:48 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2001-03-12 11:31:39 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _RTL_REF_HXX_
|
|
|
|
#define _RTL_REF_HXX_
|
|
|
|
|
|
|
|
#ifndef _SAL_TYPES_H_
|
|
|
|
#include <sal/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _OSL_DIAGNOSE_H_
|
|
|
|
#include <osl/diagnose.h>
|
|
|
|
#endif
|
|
|
|
#ifndef _OSL_INTERLCK_H_
|
|
|
|
#include <osl/interlck.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace rtl
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Interface for a reference type.
|
|
|
|
*/
|
|
|
|
class IReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** @see osl_incrementInterlockedCount.
|
|
|
|
*/
|
|
|
|
virtual oslInterlockedCount SAL_CALL acquire() = 0;
|
|
|
|
|
|
|
|
/** @see osl_decrementInterlockedCount.
|
|
|
|
*/
|
|
|
|
virtual oslInterlockedCount SAL_CALL release() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Template reference class for reference type derived from IReference.
|
|
|
|
*/
|
|
|
|
template <class reference_type>
|
|
|
|
class Reference
|
|
|
|
{
|
|
|
|
/** The <b>reference_type</b> body pointer.
|
|
|
|
*/
|
|
|
|
reference_type * m_pBody;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** Constructor...
|
|
|
|
*/
|
|
|
|
inline Reference()
|
|
|
|
: m_pBody (0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
/** Constructor...
|
|
|
|
*/
|
|
|
|
inline Reference (reference_type * pBody)
|
|
|
|
: m_pBody (pBody)
|
|
|
|
{
|
|
|
|
if (m_pBody)
|
|
|
|
m_pBody->acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Copy constructor...
|
|
|
|
*/
|
|
|
|
inline Reference (const Reference<reference_type> & handle)
|
|
|
|
: m_pBody (handle.m_pBody)
|
|
|
|
{
|
|
|
|
if (m_pBody)
|
|
|
|
m_pBody->acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Destructor...
|
|
|
|
*/
|
|
|
|
inline ~Reference()
|
|
|
|
{
|
|
|
|
if (m_pBody)
|
|
|
|
m_pBody->release();
|
|
|
|
}
|
|
|
|
|
2003-09-04 09:56:04 +00:00
|
|
|
/** Set...
|
|
|
|
Similar to assignment.
|
2001-03-12 11:31:39 +00:00
|
|
|
*/
|
|
|
|
inline Reference<reference_type> &
|
2003-09-04 09:56:04 +00:00
|
|
|
SAL_CALL set (reference_type * pBody)
|
2001-03-12 11:31:39 +00:00
|
|
|
{
|
2003-09-04 09:56:04 +00:00
|
|
|
if (pBody)
|
|
|
|
pBody->acquire();
|
2001-03-12 11:31:39 +00:00
|
|
|
if (m_pBody)
|
|
|
|
m_pBody->release();
|
2003-09-04 09:56:04 +00:00
|
|
|
m_pBody = pBody;
|
2001-03-12 11:31:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-09-04 09:56:04 +00:00
|
|
|
/** Assignment.
|
|
|
|
Unbinds this instance from its body (if bound) and
|
|
|
|
bind it to the body represented by the handle.
|
2001-03-12 11:31:39 +00:00
|
|
|
*/
|
|
|
|
inline Reference<reference_type> &
|
2003-09-04 09:56:04 +00:00
|
|
|
SAL_CALL operator= (const Reference<reference_type> & handle)
|
2001-03-12 11:31:39 +00:00
|
|
|
{
|
2003-09-04 09:56:04 +00:00
|
|
|
return set( handle.m_pBody );
|
2001-03-12 11:31:39 +00:00
|
|
|
}
|
|
|
|
|
2003-09-04 09:56:04 +00:00
|
|
|
/** Assignment...
|
2001-03-12 11:31:39 +00:00
|
|
|
*/
|
|
|
|
inline Reference<reference_type> &
|
2003-09-04 09:56:04 +00:00
|
|
|
SAL_CALL operator= (reference_type * pBody)
|
2001-03-12 11:31:39 +00:00
|
|
|
{
|
2003-09-04 09:56:04 +00:00
|
|
|
return set( pBody );
|
2001-03-12 11:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Unbind the body from this handle.
|
2001-04-26 12:34:01 +00:00
|
|
|
Note that for a handle representing a large body,
|
|
|
|
"handle.clear().set(new body());" _might_
|
|
|
|
perform a little bit better than "handle.set(new body());",
|
|
|
|
since in the second case two large objects exist in memory
|
|
|
|
(the old body and the new body).
|
2001-03-12 11:31:39 +00:00
|
|
|
*/
|
|
|
|
inline Reference<reference_type> & SAL_CALL clear()
|
|
|
|
{
|
|
|
|
if (m_pBody)
|
|
|
|
{
|
|
|
|
m_pBody->release();
|
|
|
|
m_pBody = 0;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Get the body. Can be used instead of operator->().
|
2001-04-26 12:34:01 +00:00
|
|
|
I.e. handle->someBodyOp() and handle.get()->someBodyOp()
|
|
|
|
are the same.
|
2001-03-12 11:31:39 +00:00
|
|
|
*/
|
|
|
|
inline reference_type * SAL_CALL get() const
|
|
|
|
{
|
|
|
|
return m_pBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Probably most common used: handle->someBodyOp().
|
|
|
|
*/
|
|
|
|
inline reference_type * SAL_CALL operator->() const
|
|
|
|
{
|
|
|
|
OSL_PRECOND(m_pBody, "Reference::operator->() : null body");
|
|
|
|
return m_pBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Allows (*handle).someBodyOp().
|
|
|
|
*/
|
|
|
|
inline reference_type & SAL_CALL operator*() const
|
|
|
|
{
|
|
|
|
OSL_PRECOND(m_pBody, "Reference::operator*() : null body");
|
|
|
|
return *m_pBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns True if the handle does point to a valid body.
|
|
|
|
*/
|
2001-03-13 11:00:11 +00:00
|
|
|
inline sal_Bool SAL_CALL is() const
|
2001-03-12 11:31:39 +00:00
|
|
|
{
|
|
|
|
return (m_pBody != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns True if this points to pBody.
|
|
|
|
*/
|
|
|
|
inline sal_Bool SAL_CALL operator== (const reference_type * pBody) const
|
|
|
|
{
|
|
|
|
return (m_pBody == pBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns True if handle points to the same body.
|
|
|
|
*/
|
|
|
|
inline sal_Bool
|
|
|
|
SAL_CALL operator== (const Reference<reference_type> & handle) const
|
|
|
|
{
|
|
|
|
return (m_pBody == handle.m_pBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Needed to place References into STL collection.
|
|
|
|
*/
|
|
|
|
inline sal_Bool
|
|
|
|
SAL_CALL operator!= (const Reference<reference_type> & handle) const
|
|
|
|
{
|
|
|
|
return (m_pBody != handle.m_pBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Needed to place References into STL collection.
|
|
|
|
*/
|
|
|
|
inline sal_Bool
|
|
|
|
SAL_CALL operator< (const Reference<reference_type> & handle) const
|
|
|
|
{
|
|
|
|
return (m_pBody < handle.m_pBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Needed to place References into STL collection.
|
|
|
|
*/
|
|
|
|
inline sal_Bool
|
|
|
|
SAL_CALL operator> (const Reference<reference_type> & handle) const
|
|
|
|
{
|
|
|
|
return (m_pBody > handle.m_pBody);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-10-11 08:01:10 +00:00
|
|
|
/** @internal
|
|
|
|
Enables boost::mem_fn and boost::bind to recognize Reference.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline T * get_pointer( Reference<T> const& r )
|
|
|
|
{
|
|
|
|
return r.get();
|
|
|
|
}
|
2001-03-12 11:31:39 +00:00
|
|
|
|
|
|
|
} // namespace rtl
|
|
|
|
|
|
|
|
#endif /* !_RTL_REF_HXX_ */
|