Skip to content

Commit

Permalink
Improved autogenerator, uses class autoloading to cut down on copy paste
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Aug 15, 2022
1 parent 5cb75b5 commit 7a9934d
Show file tree
Hide file tree
Showing 17 changed files with 1,085 additions and 291 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ config.json
docs
testdata/maxpower.pcm
testdata/onandon.pcm
buildtools/composer.phar
docs/doxygen_sqlite3.db
src/build
.vs
Expand Down
127 changes: 127 additions & 0 deletions buildtools/classes/Generator/CoroGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Dpp\Generator;

use Dpp\StructGeneratorInterface;

/**
* Generate header and .cpp file for coroutine calls (starting with 'co_')
*/
class CoroGenerator implements StructGeneratorInterface
{

/**
* @inheritDoc
*/
public function generateHeaderStart(): string
{
return <<<EOT
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* Copyright 2022 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
/* Auto @generated by buildtools/make_coro_struct.php.
*
* DO NOT EDIT BY HAND!
*
* To re-generate this header file re-run the script!
*/
EOT;
}

/**
* @inheritDoc
*/
public function generateCppStart(): string
{
return $this->generateHeaderStart() . <<<EOT
#include <dpp/export.h>
#include <dpp/snowflake.h>
#include <dpp/cluster.h>
#include <dpp/coro.h>
namespace dpp {
EOT;
}

/**
* @inheritDoc
*/
public function checkForChanges(): bool
{
/* Check if we need to re-generate by comparing modification times */
$us = file_exists('include/dpp/cluster_coro_calls.h') ? filemtime('include/dpp/cluster_coro_calls.h') : 0;
$them = filemtime('include/dpp/cluster.h');
$thist = filemtime('buildtools/make_coro_struct.php');
if ($them <= $us && $thist <= $us) {
echo "-- No change required.\n";
return false;
}

echo "-- Autogenerating include/dpp/cluster_coro_calls.h\n";
return true;
}

/**
* @inheritDoc
*/
public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string
{
return "auto inline co_{$currentFunction}($noDefaults) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->$currentFunction($parameterNames cc); }); \n}\n\n";
}

/**
* @inheritDoc
*/
public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string
{
return '';
}

/**
* @inheritDoc
*/
public function getCommentArray(): array
{
return [" * \memberof dpp::cluster"];
}

/**
* @inheritDoc
*/
public function saveHeader(string $content): void
{
file_put_contents('include/dpp/cluster_coro_calls.h', $content);
}

/**
* @inheritDoc
*/
public function saveCpp(string $cppcontent): void
{
/* No cpp file to save, code is all inline */
}

}
131 changes: 131 additions & 0 deletions buildtools/classes/Generator/SyncGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Dpp\Generator;

use Dpp\StructGeneratorInterface;

/**
* Generate header and .cpp file for synchronous calls (ending in '_sync')
*/
class SyncGenerator implements StructGeneratorInterface
{

/**
* @inheritDoc
*/
public function generateHeaderStart(): string
{
return <<<EOT
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* Copyright 2022 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
/* Auto @generated by buildtools/make_sync_struct.php.
*
* DO NOT EDIT BY HAND!
*
* To re-generate this header file re-run the script!
*/
EOT;
}

/**
* @inheritDoc
*/
public function generateCppStart(): string
{
return $this->generateHeaderStart() . <<<EOT
#include <dpp/export.h>
#include <dpp/snowflake.h>
#include <dpp/cluster.h>
namespace dpp {
EOT;
}

/**
* @inheritDoc
*/
public function checkForChanges(): bool
{
/* Check if we need to re-generate by comparing modification times */
$us = file_exists('include/dpp/cluster_sync_calls.h') ? filemtime('include/dpp/cluster_sync_calls.h') : 0;
$them = filemtime('include/dpp/cluster.h');
$thist = filemtime('buildtools/make_sync_struct.php');
if ($them <= $us && $thist <= $us) {
echo "-- No change required.\n";
return false;
}

echo "-- Autogenerating include/dpp/cluster_sync_calls.h\n";
echo "-- Autogenerating src/dpp/cluster_sync_calls.cpp\n";
return true;
}

/**
* @inheritDoc
*/
public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string
{
return "$returnType {$currentFunction}_sync($parameters);\n\n";
}

/**
* @inheritDoc
*/
public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string
{
return "$returnType cluster::{$currentFunction}_sync($noDefaults) {\n\treturn dpp::sync<$returnType>(this, &cluster::$currentFunction$parameterNames);\n}\n\n";
}

/**
* @inheritDoc
*/
public function getCommentArray(): array
{
return [
" * \memberof dpp::cluster",
" * @throw dpp::rest_exception upon failure to execute REST function",
" * @warning This function is a blocking (synchronous) call and should only be used from within a separate thread.",
" * Avoid direct use of this function inside an event handler.",
];
}

/**
* @inheritDoc
*/
public function saveHeader(string $content): void
{
file_put_contents('include/dpp/cluster_sync_calls.h', $content);
}

/**
* @inheritDoc
*/
public function saveCpp(string $cppcontent): void
{
file_put_contents('src/dpp/cluster_sync_calls.cpp', $cppcontent);
}
}
75 changes: 75 additions & 0 deletions buildtools/classes/StructGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Dpp;

/**
* Represents a header/cpp generator used to auto-generate cpp/.h files.
*/
interface StructGeneratorInterface
{
/**
* Generate the start of the header file
*
* @return string header content
*/
public function generateHeaderStart(): string;

/**
* Generate the start of the cpp file
*
* @return string cpp content
*/
public function generateCppStart(): string;

/**
* Check if the script should run and re-generate content or not
*
* @return string true if the script should run, false to exit
*/
public function checkForchanges(): bool;

/**
* Generate header definition for a function
*
* @param string $returnType Return type of function
* @param string $currentFunction Current function name
* @param string $parameters Current function parameters with default values
* @param string $noDefaults Current function parameters without default values
* @param string $parameterNames Parameter names only
* @return string header content to append
*/
public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string;

/**
* Generate cpp definition for a function
*
* @param string $returnType Return type of function
* @param string $currentFunction Current function name
* @param string $parameters Current function parameters with default values
* @param string $noDefaults Current function parameters without default values
* @param string $parameterNames Parameter names only
* @return string cpp content to append
*/
public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string;

/**
* Return comment lines to add to each header definition
*
* @return array Comment lines to add
*/
public function getCommentArray(): array;

/**
* Save the .h file
*
* @param string $content Content to save
*/
public function saveHeader(string $content): void;

/**
* Save the .cpp file
*
* @param string $cppcontent Content to save
*/
public function saveCpp(string $cppcontent): void;
};
17 changes: 17 additions & 0 deletions buildtools/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "brainboxdotcc/dpp",
"description": "DPP Build Tools",
"type": "project",
"license": "Apache 2.0",
"autoload": {
"psr-4": {
"Dpp\\": "classes/"
}
},
"authors": [
{
"name": "brain"
}
],
"require": {}
}
Loading

0 comments on commit 7a9934d

Please sign in to comment.