STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

winstl_error_desc.h

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_error_desc.h
00003  *
00004  * Purpose:     Converts a Win32 error code to a printable string.
00005  *
00006  * Created:     13th July 2003
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2003-2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef WINSTL_INCL_H_WINSTL_ERROR_DESC
00046 #define WINSTL_INCL_H_WINSTL_ERROR_DESC
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define WINSTL_VER_H_WINSTL_ERROR_DESC_MAJOR    2
00050 # define WINSTL_VER_H_WINSTL_ERROR_DESC_MINOR    0
00051 # define WINSTL_VER_H_WINSTL_ERROR_DESC_REVISION 1
00052 # define WINSTL_VER_H_WINSTL_ERROR_DESC_EDIT     28
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef WINSTL_INCL_H_WINSTL
00060 # include "winstl.h"                        // Include the WinSTL root header
00061 #endif /* !WINSTL_INCL_H_WINSTL */
00062 #ifndef WINSTL_INCL_H_WINSTL_FUNCTIONS
00063 # include "winstl_functions.h"              // winstl::FormatMessage()
00064 #endif /* !WINSTL_INCL_H_WINSTL_FUNCTIONS */
00065 #ifndef WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS
00066 # include "winstl_filesystem_traits.h"      // load_library
00067 #endif /* !WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS */
00068 #ifndef WINSTL_INCL_H_WINSTL_STRING_ACCESS
00069 # include "winstl_string_access.h"          // c_str_ptr, etc.
00070 #endif /* WINSTL_INCL_H_WINSTL_STRING_ACCESS */
00071 
00072 /* 
00073  * Namespace
00074  */
00075 
00076 #ifndef _WINSTL_NO_NAMESPACE
00077 # if defined(_STLSOFT_NO_NAMESPACE) || \
00078      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00079 /* There is no stlsoft namespace, so must define ::winstl */
00080 namespace winstl
00081 {
00082 # else
00083 /* Define stlsoft::winstl_project */
00084 
00085 namespace stlsoft
00086 {
00087 
00088 namespace winstl_project
00089 {
00090 
00091 # endif /* _STLSOFT_NO_NAMESPACE */
00092 #endif /* !_WINSTL_NO_NAMESPACE */
00093 
00094 /* 
00095 
00098 
00099 /* 
00100  * Classes
00101  */
00102 
00106 template<   ss_typename_param_k C
00107         ,   ss_typename_param_k T = filesystem_traits<C>
00108         >
00109 class basic_error_desc
00110 {
00111 public:
00113     typedef C                       char_type;
00115     typedef T                       traits_type;
00117     typedef basic_error_desc<C, T>  class_type;
00118 
00119 // Construction
00120 public:
00125     ss_explicit_k basic_error_desc(ws_dword_t error = GetLastError(), char_type const *paths = NULL);
00126 public:
00128     ~basic_error_desc() winstl_throw_0();
00129 
00130 // Attributes
00131 public:
00133     char_type const *get_description() const;
00134 
00135 // Accessors
00136 public:
00138     operator char_type const *() const;
00139 
00140 // Members
00141 private:
00142     char_type   *m_message;
00143 
00144 // Not to be implemented
00145 public:
00146     basic_error_desc(class_type const &);
00147     basic_error_desc &operator =(class_type const &);
00148 };
00149 
00150 /* Typedefs to commonly encountered types. */
00152 typedef basic_error_desc<ws_char_a_t>   error_desc_a;
00154 typedef basic_error_desc<ws_char_w_t>   error_desc_w;
00156 typedef basic_error_desc<TCHAR>         error_desc;
00157 
00158 /* 
00159  * Implementation
00160  */
00161 
00162 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00163 
00164 template<   ss_typename_param_k C
00165         ,   ss_typename_param_k T
00166         >
00167 inline basic_error_desc<C, T>::basic_error_desc(ws_dword_t error /* = ::GetLastError() */, char_type const *paths /* = NULL */)
00168     : m_message(NULL)
00169 {
00170     ws_dword_t  cch =   0;
00171 
00172     if(NULL != paths)
00173     {
00174         HINSTANCE   hinstSource =   traits_type::load_library(paths);
00175 
00176         if(NULL != hinstSource)
00177         {
00178             cch =   FormatMessage(error, hinstSource, &m_message);
00179 
00180             traits_type::free_library(hinstSource);
00181         }
00182     }
00183 
00184     if(NULL == m_message)
00185     {
00186         cch = FormatMessage(error, NULL, &m_message);
00187     }
00188 
00189     if(0 == cch)
00190     {
00191         m_message = NULL;
00192     }
00193 }
00194 
00195 template<   ss_typename_param_k C
00196         ,   ss_typename_param_k T
00197         >
00198 inline basic_error_desc<C, T>::~basic_error_desc() winstl_throw_0()
00199 {
00200     if(m_message != NULL)
00201     {
00202         ::LocalFree(m_message);
00203     }
00204 }
00205 
00206 template<   ss_typename_param_k C
00207         ,   ss_typename_param_k T
00208         >
00209 inline ss_typename_type_k basic_error_desc<C, T>::char_type const *basic_error_desc<C, T>::get_description() const
00210 {
00211     return (NULL != m_message) ? m_message : "";
00212 }
00213 
00214 template<   ss_typename_param_k C
00215         ,   ss_typename_param_k T
00216         >
00217 #if defined(__STLSOFT_COMPILER_IS_GCC)
00218 inline basic_error_desc<C, T>::operator C const *() const
00219 #else /* ? compiler */
00220 inline basic_error_desc<C, T>::operator ss_typename_type_k basic_error_desc<C, T>::char_type const *() const
00221 #endif /* compiler */
00222 {
00223     return get_description();
00224 }
00225 
00226 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00227 
00228 /* 
00229  * String access shims
00230  */
00231 
00232 #ifndef __STLSOFT_CF_TEMPLATE_SHIMS_NOT_SUPPORTED
00233 
00235 template<   ss_typename_param_k C
00236         ,   ss_typename_param_k T
00237         >
00238 inline C const *c_str_ptr_null(basic_error_desc<C, T> const &e)
00239 {
00240     C const *p  =   e;
00241 
00242     return ('\0' != *e) ? p : NULL;
00243 }
00244 
00246 template<   ss_typename_param_k C
00247         ,   ss_typename_param_k T
00248         >
00249 inline C const *c_str_ptr(basic_error_desc<C, T> const &e)
00250 {
00251     return e;
00252 }
00253 
00255 template<   ss_typename_param_k C
00256         ,   ss_typename_param_k T
00257         >
00258 inline C const *get_ptr(basic_error_desc<C, T> const &e)
00259 {
00260     return e;
00261 }
00262 
00263 #endif /* !__STLSOFT_CF_TEMPLATE_SHIMS_NOT_SUPPORTED */
00264 
00266 // Unit-testing
00267 
00268 #ifdef STLSOFT_UNITTEST
00269 
00270 namespace unittest
00271 {
00272     ss_bool_t test_winstl_error_desc(unittest_reporter *r)
00273     {
00274         using stlsoft::unittest::unittest_initialiser;
00275 
00276         ss_bool_t               bSuccess    =   true;
00277 
00278         unittest_initialiser    init(r, "WinSTL", "error_desc", __FILE__);
00279 
00280 #if 0
00281         if(<<TODO>>)
00282         {
00283             r->report("<<TODO>> failed ", __LINE__);
00284             bSuccess = false;
00285         }
00286 #endif /* 0 */
00287 
00288         return bSuccess;
00289     }
00290 
00291     unittest_registrar    unittest_winstl_error_desc(test_winstl_error_desc);
00292 
00293 } // namespace unittest
00294 
00295 #endif /* STLSOFT_UNITTEST */
00296 
00297 /* 
00298 
00300 
00301 /* 
00302 
00303 #ifndef _WINSTL_NO_NAMESPACE
00304 # if defined(_STLSOFT_NO_NAMESPACE) || \
00305      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00306 } // namespace winstl
00307 # else
00308 } // namespace winstl_project
00309 } // namespace stlsoft
00310 # endif /* _STLSOFT_NO_NAMESPACE */
00311 #endif /* !_WINSTL_NO_NAMESPACE */
00312 
00313 /* 
00314  * Namespace
00315  *
00316  * The string access shims exist either in the stlsoft namespace, or in the
00317  * global namespace. This is required by the lookup rules.
00318  *
00319  */
00320 
00321 #ifndef _WINSTL_NO_NAMESPACE
00322 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00323      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00324 namespace stlsoft
00325 {
00326 # else /* ? _STLSOFT_NO_NAMESPACE */
00327 /* There is no stlsoft namespace, so must define in the global namespace */
00328 # endif /* !_STLSOFT_NO_NAMESPACE */
00329 
00330 using ::winstl::c_str_ptr_null;
00331 
00332 using ::winstl::c_str_ptr;
00333 
00334 using ::winstl::get_ptr;
00335 
00336 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00337      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00338 } // namespace stlsoft
00339 # else /* ? _STLSOFT_NO_NAMESPACE */
00340 /* There is no stlsoft namespace, so must define in the global namespace */
00341 # endif /* !_STLSOFT_NO_NAMESPACE */
00342 #endif /* !_WINSTL_NO_NAMESPACE */
00343 
00344 /* 
00345 
00346 #endif /* WINSTL_INCL_H_WINSTL_ERROR_DESC */
00347 
00348 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004