00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/generic/util/Error.hpp $ 00003 // $Id: Error.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 #ifndef TORC_GENERIC_UTIL_ERROR_HPP 00017 #define TORC_GENERIC_UTIL_ERROR_HPP 00018 00019 #include <map> 00020 #include <string> 00021 #include <vector> 00022 00023 //BOOST 00024 #include <boost/any.hpp> 00025 #include <boost/cstdint.hpp> 00026 00027 #include "torc/generic/util/MessageId.hpp" 00028 00029 namespace torc { 00030 00031 namespace generic { 00032 00033 /** 00034 * @brief The Error object thrown by different methods of EdifOM 00035 * 00036 * The Error class is used to convey exception information to the user. The error object is constructed by the throwing method and propagated upwards by the calling functions. 00037 * 00038 * @note This class does not inherit from std::exception by design. Clients should keep that in mind while using this class. 00039 */ 00040 class Error 00041 { 00042 public: 00043 /** 00044 * Context sensitive data for the error object. The Context map stores a boost::any inSource corresponding to a name. As the Error bubbles up, this data may be augmented by catching contexts. 00045 */ 00046 typedef std::map< std::string, boost::any > Context; 00047 00048 /** 00049 * Represents the throw and catch locations of the exception. Contains function, file and line numbers. 00050 */ 00051 struct StackFrameInfo 00052 { 00053 private: 00054 std::string mFunction; 00055 std::string mFile; 00056 uint32_t mLine; 00057 00058 public: 00059 StackFrameInfo(const std::string &inFunction, 00060 const std::string &inFile, 00061 uint32_t inLine); 00062 00063 ~StackFrameInfo() throw(); 00064 00065 StackFrameInfo(const StackFrameInfo & source); 00066 00067 StackFrameInfo & 00068 operator=(const StackFrameInfo & source) throw(); 00069 00070 inline const std::string 00071 getFunction() const throw(); 00072 00073 inline const std::string 00074 getFile() const throw(); 00075 00076 inline const uint32_t 00077 getLine() const throw(); 00078 00079 00080 }; 00081 00082 /** 00083 * Constructor. 00084 * 00085 * @param[in] inId Error message identifier that can be used to look up the message in MessageTable. 00086 * @param[in] inContext Context data that will be saved inside this error object. 00087 * @param[in] inFunction Function name from where this exception is being thrown. This will typically be set to __FUNCTION__. 00088 * @param[in] inFile File name from where this exception is being thrown. This will typically be set to __FILE__. 00089 * @param[in] inLine Line No. in the file from where this exception is being thrown. This will typically be set to __LINE__. 00090 */ 00091 Error(MessageId inId, 00092 const Context &inContext, 00093 const std::string &inFunction, 00094 const std::string &inFile, uint32_t inLine); 00095 00096 /** 00097 * @overload 00098 */ 00099 Error(MessageId inId, 00100 const std::string &inFunction, 00101 const std::string &inFile, uint32_t inLine); 00102 00103 ~Error() throw(); 00104 00105 Error(const Error & source) throw(); 00106 00107 Error & 00108 operator=(const Error & source) throw(); 00109 00110 /** 00111 * Get the complete stack trace. 00112 * 00113 * @return vector containing the StackFrameInfo objects. 00114 */ 00115 inline const std::vector<StackFrameInfo> & 00116 getStackTrace() const throw(); 00117 00118 /** 00119 * Set the current location. This method should be used by catching contexts to push location data into the error object. 00120 * 00121 * @param[in] inFunction Function name from where this exception is being thrown. This will typically be set to __FUNCTION__. 00122 * @param[in] inFile File name from where this exception is being thrown. This will typically be set to __FILE__. 00123 * @param[in] inLine Line No. in the file from where this exception is being thrown. This will typically be set to __LINE__. 00124 */ 00125 void 00126 setCurrentLocation( const std::string &inFunction, 00127 const std::string &inFile, uint32_t inLine) throw(); 00128 00129 /** 00130 * Get the map of context data for this exception. This can be looked up by interested parties for context sensitive information. Note that the value_type for this map is boost::any and therefore an appropriate boost::any_cast is required to get the actual data. 00131 * 00132 * @return const reference to a Context object. 00133 */ 00134 inline const Context 00135 getContextData() const throw(); 00136 00137 /** 00138 * Save a context sensitive data with a meaningful name, that can be retreived by interested catching contexts. 00139 * 00140 * @param[in] inName Name of the data. 00141 * @param[in] inSource Any type of data. The only restrictions are that the type of data should be copy constructible and assignable. 00142 */ 00143 void 00144 saveContextData(const std::string &inName, 00145 const boost::any &inSource) throw(); 00146 00147 /** 00148 * Get the error message Id for this error. 00149 * 00150 * @return MessageId corresponding to this error 00151 */ 00152 inline const MessageId 00153 getErrorMessageId() const throw(); 00154 00155 private: 00156 std::vector<StackFrameInfo> mStackTrace; 00157 Context mContextData; 00158 MessageId mErrorMessageId; 00159 }; 00160 00161 inline const std::string 00162 Error::StackFrameInfo::getFunction() const throw() { 00163 return mFunction; 00164 } 00165 00166 inline const std::string 00167 Error::StackFrameInfo::getFile() const throw() { 00168 return mFile; 00169 } 00170 00171 inline const uint32_t 00172 Error::StackFrameInfo::getLine() const throw() { 00173 return mLine; 00174 } 00175 00176 /** 00177 * Get the complete stack trace. 00178 * 00179 * @return vector containing the StackFrameInfo objects. 00180 */ 00181 inline const std::vector<Error::StackFrameInfo> & 00182 Error::getStackTrace() const throw() { 00183 return mStackTrace; 00184 } 00185 00186 /** 00187 * Get the map of context data for this exception. This can be looked up by interested parties for context sensitive information. Note that the value_type for this map is boost::any and therefore an appropriate boost::any_cast is required to get the actual data. 00188 * 00189 * @return const reference to a Context object. 00190 */ 00191 inline const Error::Context 00192 Error::getContextData() const throw() { 00193 return mContextData; 00194 } 00195 00196 /** 00197 * Get the error message Id for this error. 00198 * 00199 * @return MessageId corresponding to this error 00200 */ 00201 inline const MessageId 00202 Error::getErrorMessageId() const throw() { 00203 return mErrorMessageId; 00204 } 00205 00206 } // namespace torc::generic 00207 00208 } // namespace torc 00209 #endif // TORC_GENERIC_UTIL_ERROR_HPP