error.hpp

Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2005-2010 Taneli Kalvas. All rights reserved.
00006  *
00007  * You can redistribute this software and/or modify it under the terms
00008  * of the GNU General Public License as published by the Free Software
00009  * Foundation; either version 2 of the License, or (at your option)
00010  * any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this library (file "COPYING" included in the package);
00019  * if not, write to the Free Software Foundation, Inc., 51 Franklin
00020  * Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  * 
00022  * If you have questions about your rights to use or distribute this
00023  * software, please contact Berkeley Lab's Technology Transfer
00024  * Department at TTD@lbl.gov. Other questions, comments and bug
00025  * reports should be sent directly to the author via email at
00026  * taneli.kalvas@jyu.fi.
00027  * 
00028  * NOTICE. This software was developed under partial funding from the
00029  * U.S.  Department of Energy.  As such, the U.S. Government has been
00030  * granted for itself and others acting on its behalf a paid-up,
00031  * nonexclusive, irrevocable, worldwide license in the Software to
00032  * reproduce, prepare derivative works, and perform publicly and
00033  * display publicly.  Beginning five (5) years after the date
00034  * permission to assert copyright is obtained from the U.S. Department
00035  * of Energy, and subject to any subsequent five (5) year renewals,
00036  * the U.S. Government is granted for itself and others acting on its
00037  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
00038  * the Software to reproduce, prepare derivative works, distribute
00039  * copies to the public, perform publicly and display publicly, and to
00040  * permit others to do so.
00041  */
00042 
00043 #ifndef ERROR_HPP
00044 #define ERROR_HPP 1
00045 
00046 
00047 #include <string>
00048 #include <cstring>
00049 #include <stdint.h>
00050 #include <sstream>
00051 #include <errno.h>
00052 
00053 
00054 template <class T>
00055 inline std::string to_string( const T& t )
00056 {
00057     std::stringstream ss;
00058     ss << t;
00059     return( ss.str() );
00060 }
00061 
00062 
00065 #define ERROR_LOCATION ErrorLocation( __FILE__, __LINE__, __func__ )
00066 
00067 
00074 struct ErrorLocation {
00075     ErrorLocation() {}
00076     ErrorLocation( const char *file, int line, const char *func ) :
00077         _file(file), _line(line), _func(func) {}
00078     const char *_file;
00079     int         _line;
00080     const char *_func;
00081 };
00082 
00083 
00086 struct Error {
00087     ErrorLocation  _loc;
00088     std::string    _error_str;
00089     Error() {}
00090     Error( ErrorLocation loc, const std::string &str ) 
00091         : _loc(loc), _error_str(str) {}
00092 };
00093 
00094 
00097 struct ErrorNoMem : public Error {
00098     ErrorNoMem( ErrorLocation loc ) 
00099         : Error( loc, "memory allocation error" ) {}
00100 };
00101 
00102 
00105 struct ErrorErrno : public Error {
00106     int _ierrno;
00107     ErrorErrno( ErrorLocation loc ) : _ierrno(errno) {
00108         _loc = loc;
00109 #if defined(WIN32) || defined(__MINGW32__)
00110         _error_str = "errno " + to_string(_ierrno);
00111 #else
00112         char buf[1024];
00113         strerror_r( _ierrno, buf, 1024 );
00114         _error_str = buf;
00115 #endif
00116     }
00117 };
00118 
00119 
00122 struct ErrorUnimplemented : public Error {
00123     ErrorUnimplemented( ErrorLocation loc ) 
00124         : Error( loc, "feature unimplemented" ) {}
00125     ErrorUnimplemented( ErrorLocation loc, const std::string &str ) 
00126         : Error( loc, str ) {}
00127 };
00128 
00129 
00132 struct ErrorDim : public Error {
00133     ErrorDim( ErrorLocation loc )
00134         : Error( loc, "dimension mismatch" ) {}
00135     ErrorDim( ErrorLocation loc, const std::string &str )
00136         : Error( loc, str ) {}
00137 };
00138 
00139 
00142 struct ErrorRange : public Error {
00143     ErrorRange( ErrorLocation loc, uint32_t i, uint32_t n, uint32_t j, uint32_t m ) {
00144         std::ostringstream ss;
00145         ss << "index out of range ( " << i << " >= " << n << " || ";
00146         ss << j << " >= " << m << " )";
00147         _error_str = ss.str();
00148         _loc = loc;
00149     }
00150     ErrorRange( ErrorLocation loc, uint32_t i, uint32_t n ) {
00151         std::ostringstream ss;
00152         ss << "index out of range ( " << i << " >= " << n << " )";
00153         _error_str = ss.str();
00154         _loc = loc;
00155     }
00156 };
00157 
00158 
00159 #endif
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 

Generated on Thu Apr 21 13:39:59 2011 for IBSimu by  doxygen 1.4.7