1
+ <?php
2
+
3
+ namespace Dpp \Generator ;
4
+
5
+ use Dpp \StructGeneratorInterface ;
6
+
7
+ /**
8
+ * Generate header and .cpp file for synchronous calls (ending in '_sync')
9
+ */
10
+ class SyncGenerator implements StructGeneratorInterface
11
+ {
12
+
13
+ /**
14
+ * @inheritDoc
15
+ */
16
+ public function generateHeaderStart (): string
17
+ {
18
+ return <<<EOT
19
+ /************************************************************************************
20
+ *
21
+ * D++, A Lightweight C++ library for Discord
22
+ *
23
+ * Copyright 2022 Craig Edwards and D++ contributors
24
+ * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
25
+ *
26
+ * Licensed under the Apache License, Version 2.0 (the "License");
27
+ * you may not use this file except in compliance with the License.
28
+ * You may obtain a copy of the License at
29
+ *
30
+ * http://www.apache.org/licenses/LICENSE-2.0
31
+ *
32
+ * Unless required by applicable law or agreed to in writing, software
33
+ * distributed under the License is distributed on an "AS IS" BASIS,
34
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
+ * See the License for the specific language governing permissions and
36
+ * limitations under the License.
37
+ *
38
+ ************************************************************************************/
39
+
40
+
41
+ /* Auto @generated by buildtools/make_sync_struct.php.
42
+ *
43
+ * DO NOT EDIT BY HAND!
44
+ *
45
+ * To re-generate this header file re-run the script!
46
+ */
47
+
48
+ EOT ;
49
+ }
50
+
51
+ /**
52
+ * @inheritDoc
53
+ */
54
+ public function generateCppStart (): string
55
+ {
56
+ return $ this ->generateHeaderStart () . <<<EOT
57
+
58
+ #include <dpp/export.h>
59
+ #include <dpp/snowflake.h>
60
+ #include <dpp/cluster.h>
61
+
62
+ namespace dpp {
63
+
64
+
65
+ EOT ;
66
+ }
67
+
68
+ /**
69
+ * @inheritDoc
70
+ */
71
+ public function checkForChanges (): bool
72
+ {
73
+ /* Check if we need to re-generate by comparing modification times */
74
+ $ us = file_exists ('include/dpp/cluster_sync_calls.h ' ) ? filemtime ('include/dpp/cluster_sync_calls.h ' ) : 0 ;
75
+ $ them = filemtime ('include/dpp/cluster.h ' );
76
+ $ thist = filemtime ('buildtools/make_sync_struct.php ' );
77
+ if ($ them <= $ us && $ thist <= $ us ) {
78
+ echo "-- No change required. \n" ;
79
+ return false ;
80
+ }
81
+
82
+ echo "-- Autogenerating include/dpp/cluster_sync_calls.h \n" ;
83
+ echo "-- Autogenerating src/dpp/cluster_sync_calls.cpp \n" ;
84
+ return true ;
85
+ }
86
+
87
+ /**
88
+ * @inheritDoc
89
+ */
90
+ public function generateHeaderDef (string $ returnType , string $ currentFunction , string $ parameters , string $ noDefaults , string $ parameterNames ): string
91
+ {
92
+ return "$ returnType {$ currentFunction }_sync( $ parameters); \n\n" ;
93
+ }
94
+
95
+ /**
96
+ * @inheritDoc
97
+ */
98
+ public function generateCppDef (string $ returnType , string $ currentFunction , string $ parameters , string $ noDefaults , string $ parameterNames ): string
99
+ {
100
+ return "$ returnType cluster:: {$ currentFunction }_sync( $ noDefaults) { \n\treturn dpp::sync< $ returnType>(this, &cluster:: $ currentFunction$ parameterNames); \n} \n\n" ;
101
+ }
102
+
103
+ /**
104
+ * @inheritDoc
105
+ */
106
+ public function getCommentArray (): array
107
+ {
108
+ return [
109
+ " * \memberof dpp::cluster " ,
110
+ " * @throw dpp::rest_exception upon failure to execute REST function " ,
111
+ " * @warning This function is a blocking (synchronous) call and should only be used from within a separate thread. " ,
112
+ " * Avoid direct use of this function inside an event handler. " ,
113
+ ];
114
+ }
115
+
116
+ /**
117
+ * @inheritDoc
118
+ */
119
+ public function saveHeader (string $ content ): void
120
+ {
121
+ file_put_contents ('include/dpp/cluster_sync_calls.h ' , $ content );
122
+ }
123
+
124
+ /**
125
+ * @inheritDoc
126
+ */
127
+ public function saveCpp (string $ cppcontent ): void
128
+ {
129
+ file_put_contents ('src/dpp/cluster_sync_calls.cpp ' , $ cppcontent );
130
+ }
131
+ }
0 commit comments