libcamgm
Exception.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | _ _ _ _ __ _ |
4 | | | | | | \_/ | / \ | | |
5 | | | | | | |_| | / /\ \ | | |
6 | | |__ | | | | | | / ____ \ | |__ |
7 | |____||_| |_| |_|/ / \ \|____| |
8 | |
9 | core library |
10 | |
11 | (C) SUSE Linux Products GmbH |
12 \----------------------------------------------------------------------/
13 
14  File: Exception.hpp
15 
16  Author: Michael Calmer
17  Maintainer: Michael Calmer
18 
19 /-*/
54 #ifndef CA_MGM_EXCEPTION_HPP
55 #define CA_MGM_EXCEPTION_HPP
56 
57 #include "ca-mgm/config.h"
58 #include <ca-mgm/String.hpp>
59 #include <string.h>
60 
61 namespace CA_MGM_NAMESPACE
62 {
63 
64  class Exception : public std::exception
65  {
66  friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
67 
68  protected:
69  Exception(const char* file, int line, const char* msg,
70  int errorCode, const Exception *otherException = 0);
71 
72  public:
73  Exception(const Exception& e);
74  Exception& operator= (const Exception& rhs);
75  virtual ~Exception() throw();
76 
81  virtual const char* type() const;
86  virtual const char* getMessage() const;
91  virtual std::string getFullMessage() const;
92 
96  const char* getFile() const;
97  int getLine() const;
98 
104  int getErrorCode() const;
105 
109  virtual const char* what() const throw();
110 
111  private:
112  char* m_file;
113  int m_line;
114  char* m_msg;
116  };
117 
118 namespace ExceptionDetail
119 {
120  unsigned const BUFSZ = 1024;
121 
122  template <typename exType>
123  struct Errno
124  {
125  static exType simple(char const * file, int line, int errnum)
126  {
127  return exType(file, line, ::strerror(errnum), errnum);
128  }
129 
130  template <typename Mtype>
131  static exType format(char const * file, int line,
132  Mtype const & msg, int errnum)
133  {
134  return format(file, line, msg.c_str(), errnum);
135  }
136 
137  static exType format(char const * file, int line,
138  char const * msg, int errnum)
139  {
140  return exType(file, line, str::form("%s: %d(%s)", msg, errnum, ::strerror(errnum)).c_str(), errnum);
141  }
142  }; // struct Errno
143 }
144 
152 #define CA_MGM_DECLARE_EXCEPTION2(NAME, BASE) \
153 class NAME##Exception : public BASE \
154 { \
155 public: \
156  NAME##Exception(const char* file, int line, const char* msg, int errorCode = 0, const ca_mgm::Exception* otherException = 0); \
157  virtual ~NAME##Exception() throw(); \
158  virtual const char* type() const; \
159 };
160 
167 #define CA_MGM_DECLARE_EXCEPTION(NAME) CA_MGM_DECLARE_EXCEPTION2(NAME, ca_mgm::Exception)
168 
177 #define CA_MGM_DEFINE_EXCEPTION2(NAME, BASE) \
178 NAME##Exception::NAME##Exception(const char* file, int line, const char* msg, int errorCode, const ::ca_mgm::Exception* otherException) \
179  : BASE(file, line, msg, errorCode, otherException) {} \
180 NAME##Exception::~NAME##Exception() throw() { } \
181 const char* NAME##Exception::type() const { return #NAME "Exception"; }\
182 
183 
191 #define CA_MGM_DEFINE_EXCEPTION(NAME) CA_MGM_DEFINE_EXCEPTION2(NAME, ca_mgm::Exception)
192 
200 #define CA_MGM_THROW(exType, msg) throw exType(__FILE__, __LINE__, (msg))
201 
209 #define CA_MGM_THROW_SUBEX(exType, msg, subex) \
210 throw exType(__FILE__, __LINE__, (msg), -1, &(subex))
211 
218 #define CA_MGM_THROW_ERR(exType, msg, err) \
219 throw exType(__FILE__, __LINE__, (msg), (err))
220 
225 #define CA_MGM_THROW_ERRNO(exType) CA_MGM_THROW_ERRNO1(exType, errno)
226 
233 #define CA_MGM_THROW_ERRNO1(exType, errnum) \
234 throw ::ca_mgm::ExceptionDetail::Errno< exType >::simple(__FILE__, __LINE__, (errnum))
235 
242 #define CA_MGM_THROW_ERRNO_MSG(exType, msg) \
243 CA_MGM_THROW_ERRNO_MSG1(exType, (msg), errno)
244 
252 #define CA_MGM_THROW_ERRNO_MSG1(exType, msg, errnum) \
253 throw ::ca_mgm::ExceptionDetail::Errno< exType >:: \
254  format(__FILE__, __LINE__, (msg), (errnum))
255 
256 
267 
277 
289 
299 
300 
309 
317 
319 
320 } // End of CA_MGM_NAMESPACE
321 
322 #endif /* CA_MGM_EXCEPTION_HPP */
Definition: Exception.hpp:64
std::string strerror(int errno_r)
int m_errorCode
Definition: Exception.hpp:115
static exType format(char const *file, int line, Mtype const &msg, int errnum)
Definition: Exception.hpp:131
int m_line
Definition: Exception.hpp:113
Definition: Exception.hpp:123
char * m_file
Definition: Exception.hpp:112
std::ostream & operator<<(std::ostream &ostr, const PathName &path)
unsigned const BUFSZ
Definition: Exception.hpp:120
char * m_msg
Definition: Exception.hpp:114
static exType simple(char const *file, int line, int errnum)
Definition: Exception.hpp:125
std::string form(const char *format,...)
static exType format(char const *file, int line, char const *msg, int errnum)
Definition: Exception.hpp:137
#define CA_MGM_DECLARE_EXCEPTION(NAME)
Definition: Exception.hpp:167