OWBI1_Exception.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #ifndef OWBI1_EXCEPTION_HPP_INCLUDE_GUARD_
00037 #define OWBI1_EXCEPTION_HPP_INCLUDE_GUARD_
00038 #include "OWBI1_config.h"
00039 #include "OWBI1_AutoPtr.hpp"
00040 #if defined(OWBI1_NON_THREAD_SAFE_EXCEPTION_HANDLING)
00041 #include "OWBI1_OW_Fwd.hpp" // for Mutex
00042 #endif
00043 #include <iosfwd>
00044 #include <exception>
00045 #include <new>
00046 
00047 namespace OWBI1
00048 {
00049 
00060 class OWBI1_OWBI1PROVIFC_API Exception : public std::exception
00061 {
00062 protected:
00079    Exception(const char* file, int line, const char* msg, int errorCode, const Exception* otherException = 0, int subClassId = UNKNOWN_SUBCLASS_ID);
00080 
00081 
00082 #ifdef OWBI1_WIN32
00083    // Can't seem to catch exceptions by reference if copy CTOR is
00084    // protected on Windoz
00085 public:
00086 #endif
00087    Exception(const Exception& e);
00088    Exception& operator= (const Exception& rhs);
00089 #ifdef OWBI1_WIN32
00090 protected:
00091 #endif
00092    void swap(Exception& x);
00093    virtual ~Exception() throw();
00094    void setSubClassId(int subClassId);
00095    void setErrorCode(int errorCode);
00096 
00097 public:
00098 
00099    static const int UNKNOWN_SUBCLASS_ID = -1;
00100    static const int UNKNOWN_ERROR_CODE = -1;
00101 
00106    virtual const char* type() const;
00111    virtual const char* getMessage() const;
00115    const char* getFile() const;
00116    int getLine() const;
00117    int getSubClassId() const;
00121    const Exception* getSubException() const;
00127    int getErrorCode() const;
00128 
00132    virtual const char* what() const throw();
00133 
00145    virtual Exception* clone() const;
00146 
00153    char* dupString(const char* str);
00154 
00155 private:
00156    char* m_file;
00157    int m_line;
00158    char* m_msg;
00159    int m_subClassId;
00160    const Exception* m_subException;
00161    int m_errorCode;
00162 
00163 #if defined(OWBI1_NON_THREAD_SAFE_EXCEPTION_HANDLING)
00164    static ::OpenWBEM::Mutex* m_mutex;
00165 #endif
00166 
00167 };
00168 
00169 
00170 namespace ExceptionDetail
00171 {
00172 
00173    // Reconciles GNU strerror_r and POSIX strerror_r.
00174    //
00175    OWBI1_OWBI1PROVIFC_API void portable_strerror_r(int errnum, char * buf, unsigned n);
00176 
00177    struct OWBI1_OWBI1PROVIFC_API FormatMsgImpl;
00178 
00179    class OWBI1_OWBI1PROVIFC_API FormatMsg
00180    {
00181 
00182 #ifdef OWBI1_WIN32
00183 #pragma warning (push)
00184 #pragma warning (disable: 4251)
00185 #endif
00186 
00187       AutoPtr<FormatMsgImpl> pImpl;
00188 
00189 #ifdef OWBI1_WIN32
00190 #pragma warning (pop)
00191 #endif
00192 
00193    public:
00194       FormatMsg(char const * msg, int errnum);
00195       ~FormatMsg();
00196       char const * get() const;
00197    private:
00198       FormatMsg(const FormatMsg&);
00199       FormatMsg& operator=(const FormatMsg&);
00200    };
00201 
00202    unsigned const BUFSZ = 1024;
00203 
00204    template <typename exType>
00205    struct Errno
00206    {
00207       static exType simple(char const * file, int line, int errnum)
00208       {
00209          char buf[BUFSZ];
00210          portable_strerror_r(errnum, buf, BUFSZ);
00211          return exType(file, line, buf, errnum);
00212       }
00213 
00214       template <typename Mtype>
00215       static exType format(char const * file, int line,
00216                            Mtype const & msg, int errnum)
00217       {
00218          return format(file, line, msg.c_str(), errnum);
00219       }
00220 
00221       static exType format(char const * file, int line,
00222                            char const * msg, int errnum)
00223       {
00224          FormatMsg fm(msg, errnum);
00225          return exType(file, line, fm.get(), errnum);
00226       }
00227    }; // struct Errno
00228 
00229 } // namespace ExceptionDetail
00230 
00235 OWBI1_OWBI1PROVIFC_API std::ostream& operator<< (std::ostream& os, const Exception& e);
00236 
00244 #define OWBI1_THROW(exType, msg) throw exType(__FILE__, __LINE__, (msg))
00245 
00249 #define OWBI1_THROWL(exType, line, msg) throw exType(__FILE__, (line), (msg))
00250 
00258 #define OWBI1_THROW_SUBEX(exType, msg, subex) \
00259 throw exType(__FILE__, __LINE__, (msg), \
00260              ::OWBI1::Exception::UNKNOWN_ERROR_CODE, &(subex))
00261 
00268 #define OWBI1_THROW_ERR(exType, msg, err) \
00269 throw exType(__FILE__, __LINE__, (msg), (err))
00270 
00276 #define OWBI1_THROW_ERRNO(exType) OWBI1_THROW_ERRNO1(exType, errno)
00277 
00283 #define OWBI1_THROW_ERRNO1(exType, errnum) \
00284 throw ExceptionDetail::Errno< exType >::simple(__FILE__, __LINE__, (errnum))
00285 
00291 #define OWBI1_THROW_ERRNO_MSG(exType, msg) \
00292 OWBI1_THROW_ERRNO_MSG1(exType, (msg), errno)
00293 
00299 #define OWBI1_THROW_ERRNO_MSG1(exType, msg, errnum) \
00300 throw ExceptionDetail::Errno< exType >:: \
00301       format(__FILE__, __LINE__, (msg), (errnum))
00302 
00311 #define OWBI1_THROW_ERR_SUBEX(exType, msg, err, subex) \
00312 throw exType(__FILE__, __LINE__, (msg), (err), &(subex))
00313 
00321 #define OWBI1_DECLARE_EXCEPTION2(NAME, BASE) \
00322 class NAME##Exception : public BASE \
00323 { \
00324 public: \
00325    NAME##Exception(const char* file, int line, const char* msg, int errorCode = ::OWBI1::Exception::UNKNOWN_ERROR_CODE, const Exception* otherException = 0, int subClassId = ::OWBI1::Exception::UNKNOWN_SUBCLASS_ID); \
00326    virtual ~NAME##Exception() throw(); \
00327    virtual const char* type() const; \
00328    virtual NAME##Exception* clone() const; \
00329 };
00330 
00342 #define OWBI1_DECLARE_APIEXCEPTION2(NAME, BASE, LINKAGE_SPEC) \
00343 class LINKAGE_SPEC NAME##Exception : public BASE \
00344 { \
00345 public: \
00346    NAME##Exception(const char* file, int line, const char* msg, int errorCode = ::OWBI1::Exception::UNKNOWN_ERROR_CODE, const Exception* otherException = 0, int subClassId = ::OWBI1::Exception::UNKNOWN_SUBCLASS_ID); \
00347    virtual ~NAME##Exception() throw(); \
00348    virtual const char* type() const; \
00349    virtual NAME##Exception* clone() const; \
00350 };
00351 
00352 
00353 
00354 
00361 #define OWBI1_DECLARE_EXCEPTION(NAME) OWBI1_DECLARE_EXCEPTION2(NAME, ::OWBI1::Exception)
00362 
00371 #define OWBI1_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC) OWBI1_DECLARE_APIEXCEPTION2(NAME, ::OWBI1::Exception, LINKAGE_SPEC)
00372 
00381 #define OWBI1_DEFINE_EXCEPTION2(NAME, BASE) \
00382 NAME##Exception::NAME##Exception(const char* file, int line, const char* msg, int errorCode, const ::OWBI1::Exception* otherException, int subClassId) \
00383    : BASE(file, line, msg, errorCode, otherException, subClassId) {} \
00384 NAME##Exception::~NAME##Exception() throw() { } \
00385 NAME##Exception* NAME##Exception::clone() const { return new(std::nothrow) NAME##Exception(*this); } \
00386 const char* NAME##Exception::type() const { return #NAME "Exception"; }
00387 
00397 #define OWBI1_DEFINE_EXCEPTION_WITH_BASE_AND_ID_AUX(NAME, BASE, SUB_CLASS_ID) \
00398 NAME##Exception::NAME##Exception(const char* file, int line, const char* msg, int errorCode, const ::OWBI1::Exception* otherException, int subClassId) \
00399    : BASE(file, line, msg, errorCode, otherException, subClassId == ::OWBI1::Exception::UNKNOWN_SUBCLASS_ID ? (SUB_CLASS_ID) : subClassId) {} \
00400 NAME##Exception::~NAME##Exception() throw() { } \
00401 NAME##Exception* NAME##Exception::clone() const { return new(std::nothrow) NAME##Exception(*this); } \
00402 const char* NAME##Exception::type() const { return #NAME "Exception"; }
00403 
00412 #define OWBI1_DEFINE_EXCEPTION(NAME) OWBI1_DEFINE_EXCEPTION_WITH_BASE_AND_ID_AUX(NAME, ::OWBI1::Exception, ::OWBI1::Exception::UNKNOWN_SUBCLASS_ID)
00413 
00422 #define OWBI1_DEFINE_EXCEPTION_WITH_ID(NAME) OWBI1_DEFINE_EXCEPTION_WITH_BASE_AND_ID_AUX(NAME, ::OWBI1::Exception, ::OpenWBEM::ExceptionIds::NAME##ExceptionId)
00423 
00433 #define OWBI1_DEFINE_EXCEPTION_WITH_BASE_AND_ID(NAME, BASE) OWBI1_DEFINE_EXCEPTION_WITH_BASE_AND_ID_AUX(NAME, BASE, ::OpenWBEM::ExceptionIds::NAME##ExceptionId)
00434 
00435 } // end namespace OWBI1
00436 
00437 #endif

Generated on Thu Feb 9 08:48:27 2006 for openwbem by  doxygen 1.4.6