Main MRPT website > C++ reference for MRPT 1.9.9
xsexception.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifdef __cplusplus
10 #ifndef XSEXCEPTION_H
11 #define XSEXCEPTION_H
12 
13 #include <exception>
14 #include "xsresultvalue.h"
15 #include "xsstring.h"
16 
17 /*! \brief Exception class for Xsens public libraries. Inherits from
18  * std::exception
19 */
20 class XsException : public std::exception
21 {
22  public:
23  //! \brief Copy constructor
24  XsException(XsException const& e)
25  : m_code(e.m_code), m_description(e.m_description)
26  {
27  }
28 
29  /*! \brief Initializing constructor
30  \details This constructor uses the value in \a err and the supplied \a
31  description to create a full
32  text for when the user requests what() or text()
33  \param err The error code that the exception should report
34  \param description A description of the error. The constructor prefixes
35  this with a textual
36  description of the error code unless prefix is
37  false.
38  \param prefix Whether to prefix the description with a textual
39  description of the error code or not (default is yes)
40  */
41  XsException(
42  XsResultValue err, XsString const& description, bool prefix = true)
43  : m_code(err), m_description(description)
44  {
45  if (prefix && (m_code != XRV_OK))
46  {
47  char codeString[16];
48  sprintf(codeString, "%d: ", (int)m_code);
49  XsString rv(codeString);
50  rv << XsResultValue_toString(m_code);
51  if (!m_description.empty()) rv << "\tInfo: ";
52  rv.append(m_description);
53  m_description = rv;
54  }
55  }
56 
57  /*! \brief Initializing constructor
58  \param description A description of the error.
59  */
60  explicit XsException(XsString const& description)
61  : m_code(XRV_ERROR), m_description(description)
62  {
63  }
64 
65  //! \brief Destructor
66  virtual ~XsException() throw() {}
67  //! \brief Assignment operator, copies \a e to this
68  XsException& operator=(XsException const& e)
69  {
70  m_code = e.m_code;
71  m_description = e.m_description;
72  return *this;
73  }
74 
75  //! \brief Returns the error value supplied during construction
76  inline XsResultValue code() const throw() { return m_code; }
77  //! \brief Returns a description of the error that occurred as a char const*
78  inline char const* what() const throw() { return m_description.c_str(); }
79  //! \brief Returns a description of the error that occurred as a XsString
80  inline XsString const& text() const throw() { return m_description; }
81  /*! \brief Throw/raise the exception object
82  \note Override this in inheriting classes to make sure the proper type
83  of exception is raised
84  */
85  virtual void raise() const { throw * this; }
86  private:
87  /** The supplied error code */
88  XsResultValue m_code;
89  /** The supplied description, prefixed with a description of the error code
90  */
91  XsString m_description;
92 };
93 
94 #endif // file guard
95 #endif // __cplusplus guard
XsString
struct XsString XsString
Definition: xsstring.h:34
xsstring.h
XRV_OK
@ XRV_OK
Operation was performed successfully.
Definition: xsens_std.h:34
XsResultValue_toString
const XSTYPES_DLL_API char * XsResultValue_toString(XsResultValue result)
XsResultValue
XsResultValue
Xsens result values.
Definition: xsresultvalue.h:27
mrpt::system::os::sprintf
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:189
XRV_ERROR
@ XRV_ERROR
Definition: xsens_std.h:106
code
Definition: inftrees.h:28
xsresultvalue.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST