DDS  ver. 3.6
ErrorCode.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // error cods collection file
4 //
5 #ifndef _DDS_ERRORCODE_H_
6 #define _DDS_ERRORCODE_H_
7 
8 // API
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <string.h>
12 // STD
13 #include <sstream>
14 #include <string>
15 
16 namespace dds::misc
17 {
18  typedef int ERRORCODE;
19  typedef const ERRORCODE ERRORCODE_C;
20 
21  ERRORCODE_C erOK(EXIT_SUCCESS);
22  ERRORCODE_C erTrue(-1);
23  ERRORCODE_C erFalse(-2);
24  ERRORCODE_C erError(EXIT_FAILURE);
26 
27 #define SUCCEEDED(er) (er <= erOK)
28 #define FAILED(er) (!SUCCEEDED(er))
29 
30  // General Error codes
31  ERRORCODE_C BASE_FOR_GENERAL_ERR(50); // <<<----- BASE counter
34 
35  // XML Error codes
36  ERRORCODE_C BASE_FOR_XML_ERR(100); // <<<----- BASE counter
40 
49  inline int errno2str(std::string* _msg)
50  {
51  if (!_msg)
52  return erNULLArg;
53  char* p = strerror(errno);
54  *_msg = p;
55  return errno;
56  }
63  inline std::string errno2str()
64  {
65  char* p = strerror(errno);
66  return std::string(p);
67  }
76  class system_error : public std::exception
77  {
78  public:
79  explicit system_error(const std::string& _ErrorPrefix)
80  {
81  m_errno = errno;
82  const char* const szError = strerror(m_errno);
83  std::stringstream ss;
84  if (!_ErrorPrefix.empty())
85  ss << _ErrorPrefix << ". ";
86  ss << "System error description [" << m_errno << "]: " << szError;
87  m_Msg = ss.str();
88  }
89  virtual ~system_error() throw()
90  {
91  }
92  virtual const char* what() const throw()
93  {
94  return m_Msg.c_str();
95  }
96  int getErrno() const throw()
97  {
98  return m_errno;
99  }
100 
101  private:
102  std::string m_Msg;
103  int m_errno;
104  };
105 }; // namespace dds::misc
106 
107 #endif // _DDS_ERRORCODE_H_
virtual const char * what() const
Definition: ErrorCode.h:92
ERRORCODE_C erNULLArg(BASE_FOR_GENERAL_ERR+1)
ERRORCODE_C erXMLNullNode(BASE_FOR_XML_ERR+3)
ERRORCODE_C erOK(EXIT_SUCCESS)
ERRORCODE_C erXMLReadConfig(BASE_FOR_XML_ERR+2)
ERRORCODE_C erXMLInit(BASE_FOR_XML_ERR+1)
int ERRORCODE
Definition: ErrorCode.h:18
ERRORCODE_C erFILE_NOT_FOUND(BASE_FOR_GENERAL_ERR+2)
ERRORCODE_C BASE_FOR_XML_ERR(100)
int errno2str(std::string *_msg)
Retrieves a string, which represent the last error.
Definition: ErrorCode.h:49
The system_error exception class retrieves a string, which represent the last error.
Definition: ErrorCode.h:76
ERRORCODE_C erNotImpl(3)
int getErrno() const
Definition: ErrorCode.h:96
ERRORCODE_C erError(EXIT_FAILURE)
ERRORCODE_C erFalse(-2)
ERRORCODE_C erTrue(-1)
const ERRORCODE ERRORCODE_C
Definition: ErrorCode.h:19
virtual ~system_error()
Definition: ErrorCode.h:89
system_error(const std::string &_ErrorPrefix)
Definition: ErrorCode.h:79
ERRORCODE_C BASE_FOR_GENERAL_ERR(50)
Definition: BoostHelper.h:14