Skip to content

Commit 7a9934d

Browse files
Improved autogenerator, uses class autoloading to cut down on copy paste
1 parent 5cb75b5 commit 7a9934d

17 files changed

+1085
-291
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ config.json
44
docs
55
testdata/maxpower.pcm
66
testdata/onandon.pcm
7+
buildtools/composer.phar
78
docs/doxygen_sqlite3.db
89
src/build
910
.vs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
58+
#include <dpp/export.h>
59+
#include <dpp/snowflake.h>
60+
#include <dpp/cluster.h>
61+
#include <dpp/coro.h>
62+
63+
namespace dpp {
64+
65+
66+
EOT;
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
public function checkForChanges(): bool
73+
{
74+
/* Check if we need to re-generate by comparing modification times */
75+
$us = file_exists('include/dpp/cluster_coro_calls.h') ? filemtime('include/dpp/cluster_coro_calls.h') : 0;
76+
$them = filemtime('include/dpp/cluster.h');
77+
$thist = filemtime('buildtools/make_coro_struct.php');
78+
if ($them <= $us && $thist <= $us) {
79+
echo "-- No change required.\n";
80+
return false;
81+
}
82+
83+
echo "-- Autogenerating include/dpp/cluster_coro_calls.h\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 "auto inline co_{$currentFunction}($noDefaults) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->$currentFunction($parameterNames cc); }); \n}\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 '';
101+
}
102+
103+
/**
104+
* @inheritDoc
105+
*/
106+
public function getCommentArray(): array
107+
{
108+
return [" * \memberof dpp::cluster"];
109+
}
110+
111+
/**
112+
* @inheritDoc
113+
*/
114+
public function saveHeader(string $content): void
115+
{
116+
file_put_contents('include/dpp/cluster_coro_calls.h', $content);
117+
}
118+
119+
/**
120+
* @inheritDoc
121+
*/
122+
public function saveCpp(string $cppcontent): void
123+
{
124+
/* No cpp file to save, code is all inline */
125+
}
126+
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Dpp;
4+
5+
/**
6+
* Represents a header/cpp generator used to auto-generate cpp/.h files.
7+
*/
8+
interface StructGeneratorInterface
9+
{
10+
/**
11+
* Generate the start of the header file
12+
*
13+
* @return string header content
14+
*/
15+
public function generateHeaderStart(): string;
16+
17+
/**
18+
* Generate the start of the cpp file
19+
*
20+
* @return string cpp content
21+
*/
22+
public function generateCppStart(): string;
23+
24+
/**
25+
* Check if the script should run and re-generate content or not
26+
*
27+
* @return string true if the script should run, false to exit
28+
*/
29+
public function checkForchanges(): bool;
30+
31+
/**
32+
* Generate header definition for a function
33+
*
34+
* @param string $returnType Return type of function
35+
* @param string $currentFunction Current function name
36+
* @param string $parameters Current function parameters with default values
37+
* @param string $noDefaults Current function parameters without default values
38+
* @param string $parameterNames Parameter names only
39+
* @return string header content to append
40+
*/
41+
public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string;
42+
43+
/**
44+
* Generate cpp definition for a function
45+
*
46+
* @param string $returnType Return type of function
47+
* @param string $currentFunction Current function name
48+
* @param string $parameters Current function parameters with default values
49+
* @param string $noDefaults Current function parameters without default values
50+
* @param string $parameterNames Parameter names only
51+
* @return string cpp content to append
52+
*/
53+
public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string;
54+
55+
/**
56+
* Return comment lines to add to each header definition
57+
*
58+
* @return array Comment lines to add
59+
*/
60+
public function getCommentArray(): array;
61+
62+
/**
63+
* Save the .h file
64+
*
65+
* @param string $content Content to save
66+
*/
67+
public function saveHeader(string $content): void;
68+
69+
/**
70+
* Save the .cpp file
71+
*
72+
* @param string $cppcontent Content to save
73+
*/
74+
public function saveCpp(string $cppcontent): void;
75+
};

buildtools/composer.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "brainboxdotcc/dpp",
3+
"description": "DPP Build Tools",
4+
"type": "project",
5+
"license": "Apache 2.0",
6+
"autoload": {
7+
"psr-4": {
8+
"Dpp\\": "classes/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "brain"
14+
}
15+
],
16+
"require": {}
17+
}

0 commit comments

Comments
 (0)