-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathstatus_msg.h
165 lines (131 loc) · 4.99 KB
/
status_msg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright (c) 2014-2018, Adrian Rossiter
Antiprism - http://www.antiprism.com
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/*!\file status_msg.h
\brief class to handle return status
*/
#ifndef STATUS_MSG_H
#define STATUS_MSG_H
#include <string>
/// Status
class Status {
private:
enum { STATUS_OK = 0, STATUS_WARNING = 1 << 3, STATUS_ERROR = 1 << 10 };
int status_code;
std::string message;
void set_status_message(const std::string &msg, int code, int type);
public:
/// Default constructor
Status() : status_code(STATUS_OK) {}
/// Check if status is ok
/**\return true if the status is normal, otherwise false.*/
operator bool() const;
/// Check if status is ok
/**\return true if the status is normal, otherwise false.*/
bool is_ok() const;
/// Check if status is warning
/**\return true if the status is normal, otherwise false.*/
bool is_warning() const;
/// Check if status is error
/**\return true if the status is normal, otherwise false.*/
bool is_error() const;
/// Set status to ok, with message and type
/**\param msg status message
* \param code low number indicating type of ok status
* \return reference to this status.*/
Status &set_ok(const std::string &msg = std::string(), int code = 0);
/// Set status to warning, with message and type
/**\param msg status message
* \param code low number indicating type of warning status
* \return reference to this status.*/
Status &set_warning(const std::string &msg, int code = 0);
/// Set status to error, with message and type
/**\param msg status message
* \param code low number indicating type of error status
* \return reference to this status.*/
Status &set_error(const std::string &msg, int code = 0);
/// Get status message
/**\return The status message.*/
const std::string &msg() const;
/// Get status message as C string
/**\return The status message.*/
const char *c_msg() const;
/// Get status code
/**The code is a low number, and particular to the status type
* \return The status code.*/
int code() const;
/// Get an ok status, with message and type
/**\param msg status message
* \param code low number indicating type of ok status.
* \return am ok status.*/
static Status ok(const std::string &msg = std::string(), int code = 0);
/// Get a warning status, with message and type
/**\param msg status message
* \param code low number indicating type of warning status.
* \return a warning status.*/
static Status warning(const std::string &msg, int code = 0);
/// Get an error status, with message and type
/**\param msg status message
* \param code low number indicating type of error status.
* \return an error status.*/
static Status error(const std::string &msg, int code = 0);
};
inline void Status::set_status_message(const std::string &msg, int code,
int type)
{
message = msg;
status_code = code + type;
}
inline Status::operator bool() const { return is_ok(); }
inline bool Status::is_ok() const
{
return status_code >= STATUS_OK && status_code < STATUS_WARNING;
}
inline bool Status::is_warning() const
{
return status_code >= STATUS_WARNING && status_code < STATUS_ERROR;
}
inline bool Status::is_error() const { return status_code >= STATUS_ERROR; }
inline Status &Status::set_ok(const std::string &msg, int code)
{
set_status_message(msg, code, STATUS_OK);
return *this;
}
inline Status &Status::set_warning(const std::string &msg, int code)
{
set_status_message(msg, code, STATUS_WARNING);
return *this;
}
inline Status &Status::set_error(const std::string &msg, int code)
{
set_status_message(msg, code, STATUS_ERROR);
return *this;
}
inline const std::string &Status::msg() const { return message; }
inline const char *Status::c_msg() const { return message.c_str(); }
inline int Status::code() const
{
int ret = status_code;
if (is_warning())
ret += STATUS_WARNING;
else if (is_error())
ret += STATUS_ERROR;
return ret;
}
#endif // STATUS_MSG_H