PMDA++  0.4.4
Header-only C++ library for writing PCP PMDAs
exception.hpp
Go to the documentation of this file.
1 // Copyright Paul Colby 2013 - 2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 /**
7  * @file
8  * @brief Defines the pcp::exception class.
9  */
10 
11 #ifndef __PCP_CPP_EXCEPTION_HPP__
12 #define __PCP_CPP_EXCEPTION_HPP__
13 
14 #include "config.hpp"
15 
16 #include <string>
17 
18 PCP_CPP_BEGIN_NAMESPACE
19 
20 namespace pcp {
21 
22 /**
23  * @brief Base class for all PMDA++ exceptions.
24  */
25 class exception : public std::exception {
26 
27 public:
28 
29  /**
30  * @brief Constructor.
31  *
32  * If the optional \a message is not provided (or is empty), then either
33  * pmErrStr_r (if available) or pmErrStr will be used to fetch PCP's message
34  * for \a pm_error_code.
35  *
36  * @param pm_error_code PCP error code.
37  * @param message Error message.
38  *
39  * @see pmErrStr
40  * @see pmErrStr_r
41  */
43  const std::string &message = std::string())
44  : pm_error_code(pm_error_code),
46  {
47  if (message.empty()) {
48 #ifdef PM_MAXERRMSGLEN // pmErrStr_r and PM_MAXERRMSGLEN added in PCP 3.6.0.
49  char buffer[PM_MAXERRMSGLEN];
50  pmErrStr_r(pm_error_code, buffer, sizeof(buffer));
51  this->message.assign(buffer);
52 #else
53  this->message.assign(pmErrStr(pm_error_code));
54 #endif
55  }
56  }
57 
58  /**
59  * @brief Copy constructor.
60  *
61  * @param other Other pcp::exception object to copy.
62  */
63  exception(const pcp::exception &other)
64  : std::exception(other),
65  pm_error_code(other.error_code()),
66  message(other.what())
67  {
68  }
69 
70  /**
71  * @brief Virtual destructor.
72  *
73  * For safe polymorphic destruction.
74  */
75  virtual ~exception() throw() { }
76 
77  /**
78  * @brief Get this exception's error code.
79  *
80  * @return This exception's error code.
81  */
82  virtual int error_code() const
83  {
84  return pm_error_code;
85  }
86 
87  /**
88  * @brief Get this exception's error message.
89  *
90  * @return This exception's error message.
91  */
92  virtual const char * what() const throw()
93  {
94  return message.c_str();
95  }
96 
97 protected:
98  int pm_error_code; ///< PCP error code.
99  std::string message; ///< Error message.
100 };
101 
102 } // pcp namespace.
103 
104 PCP_CPP_END_NAMESPACE
105 
106 #endif
exception(const int pm_error_code, const std::string &message=std::string())
Constructor.
Definition: exception.hpp:42
Definition: atom.hpp:20
STL namespace.
Sets up common PMDA++ library macros.
exception(const pcp::exception &other)
Copy constructor.
Definition: exception.hpp:63
Base class for all PMDA++ exceptions.
Definition: exception.hpp:25
virtual ~exception()
Virtual destructor.
Definition: exception.hpp:75
std::string message
Error message.
Definition: exception.hpp:99
virtual int error_code() const
Get this exception&#39;s error code.
Definition: exception.hpp:82
int pm_error_code
PCP error code.
Definition: exception.hpp:98
virtual const char * what() const
Get this exception&#39;s error message.
Definition: exception.hpp:92