Go to the documentation of this file.00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifdef MONGO_CODE_EXAMPLE
00031 mongo_connection conn[1];
00032
00033 MONGO_TRY{
00034 mongo_find_one(...);
00035 MONGO_THROW(conn, MONGO_EXCEPT_NETWORK);
00036 }MONGO_CATCH{
00037 switch(conn->exception->type){
00038 case MONGO_EXCEPT_NETWORK:
00039 do_something();
00040 case MONGO_EXCEPT_FIND_ERR:
00041 do_something();
00042 default:
00043 MONGO_RETHROW();
00044 }
00045 }
00046 #endif
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef _MONGO_EXCEPT_H_
00067 #define _MONGO_EXCEPT_H_
00068
00069 #include <setjmp.h>
00070
00071
00072 typedef enum{
00073 MONGO_EXCEPT_NETWORK=1,
00074 MONGO_EXCEPT_FIND_ERR
00075 }mongo_exception_type;
00076
00077
00078 typedef struct {
00079 jmp_buf base_handler;
00080 jmp_buf *penv;
00081 int caught;
00082 volatile mongo_exception_type type;
00083 }mongo_exception_context;
00084
00085 #define MONGO_TRY MONGO_TRY_GENERIC(conn)
00086 #define MONGO_CATCH MONGO_CATCH_GENERIC(conn)
00087 #define MONGO_THROW(e) MONGO_THROW_GENERIC(conn, e)
00088 #define MONGO_RETHROW() MONGO_RETHROW_GENERIC(conn)
00089
00090
00091
00092
00093 #define MONGO_INIT_EXCEPTION(exception_ptr) \
00094 do{ \
00095 mongo_exception_type t; \
00096 (exception_ptr)->penv = &(exception_ptr)->base_handler; \
00097 if ((t = setjmp((exception_ptr)->base_handler))) { \
00098 switch(t){ \
00099 case MONGO_EXCEPT_NETWORK: bson_fatal_msg(0, "network error"); \
00100 case MONGO_EXCEPT_FIND_ERR: bson_fatal_msg(0, "error in find"); \
00101 default: bson_fatal_msg(0, "unknown exception"); \
00102 } \
00103 } \
00104 }while(0)
00105
00106 #define MONGO_TRY_GENERIC(connection) \
00107 { \
00108 jmp_buf *exception__prev, exception__env; \
00109 exception__prev = (connection)->exception.penv; \
00110 (connection)->exception.penv = &exception__env; \
00111 if (setjmp(exception__env) == 0) { \
00112 do
00113
00114 #define MONGO_CATCH_GENERIC(connection) \
00115 while ((connection)->exception.caught = 0, \
00116 (connection)->exception.caught); \
00117 } \
00118 else { \
00119 (connection)->exception.caught = 1; \
00120 } \
00121 (connection)->exception.penv = exception__prev; \
00122 } \
00123 if (!(connection)->exception.caught ) { } \
00124 else
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 #define MONGO_THROW_GENERIC(connection, type_in) \
00136 for (;; longjmp(*(connection)->exception.penv, type_in)) \
00137 (connection)->exception.type = type_in
00138
00139 #define MONGO_RETHROW_GENERIC(connection) \
00140 MONGO_THROW_GENERIC(connection, (connection)->exception.type)
00141
00142
00143 #endif