📄 scoped_handle.hpp
字号:
///
/// \deprecated Deprecated in favour of get()
resource_type handle() const
{
#if defined(STLSOFT_COMPILER_IS_WATCOM)
return (resource_type)(m_hh.get());
#else /* ? compiler */
return const_cast<resource_type>(m_hh.get());
#endif /* compiler */
}
/// \brief Provides the bare resource handle to the caller. Does not detach the
/// handle from the managing instance.
resource_type get() const
{
return const_cast<class_type&>(*this).m_hh.get();
}
/// @}
/// \name Implementation
/// @{
private:
resource_type get_null_value_() const
{
return m_hNull;
}
/// @}
/// \name Members
/// @{
private:
holder_type m_hh; //!< The handle to the managed resource
const resource_type m_hNull; //!< The value for the null handle
void (*m_tfn)(holder_type &, degenerate_function_type); //!< The function translator function
degenerate_function_type m_fn; //!< The actual resource release function
/// @}
/// \name Not to be implemented
/// @{
private:
scoped_handle(class_type const&);
class_type& operator =(class_type const&);
/// @}
};
// scoped_handle<void>
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
STLSOFT_TEMPLATE_SPECIALISATION
class scoped_handle<void>
{
/// \name Types
/// @{
private:
typedef void (STLSOFT_CDECL *degenerate_function_type)();
public:
/// \brief The resource type
typedef void resource_type;
/// \brief The handle type
typedef void handle_type;
/// \brief The instantiation of the type
typedef scoped_handle<void> class_type;
/// @}
/// \name Construction
/// @{
public:
#ifdef STLSOFT_CF_CDECL_SUPPORTED
# if !defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED)
/// \brief Construct from a resource handle and a clean-up function with void return type
scoped_handle( void (STLSOFT_CDECL *f)())
: m_bInvoked(false)
, m_tfn(&function_translator_cdecl_void<void>::translate)
, m_fn(reinterpret_cast<degenerate_function_type>(f))
{
STLSOFT_MESSAGE_ASSERT("Precondition violation: cannot initialise with a NULL function pointer", NULL != f);
}
# endif /* !STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED */
# if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT)
/// \brief Construct from a resource handle and a clean-up function with non-void return type
template <ss_typename_param_k R>
scoped_handle( R (STLSOFT_CDECL *f)())
: m_bInvoked(false)
, m_tfn(&function_translator_cdecl_void<R>::translate)
, m_fn(reinterpret_cast<degenerate_function_type>(f))
{
STLSOFT_MESSAGE_ASSERT("Precondition violation: cannot initialise with a NULL function pointer", NULL != f);
}
# endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
#endif /* STLSOFT_CF_CDECL_SUPPORTED */
#ifdef STLSOFT_CF_FASTCALL_SUPPORTED
# if !defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED)
/// \brief Construct from a resource handle and a clean-up "fastcall" function with void return type
scoped_handle( void (STLSOFT_FASTCALL *f)())
: m_bInvoked(false)
, m_tfn(&function_translator_fastcall_void<void>::translate)
, m_fn(reinterpret_cast<degenerate_function_type>(f))
{
STLSOFT_MESSAGE_ASSERT("Precondition violation: cannot initialise with a NULL function pointer", NULL != f);
}
# endif /* !STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED */
# if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT)
/// \brief Construct from a resource handle and a clean-up "fastcall" function with non-void return type
template <ss_typename_param_k R>
scoped_handle( R (STLSOFT_FASTCALL *f)())
: m_bInvoked(false)
, m_tfn(&function_translator_fastcall_void<R>::translate)
, m_fn(reinterpret_cast<degenerate_function_type>(f))
{
STLSOFT_MESSAGE_ASSERT("Precondition violation: cannot initialise with a NULL function pointer", NULL != f);
}
# endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
#endif /* STLSOFT_CF_FASTCALL_SUPPORTED */
#ifdef STLSOFT_CF_STDCALL_SUPPORTED
# if !defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED)
/// \brief Construct from a resource handle and a clean-up "stdcall" function with void return type
scoped_handle( void (STLSOFT_STDCALL *f)())
: m_bInvoked(false)
, m_tfn(&function_translator_stdcall_void<void>::translate)
, m_fn(reinterpret_cast<degenerate_function_type>(f))
{
STLSOFT_MESSAGE_ASSERT("Precondition violation: cannot initialise with a NULL function pointer", NULL != f);
}
# endif /* !STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED */
# if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT)
/// \brief Construct from a resource handle and a clean-up "stdcall" function with non-void return type
template <ss_typename_param_k R>
scoped_handle( R (STLSOFT_STDCALL *f)())
: m_bInvoked(false)
, m_tfn(&function_translator_stdcall_void<R>::translate)
, m_fn(reinterpret_cast<degenerate_function_type>(f))
{
STLSOFT_MESSAGE_ASSERT("Precondition violation: cannot initialise with a NULL function pointer", NULL != f);
}
# endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
#endif /* STLSOFT_CF_STDCALL_SUPPORTED */
/// \brief "Releases" the managed resource.
///
/// Invokes the cleanup function, unless close() or detach() have
/// already been called
~scoped_handle()
{
close();
}
/// @}
/// \name Attributes
/// @{
public:
/// \brief Indicates whether the instance holds a non-"null" resource
bool empty() const
{
return m_bInvoked;
}
/// @}
/// \name Operations
/// @{
public:
/// \brief Closes the handle immediately
///
/// \note Calling this method more than once has no effect.
void close()
{
if(!empty())
{
m_tfn(m_fn);
m_bInvoked = true;
}
}
/// \brief Detaches the resource, and returns it to the caller.
///
/// \remarks Calling this method removes the resource from the managing
/// instance, so it will not be automatically closed.
resource_type detach()
{
m_bInvoked = true;
}
/// @}
/// \name Accessors
/// @{
public:
/// \brief Provides the bare resource handle to the caller. Does not
/// detach the handle from the managing instance.
///
/// \deprecated Deprecated in favour of get()
resource_type handle() const
{
}
/// \brief Provides the bare resource handle to the caller. Does not detach the
/// handle from the managing instance.
resource_type get() const
{
}
/// @}
/// \name Members
/// @{
private:
ss_bool_t m_bInvoked; //!< Indicates whether the cleanup function has been invoked
void (*m_tfn)(degenerate_function_type); //!< The function translator function
degenerate_function_type m_fn; //!< The actual resource release function
/// @}
/// \name Not to be implemented
/// @{
private:
scoped_handle(class_type const&);
class_type& operator =(class_type const&);
/// @}
};
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* swapping
*/
template< ss_typename_param_k H
>
inline void swap(scoped_handle<H>& lhs, scoped_handle<H>& rhs)
{
lhs.swap(rhs);
}
////////////////////////////////////////////////////////////////////////////
// Shims
template<ss_typename_param_k H>
#if defined(STLSOFT_COMPILER_IS_WATCOM)
inline H get_handle(scoped_handle<H> const& h)
#else /* ? compiler */
inline ss_typename_type_k scoped_handle<H>::handle_type get_handle(scoped_handle<H> const& h)
#endif /* compiler */
{
return h.get();
}
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/scoped_handle_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 H
>
inline void swap(stlsoft_ns_qual(scoped_handle)<H>& lhs, stlsoft_ns_qual(scoped_handle)<H>& rhs)
{
lhs.swap(rhs);
}
} // namespace std
# endif /* INTEL && _MSC_VER < 1310 */
#endif /* STLSOFT_CF_std_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Compiler warnings
*/
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER >= 1400
# pragma warning(pop)
#endif /* compiler */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -