# common/include/pion/PionException.hpp

    00001 // -----------------------------------------------------------------------
    00002 // pion-common: a collection of common libraries used by the Pion Platform
    00003 // -----------------------------------------------------------------------
    00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
    00005 //
    00006 // Distributed under the Boost Software License, Version 1.0.
    00007 // See http://www.boost.org/LICENSE_1_0.txt
    00008 //
    00009
    00010 #ifndef __PION_PIONEXCEPTION_HEADER__
    00011 #define __PION_PIONEXCEPTION_HEADER__
    00012
    00013 #include <pion/PionConfig.hpp>
    00014 #include <exception>
    00015 #include <string>
    00016 #include <cstdio>
    00017
    00018
    00019 namespace pion {    // begin namespace pion
    00020
    00024 class PionException :
    00025     public std::exception
    00026 {
    00027 public:
    00028     // virtual destructor does not throw
    00029     virtual ~PionException() throw () {}
    00030
    00031     // constructors used for constant messages
    00032     PionException(const char *what_msg) : m_what_msg(what_msg) {}
    00033     PionException(const std::string& what_msg) : m_what_msg(what_msg) {}
    00034
    00035     // constructors used for messages with a parameter
    00036     PionException(const char *description, const std::string& param)
    00037         : m_what_msg(std::string(description) + param) {}
    00038     PionException(std::string description, const std::string& param)
    00039         : m_what_msg(description + param) {}
    00040
    00042     virtual const char* what() const throw() {
    00043         return m_what_msg.c_str();
    00044     }
    00045
    00046 private:
    00047
    00048     // message returned by what() function
    00049     const std::string   m_what_msg;
    00050 };
    00051
    00052
    00056 class BadAssertException : public PionException {
    00057 public:
    00058     BadAssertException(const std::string& file, unsigned long line)
    00059         : PionException(make_string(file, line)) {}
    00060
    00061 private:
    00062     static std::string make_string(const std::string& file, unsigned long line) {
    00063         std::string result("Assertion failed at ");
    00064         result += file;
    00065         char line_buf[50];
    00066         sprintf(line_buf, " line %lu", line);
    00067         result += line_buf;
    00068         return result;
    00069     }
    00070 };
    00071
    00072 }   // end namespace pion
    00073
    00074
    00075 // define PION_ASSERT macro to check assertions when debugging mode is enabled
    00076 #ifdef NDEBUG
    00077     #define PION_ASSERT(EXPR)   ((void)0);
    00078 #else
    00079     #define PION_ASSERT(EXPR)   if (!(EXPR)) { throw BadAssertException(__FILE__, __LINE__); }
    00080 #endif
    00081
    00082
    00083 #endif