Debian Codegraph @

buildtools/classes/Generator/CoroGenerator.php

raw symbols SCIP
1 <?php
2
3 namespace Dpp\Generator;
4
5 use Dpp\StructGeneratorInterface;
6
7 /**
8 * Generate header and .cpp file for coroutine calls (starting with 'co_')
9 */
10 class CoroGenerator 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_coro_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 #ifndef DPP_NO_CORO
58
59 #include <dpp/export.h>
60 #include <dpp/snowflake.h>
61 #include <dpp/cluster.h>
62 #include <dpp/coro.h>
63
64 namespace dpp {
65
66
67 EOT;
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function checkForChanges(): bool
74 {
75 /* Check if we need to re-generate by comparing modification times */
76 $us = file_exists('include/dpp/cluster_coro_calls.h') ? filemtime('include/dpp/cluster_coro_calls.h') : 0;
77 $them = filemtime('include/dpp/cluster.h');
78 if ($them <= $us) {
79 echo "-- No change required.\n";
80 return false;
81 }
82
83 echo "-- Autogenerating include/dpp/cluster_coro_calls.h\n";
84 echo "-- Autogenerating src/dpp/cluster_coro_calls.cpp\n";
85 return true;
86 }
87
88 /**
89 * @inheritDoc
90 */
91 public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterTypes, string $parameterNames): string
92 {
93 return "[[nodiscard]] async<confirmation_callback_t> co_{$currentFunction}($parameters);\n\n";
94 }
95
96 /**
97 * @inheritDoc
98 */
99 public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterTypes, string $parameterNames): string
100 {
101 /* if (substr($parameterNames, 0, 2) === ", ")
102 $parameterNames = substr($parameterNames, 2); */
103 return "async<confirmation_callback_t> cluster::co_${currentFunction}($noDefaults) {\n\treturn async{ this, static_cast<void (cluster::*)($parameterTypes". (!empty($parameterTypes) ? ", " : "") . "command_completion_event_t)>(&cluster::$currentFunction)$parameterNames };\n}\n\n";
104 }
105
106 /**
107 * @inheritDoc
108 */
109 public function getCommentArray(): array
110 {
111 return [" * \memberof dpp::cluster"];
112 }
113
114 /**
115 * @inheritDoc
116 */
117 public function saveHeader(string $content): void
118 {
119 $content .= "[[nodiscard]] async<http_request_completion_t> co_request(const std::string &url, http_method method, const std::string &postdata = \"\", const std::string &mimetype = \"text/plain\", const std::multimap<std::string, std::string> &headers = {}, const std::string &protocol = \"1.1\");\n\n";
120 file_put_contents('include/dpp/cluster_coro_calls.h', $content);
121 }
122
123 /**
124 * @inheritDoc
125 */
126 public function saveCpp(string $cppcontent): void
127 {
128 $cppcontent .= "dpp::async<dpp::http_request_completion_t> dpp::cluster::co_request(const std::string &url, http_method method, const std::string &postdata, const std::string &mimetype, const std::multimap<std::string, std::string> &headers, const std::string &protocol) {\n\treturn async<http_request_completion_t>{ [&, this] <typename C> (C &&cc) { return this->request(url, method, std::forward<C>(cc), postdata, mimetype, headers, protocol); }};\n}
129
130 #endif
131 ";
132 file_put_contents('src/dpp/cluster_coro_calls.cpp', $cppcontent);
133 }
134
135 }