PVData C++  8.0.2
status.h
1 /* status.h */
2 /*
3  * Copyright information and license terms for this software can be
4  * found in the file LICENSE that is included with the distribution
5  */
6 /**
7  * @author mse
8  */
9 #ifndef STATUS_H
10 #define STATUS_H
11 
12 #include <ostream>
13 
14 #include <pv/serialize.h>
15 #include <pv/byteBuffer.h>
16 #include <pv/sharedPtr.h>
17 
18 #include <shareLib.h>
19 
20 namespace epics { namespace pvData {
21 
22  /**
23  * @brief Status.
24  *
25  * This is a class for returning status to clients.
26  * @author mse
27  */
28  class epicsShareClass Status : public epics::pvData::Serializable {
29  public:
31  /**
32  * Status type enum.
33  */
34  enum StatusType {
35  /** Operation completed successfully. */
37  /** Operation completed successfully, but there is a warning message. */
39  /** Operation failed due to an error. */
41  /** Operation failed due to an unexpected error. */
43  };
44 
45  static const char* StatusTypeName[];
46 
47  static Status Ok;
48 
49  static inline Status warn(const std::string& m) { return Status(STATUSTYPE_WARNING, m); }
50  static inline Status error(const std::string& m) { return Status(STATUSTYPE_ERROR, m); }
51  static inline Status fatal(const std::string& m) { return Status(STATUSTYPE_FATAL, m); }
52 
53  /**
54  * Creates OK status; STATUSTYPE_OK, empty message and stackDump.
55  */
57 
58  /**
59  * Create non-OK status.
60  */
62 
63  /**
64  * Create non-OK status.
65  */
66  Status(StatusType type, std::string const & message, std::string const & stackDump);
67 
68  virtual ~Status() {}
69 
70  /**
71  * Get status type.
72  * @return status type, non-<code>null</code>.
73  */
74  inline StatusType getType() const { return m_statusType; }
75 
76  /**
77  * Get error message describing an error. Required if error status.
78  * @return error message.
79  */
80  inline const std::string& getMessage() const { return m_message; }
81 
82  /**
83  * Get stack dump where error (exception) happened. Optional.
84  * @return stack dump.
85  */
86  inline const std::string& getStackDump() const { return m_stackDump; }
87 
88  /**
89  * Convenient OK test. Same as <code>(getType() == StatusType.OK)</code>.
90  * NOTE: this will return <code>false</code> on WARNING message although operation succeeded.
91  * To check if operation succeeded, use <code>isSuccess</code>.
92  * @return OK status.
93  * @see #isSuccess()
94  */
95  inline bool isOK() const {
96  return (m_statusType == STATUSTYPE_OK);
97  }
98 
99  /**
100  * Check if operation succeeded (OK or WARNING).
101  * @return operation success status.
102  */
103  inline bool isSuccess() const {
105  }
106 
107 #if __cplusplus>=201103L
108  FORCE_INLINE explicit operator bool() const {
109  return isSuccess();
110  }
111 #else
112  private:
113  typedef bool (Status::*truth_type)() const;
114  public:
116  return isSuccess() ? &Status::isSuccess : 0;
117  }
118 #endif
119 
120  /** override this Status if the other has higher StatusType
121  @code
122  Status ret;
123  ret |= call1();
124  if(ret)
125  ret |= call2();
126  return ret;
127  @endcode
128  */
129  void maximize(const Status& o);
130 
131  //! short hand for "this->maximize(o)"
133  maximize(o);
134  return *this;
135  }
136 
139 
140  void dump(std::ostream& o) const;
141 
142  private:
143 
147 
148  };
149 
150  FORCE_INLINE std::ostream& operator<<(std::ostream& o, const Status& status) {
151  status.dump(o);
152  return o;
153  }
154 
155  FORCE_INLINE std::ostream& operator<<(std::ostream& o, const Status::StatusType& statusType) {
156  o << Status::StatusTypeName[statusType];
157  return o;
158  }
159 
160 }}
161 #endif /* STATUS_H */
#define POINTER_DEFINITIONS(clazz)
Definition: sharedPtr.h:198
#define FORCE_INLINE
Definition: templateMeta.h:20
epicsShareFunc bool yajl_parse_helper(std::istream &src, yajl_handle handle)