00001 #ifndef _LCORE_LCoreError_H_ // -*-c++-*- 00002 00003 /* 00004 * Copyright (C) 2001 Eric M. Hopper <hopper@omnifarious.mn.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU Lesser General Public License as published 00008 * by the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #ifdef __GNUG__ 00022 # pragma interface 00023 #endif 00024 00025 /* $Header: /home/hopper/src/cvs/C++/LCore/LCore/LCoreError.h,v 1.8 2002/05/09 21:55:21 hopper Exp $ */ 00026 00027 // For a log, see ../Changelog 00028 00029 //! Anti-double inclusion macro. 00030 #define _LCORE_LCoreError_H_ 00031 00032 #include <iosfwd> 00033 00034 namespace strmod { 00035 namespace lcore { 00036 00037 /** \file LCore/LCoreError.h 00038 * This file defines LCORE_GET_COMPILERINFO, and contains LCoreError, and 00039 * LCoreError::CompilerInfo. 00040 */ 00041 00042 #if defined __GNUC__ && defined __GNUC_MINOR__ && ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6))) 00043 /** 00044 * \def LCORE_GET_COMPILERINFO() 00045 * \brief Construct a temporary ::LCoreError::CompilerInfo object with values 00046 * the compiler will provide in macros. 00047 * This is the g++ version, and therefore has the function name available. */ 00048 # define LCORE_GET_COMPILERINFO() \ 00049 ::strmod::lcore::LCoreError::CompilerInfo(__FILE__, __LINE__, __PRETTY_FUNCTION__) 00050 # else 00051 /** \def LCORE_GET_COMPILERINFO() 00052 * \brief Construct a temporary ::LCoreError::CompilerInfo object with values 00053 * the compiler will provide in macros. 00054 * This isn't the g++ version, and so has no function name. */ 00055 # define LCORE_GET_COMPILERINFO() \ 00056 ::strmod::lcore::LCoreError::CompilerInfo(__FILE__, __LINE__) 00057 #endif 00058 00059 /** \class LCoreError LCoreError.h LCore/LCoreError.h 00060 * \brief Represents an error, may be inside a thrown exception. 00061 * 00062 * This is a very simple class. It just holds pointers to information. It 00063 * expects that the information it points to will not go away. In fact, it's 00064 * best if the information being pointed to consists of all constant strings 00065 * allocated at program start and never destroyed. 00066 * 00067 * This class is designed to be able to be embedded inside of an exception. It 00068 * does no dynamic allocation, or any other operation capable of throwing an 00069 * exception for this reason. 00070 * 00071 * Derived classes need to be designed with the idea that there is no virtual 00072 * destructor. They should also handle 'slicing' elegantly. 00073 * 00074 * Everything in this class is 'const' so that the class may also be easily used 00075 * in threading situations. This also requires that everything the class points 00076 * at be similarily immutable. 00077 */ 00078 class LCoreError { 00079 public: 00080 class CompilerInfo; 00081 00082 //! Contruct an LCoreError with however much information you can give it. 00083 explicit inline 00084 LCoreError(const char *desc = 0, const char *sourcefile = 0, 00085 unsigned int line = 0, const char *func = 0) throw (); 00086 //! Contruct an LCoreError with a description, and a CompilerInfo object 00087 explicit inline LCoreError(const char *desc, const CompilerInfo &inf) 00088 throw(); 00089 //! Construct an LCoreError with a CompilerInfo object, but no description. 00090 inline LCoreError(const CompilerInfo &inf) throw(); 00091 00092 //! Get the description, may return NULL. 00093 const char *getDesc() const throw () { return desc_; } 00094 //! Get the source file, may return NULL. 00095 const char *getSourceFile() const throw () { return sourcefile_; } 00096 //! Get the line number. 00097 unsigned int getLine() const throw () { return line_; } 00098 //! Get the function name, may return NULL. 00099 const char *getFunc() const throw () { return func_; } 00100 00101 private: 00102 const char * const desc_; 00103 const char * const sourcefile_; 00104 const unsigned int line_; 00105 const char * const func_; 00106 }; 00107 00108 /** \class LCoreError::CompilerInfo LCoreError.h LCore/LCoreError.h 00109 * \brief A class collecting together all compile-time information a compiler 00110 * will give it. 00111 * 00112 * This class is largely so that macros that autmatically extract the 00113 * information from the compiler are easier to write. See 00114 * LCORE_GET_COMPILERINFO(). 00115 */ 00116 class LCoreError::CompilerInfo 00117 { 00118 public: 00119 //! Constructs with a source file, line number, and possible function name. 00120 explicit inline 00121 CompilerInfo(const char *sourcefile, unsigned int line, const char *func = 0) 00122 throw(); 00123 00124 //! Get the source file, may return NULL. 00125 const char *getSourceFile() const throw() { return sourcefile_; } 00126 //! Get the line number. 00127 unsigned int getLine() const throw () { return line_; } 00128 //! Get the function name, may return NULL. 00129 const char *getFunc() const throw () { return func_; } 00130 00131 private: 00132 const char * const sourcefile_; 00133 const unsigned int line_; 00134 const char * const func_; 00135 }; 00136 00137 //-----------------------------inline functions-------------------------------- 00138 00139 /** 00140 * \param desc A general description of the context of the error. 00141 * \param sourcefile The name of the file the error occured in. 00142 * \param line The line number the error occured on. 00143 * \param func The name of the function the error occured in. 00144 */ 00145 inline 00146 LCoreError::LCoreError(const char *desc, const char *sourcefile, 00147 unsigned int line, const char *func) throw () 00148 : desc_(desc), sourcefile_(sourcefile), line_(line), func_(func) 00149 { 00150 } 00151 00152 inline 00153 LCoreError::LCoreError(const char *desc, const CompilerInfo &inf) throw() 00154 : desc_(desc), 00155 sourcefile_(inf.getSourceFile()), line_(inf.getLine()), 00156 func_(inf.getFunc()) 00157 { 00158 } 00159 00160 inline LCoreError::LCoreError(const CompilerInfo &inf) throw() 00161 : desc_(0), 00162 sourcefile_(inf.getSourceFile()), line_(inf.getLine()), 00163 func_(inf.getFunc()) 00164 { 00165 } 00166 00167 //-- 00168 00169 inline LCoreError::CompilerInfo::CompilerInfo(const char *sourcefile, 00170 unsigned int line, 00171 const char *func) throw() 00172 : sourcefile_(sourcefile), line_(line), func_(func) 00173 { 00174 } 00175 00176 //-- 00177 00178 //! Print out an LCoreError on an iostream. 00179 std::ostream &operator <<(std::ostream &os, const LCoreError &err); 00180 00181 } // namespace lcore 00182 } // namespace strmod 00183 00184 #endif
1.3-rc1