⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dl_call.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 5 页
字号:
    {
        return this;
    }
};

template<   int                 CC
        ,   ss_typename_param_k S
        >
struct function_descriptor
    : public function_descriptor_base
{
    enum { value = CC };

    ss_explicit_k function_descriptor(S const& functionName)
        : FunctionName(functionName)
        , CallingConvention(CC)
    {
#if defined(STLSOFT_CF_FASTCALL_SUPPORTED)
# if defined(STLSOFT_CF_STDCALL_SUPPORTED)
        STLSOFT_STATIC_ASSERT(  CC == calling_convention::cdeclCallConv ||  CC == calling_convention::fastcallCallConv ||  CC == calling_convention::stdcallCallConv);
# else /* ? STLSOFT_CF_STDCALL_SUPPORTED */
        STLSOFT_STATIC_ASSERT(  CC == calling_convention::cdeclCallConv ||  CC == calling_convention::fastcallCallConv);
# endif /* STLSOFT_CF_STDCALL_SUPPORTED */
#else /* ? STLSOFT_CF_FASTCALL_SUPPORTED */
# if defined(STLSOFT_CF_STDCALL_SUPPORTED)
        STLSOFT_STATIC_ASSERT(  CC == calling_convention::cdeclCallConv ||  CC == calling_convention::stdcallCallConv);
# else /* ? STLSOFT_CF_STDCALL_SUPPORTED */
        STLSOFT_STATIC_ASSERT(  CC == calling_convention::cdeclCallConv);
# endif /* STLSOFT_CF_STDCALL_SUPPORTED */
#endif /* STLSOFT_CF_FASTCALL_SUPPORTED */
    }

    ss_explicit_k function_descriptor(S const& functionName, int cc)
        : FunctionName(functionName)
        , CallingConvention(cc)
    {}

    S const     &FunctionName;
    const int   CallingConvention;

private:
    function_descriptor& operator =(function_descriptor const&);
};

template<   int                 cc
        ,   ss_typename_param_k S
        >
inline function_descriptor<cc, S> fn_desc(S const& functionName)
{
    return function_descriptor<cc, S>(functionName);
}

template<   ss_typename_param_k    S
        >
inline function_descriptor<0, S> fn_desc(int cc, S const& functionName)
{
    return function_descriptor<0, S>(functionName, cc);
}

/* /////////////////////////////////////////////////////////////////////////
 * Traits
 */

/** \brief Traits class that provides a mechanism for declaring specific
 *   (e.g. aggregate and user-defined) types to be compatible with
 *   \link winstl::dl_call dl_call()\endlink.
 *
 * \ingroup group__library__dl
 *
 * To specify your type being dl_call()-compatible, simply specialise the
 * traits template as follows (for the notional type <code>MyType</code>):
 *
\code
namespace MyNamespace
{
  class MyType
  {};

} // MyNamespace

namespace winstl
{
  template <>
  struct is_valid_dl_call_arg<MyNamespace::MyType>
  {
    enum { value = 1 };
  };
} // namespace winstl
\endcode
 */
template<ss_typename_param_k T>
struct is_valid_dl_call_arg
{
    enum { value = 0 };
};

/* Internal traits class used by the DL Library.
 *
 * \note This is a struct, rather than a namespace, because namespaces are
 *        open, and we want this to be closed.
 */
struct dl_call_traits
{
/// \name Member Types
/// @{
public:
    typedef FARPROC         entry_point_type;
    typedef HINSTANCE       library_handle_type;
    typedef winstl::module  module_wrapper_type;
/// @}

/// \name Dynamic Library Functions
/// @{
public:
    static entry_point_type get_symbol(library_handle_type hLib, char const* functionName)
    {
        return ::GetProcAddress(hLib, functionName);
    }
/// @}

/// \name Function Descriptor Discrimination
/// @{
public:
    struct is_not_fd
    {};

    struct is_fd
    {};
/// @}

/// \name Module Discrimination
/// @{
public:
    struct library_is_handle
    {};

    struct library_is_not_handle
    {};
/// @}

};


// These structures used for selecting lock_name_() function templates
template<   ss_typename_param_k T
        >
inline T const& lock_name_(T const& t, dl_call_traits::is_not_fd)
{
    return t;
}

template<   int                 cc
        ,   ss_typename_param_k S
        >
inline S const& lock_name_(function_descriptor<cc, S> const& fd, dl_call_traits::is_fd)
{
    return fd.FunctionName;
}

inline dl_call_traits::is_fd test_fd_(function_descriptor_base const*)
{
    return dl_call_traits::is_fd();
}

inline dl_call_traits::is_not_fd test_fd_(...)
{
    return dl_call_traits::is_not_fd();
}

#if defined(STLSOFT_COMPILER_IS_MSVC) || \
    defined(STLSOFT_COMPILER_IS_GCCx)
inline dl_call_traits::library_is_handle test_library_(dl_call_traits::library_handle_type )
{
    return dl_call_traits::library_is_handle();
}
#else /* ? compiler */
inline dl_call_traits::library_is_handle test_library_(dl_call_traits::library_handle_type const&)
{
    return dl_call_traits::library_is_handle();
}
#endif /* compiler */

template <ss_typename_param_k T>
inline dl_call_traits::library_is_not_handle test_library_(T const&)
{
    return dl_call_traits::library_is_not_handle();
}

/* /////////////////////////////////////////////////////////////////////////
 * Helper functions
 */

inline dl_call_traits::entry_point_type lookup_symbol_(dl_call_traits::library_handle_type hinst, char const* functionName)
{
    dl_call_traits::entry_point_type    fp  =   dl_call_traits::get_symbol(hinst, functionName);

    if(NULL == fp)
    {
        STLSOFT_THROW_X(missing_entry_point_exception(functionName, ::GetLastError()));
    }

    return fp;
}

template <ss_typename_param_k C>
inline calling_convention::calling_convention determine_calling_convention_(C const*& functionName)
{
    typedef stlsoft::basic_string_view<C>   string_t;

    calling_convention::calling_convention  cc = calling_convention::cdeclCallConv;
    string_t                                s0;
    string_t                                s1;

    if(stlsoft::split(functionName, ':', s0, s1))
    {
#ifdef STLSOFT_CF_CDECL_SUPPORTED
        if( s0 == "C" ||
            s0 == "cdecl")
        {
                cc = calling_convention::cdeclCallConv;
        } else
#endif /* STLSOFT_CF_CDECL_SUPPORTED */
#ifdef STLSOFT_CF_FASTCALL_SUPPORTED
        if( s0 == "F" ||
            s0 == "fastcall")
        {
                cc = calling_convention::fastcallCallConv;
        } else
#endif // STLSOFT_CF_FASTCALL_SUPPORTED
#ifdef STLSOFT_CF_STDCALL_SUPPORTED
        if( s0 == "S" ||
            s0 == "stdcall")
        {
                cc = calling_convention::stdcallCallConv;
        } else
#endif // STLSOFT_CF_STDCALL_SUPPORTED
        {
            STLSOFT_THROW_X(invalid_calling_convention_exception(s0.c_str()));
        }

        functionName = s1.base();
    }

    return cc;
}

template <ss_typename_param_k S>
char const* detect_cc_( dl_call_traits::is_not_fd
                    ,   char const                              *functionName
                    ,   S const                                 &
                    ,   calling_convention::calling_convention  &cc)
{
    cc = determine_calling_convention_(functionName);

    return functionName;
}

template<   int                 CC
        ,   ss_typename_param_k C
        >
char const* detect_cc_( dl_call_traits::is_fd
                    ,   char const                              *functionName
                    ,   function_descriptor<CC, C> const        &fd
                    ,   calling_convention::calling_convention  &cc)
{
    cc = calling_convention::from_int(fd.CallingConvention);

    return functionName;
}

/* ////////////////////////////////////////////////////////////////////// */

/** \name Invocators
 *
 * \ingroup group__library__dl
 *
 * These calling convention-specific functions perform the invocation of the
 * given function pointer with the requisite arguments.
 *
 * @{
 */

//[<[STLSOFT-AUTO:DL_CALL-INVOCATORS:BEGIN]>]

// 0 params

#ifdef STLSOFT_CF_CDECL_SUPPORTED
template< ss_typename_param_k R
        >
inline R dl_call_invoke_cdecl(dl_call_traits::entry_point_type fp)
{
  R (STLSOFT_CDECL *pfn)();

  reinterpret_cast<dl_call_traits::entry_point_type&>(pfn) = fp;

  return pfn();
}
#endif // STLSOFT_CF_CDECL_SUPPORTED

#ifdef STLSOFT_CF_FASTCALL_SUPPORTED
template< ss_typename_param_k R
        >
inline R dl_call_invoke_fastcall(dl_call_traits::entry_point_type fp)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -