core/vil/vil_exception.h

Go to the documentation of this file.
00001 #ifndef vil_exception_h_
00002 #define vil_exception_h_
00003 //:
00004 // \file
00005 // \brief Exceptions thrown by vil, 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 #include <vil/vil_pixel_format.h>
00015 
00016 
00017 //: Throw an exception indicating a definite problem.
00018 // If exceptions have been disabled, this function
00019 // will abort.
00020 template <class T>
00021 void vil_exception_error(T exception)
00022 {
00023 #if !defined VXL_LEGACY_ERROR_REPORTING && VCL_HAS_EXCEPTIONS
00024   throw exception;
00025 #else
00026   vcl_cerr << "\nERROR: " << exception.what() << vcl_endl;
00027   vcl_abort();
00028 #endif
00029 }
00030 
00031 //: Throw an exception indicating a potential problem.
00032 // If exceptions have been disabled, this function
00033 // will return.
00034 template <class T>
00035 void vil_exception_warning(T exception)
00036 {
00037 #if !defined VXL_LEGACY_ERROR_REPORTING && VCL_HAS_EXCEPTIONS
00038   throw exception;
00039 #else
00040   vcl_cerr << "\nWARNING: " << exception.what() << vcl_endl;
00041 #endif
00042 }
00043 
00044   //: Indicates that a function call failed because the pixel types were incompatible.
00045   class vil_exception_pixel_formats_incompatible
00046 #if VCL_HAS_EXCEPTIONS
00047   : public vcl_logic_error
00048 #endif
00049   {
00050    public:
00051     enum vil_pixel_format src_type, dest_type;
00052     vcl_string operation_name;
00053     vil_exception_pixel_formats_incompatible(
00054       enum vil_pixel_format src, enum vil_pixel_format dest, const vcl_string& operation) :
00055 #if VCL_HAS_EXCEPTIONS
00056     vcl_logic_error(operation + ": Pixel formats incompatible."),
00057 #endif
00058       src_type(src), dest_type(dest), operation_name(operation) {}
00059 #if VCL_HAS_EXCEPTIONS
00060     virtual ~vil_exception_pixel_formats_incompatible() throw() {}
00061 #else
00062     const char * what() const {return "Pixel formats incompatible.";}
00063 #endif
00064   };
00065 
00066 
00067   //: Indicates that a function call failed because a pixel format could not be handled.
00068   class vil_exception_unsupported_pixel_format
00069 #if VCL_HAS_EXCEPTIONS
00070   : public vcl_logic_error
00071 #endif
00072   {
00073    public:
00074     enum vil_pixel_format src_type;
00075     vcl_string operation_name;
00076     vil_exception_unsupported_pixel_format(
00077       enum vil_pixel_format src, const vcl_string& operation) :
00078 #if VCL_HAS_EXCEPTIONS
00079     vcl_logic_error(operation + ": Unsupported pixel format."),
00080 #endif
00081       src_type(src), operation_name(operation) {}
00082 #if VCL_HAS_EXCEPTIONS
00083     virtual ~vil_exception_unsupported_pixel_format() throw() {}
00084 #else
00085     const char * what() const {return "Unsupported Pixel formats.";}
00086 #endif
00087   };
00088 
00089 
00090   //: Indicates that some reference was made to pixels beyond the bounds of an image.
00091   // In most cases of out-of-bounds access, you will not get this exception. For efficiency
00092   // reasons, vil may not test for this problem, or may if you are lucky trip an assert.
00093   // This function is only used in cases where easy of use, and risk of mistakes are high,
00094   // and inefficiency is very low.
00095   class vil_exception_out_of_bounds
00096 #if VCL_HAS_EXCEPTIONS
00097   : public vcl_logic_error
00098 #endif
00099   {
00100    public:
00101     vcl_string operation_name;
00102     vil_exception_out_of_bounds(
00103       const vcl_string& operation) :
00104 #if VCL_HAS_EXCEPTIONS
00105     vcl_logic_error(operation + ": Pixel access out-of-bounds."),
00106 #endif
00107       operation_name(operation) {}
00108 #if VCL_HAS_EXCEPTIONS
00109     virtual ~vil_exception_out_of_bounds() throw() {}
00110 #else
00111     const char * what() const {return "Pixel access out-of-bounds.";}
00112 #endif
00113   };
00114 
00115 
00116 
00117   //: Indicates that some operation is not supported.
00118   // In most cases you will not get this exception. For efficiency
00119   // reasons, vil may not test for this problem, or may if you are lucky trip an assert.
00120   // This function is only used in cases where easy of use, and risk of mistakes are high,
00121   // and inefficiency is very low.
00122   class vil_exception_unsupported_operation
00123 #if VCL_HAS_EXCEPTIONS
00124   : public vcl_logic_error
00125 #endif
00126   {
00127    public:
00128     vcl_string operation_name;
00129     vil_exception_unsupported_operation(
00130       const vcl_string& operation) :
00131 #if VCL_HAS_EXCEPTIONS
00132     vcl_logic_error(operation + ": Unsupported operation."),
00133 #endif
00134       operation_name(operation) {}
00135 #if VCL_HAS_EXCEPTIONS
00136     virtual ~vil_exception_unsupported_operation() throw() {}
00137 #else
00138     const char * what() const {return "Unsupported operation.";}
00139 #endif
00140   };
00141 
00142 
00143   //: Indicates that an image load or save operation failed.
00144   // Generally this should be thrown, only after checks on the image type
00145   // have been passed by the file format object, and while an
00146   // unrecoverable error is detected inside the image_resource constructor,
00147   // or similar.
00148   class vil_exception_image_io
00149 #if VCL_HAS_EXCEPTIONS
00150   : public vcl_runtime_error
00151 #endif
00152   {
00153    public:
00154 #if !VCL_HAS_EXCEPTIONS
00155     vcl_string full_what;
00156 #endif
00157     vcl_string function_name, file_type, filename;
00158     vil_exception_image_io(const vcl_string& function,
00159                            const vcl_string& type,
00160                            const vcl_string& name) :
00161 #if VCL_HAS_EXCEPTIONS
00162       vcl_runtime_error
00163 #else
00164       full_what
00165 #endif
00166       ("Unrecoverable failure in " + function + " while loading "
00167        + name + " using " + type + " loader."),
00168       function_name(function), file_type(type), filename(name) {}
00169 #if VCL_HAS_EXCEPTIONS
00170     virtual ~vil_exception_image_io() throw() {}
00171 #else
00172     const char * what() const { return full_what.c_str(); }
00173 #endif
00174   };
00175 
00176 
00177 
00178 
00179 
00180 #endif // vil_exception_h_
00181 

Generated on Tue Oct 14 05:07:46 2008 for core/vil by  doxygen 1.5.1