Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef CASTOR_EXCEPTION_H
00018 #define CASTOR_EXCEPTION_H 1
00019
00020 #ifndef _GNU_SOURCE
00021 # define _GNU_SOURCE
00022 #endif
00023 #include <stdio.h>
00024
00025 #include <stdlib.h>
00026 #include <exception>
00027 #include <string>
00028 #include <iostream>
00029 #include <cstdarg>
00030
00031 namespace castor {
00032
00033 class Exception : public std::exception {
00034
00035 protected:
00036
00037 std::string reason;
00038
00039 public:
00040
00041 Exception(const std::string what = "unknown exception occured", ...) throw() :
00042 std::exception(), reason()
00043 {
00044 va_list params;
00045 va_start(params, what);
00046 setReason(what, params);
00047 va_end(params);
00048 }
00049
00050 virtual ~Exception() throw() {
00051 }
00052
00053 virtual const char *what() const throw() {
00054 return this->reason.c_str();
00055 }
00056
00057 protected:
00058
00059 void setReason(const std::string &what, va_list &args) {
00060
00061 char *result = NULL;
00062
00063 if (vasprintf(&result, what.c_str(), args) == -1) {
00064 std::cout << "Exception: Error while setting reason!" << std::endl;
00065 }
00066
00067 this->reason = std::string(result);
00068
00069 free(result);
00070 }
00071 };
00072 }
00073
00074 std::ostream &operator << (std::ostream &os, const castor::Exception &x);
00075
00076 #endif
00077