forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocketgenerator.cpp
85 lines (76 loc) · 4.48 KB
/
websocketgenerator.cpp
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
/* Copyright (c) 2015-2017, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include "websocketgenerator.h"
#include "global.h"
#include "filewriter.h"
#include "projectfilegenerator.h"
#define ENDPOINT_HEADER_TEMPLATE \
"#ifndef %1ENDPOINT_H\n" \
"#define %1ENDPOINT_H\n" \
"\n" \
"#include \"applicationendpoint.h\"\n" \
"\n" \
"class T_CONTROLLER_EXPORT %2Endpoint : public ApplicationEndpoint\n" \
"{\n" \
"public:\n" \
" %2Endpoint() { }\n" \
" %2Endpoint(const %2Endpoint &other);\n" \
"\n" \
"protected:\n" \
" bool onOpen(const TSession &httpSession);\n" \
" void onClose(int closeCode);\n" \
" void onTextReceived(const QString &text);\n" \
" void onBinaryReceived(const QByteArray &binary);\n" \
"};\n" \
"\n" \
"T_DECLARE_CONTROLLER(%2Endpoint, %3endpoint)\n" \
"\n" \
"#endif // %1ENDPOINT_H\n"
#define ENDPOINT_IMPL_TEMPLATE \
"#include \"%1endpoint.h\"\n" \
"\n" \
"%2Endpoint::%2Endpoint(const %2Endpoint &)\n" \
" : ApplicationEndpoint()\n" \
"{ }\n" \
"\n" \
"bool %2Endpoint::onOpen(const TSession &)\n" \
"{\n" \
" return true;\n" \
"}\n" \
"\n" \
"void %2Endpoint::onClose(int)\n" \
"{ }\n" \
"\n" \
"void %2Endpoint::onTextReceived(const QString &)\n" \
"{\n" \
" // write code\n" \
"}\n" \
"\n" \
"void %2Endpoint::onBinaryReceived(const QByteArray &)\n" \
"{ }\n" \
"\n\n" \
"// Don't remove below this line\n" \
"T_REGISTER_CONTROLLER(%1endpoint)\n"
WebSocketGenerator::WebSocketGenerator(const QString &n)
{
name = fieldNameToEnumName(n);
name.remove(QRegExp("endpoint$", Qt::CaseInsensitive));
}
bool WebSocketGenerator::generate(const QString &dst) const
{
// Writes each files
QDir dstDir(dst);
QString output = QString(ENDPOINT_HEADER_TEMPLATE).arg(name.toUpper()).arg(name).arg(name.toLower());
FileWriter(dstDir.filePath(name.toLower() + "endpoint.h")).write(output, false);
output = QString(ENDPOINT_IMPL_TEMPLATE).arg(name.toLower()).arg(name);
FileWriter(dstDir.filePath(name.toLower() + "endpoint.cpp")).write(output, false);
// Updates the project file
ProjectFileGenerator progen(dstDir.filePath("controllers.pro"));
QStringList files;
files << name.toLower() + "endpoint.h" << name.toLower() + "endpoint.cpp";
return progen.add(files);
}