torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Error.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_ERROR_HPP
17 #define TORC_GENERIC_ERROR_HPP
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 //BOOST
24 #include <boost/any.hpp>
25 #include <boost/cstdint.hpp>
26 
28 
29 namespace torc {
30 namespace generic {
31 
32 /**
33  * @brief The Error object thrown by different methods of EdifOM
34  *
35  * The Error class is used to convey exception information to the user. The error object is
36  * constructed by the throwing method and propagated upwards by the calling functions.
37  *
38  * @note This class does not inherit from std::exception by design. Clients should keep that in
39  * mind while using this class.
40  */
41 class Error {
42 public:
43  /**
44  * Context sensitive data for the error object. The Context map stores a boost::any inSource
45  * corresponding to a name. As the Error bubbles up, this data may be augmented by catching
46  * contexts.
47  */
48  typedef std::map<std::string, boost::any> Context;
49 
50  /**
51  * Represents the throw and catch locations of the exception. Contains function, file and line
52  * numbers.
53  */
54  struct StackFrameInfo {
55  private:
58  uint32_t mLine;
59 
60  public:
61  StackFrameInfo(const std::string& inFunction, const std::string& inFile, uint32_t inLine);
62 
63  ~StackFrameInfo() throw ();
64 
65  StackFrameInfo(const StackFrameInfo& source);
66 
67  StackFrameInfo& operator=(const StackFrameInfo& source);
68 
69  inline const std::string getFunction() const;
70 
71  inline const std::string getFile() const;
72 
73  inline const uint32_t getLine() const;
74 
75  };
76 
77  /**
78  * Constructor.
79  *
80  * @param[in] inId Error message identifier that can be used to look up the message in
81  * MessageTable.
82  * @param[in] inContext Context data that will be saved inside this error object.
83  * @param[in] inFunction Function name from where this exception is being thrown. This will
84  * typically be set to __FUNCTION__.
85  * @param[in] inFile File name from where this exception is being thrown. This will typically
86  * be set to __FILE__.
87  * @param[in] inLine Line No. in the file from where this exception is being thrown. This will
88  * typically be set to __LINE__.
89  */
90  Error(MessageId inId, const Context& inContext, const std::string& inFunction,
91  const std::string& inFile, uint32_t inLine);
92 
93  /**
94  * @overload
95  */
96  Error(MessageId inId, const std::string& inFunction, const std::string& inFile,
97  uint32_t inLine);
98 
99  ~Error() throw ();
100 
101  Error(const Error& source);
102 
103  Error& operator=(const Error& source);
104 
105  /**
106  * Get the complete stack trace.
107  *
108  * @return vector containing the StackFrameInfo objects.
109  */
110  inline const std::vector<StackFrameInfo>& getStackTrace() const;
111 
112  /**
113  * Set the current location. This method should be used by catching contexts to push location
114  * data into the error object.
115  *
116  * @param[in] inFunction Function name from where this exception is being thrown. This will
117  * typically be set to __FUNCTION__.
118  * @param[in] inFile File name from where this exception is being thrown. This will typically
119  * be set to __FILE__.
120  * @param[in] inLine Line No. in the file from where this exception is being thrown. This will
121  * typically be set to __LINE__.
122  */
123  void setCurrentLocation(const std::string& inFunction, const std::string& inFile,
124  uint32_t inLine);
125 
126  /**
127  * Get the map of context data for this exception. This can be looked up by interested parties
128  * for context sensitive information. Note that the value_type for this map is boost::any and
129  * therefore an appropriate boost::any_cast is required to get the actual data.
130  *
131  * @return const reference to a Context object.
132  */
133  inline const Context getContextData() const;
134 
135  /**
136  * Save a context sensitive data with a meaningful name, that can be retreived by interested
137  * catching contexts.
138  *
139  * @param[in] inName Name of the data.
140  * @param[in] inSource Any type of data. The only restrictions are that the type of data should
141  * be copy constructible and assignable.
142  */
143  void saveContextData(const std::string& inName, const boost::any& inSource);
144 
145  /**
146  * Get the error message Id for this error.
147  *
148  * @return MessageId corresponding to this error
149  */
150  inline const MessageId getErrorMessageId() const;
151 
152 private:
156 };
157 
158 inline const std::string Error::StackFrameInfo::getFunction() const {
159  return mFunction;
160 }
161 
163  return mFile;
164 }
165 
166 inline const uint32_t Error::StackFrameInfo::getLine() const {
167  return mLine;
168 }
169 
170 /**
171  * Get the complete stack trace.
172  *
173  * @return vector containing the StackFrameInfo objects.
174  */
175 inline const std::vector<Error::StackFrameInfo>& Error::getStackTrace() const {
176  return mStackTrace;
177 }
178 
179 /**
180  * Get the map of context data for this exception. This can be looked up by interested parties for
181  * context sensitive information. Note that the value_type for this map is boost::any and therefore
182  * an appropriate boost::any_cast is required to get the actual data.
183  *
184  * @return const reference to a Context object.
185  */
186 inline const Error::Context Error::getContextData() const {
187  return mContextData;
188 }
189 
190 /**
191  * Get the error message Id for this error.
192  *
193  * @return MessageId corresponding to this error
194  */
195 inline const MessageId Error::getErrorMessageId() const {
196  return mErrorMessageId;
197 }
198 
199 } // namespace generic
200 } // namespace torc
201 
202 #endif // TORC_GENERIC_ERROR_HPP
const std::string getFunction() const
Definition: Error.hpp:158
MessageId mErrorMessageId
Definition: Error.hpp:155
std::string string
std::map< std::string, boost::any > Context
Definition: Error.hpp:48
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
const std::vector< StackFrameInfo > & getStackTrace() const
Definition: Error.hpp:175
Context mContextData
Definition: Error.hpp:154
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
StackFrameInfo(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:25
const std::string getFile() const
Definition: Error.hpp:162
const Context getContextData() const
Definition: Error.hpp:186
const MessageId getErrorMessageId() const
Definition: Error.hpp:195
const uint32_t getLine() const
Definition: Error.hpp:166
std::vector< StackFrameInfo > mStackTrace
Definition: Error.hpp:153
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73