xref: /aosp_15_r20/external/marisa-trie/include/marisa/exception.h (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1*ab8db090SAndroid Build Coastguard Worker #ifndef MARISA_EXCEPTION_H_
2*ab8db090SAndroid Build Coastguard Worker #define MARISA_EXCEPTION_H_
3*ab8db090SAndroid Build Coastguard Worker 
4*ab8db090SAndroid Build Coastguard Worker #include <exception>
5*ab8db090SAndroid Build Coastguard Worker 
6*ab8db090SAndroid Build Coastguard Worker #include "marisa/base.h"
7*ab8db090SAndroid Build Coastguard Worker 
8*ab8db090SAndroid Build Coastguard Worker namespace marisa {
9*ab8db090SAndroid Build Coastguard Worker 
10*ab8db090SAndroid Build Coastguard Worker // An exception object keeps a filename, a line number, an error code and an
11*ab8db090SAndroid Build Coastguard Worker // error message. The message format is as follows:
12*ab8db090SAndroid Build Coastguard Worker //  "__FILE__:__LINE__: error_code: error_message"
13*ab8db090SAndroid Build Coastguard Worker class Exception : public std::exception {
14*ab8db090SAndroid Build Coastguard Worker  public:
Exception(const char * filename,int line,ErrorCode error_code,const char * error_message)15*ab8db090SAndroid Build Coastguard Worker   Exception(const char *filename, int line,
16*ab8db090SAndroid Build Coastguard Worker       ErrorCode error_code, const char *error_message)
17*ab8db090SAndroid Build Coastguard Worker       : std::exception(), filename_(filename), line_(line),
18*ab8db090SAndroid Build Coastguard Worker         error_code_(error_code), error_message_(error_message) {}
Exception(const Exception & ex)19*ab8db090SAndroid Build Coastguard Worker   Exception(const Exception &ex)
20*ab8db090SAndroid Build Coastguard Worker       : std::exception(), filename_(ex.filename_), line_(ex.line_),
21*ab8db090SAndroid Build Coastguard Worker         error_code_(ex.error_code_), error_message_(ex.error_message_) {}
~Exception()22*ab8db090SAndroid Build Coastguard Worker   virtual ~Exception() throw() {}
23*ab8db090SAndroid Build Coastguard Worker 
24*ab8db090SAndroid Build Coastguard Worker   Exception &operator=(const Exception &rhs) {
25*ab8db090SAndroid Build Coastguard Worker     filename_ = rhs.filename_;
26*ab8db090SAndroid Build Coastguard Worker     line_ = rhs.line_;
27*ab8db090SAndroid Build Coastguard Worker     error_code_ = rhs.error_code_;
28*ab8db090SAndroid Build Coastguard Worker     error_message_ = rhs.error_message_;
29*ab8db090SAndroid Build Coastguard Worker     return *this;
30*ab8db090SAndroid Build Coastguard Worker   }
31*ab8db090SAndroid Build Coastguard Worker 
filename()32*ab8db090SAndroid Build Coastguard Worker   const char *filename() const {
33*ab8db090SAndroid Build Coastguard Worker     return filename_;
34*ab8db090SAndroid Build Coastguard Worker   }
line()35*ab8db090SAndroid Build Coastguard Worker   int line() const {
36*ab8db090SAndroid Build Coastguard Worker     return line_;
37*ab8db090SAndroid Build Coastguard Worker   }
error_code()38*ab8db090SAndroid Build Coastguard Worker   ErrorCode error_code() const {
39*ab8db090SAndroid Build Coastguard Worker     return error_code_;
40*ab8db090SAndroid Build Coastguard Worker   }
error_message()41*ab8db090SAndroid Build Coastguard Worker   const char *error_message() const {
42*ab8db090SAndroid Build Coastguard Worker     return error_message_;
43*ab8db090SAndroid Build Coastguard Worker   }
44*ab8db090SAndroid Build Coastguard Worker 
what()45*ab8db090SAndroid Build Coastguard Worker   virtual const char *what() const throw() {
46*ab8db090SAndroid Build Coastguard Worker     return error_message_;
47*ab8db090SAndroid Build Coastguard Worker   }
48*ab8db090SAndroid Build Coastguard Worker 
49*ab8db090SAndroid Build Coastguard Worker  private:
50*ab8db090SAndroid Build Coastguard Worker   const char *filename_;
51*ab8db090SAndroid Build Coastguard Worker   int line_;
52*ab8db090SAndroid Build Coastguard Worker   ErrorCode error_code_;
53*ab8db090SAndroid Build Coastguard Worker   const char *error_message_;
54*ab8db090SAndroid Build Coastguard Worker };
55*ab8db090SAndroid Build Coastguard Worker 
56*ab8db090SAndroid Build Coastguard Worker // These macros are used to convert a line number to a string constant.
57*ab8db090SAndroid Build Coastguard Worker #define MARISA_INT_TO_STR(value) #value
58*ab8db090SAndroid Build Coastguard Worker #define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
59*ab8db090SAndroid Build Coastguard Worker #define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
60*ab8db090SAndroid Build Coastguard Worker 
61*ab8db090SAndroid Build Coastguard Worker // MARISA_THROW throws an exception with a filename, a line number, an error
62*ab8db090SAndroid Build Coastguard Worker // code and an error message. The message format is as follows:
63*ab8db090SAndroid Build Coastguard Worker //  "__FILE__:__LINE__: error_code: error_message"
64*ab8db090SAndroid Build Coastguard Worker #define MARISA_THROW(error_code, error_message) \
65*ab8db090SAndroid Build Coastguard Worker   (throw marisa::Exception(__FILE__, __LINE__, error_code, \
66*ab8db090SAndroid Build Coastguard Worker        __FILE__ ":" MARISA_LINE_STR ": " #error_code ": " error_message))
67*ab8db090SAndroid Build Coastguard Worker 
68*ab8db090SAndroid Build Coastguard Worker // MARISA_THROW_IF throws an exception if `condition' is true.
69*ab8db090SAndroid Build Coastguard Worker #define MARISA_THROW_IF(condition, error_code) \
70*ab8db090SAndroid Build Coastguard Worker   (void)((!(condition)) || (MARISA_THROW(error_code, #condition), 0))
71*ab8db090SAndroid Build Coastguard Worker 
72*ab8db090SAndroid Build Coastguard Worker // MARISA_DEBUG_IF is ignored if _DEBUG is undefined. So, it is useful for
73*ab8db090SAndroid Build Coastguard Worker // debugging time-critical codes.
74*ab8db090SAndroid Build Coastguard Worker #ifdef _DEBUG
75*ab8db090SAndroid Build Coastguard Worker  #define MARISA_DEBUG_IF(cond, error_code) MARISA_THROW_IF(cond, error_code)
76*ab8db090SAndroid Build Coastguard Worker #else
77*ab8db090SAndroid Build Coastguard Worker  #define MARISA_DEBUG_IF(cond, error_code)
78*ab8db090SAndroid Build Coastguard Worker #endif
79*ab8db090SAndroid Build Coastguard Worker 
80*ab8db090SAndroid Build Coastguard Worker }  // namespace marisa
81*ab8db090SAndroid Build Coastguard Worker 
82*ab8db090SAndroid Build Coastguard Worker #endif  // MARISA_EXCEPTION_H_
83