00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #ifndef UNIXSTL_INCL_UNIXSTL_ERROR_HPP_EXCEPTIONS
00049 #define UNIXSTL_INCL_UNIXSTL_ERROR_HPP_EXCEPTIONS
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define UNIXSTL_VER_UNIXSTL_ERROR_HPP_EXCEPTIONS_MAJOR 4
00053 # define UNIXSTL_VER_UNIXSTL_ERROR_HPP_EXCEPTIONS_MINOR 2
00054 # define UNIXSTL_VER_UNIXSTL_ERROR_HPP_EXCEPTIONS_REVISION 5
00055 # define UNIXSTL_VER_UNIXSTL_ERROR_HPP_EXCEPTIONS_EDIT 53
00056 #endif
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
00071 # include <unixstl/unixstl.h>
00072 #endif
00073 #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS
00074 # include <stlsoft/error/exceptions.hpp>
00075 #endif
00076 #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_EXCEPTION_STRING
00077 # include <stlsoft/util/exception_string.hpp>
00078 #endif
00079 #ifndef STLSOFT_INCL_STLSOFT_INTERNAL_H_SAFESTR
00080 # include <stlsoft/internal/safestr.h>
00081 #endif
00082
00083 #ifndef STLSOFT_INCL_H_ERRNO
00084 # define STLSOFT_INCL_H_ERRNO
00085 # include <errno.h>
00086 #endif
00087 #ifndef STLSOFT_INCL_H_STRING
00088 # define STLSOFT_INCL_H_STRING
00089 # include <string.h>
00090 #endif
00091
00092
00093
00094
00095
00096 #ifndef _UNIXSTL_NO_NAMESPACE
00097 # if defined(_STLSOFT_NO_NAMESPACE) || \
00098 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00099
00100 namespace unixstl
00101 {
00102 # else
00103
00104
00105 namespace stlsoft
00106 {
00107
00108 namespace unixstl_project
00109 {
00110
00111 # endif
00112 #endif
00113
00114
00115
00116
00117
00123 class unix_exception
00124 : public stlsoft_ns_qual(os_exception)
00125 {
00128 protected:
00129 typedef stlsoft_ns_qual(exception_string) string_type;
00130 public:
00131 typedef stlsoft_ns_qual(os_exception) parent_class_type;
00132 typedef int error_code_type;
00133 typedef unix_exception class_type;
00135
00138 public:
00139 ss_explicit_k unix_exception(error_code_type err)
00140 : m_reason()
00141 , m_errorCode(err)
00142 {}
00143 unix_exception(char const* reason, error_code_type err)
00144 : m_reason(class_type::create_reason_(reason, err))
00145 , m_errorCode(err)
00146 {}
00147 protected:
00148 unix_exception(string_type const& reason, error_code_type err)
00149 : m_reason(reason)
00150 , m_errorCode(err)
00151 {}
00152 public:
00158 virtual ~unix_exception() stlsoft_throw_0()
00159 {}
00161
00164 public:
00165 virtual char const* what() const stlsoft_throw_0()
00166 {
00167 if(!m_reason.empty())
00168 {
00169 return m_reason.c_str();
00170 }
00171 else
00172 {
00173 char const* s = this->strerror();
00174
00175 UNIXSTL_ASSERT(NULL != s);
00176
00177 return (*s != '\0') ? s : "UNIX system error";
00178 }
00179 }
00180
00182 error_code_type get_error_code() const
00183 {
00184 return m_errorCode;
00185 }
00186
00190 error_code_type get_errno() const
00191 {
00192 return get_error_code();
00193 }
00194
00198 char const* strerror() const
00199 {
00200 return strerror_(m_errorCode);
00201 }
00203
00206 private:
00207 static char const* strerror_(int code)
00208 {
00209 #if defined(STLSOFT_USING_SAFE_STR_FUNCTIONS) && \
00210 defined(STLSOFT_COMPILER_IS_MSVC)
00211 # pragma warning(push)
00212 # pragma warning(disable : 4996 )
00213 #endif
00214
00215 return ::strerror(code);
00216
00217 #if defined(STLSOFT_USING_SAFE_STR_FUNCTIONS) && \
00218 defined(STLSOFT_COMPILER_IS_MSVC)
00219 # pragma warning(pop)
00220 #endif
00221 }
00222
00223 static string_type create_reason_(char const* reason, error_code_type err)
00224 {
00225 if( err == ENOMEM ||
00226 NULL == reason ||
00227 '\0' == reason[0])
00228 {
00229 return string_type();
00230 }
00231 else
00232 {
00233 strerror_(0);
00234
00235 string_type r(reason);
00236 char const* s = strerror_(err);
00237
00238 UNIXSTL_ASSERT(NULL != s);
00239
00240 if(*s != '\0')
00241 {
00242 return r + ": " + s;
00243 }
00244
00245 return r;
00246 }
00247 }
00249
00252 private:
00253 const string_type m_reason;
00254 error_code_type m_errorCode;
00256
00259 private:
00260 class_type& operator =(class_type const&);
00262 };
00263
00264
00265
00266
00267
00273
00274 struct unix_exception_policy
00275 {
00278 public:
00280 typedef unix_exception thrown_type;
00281 typedef int error_code_type;
00283
00286 public:
00288 void operator ()() const
00289 {
00290 STLSOFT_THROW_X(thrown_type(errno));
00291 }
00293 void operator ()(error_code_type err) const
00294 {
00295 STLSOFT_THROW_X(thrown_type(err));
00296 }
00298 void operator ()(char const* reason, error_code_type err) const
00299 {
00300 STLSOFT_THROW_X(thrown_type(reason, err));
00301 }
00303 };
00304
00306
00307
00308 #ifdef STLSOFT_UNITTEST
00309 # include "./unittest/exceptions_unittest_.h"
00310 #endif
00311
00312
00313
00314 #ifndef _UNIXSTL_NO_NAMESPACE
00315 # if defined(_STLSOFT_NO_NAMESPACE) || \
00316 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00317 }
00318 # else
00319 }
00320 }
00321 # endif
00322 #endif
00323
00324
00325
00326 #endif
00327
00328