-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.h
More file actions
73 lines (60 loc) · 1.85 KB
/
code.h
File metadata and controls
73 lines (60 loc) · 1.85 KB
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
#ifndef SSERVER_CODE_H_INCLUDED
#define SSERVER_CODE_H_INCLUDED
#include <map>
#include <string>
#include <stdexcept>
class Codes {
public:
enum Code { OK = 200,
BAD_REQUEST = 400,
NOT_FOUND = 404,
OVERLOADED = 405,
NOT_ACCEPTABLE = 406,
CONFLICT = 409,
UNAVAILABLE = 503 };
static const std::string& string(Code code) {
return strings_.at(code);
}
private:
static std::map<Code, std::string> strings_;
};
typedef Codes::Code Code;
class BaseProtocolError : public std::runtime_error {
public:
explicit BaseProtocolError(Codes::Code code) :
std::runtime_error(Codes::string(code)) {} // have copy of string here, optimize if need
};
template<Code code>
class ProtocolError : public BaseProtocolError {
public:
ProtocolError() : BaseProtocolError(code) {}
};
typedef ProtocolError<Codes::BAD_REQUEST> BadRequest;
typedef ProtocolError<Codes::NOT_FOUND> NotFound;
typedef ProtocolError<Codes::OVERLOADED> Overloaded;
typedef ProtocolError<Codes::NOT_ACCEPTABLE> NotAcceptable;
typedef ProtocolError<Codes::CONFLICT> Conflict;
typedef ProtocolError<Codes::UNAVAILABLE> Unavailable;
template<typename T>
inline void notify(T& fd, Code code = Codes::OK) {
REQUIRE(fd.write((char*)&code, sizeof(Code)) == sizeof(Code),
"couldn't notify");
}
template<typename T>
inline void notify(T* fd, Code code = Codes::OK) {
REQUIRE(fd, "NULL pointer in notify");
notify(*fd, code);
}
template<typename T>
inline Code suppress(T& fd) {
Code code;
REQUIRE(fd.read((char*)&code, sizeof(Code)) == sizeof(Code),
"couldn't get notification");
return code;
}
template<typename T>
inline Code suppress(T* fd) {
REQUIRE(fd, "NULL pointer in suppress");
return suppress(*fd);
}
#endif // SSERVER_CODE_H_INCLUDED