pion-net  4.0.9
PionException.hpp
1 // -----------------------------------------------------------------------
2 // pion-common: a collection of common libraries used by the Pion Platform
3 // -----------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_PIONEXCEPTION_HEADER__
11 #define __PION_PIONEXCEPTION_HEADER__
12 
13 #include <pion/PionConfig.hpp>
14 #include <exception>
15 #include <string>
16 #include <cstdio>
17 
18 
19 namespace pion { // begin namespace pion
20 
25  public std::exception
26 {
27 public:
28  // virtual destructor does not throw
29  virtual ~PionException() throw () {}
30 
31  // constructors used for constant messages
32  PionException(const char *what_msg) : m_what_msg(what_msg) {}
33  PionException(const std::string& what_msg) : m_what_msg(what_msg) {}
34 
35  // constructors used for messages with a parameter
36  PionException(const char *description, const std::string& param)
37  : m_what_msg(std::string(description) + param) {}
38  PionException(std::string description, const std::string& param)
39  : m_what_msg(description + param) {}
40 
42  virtual const char* what() const throw() {
43  return m_what_msg.c_str();
44  }
45 
46 private:
47 
48  // message returned by what() function
49  const std::string m_what_msg;
50 };
51 
52 
57 public:
58  BadAssertException(const std::string& file, unsigned long line)
59  : PionException(make_string(file, line)) {}
60 
61 private:
62  static std::string make_string(const std::string& file, unsigned long line) {
63  std::string result("Assertion failed at ");
64  result += file;
65  char line_buf[50];
66  sprintf(line_buf, " line %lu", line);
67  result += line_buf;
68  return result;
69  }
70 };
71 
72 } // end namespace pion
73 
74 
75 // define PION_ASSERT macro to check assertions when debugging mode is enabled
76 #ifdef NDEBUG
77  #define PION_ASSERT(EXPR) ((void)0);
78 #else
79  #define PION_ASSERT(EXPR) if (!(EXPR)) { throw BadAssertException(__FILE__, __LINE__); }
80 #endif
81 
82 
83 #endif
virtual const char * what() const
returns a descriptive message for the exception