📄 dl_call.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: unixstl/dl/dl_call.hpp
*
* Purpose: Invocation of functions in dynamic libraries.
*
* Created: sometime in 1998
* Updated: 22nd March 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 1998-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file unixstl/dl/dl_call.hpp
*
* \brief [C++ only] Definition of the unixstl::dl_call() function suite
* (\ref group__library__dl "DL" Library).
*/
#ifndef UNIXSTL_INCL_UNIXSTL_DL_HPP_DL_CALL
#define UNIXSTL_INCL_UNIXSTL_DL_HPP_DL_CALL
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define UNIXSTL_VER_UNIXSTL_DL_HPP_DL_CALL_MAJOR 2
# define UNIXSTL_VER_UNIXSTL_DL_HPP_DL_CALL_MINOR 3
# define UNIXSTL_VER_UNIXSTL_DL_HPP_DL_CALL_REVISION 5
# define UNIXSTL_VER_UNIXSTL_DL_HPP_DL_CALL_EDIT 38
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
# include <unixstl/unixstl.h>
#endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
#ifndef UNIXSTL_INCL_UNIXSTL_HPP_ERROR_UNIX_EXCEPTIONS
# include <unixstl/error/exceptions.hpp>
#endif /* !UNIXSTL_INCL_UNIXSTL_ERROR_HPP_UNIX_EXCEPTIONS */
#ifndef UNIXSTL_INCL_UNIXSTL_DL_HPP_MODULE
# include <unixstl/dl/module.hpp>
#endif /* !UNIXSTL_INCL_UNIXSTL_D:_HPP_MODULE */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_FUNCTION_POINTER_TYPE
# include <stlsoft/meta/is_function_pointer_type.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_FUNCTION_POINTER_TYPE */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_FUNDAMENTAL_TYPE
# include <stlsoft/meta/is_fundamental_type.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_FUNDAMENTAL_TYPE */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_POINTER_TYPE
# include <stlsoft/meta/is_pointer_type.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_POINTER_TYPE */
#ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
# include <stlsoft/shims/access/string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _UNIXSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::unixstl */
namespace unixstl
{
# else
/* Define stlsoft::unixstl_project */
namespace stlsoft
{
namespace unixstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_UNIXSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/** \brief Indicates an entry point cannot be located in a dynamic library.
*
* \ingroup group__library__dl__error
*/
class missing_entry_point_exception
: public unix_exception
{
/// \name Types
/// @{
public:
typedef unix_exception parent_class_type;
typedef missing_entry_point_exception class_type;
private:
typedef parent_class_type::string_type string_type;
public:
typedef parent_class_type::error_code_type error_code_type;
/// @}
/// \name Construction
/// @{
public:
/// \brief Constructs an instance of the exception based on the given missing
/// function name, and Windows error code.
missing_entry_point_exception(char const* functionName, error_code_type err)
: parent_class_type(class_type::create_reason_(functionName), err)
{}
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
virtual ~missing_entry_point_exception() stlsoft_throw_0()
{}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/// @}
// Implementation
private:
static string_type create_reason_(char const* functionName)
{
return "Failed to find procedure \"" + string_type(functionName) + '"';
}
// Not to be implemented
private:
class_type& operator =(class_type const&);
};
/* /////////////////////////////////////////////////////////////////////////
* Traits
*/
/** \brief Traits class that provides a mechanism for declaring specific
* (e.g. aggregate and user-defined) types to be compatible with
* \link unixstl::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 unixstl
{
template <>
struct is_valid_dl_call_arg<MyNamespace::MyType>
{
enum { value = 1 };
};
} // namespace unixstl
\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 void (*entry_point_type)(void);
typedef void *library_handle_type;
typedef unixstl::module module_wrapper_type;
/// @}
/// \name Dynamic Library Functions
/// @{
public:
static entry_point_type get_symbol(library_handle_type hLib, char const* functionName)
{
#if 0
return (dl_call_traits::entry_point_type)::dlsym(hLib, functionName);
#else /* ? 0 */
// If this static assert fires, this component cannot be used. (It'd also be
// something amazing, unfathomable operating system architecture, but it's
// possible.)
STLSOFT_STATIC_ASSERT(sizeof(dl_call_traits::entry_point_type) == sizeof(::dlsym(hLib, functionName)));
union
{
dl_call_traits::entry_point_type pfn;
void *pv;
} u;
u.pv = ::dlsym(hLib, functionName);
return u.pfn;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -