contrib/mul/mbl/mbl_exception.h
Go to the documentation of this file.
00001 #ifndef mbl_exception_h_
00002 #define mbl_exception_h_
00003 //:
00004 // \file
00005 // \brief Exceptions thrown by mbl, and a mechanism for turning them off.
00006 // \author Ian Scott.
00007 
00008 #include <vcl_string.h>
00009 #include <vcl_cstdlib.h>
00010 #include <vcl_iostream.h>
00011 #if VCL_HAS_EXCEPTIONS
00012 # include <vcl_stdexcept.h>
00013 #endif
00014 
00015 
00016 //: Throw an exception indicating a real problem.
00017 // If exceptions have been disabled, this function
00018 // may abort.
00019 template <class T>
00020 void mbl_exception_error(T exception)
00021 {
00022   vcl_cerr << "\nERROR: " << exception.what() << vcl_endl;
00023 #if !defined MBL_EXCEPTIONS_DISABLE  && VCL_HAS_EXCEPTIONS
00024   throw exception;
00025 #else
00026   vcl_abort();
00027 #endif
00028 }
00029 
00030 //: Throw an exception indicating a potential problem.
00031 // If exceptions have been disabled, this function
00032 // may return.
00033 template <class T>
00034 void mbl_exception_warning(T exception)
00035 {
00036   vcl_cerr << "\nWARNING: " << exception.what() << vcl_endl;
00037 #if !defined MBL_EXCEPTIONS_DISABLE  && VCL_HAS_EXCEPTIONS
00038   throw exception;
00039 #endif
00040 }
00041 
00042 
00043 #if !VCL_HAS_EXCEPTIONS
00044 
00045   //: Indicates that mbl_cloneables_factory has not heard of value name.
00046   class mbl_exception_no_name_in_factory
00047   {
00048     vcl_string msg_;
00049    public:
00050     mbl_exception_no_name_in_factory(vcl_string failed_name, vcl_string valid_names)
00051       : msg_(vcl_string("No such value: ")+failed_name+"\nValid values are: ["+valid_names+"]") {}
00052     const char * what() const {return msg_.c_str();}
00053   };
00054 
00055 #else
00056 
00057   //: Indicates that mbl_cloneables_factory has not heard of value name.
00058   class mbl_exception_no_name_in_factory : public vcl_logic_error
00059   {
00060    public:
00061     vcl_string failed_value, valid_values;
00062     mbl_exception_no_name_in_factory(const vcl_string& failed_name, const vcl_string& valid_names):
00063       vcl_logic_error(vcl_string("No such value: ") +failed_name + "\nValid values are: ["+valid_names+"]"),
00064         failed_value(failed_name), valid_values(valid_names) {}
00065     virtual ~mbl_exception_no_name_in_factory() throw() {}
00066   };
00067 
00068 #endif
00069 
00070 
00071 #if !VCL_HAS_EXCEPTIONS
00072 
00073   //: General purpose - a replacement for vcl_abort.
00074   // The only point of catching this exception, is to
00075   // give you a chance to save your data. If this exception
00076   // is thrown, then the program correctness is in doubt.
00077   class mbl_exception_abort
00078   {
00079     vcl_string msg_;
00080    public:
00081     mbl_exception_abort(const vcl_string& comment);
00082     const char * what() const {return msg_.c_str();}
00083   };
00084 
00085 #else
00086 
00087   //: General purpose - a replacement for vcl_abort.
00088   // The only point of catching this exception, is to
00089   // give you a chance to save your data. If this exception
00090   // is thrown, then the program correctness is in doubt.
00091   class mbl_exception_abort : public vcl_logic_error
00092   {
00093    public:
00094     mbl_exception_abort(const vcl_string& comment);
00095     virtual ~mbl_exception_abort() throw() {}
00096   };
00097 
00098 #endif
00099 
00100 
00101 #if !VCL_HAS_EXCEPTIONS
00102 
00103   //: Indicates a problem whilst parsing text configuration data.
00104   class mbl_exception_parse_error
00105   {
00106     vcl_string msg_;
00107    public:
00108     mbl_exception_parse_error(const vcl_string &msg)
00109       : msg_(msg) {}
00110     const char * what() const {return msg_.c_str();}
00111   };
00112 
00113 #else
00114 
00115   //: Indicates a problem whilst parsing text configuration data.
00116   class mbl_exception_parse_error: public vcl_runtime_error
00117   {
00118    public:
00119     mbl_exception_parse_error(const vcl_string &msg)
00120       : vcl_runtime_error(msg) {}
00121     virtual ~mbl_exception_parse_error() throw() {}
00122   };
00123 
00124 #endif
00125 
00126 #if !VCL_HAS_EXCEPTIONS
00127 
00128   //: Indicates a problem whilst parsing a file.
00129   class mbl_exception_parse_file_error
00130   {
00131     vcl_string msg_;
00132     vcl_string filename_;
00133    public:
00134      mbl_exception_parse_file_error(const vcl_string &msg, const vcl_string& filename)
00135       : msg_(msg+" "+filename), filename_(filename) {}
00136     const char * what() const {return msg_.c_str();}
00137     const char * filename() const {return filename_.c_str();}
00138   };
00139 
00140 #else
00141 
00142   //: Indicates a problem whilst parsing a file.
00143   class mbl_exception_parse_file_error: public mbl_exception_parse_error
00144   {
00145     vcl_string filename_;
00146    public:
00147      mbl_exception_parse_file_error(const vcl_string &msg, const vcl_string& filename)
00148       : mbl_exception_parse_error(msg+" "+filename), filename_(filename) {}
00149     const char * filename() const {return filename_.c_str();}
00150     virtual ~mbl_exception_parse_file_error() throw() {}
00151   };
00152 
00153 #endif
00154 
00155 #if !VCL_HAS_EXCEPTIONS
00156 
00157   //: Indicates that an expected property label was missing.
00158   class mbl_exception_missing_property
00159   {
00160     vcl_string msg_;
00161    public:
00162     mbl_exception_missing_property(const vcl_string &missing)
00163       : msg_(vcl_string("Couldn't find expected property label: \""+missing+'\"')) {}
00164     const char * what() const {return msg_.c_str();}
00165   };
00166 
00167 #else
00168 
00169   //: Indicates a problem whilst parsing text configuration data.
00170   class mbl_exception_missing_property: public mbl_exception_parse_error
00171   {
00172    public:
00173    vcl_string missing_label;
00174     mbl_exception_missing_property(const vcl_string &missing)
00175       : mbl_exception_parse_error(
00176           vcl_string("Couldn't find expected property label: \""+missing+'\"')),
00177         missing_label(missing)
00178     {}
00179     virtual ~mbl_exception_missing_property() throw() {}
00180   };
00181 
00182 #endif
00183 
00184 #if !VCL_HAS_EXCEPTIONS
00185 
00186   //: Indicates that mbl_exception_look_for_unused_props found some unused properties.
00187   class mbl_exception_unused_props
00188   {
00189     vcl_string msg_;
00190    public:
00191     mbl_exception_unused_props(const vcl_string &function_name, const vcl_string &unused_props)
00192       : msg_(function_name + ": Unused properties found:\n" + unused_props) {}
00193     const char * what() const {return msg_.c_str();}
00194   };
00195 
00196 #else
00197 
00198   //: Indicates that mbl_exception_look_for_unused_props found some unused properties.
00199   class mbl_exception_unused_props : public mbl_exception_parse_error
00200   {
00201    public:
00202     vcl_string function_name, unused_properties;
00203     mbl_exception_unused_props(const vcl_string &fn_name, const vcl_string &unused_props)
00204       : mbl_exception_parse_error(fn_name + ": Unused properties found:\n" + unused_props),
00205         function_name(fn_name), unused_properties(unused_props) {}
00206     virtual ~mbl_exception_unused_props() throw() {}
00207   };
00208 
00209 #endif
00210 
00211 
00212 #if !VCL_HAS_EXCEPTIONS
00213 
00214   //: Indicates a problem whilst parsing text configuration data into an mbl_read_props object.
00215   class mbl_exception_read_props_parse_error
00216   {
00217     vcl_string msg_;
00218    public:
00219     mbl_exception_read_props_parse_error(const vcl_string &msg)
00220       : msg_(vcl_string("mbl_read_props: ") + msg) {}
00221     const char * what() const {return msg_.c_str();}
00222   };
00223 
00224 #else
00225 
00226   //: Indicates a problem whilst parsing text configuration data into an mbl_read_props object.
00227   class mbl_exception_read_props_parse_error: public mbl_exception_parse_error
00228   {
00229    public:
00230     mbl_exception_read_props_parse_error(const vcl_string &msg)
00231       : mbl_exception_parse_error(vcl_string("mbl_read_props: ") + msg) {}
00232     virtual ~mbl_exception_read_props_parse_error() throw() {}
00233   };
00234 
00235 #endif
00236 
00237 #if !VCL_HAS_EXCEPTIONS
00238 
00239   //: Indicates a problem whilst parsing a block of text configuration data.
00240   class mbl_exception_parse_block_parse_error
00241   {
00242     vcl_string msg_;
00243    public:
00244     mbl_exception_parse_block_parse_error(const vcl_string &msg,
00245       const vcl_string &contents)
00246     : msg_(vcl_string("mbl_parse_block: ") + msg +
00247       "Contents of block:\n" + contents) {}
00248     const char * what() const {return msg_.c_str();}
00249   };
00250 
00251 #else
00252 
00253   //: Indicates a problem whilst parsing a block of text configuration data.
00254   class mbl_exception_parse_block_parse_error: public mbl_exception_parse_error
00255   {
00256    public:
00257     //: Description of problem
00258     vcl_string msg;
00259     //: Contents of string which failed to be parsed.
00260     vcl_string contents;
00261     mbl_exception_parse_block_parse_error(const vcl_string &msg,
00262       const vcl_string &contents)
00263     : mbl_exception_parse_error(vcl_string("mbl_parse_block: ") + msg +
00264       "Contents of block:\n" + contents), msg(msg), contents(contents) {}
00265     virtual ~mbl_exception_parse_block_parse_error() throw() {}
00266   };
00267 
00268 #endif
00269 
00270 //:Throw mbl_exception_os_error or one of its derivatives, based on errno.
00271 // If exceptions are disabled, this behaves like mbl_exception_warning() above.
00272 void mbl_exception_throw_os_error(const vcl_string& filename,
00273                                   const vcl_string& additional_comment="");
00274 
00275 
00276 #if !VCL_HAS_EXCEPTIONS
00277 
00278   //: Indicates a problem reported during an OS call.
00279   class mbl_exception_os_error
00280   {
00281     vcl_string msg_;
00282    public:
00283     //: Reported errno
00284     int errno;
00285     //: System supplied error message.
00286     vcl_string error_message;
00287     //: Filename or pathname or other id on which OS call failed.
00288     vcl_string filename;
00289     //: Optional additional comments.
00290     vcl_string additional_comment;
00291     mbl_exception_os_error(int err_no, const vcl_string &file_name,
00292       const vcl_string &comment="");
00293     virtual ~mbl_exception_os_error() throw() {}
00294     const char * what() const {return msg_.c_str();}
00295   };
00296 
00297 #else
00298 
00299   //: Indicates a problem reported during an OS call.
00300   class mbl_exception_os_error: public vcl_runtime_error
00301   {
00302    public:
00303     //: Reported errno
00304     int err_no;
00305     //: System supplied error message.
00306     vcl_string error_message;
00307     //: Filename or pathname or other id on which OS call failed.
00308     vcl_string filename;
00309     //: Optional additional comments.
00310     vcl_string additional_comment;
00311     mbl_exception_os_error(int errnum, const vcl_string &file_name,
00312       const vcl_string &comment="");
00313     virtual ~mbl_exception_os_error() throw() {}
00314   };
00315 
00316 #endif
00317 
00318 
00319 #define MACRO( E ) \
00320 class E : public mbl_exception_os_error{ public: \
00321   E (int err_no, const vcl_string &file_name, const vcl_string &comment=""): \
00322     mbl_exception_os_error(err_no, file_name, comment) {} }
00323 
00324 MACRO(mbl_exception_os_no_such_file_or_directory);
00325 MACRO(mbl_exception_os_permission_denied);
00326 MACRO(mbl_exception_os_file_exists);
00327 MACRO(mbl_exception_os_not_a_directory);
00328 MACRO(mbl_exception_os_is_a_directory);
00329 MACRO(mbl_exception_os_no_space_left_on_device);
00330 
00331 #undef MACRO
00332 
00333 #endif // mbl_exception_h_
00334