error_desc.hpp

来自「用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、W」· HPP 代码 · 共 578 行 · 第 1/2 页

HPP
578
字号
/* /////////////////////////////////////////////////////////////////////////
 * File:        winstl/error/error_desc.hpp (originally winstl_error_desc.h)
 *
 * Purpose:     Converts a Win32 error code to a printable string.
 *
 * Created:     13th July 2003
 * Updated:     10th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2003-2006, 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 winstl/error/error_desc.hpp
 *
 * \brief [C++ only] Definition of the winstl::basic_error_desc class
 *  template.
 *  (\ref group__library__error "Error" Library.)
 */

#ifndef WINSTL_INCL_WINSTL_ERROR_HPP_ERROR_DESC
#define WINSTL_INCL_WINSTL_ERROR_HPP_ERROR_DESC

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_ERROR_HPP_ERROR_DESC_MAJOR       4
# define WINSTL_VER_WINSTL_ERROR_HPP_ERROR_DESC_MINOR       0
# define WINSTL_VER_WINSTL_ERROR_HPP_ERROR_DESC_REVISION    1
# define WINSTL_VER_WINSTL_ERROR_HPP_ERROR_DESC_EDIT        57
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifndef WINSTL_INCL_WINSTL_H_FUNCTIONS
# include <winstl/functions.h>              // for winstl::FormatMessage()
#endif /* !WINSTL_INCL_WINSTL_H_FUNCTIONS */
#ifdef __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE
# pragma message(_sscomp_fileline_message("Move load_library() into system_traits"))
#endif /* __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE */
#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
# include <winstl/filesystem/filesystem_traits.hpp>    // for load_library()
#endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
#ifndef WINSTL_INCL_WINSTL_HPP_STRING_ACCESS
# include <winstl/string_access.hpp>        // for string access shims
#endif /* !WINSTL_INCL_WINSTL_HPP_STRING_ACCESS */

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */

namespace stlsoft
{

namespace winstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

/** \brief Utility class that loads the system string representation
 *   corresponding to a given error code.
 *
 * \ingroup group__library__error
 *
 * Consider the following example:
\htmlonly
<pre>
  winstl::error_desc_a  ed1(ERROR_ACCESS_DENIED);
  winstl::error_desc_w  ed2(ERROR_ACCESS_DENIED);
  winstl::error_desc    ed3(ERROR_ACCESS_DENIED);

  assert(0 == ::strcmp("Access is denied", ed1.c_str()));
  assert(0 == ::wcscmp(L"Access is denied", ed2.c_str()));
  assert(0 == ::_tcscmp(_T("Access is denied"), ed3.c_str()));
</pre>
\endhtmlonly
 *
 * By default, the strings are looked up from the Windows system DLLs. To
 * use a specific message-string DLL, simply specify this as the second
 * argument to the constructor, as in (assuming <b>MyCustomDll.DLL</b> maps
 * <code>ERROR_ACCESS_DENIED</code> to <code>"No Access!"</code>):
 *
\htmlonly
<pre>
  winstl::error_desc_a  ed1(ERROR_ACCESS_DENIED, "MyCustomDll.DLL");

  assert(0 == ::strcmp("No Access!", ed1.c_str()));
</pre>
\endhtmlonly
 *
 * \note Naturally, \ref group__concepts__string_access_shims "String Access
 *  Shim" functions <b>c_str_ptr</b>, <b>c_str_data</b>, <b>c_str_len</b>
 *  are defined for the class template, so it may be manipulated
 *  generically. (This is very handy when used with the
 *  <a href = "http://pantheios.org/">Pantheios</a> logging library.)
 */
template<   ss_typename_param_k C
        ,   ss_typename_param_k T = filesystem_traits<C>
        >
class basic_error_desc
{
/// \name Types
/// @{
public:
    /// \brief The character type
    typedef C                       char_type;
    /// \brief The traits_type
    typedef T                       traits_type;
    /// \brief The current parameterisation of the type
    typedef basic_error_desc<C, T>  class_type;
    /// \brief The size type
    typedef ws_size_t               size_type;
/// @}

/// \name Construction
/// @{
public:
    /// \brief Loads the error string associated with the given code.
    ///
    /// \param error The Win32 error whose string equivalent will be searched
    /// \param modulePath The module in which the string will be searched
    ///
    /// \note If the error string is not found in the given module, the standard
    /// system libraries will be searched
    ss_explicit_k basic_error_desc(ws_dword_t error = GetLastError(), char_type const *modulePath = NULL);

    /// \brief Loads the error string associated with the given code.
    ///
    /// \param hr The COM error whose string equivalent will be searched
    /// \param modulePath The module in which the string will be searched
    ///
    /// \note If the error string is not found in the given module, the standard
    /// system libraries will be searched
    basic_error_desc(HRESULT hr, char_type const *modulePath = NULL);

    /// \brief Loads the error string associated with the given code from
	///  the first module in the given container of paths that contains a
	///  mapping.
    ///
    /// \param error The Win32 error whose string equivalent will be searched
    /// \param modulePaths A sequence container of paths, which will be searched
    /// incrementally for the error string
    ///
    /// \note If the error string is not found in any of the given modules, the
    /// standard system libraries will be searched
    template <ss_typename_param_k S>
    basic_error_desc(ws_dword_t error, S const &modulePaths)
        : m_length(0)
        , m_message(NULL)
    {
        ss_typename_type_k S::const_iterator    b   =   modulePaths.begin();
        ss_typename_type_k S::const_iterator    e   =   modulePaths.end();

        for(; b != e && NULL == (m_message = find_message_(error, stlsoft_ns_qual(c_str_ptr)(*b), &m_length)); ++b)
        {}

        if(NULL == m_message)
        {
            m_message = find_message_(error, NULL, &m_length);
        }
    }
    /// \brief Releases any resources.
    ~basic_error_desc() stlsoft_throw_0();
/// @}

/// \name Attributes
/// @{
public:
    /// \brief The error description
    char_type const *get_description() const;
/// @}

/// \name Accessors
/// @{
public:
    /// \brief The error description
    char_type const *c_str() const;
#if !defined(WINSTL_ERROR_DESC_NO_IMPLICIT_CONVERSION)
    /// \brief Implicit conversion operator that yields the error description
    ///
    /// \deprecated This will be removed in a future version.
    operator char_type const *() const;
#endif /* !WINSTL_ERROR_DESC_NO_IMPLICIT_CONVERSION) */
    /// \brief The length of the error description
    size_type       length() const stlsoft_throw_0();
    /// \brief The length of the error description
    size_type       size() const stlsoft_throw_0();
/// @}

/// \name Implementation
/// @{
private:
    char_type   *find_message_(ws_dword_t error, char_type const *modulePath, size_type *length);
/// @}

/// \name Members
/// @{
private:
    size_type   m_length;
    char_type   *m_message;
/// @}

/// \name Not to be implemented
/// @{
public:
    basic_error_desc(class_type const &);
    basic_error_desc &operator =(class_type const &);
/// @}
};

/* Typedefs to commonly encountered types. */
/// Instantiation of the basic_error_desc template for the ANSI character type \c char
typedef basic_error_desc<ws_char_a_t>   error_desc_a;
/// Instantiation of the basic_error_desc template for the Unicode character type \c wchar_t
typedef basic_error_desc<ws_char_w_t>   error_desc_w;
/// Instantiation of the basic_error_desc template for the Win32 character type \c TCHAR
typedef basic_error_desc<TCHAR>         error_desc;

/* /////////////////////////////////////////////////////////////////////////
 * Implementation
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_error_desc<C, T>::char_type *basic_error_desc<C, T>::find_message_(ws_dword_t error, ss_typename_type_k basic_error_desc<C, T>::char_type const *modulePath, ss_typename_type_k basic_error_desc<C, T>::size_type *length)
{
    WINSTL_ASSERT(NULL != length);
    WINSTL_MESSAGE_ASSERT("Constructor initialisation order error", 0 == *length);

    ws_dword_t  cch         =   0;
    char_type   *message    =   NULL;

    STLSOFT_SUPPRESS_UNUSED(message);

    if(NULL != modulePath)
    {
        HINSTANCE   hinstSource =   traits_type::load_library(modulePath);

        if(NULL != hinstSource)
        {
            cch =   FormatMessage(error, hinstSource, &message);

⌨️ 快捷键说明

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