[4] | 1 | /**
|
---|
| 2 | * @file exception.h
|
---|
| 3 | * @author myusgun@gmail.com
|
---|
| 4 | * @brief exception
|
---|
| 5 | */
|
---|
| 6 | #ifndef __exception_h__
|
---|
| 7 | #define __exception_h__
|
---|
| 8 |
|
---|
| 9 | #include "cf/types.h"
|
---|
| 10 | #include "cf/formatter.hpp"
|
---|
| 11 |
|
---|
| 12 | #include <string>
|
---|
| 13 | #include <vector>
|
---|
| 14 | #include <exception>
|
---|
| 15 |
|
---|
| 16 | #define _SOURCE_INFO __FILE__, __LINE__, __func__
|
---|
| 17 | #define THROW_EXCEPTION(_m) throw cf::exception(STR(_m).c_str(), _SOURCE_INFO)
|
---|
| 18 | #define FORWARD_EXCEPTION(_e) throw cf::exception(_e, _SOURCE_INFO)
|
---|
| 19 |
|
---|
| 20 | namespace cf
|
---|
| 21 | {
|
---|
| 22 | /**
|
---|
| 23 | * exception
|
---|
| 24 | */
|
---|
| 25 | class exception : public std::exception
|
---|
| 26 | {
|
---|
| 27 | private:
|
---|
| 28 | std::string mMessage;
|
---|
| 29 | std::string mFile;
|
---|
| 30 | cf::int32_t mLine;
|
---|
| 31 | std::string mFunction;
|
---|
| 32 | #ifndef _HPUX
|
---|
| 33 | std::vector<exception> mStack;
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | public:
|
---|
| 37 | /**
|
---|
| 38 | * constructor
|
---|
| 39 | * @param message error message
|
---|
| 40 | * @param file source filename
|
---|
| 41 | * @param func function
|
---|
| 42 | * @param line source line
|
---|
| 43 | * @see THROW_EXCEPTION
|
---|
| 44 | */
|
---|
| 45 | exception(const cf::char_t * message,
|
---|
| 46 | const cf::char_t * file,
|
---|
| 47 | const cf::int32_t line,
|
---|
| 48 | const cf::char_t * func);
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * constructor(for stack)
|
---|
| 52 | * @param ex previous exception
|
---|
| 53 | * @param file source filename
|
---|
| 54 | * @param func function
|
---|
| 55 | * @param line source line
|
---|
| 56 | * @see FORWARD_EXCEPTION
|
---|
| 57 | * @remarks make thrown-stack
|
---|
| 58 | */
|
---|
| 59 | exception(const exception & ex,
|
---|
| 60 | const cf::char_t * file,
|
---|
| 61 | const cf::int32_t line,
|
---|
| 62 | const cf::char_t * func);
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * destructor derived from std::exception::~exception()
|
---|
| 66 | */
|
---|
| 67 | ~exception()
|
---|
| 68 | throw ();
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * std::exception::what()
|
---|
| 72 | * @return error message
|
---|
| 73 | */
|
---|
| 74 | const cf::char_t * what() const
|
---|
| 75 | throw ();
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * get source file name
|
---|
| 79 | * @return source file name
|
---|
| 80 | */
|
---|
| 81 | const cf::char_t * file() const;
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * get source line
|
---|
| 85 | * @return source line
|
---|
| 86 | */
|
---|
| 87 | cf::int32_t line() const;
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * get function name
|
---|
| 91 | * @return function name
|
---|
| 92 | */
|
---|
| 93 | const cf::char_t * function() const;
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * get stack of thrown-exceptions
|
---|
| 97 | * @return stack of thrown-exceptions
|
---|
| 98 | */
|
---|
| 99 | std::string stackTrace() const;
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * get GetLastError() in windows; otherwise, errno
|
---|
| 103 | * @return system error code
|
---|
| 104 | */
|
---|
| 105 | static cf::int32_t systemCode();
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * get error message for system error code
|
---|
| 109 | * @return error message for system error code
|
---|
| 110 | */
|
---|
| 111 | static std::string systemMessage();
|
---|
| 112 | };
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | #endif // #ifndef __exception_h__
|
---|