00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _RTL_REF_HXX_
00021 #define _RTL_REF_HXX_
00022
00023 #include <sal/types.h>
00024 #include <osl/diagnose.h>
00025 #include <osl/interlck.h>
00026
00027 namespace rtl
00028 {
00029
00032 class IReference
00033 {
00034 public:
00037 virtual oslInterlockedCount SAL_CALL acquire() = 0;
00038
00041 virtual oslInterlockedCount SAL_CALL release() = 0;
00042
00043 #if !defined _MSC_VER // public -> protected changes mangled names there
00044 protected:
00045 #endif
00046 ~IReference() {}
00047
00048 };
00049
00050
00053 template <class reference_type>
00054 class Reference
00055 {
00058 reference_type * m_pBody;
00059
00060
00061 public:
00064 inline Reference()
00065 : m_pBody (0)
00066 {}
00067
00068
00071 inline Reference (reference_type * pBody)
00072 : m_pBody (pBody)
00073 {
00074 if (m_pBody)
00075 m_pBody->acquire();
00076 }
00077
00078
00081 inline Reference (const Reference<reference_type> & handle)
00082 : m_pBody (handle.m_pBody)
00083 {
00084 if (m_pBody)
00085 m_pBody->acquire();
00086 }
00087
00088
00091 inline ~Reference()
00092 {
00093 if (m_pBody)
00094 m_pBody->release();
00095 }
00096
00100 inline Reference<reference_type> &
00101 SAL_CALL set (reference_type * pBody)
00102 {
00103 if (pBody)
00104 pBody->acquire();
00105 reference_type * const pOld = m_pBody;
00106 m_pBody = pBody;
00107 if (pOld)
00108 pOld->release();
00109 return *this;
00110 }
00111
00116 inline Reference<reference_type> &
00117 SAL_CALL operator= (const Reference<reference_type> & handle)
00118 {
00119 return set( handle.m_pBody );
00120 }
00121
00124 inline Reference<reference_type> &
00125 SAL_CALL operator= (reference_type * pBody)
00126 {
00127 return set( pBody );
00128 }
00129
00137 inline Reference<reference_type> & SAL_CALL clear()
00138 {
00139 if (m_pBody)
00140 {
00141 reference_type * const pOld = m_pBody;
00142 m_pBody = 0;
00143 pOld->release();
00144 }
00145 return *this;
00146 }
00147
00148
00153 inline reference_type * SAL_CALL get() const
00154 {
00155 return m_pBody;
00156 }
00157
00158
00161 inline reference_type * SAL_CALL operator->() const
00162 {
00163 OSL_PRECOND(m_pBody, "Reference::operator->() : null body");
00164 return m_pBody;
00165 }
00166
00167
00170 inline reference_type & SAL_CALL operator*() const
00171 {
00172 OSL_PRECOND(m_pBody, "Reference::operator*() : null body");
00173 return *m_pBody;
00174 }
00175
00176
00179 inline sal_Bool SAL_CALL is() const
00180 {
00181 return (m_pBody != 0);
00182 }
00183
00184
00187 inline sal_Bool SAL_CALL operator== (const reference_type * pBody) const
00188 {
00189 return (m_pBody == pBody);
00190 }
00191
00192
00195 inline sal_Bool
00196 SAL_CALL operator== (const Reference<reference_type> & handle) const
00197 {
00198 return (m_pBody == handle.m_pBody);
00199 }
00200
00201
00204 inline sal_Bool
00205 SAL_CALL operator!= (const Reference<reference_type> & handle) const
00206 {
00207 return (m_pBody != handle.m_pBody);
00208 }
00209
00210
00213 inline sal_Bool
00214 SAL_CALL operator< (const Reference<reference_type> & handle) const
00215 {
00216 return (m_pBody < handle.m_pBody);
00217 }
00218
00219
00222 inline sal_Bool
00223 SAL_CALL operator> (const Reference<reference_type> & handle) const
00224 {
00225 return (m_pBody > handle.m_pBody);
00226 }
00227 };
00228
00230
00232 template <typename T>
00233 inline T * get_pointer( Reference<T> const& r )
00234 {
00235 return r.get();
00236 }
00238
00239 }
00240
00241 #endif
00242
00243