-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_handler.h
More file actions
38 lines (33 loc) · 1.22 KB
/
request_handler.h
File metadata and controls
38 lines (33 loc) · 1.22 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
#ifndef REQUEST_HANDLER_H
#define REQUEST_HANDLER_H
#include <map>
#include <memory>
#include <string>
class RequestHandler {
public:
static RequestHandler* CreateByName(const char* type);
virtual void HandleRequest(void) = 0;
};
// Notes:
// * The trick here is that you can declare an object at file scope, but you
// can't do anything else, such as set a map key. But you can get around this
// by creating a class that does work in its constructor.
// * request_handler_builders must be a pointer. Otherwise, it won't necessarily
// exist when the RequestHandlerRegisterer constructor gets called.
extern std::map<std::string, RequestHandler* (*)(void)>* request_handler_builders;
template<typename T>
class RequestHandlerRegisterer {
public:
RequestHandlerRegisterer(const std::string& type) {
if (request_handler_builders == nullptr) {
request_handler_builders = new std::map<std::string, RequestHandler* (*)(void)>;
}
(*request_handler_builders)[type] = RequestHandlerRegisterer::Create;
}
static RequestHandler* Create() {
return new T;
}
};
#define REGISTER_REQUEST_HANDLER(ClassName) \
static RequestHandlerRegisterer<ClassName> ClassName##__registerer(#ClassName)
#endif // REQUEST_HANDLER_H