📄 ref_ptr.hpp
字号:
return *this;
}
#if !defined(STLSOFT_COMPILER_IS_MSVC) || \
( _MSC_VER > 1100 && \
_MSC_VER != 1300)
/// \brief Copy assignment from an instance of ref_ptr with a different counted_type (but
/// the same interface type).
///
/// \note This function template uses the copy constructor template, and has the same
/// instantiation restrictions
///
/// \note It is strongly exception-safe, as long as the implementations of the
/// add-ref and release functions - as utilised in the \c add_reference() and
/// \c release_reference() control shims - do not throw (which they must not).
template< ss_typename_param_k T2
, ss_typename_param_k U2
>
class_type& operator =(ref_ptr<T2, I, U2>& rhs)
{
class_type t(rhs);
t.swap(*this);
return *this;
}
#endif /* compiler */
#if !defined(STLSOFT_COMPILER_IS_INTEL) && \
!defined(STLSOFT_COMPILER_IS_MWERKS) && \
0
template< ss_typename_param_k I2
, ss_typename_param_k U2
>
class_type& operator =(ref_ptr<T, I2, U2>& rhs)
{
class_type t(rhs);
t.swap(*this);
return *this;
}
#endif /* compiler */
/// @}
/// \name Operations
/// @{
public:
/// \brief Swaps the managed instance of \c this with \c rhs
///
/// \note It provides the no-throw guarantee
void swap(class_type& rhs)
{
interface_type *t = rhs.m_pi;
rhs.m_pi = m_pi;
m_pi = t;
}
/// \brief Assigns a reference-counted type to the smart pointer.
///
/// \param c Pointer to a counted_type. May be NULL
/// \param bAddRef parameter that determines whether reference will be
/// <i>consumed</i> (<code>false</code>) or <i>borrowed</i>
/// (<code>true</code>).
void set(counted_type *c, bool_type bAddRef)
{
class_type t(c, bAddRef);
t.swap(*this);
}
/// Closes the instance, releasing the managed pointer.
///
/// \note Calling this method more than once has no effect.
void close()
{
if(NULL != m_pi)
{
release_reference(m_pi);
m_pi = NULL;
}
}
/// \brief Detaches the managed instance, and returns it to the caller, which
/// takes responsibility for ensuring that the resource is not leaked
counted_type *detach()
{
counted_type *r = class_type::c_from_i(m_pi);
m_pi = NULL;
return r;
}
/// @}
/// \name Equality Comparison
/// @{
public:
/// \brief Evaluates whether two instances are equal
bool_type equal(class_type const& rhs) const
{
return m_pi == rhs.m_pi;
}
/// @}
/// \name Accessors
/// @{
public:
/// \brief Determines whether the instance is empty
bool_type empty() const
{
return NULL == m_pi;
}
/// \brief Determines whether the instance is empty
bool_type operator !() const
{
return empty();
}
/// \brief Provides raw-pointer access to the instance
counted_type* get()
{
return class_type::c_from_i(m_pi);
}
/// \brief Provides raw-pointer access to the instance
counted_type const* get() const
{
return class_type::c_from_i(m_pi);
}
/// \brief Returns the interface pointer
///
/// \pre The instance must not be empty; otherwise behaviour is
/// undefined
counted_type* operator ->()
{
STLSOFT_MESSAGE_ASSERT("Dereferencing a NULL pointer!", NULL != m_pi);
return class_type::c_from_i(m_pi);
}
/// \brief Returns the interface pointer
///
/// \pre The instance must not be empty; otherwise behaviour is
/// undefined
counted_type const* operator ->() const
{
STLSOFT_MESSAGE_ASSERT("Dereferencing a NULL pointer!", NULL != m_pi);
return class_type::c_from_i(m_pi);
}
/// \brief Returns a reference to the managed instance
///
/// \pre The instance must not be empty; otherwise behaviour is
/// undefined
counted_type& operator *()
{
STLSOFT_MESSAGE_ASSERT("Dereferencing a NULL pointer!", NULL != m_pi);
return *class_type::c_from_i(m_pi);
}
/// \brief Returns a reference to the managed instance
///
/// \pre The instance must not be empty; otherwise behaviour is
/// undefined
counted_type const& operator *() const
{
STLSOFT_MESSAGE_ASSERT("Dereferencing a NULL pointer!", NULL != m_pi);
return *class_type::c_from_i(m_pi);
}
/// @}
/// \name Members
/// @{
private:
interface_type *m_pi;
/// @}
};
/* /////////////////////////////////////////////////////////////////////////
* Operators
*/
template< ss_typename_param_k T
, ss_typename_param_k I
, ss_typename_param_k U
>
inline ss_bool_t operator ==(ref_ptr<T, I, U> const& lhs, ref_ptr<T, I, U> const& rhs)
{
return lhs.equal(rhs);
}
template< ss_typename_param_k T
, ss_typename_param_k I
, ss_typename_param_k U
>
inline ss_bool_t operator !=(ref_ptr<T, I, U> const& lhs, ref_ptr<T, I, U> const& rhs)
{
return !lhs.equal(rhs);
}
/* /////////////////////////////////////////////////////////////////////////
* swapping
*/
template< ss_typename_param_k T
, ss_typename_param_k I
, ss_typename_param_k U
>
inline void swap(ref_ptr<T, I, U>& lhs, ref_ptr<T, I, U>& rhs)
{
lhs.swap(rhs);
}
/* /////////////////////////////////////////////////////////////////////////
* Shims
*/
/** \brief is_empty shim
*
* \ingroup group__library__smart_pointers
*/
template< ss_typename_param_k T
, ss_typename_param_k I /* = T */
, ss_typename_param_k U /* = I */
>
inline ss_bool_t is_empty(ref_ptr<T, I, U> const& p)
{
return NULL == p.get();
}
/** \brief get_ptr shim
*
* \ingroup group__library__smart_pointers
*/
template< ss_typename_param_k T
, ss_typename_param_k I /* = T */
, ss_typename_param_k U /* = I */
>
inline T *get_ptr(ref_ptr<T, I, U> &p)
{
return p.get();
}
/** \brief get_ptr shim
*
* \ingroup group__library__smart_pointers
*/
template< ss_typename_param_k T
, ss_typename_param_k I /* = T */
, ss_typename_param_k U /* = I */
>
inline T const* get_ptr(ref_ptr<T, I, U> const& p)
{
return p.get();
}
/** \brief Insertion operator shim
*
* \ingroup group__library__smart_pointers
*/
template< ss_typename_param_k S
, ss_typename_param_k T
, ss_typename_param_k I /* = T */
, ss_typename_param_k U /* = I */
>
inline S& operator <<(S& s, ref_ptr<T, I, U> const& p)
{
return s << *p;
}
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/ref_ptr_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _STLSOFT_NO_NAMESPACE
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */
/* In the special case of Intel behaving as VC++ 7.0 or earlier on Win32, we
* illegally insert into the std namespace.
*/
#if defined(STLSOFT_CF_std_NAMESPACE)
# if ( ( defined(STLSOFT_COMPILER_IS_INTEL) && \
defined(_MSC_VER))) && \
_MSC_VER < 1310
namespace std
{
template< ss_typename_param_k T
, ss_typename_param_k I
, ss_typename_param_k U
>
inline void swap(stlsoft_ns_qual(ref_ptr)<T, I, U>& lhs, stlsoft_ns_qual(ref_ptr)<T, I, U>& rhs)
{
lhs.swap(rhs);
}
} // namespace std
# endif /* INTEL && _MSC_VER < 1310 */
#endif /* STLSOFT_CF_std_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_REF_PTR */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -