• Main Page
  • Classes
  • Files
  • File List
  • File Members

mongo_except.h

Go to the documentation of this file.
00001 
00003 /*    Copyright 2009 10gen Inc.
00004  *
00005  *    Licensed under the Apache License, Version 2.0 (the "License");
00006  *    you may not use this file except in compliance with the License.
00007  *    You may obtain a copy of the License at
00008  *
00009  *    http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  *    Unless required by applicable law or agreed to in writing, software
00012  *    distributed under the License is distributed on an "AS IS" BASIS,
00013  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *    See the License for the specific language governing permissions and
00015  *    limitations under the License.
00016  */
00017 
00018 /* This file is based loosely on cexcept (http://www.nicemice.net/cexcept/). I
00019  * have modified it to work better with mongo's API.
00020  *
00021  * The MONGO_TRY, MONGO_CATCH, and MONGO_TROW macros assume that a pointer to
00022  * the current connection is available as 'conn'. If you would like to use a
00023  * different name, use the _GENERIC version of these macros.
00024  *
00025  * WARNING: do not return or otherwise jump (excluding MONGO_TRHOW()) out of a
00026  * MONGO_TRY block as the nessesary clean-up code will not be called. Jumping
00027  * out of the MONGO_CATCH block is OK.
00028  */
00029 
00030 #ifdef MONGO_CODE_EXAMPLE
00031     mongo_connection conn[1]; /* makes conn a ptr to the connection */
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  /* ORIGINAL CEXEPT COPYRIGHT:
00049 cexcept: README 2.0.1 (2008-Jul-23-Wed)
00050 http://www.nicemice.net/cexcept/
00051 Adam M. Costello
00052 http://www.nicemice.net/amc/
00053 
00054 The package is both free-as-in-speech and free-as-in-beer:
00055 
00056     Copyright (c) 2000-2008 Adam M. Costello and Cosmin Truta.
00057     This package may be modified only if its author and version
00058     information is updated accurately, and may be redistributed
00059     only if accompanied by this unaltered notice.  Subject to those
00060     restrictions, permission is granted to anyone to do anything with
00061     this package.  The copyright holders make no guarantees regarding
00062     this package, and are not responsible for any damage resulting from
00063     its use.
00064  */
00065 
00066 #ifndef _MONGO_EXCEPT_H_
00067 #define _MONGO_EXCEPT_H_
00068 
00069 #include <setjmp.h>
00070 
00071 /* always non-zero */
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 /* the rest of this file is implementation details */
00091 
00092 /* this is done in mongo_connect */
00093 #define MONGO_INIT_EXCEPTION(exception_ptr) \
00094     do{ \
00095         mongo_exception_type t; /* exception_ptr won't be available */\
00096         (exception_ptr)->penv = &(exception_ptr)->base_handler; \
00097         if ((t = setjmp((exception_ptr)->base_handler))) { /* yes, '=' is correct */ \
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 /* Try ends with do, and Catch begins with while(0) and ends with     */
00127 /* else, to ensure that Try/Catch syntax is similar to if/else        */
00128 /* syntax.                                                            */
00129 /*                                                                    */
00130 /* The 0 in while(0) is expressed as x=0,x in order to appease        */
00131 /* compilers that warn about constant expressions inside while().     */
00132 /* Most compilers should still recognize that the condition is always */
00133 /* false and avoid generating code for it.                            */
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

Generated on Wed May 18 2011 11:22:49 for MongoDBCDriver by  doxygen 1.7.1