Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/base/exception.hpp
Go to the documentation of this file.
00001 #ifndef SKYLARK_EXCEPTION_HPP
00002 #define SKYLARK_EXCEPTION_HPP
00003 
00004 #include <string>
00005 #include <exception>
00006 
00007 #include <boost/exception/all.hpp>
00008 
00009 const char* const skylark_errmsg[] = {
00010       "Skylark failure"
00011     , "Skylark failed while communicating (MPI)"
00012     , "Skylark failed in a call to Elemental"
00013     , "Skylark failed due to a unsupported matrix distribution"
00014     , "Skylark failed in a call to CombBLAS"
00015     , "Skylark failed in sketching a matrix"
00016     , "Skylark failed in nla operation"
00017     , "Skylark failed when allocating memory in a sketch"
00018     , "Skylark failed in a call into the Random123 layer"
00019     , "Skylark failed in I/O calls"
00020     , "Skylark failed due to a unsupported base operation"
00021 };
00022 
00024 const char* skylark_strerror(int error_code) {
00025     return skylark_errmsg[error_code - 100];
00026 }
00027 
00028 
00030 #define SKYLARK_BEGIN_TRY() try {
00031 
00032 #define SKYLARK_END_TRY()   }
00033 
00035 #define SKYLARK_THROW_EXCEPTION(x) \
00036     BOOST_THROW_EXCEPTION(x);
00037 
00039 #define SKYLARK_PRINT_EXCEPTION_DETAILS(ex) \
00040     std::cerr << boost::diagnostic_information(ex) << std::endl;
00041 
00043 #define SKYLARK_PRINT_EXCEPTION_TRACE(ex) \
00044     if (const std::string *trace = \
00045             boost::get_error_info<skylark::base::stack_trace>(ex)) { \
00046         std::cerr << *trace << std::endl; \
00047     }
00048 
00050 //XXX: only get top-most exception?
00051 #define SKYLARK_CATCH_AND_RETURN_ERROR_CODE() \
00052     catch (const skylark::base::skylark_exception& ex) { \
00053         if (int const *c = \
00054                 boost::get_error_info<skylark::base::error_code>(ex)) { \
00055             return *c; \
00056         } \
00057     }
00058 
00059 
00060 
00061 namespace skylark {
00062 namespace base {
00063 
00065 typedef boost::error_info<struct tag_error_code, int>         error_code;
00067 typedef boost::error_info<struct tag_error_msg,  std::string> error_msg;
00068 
00070 typedef boost::error_info<struct tag_stack_trace,  std::string> stack_trace;
00071 typedef boost::error_info<struct tag_append_trace, std::string> append_trace;
00072 
00074 struct skylark_exception : virtual boost::exception, virtual std::exception {
00075 
00076     skylark_exception() {
00077         *this << error_code(100);
00078     }
00079 
00080     skylark_exception& operator<< (const append_trace& rhs) {
00081 
00082         std::string trace_value = "";
00083 
00084         if( const std::string *cur_trace =
00085             boost::get_error_info<stack_trace, skylark_exception>(*this) ) {
00086             trace_value.append(*cur_trace);
00087             trace_value.append("\n");
00088         }
00089 
00090         trace_value.append(rhs.value());
00091         *this << stack_trace(trace_value);
00092         return *this;
00093     }
00094 
00095 };
00096 
00098 struct elemental_exception : virtual skylark_exception {
00099 public:
00100     using skylark_exception::operator<<;
00101 
00102     elemental_exception() {
00103         *this << error_code(102);
00104     }
00105 };
00107 struct combblas_exception : virtual skylark_exception {
00108 public:
00109     using skylark_exception::operator<<;
00110 
00111     combblas_exception() {
00112         *this << error_code(104);
00113     }
00114 };
00116 struct mpi_exception : virtual skylark_exception {
00117 public:
00118     using skylark_exception::operator<<;
00119 
00120     mpi_exception() {
00121         *this << error_code(101);
00122     }
00123 };
00124 
00126 struct sketch_exception : virtual skylark_exception {
00127 public:
00128     using skylark_exception::operator<<;
00129 
00130     sketch_exception() {
00131         *this << error_code(105);
00132     }
00133 };
00134 
00136 struct nla_exception : virtual skylark_exception {
00137 public:
00138     using skylark_exception::operator<<;
00139 
00140     nla_exception() {
00141         *this << error_code(106);
00142     }
00143 };
00144 
00145 
00147 struct random123_exception : virtual skylark_exception {
00148 public:
00149     using skylark_exception::operator<<;
00150 
00151     random123_exception() {
00152         *this << error_code(108);
00153     }
00154 };
00155 
00157 struct io_exception : virtual skylark_exception {
00158 public:
00159     using skylark_exception::operator<<;
00160 
00161     io_exception() {
00162         *this << error_code(108);
00163     }
00164 };
00165 
00167 struct allocation_exception : virtual sketch_exception  {
00168 public:
00169     using sketch_exception::operator<<;
00170 
00171     allocation_exception() {
00172         *this << error_code(107);
00173     }
00174 };
00175 
00177 struct unsupported_matrix_distribution : virtual elemental_exception {
00178 public:
00179     using elemental_exception::operator<<;
00180 
00181     unsupported_matrix_distribution() {
00182         *this << error_code(103);
00183     }
00184 };
00185 
00187 struct unsupported_base_operation : virtual elemental_exception {
00188 public:
00189     using skylark_exception::operator<<;
00190 
00191     unsupported_base_operation() {
00192         *this << error_code(109);
00193     }
00194 };
00195 
00196 
00197 } // namespace base
00198 } // namespace skylark
00199 
00200 #endif