Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:brainboxdotcc/DPP into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Aug 15, 2022
2 parents a198420 + f540efa commit 8360952
Show file tree
Hide file tree
Showing 32 changed files with 695 additions and 297 deletions.
4 changes: 3 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
"amod",
"apult",
"cplusplus",
"xmake"
"xmake",
"CORO",
"cback"
],
"flagWords": [
"hte"
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ option(BUILD_VOICE_SUPPORT "Build voice support" ON)
option(RUN_LDCONFIG "Run ldconfig after installation" ON)
option(DPP_BUILD_TEST "Build the test program" ON)
option(DPP_NO_VCPKG "No VCPKG" OFF)
option(DPP_CORO "Experimental support for C++20 coroutines" OFF)

include(CheckCXXSymbolExists)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_compile_definitions(DPP_BUILD)

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/include/dpp/version.h" version_h)
Expand Down
225 changes: 225 additions & 0 deletions buildtools/make_coro_struct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php

/* Get the content of all cluster source files into an array */
exec("cat src/dpp/cluster/*.cpp", $clustercpp);

/* These methods have signatures incompatible with this script */
$blacklist = [
'channel_edit_permissions',
'message_add_reaction',
'message_delete_reaction',
'message_delete_reaction_emoji',
'message_delete_all_reactions',
'message_delete_own_reaction',
'message_get_reactions',
'channel_typing',
];

/* The script cannot determine the correct return type of these methods,
* so we specify it by hand here.
*/
$forcedReturn = [
'direct_message_create' => 'message',
'guild_get_members' => 'guild_member_map',
'guild_search_members' => 'guild_member_map',
'message_create' => 'message',
'message_edit' => 'message',
];

/* Get the contents of cluster.h into an array */
$header = explode("\n", file_get_contents('include/dpp/cluster.h'));

/* Finite state machine state constants */
define('STATE_SEARCH_FOR_FUNCTION', 0);
define('STATE_IN_FUNCTION', 1);
define('STATE_END_OF_FUNCTION', 2);

$state = STATE_SEARCH_FOR_FUNCTION;
$currentFunction = $parameters = $returnType = '';
$content = <<<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!
*/
#ifdef DPP_CORO
EOT;
$cppcontent = $content;
$cppcontent .= <<<EOT
#include <dpp/export.h>
#include <dpp/snowflake.h>
#include <dpp/cluster.h>
#include <dpp/coro.h>
namespace dpp {
EOT;

/* 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";
exit(0);
}

echo "-- Autogenerating include/dpp/cluster_coro_calls.h\n";
/* echo "-- Autogenerating src/dpp/cluster_coro_calls.cpp\n"; */

/* Scan every line of the C++ source */
foreach ($clustercpp as $cpp) {
/* Look for declaration of function body */
if ($state == STATE_SEARCH_FOR_FUNCTION &&
preg_match('/^\s*void\s+cluster::([^(]+)\s*\((.*)command_completion_event_t\s*callback\s*\)/', $cpp, $matches)) {
$currentFunction = $matches[1];
$parameters = preg_replace('/,\s*$/', '', $matches[2]);
if (!in_array($currentFunction, $blacklist)) {
$state = STATE_IN_FUNCTION;
}
/* Scan function body */
} elseif ($state == STATE_IN_FUNCTION) {
/* End of function */
if (preg_match('/^\}\s*$/', $cpp)) {
$state = STATE_END_OF_FUNCTION;
/* look for the return type of the method */
} elseif (preg_match('/rest_request<([^>]+)>/', $cpp, $matches)) {
/* rest_request<T> */
$returnType = $matches[1];
} elseif (preg_match('/rest_request_list<([^>]+)>/', $cpp, $matches)) {
/* rest_request_list<T> */
$returnType = $matches[1] . '_map';
} elseif (preg_match('/callback\(confirmation_callback_t\(\w+, ([^(]+)\(.*, \w+\)\)/', $cpp, $matches)) {
/* confirmation_callback_t */
$returnType = $matches[1];
} elseif (!empty($forcedReturn[$currentFunction])) {
/* Forced return type */
$returnType = $forcedReturn[$currentFunction];
}
}
/* Completed parsing of function body */
if ($state == STATE_END_OF_FUNCTION && !empty($currentFunction) && !empty($returnType)) {
if (!in_array($currentFunction, $blacklist)) {
$parameterList = explode(',', $parameters);
$parameterNames = [];
foreach ($parameterList as $parameter) {
$parts = explode(' ', trim($parameter));
$parameterNames[] = trim(preg_replace('/[\s\*\&]+/', '', $parts[count($parts) - 1]));
}
$content .= getComments($currentFunction, $returnType, $parameterNames) . "\n";
$fullParameters = getFullParameters($currentFunction, $parameterNames);
$parameterNames = trim(join(', ', $parameterNames));
if (!empty($parameterNames)) {
$parameterNames = $parameterNames . ', ';
}
$noDefaults = $parameters;
$parameters = !empty($fullParameters) ? $fullParameters : $parameters;
/* $content .= "auto inline {$currentFunction}_coro($parameters);\n\n"; */
$content .= "auto inline co_{$currentFunction}($noDefaults) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->$currentFunction($parameterNames cc); }); \n}\n\n";
}
$currentFunction = $parameters = $returnType = '';
$state = STATE_SEARCH_FOR_FUNCTION;
}
}
$content .= <<<EOT
/* End of auto-generated definitions */
#endif
EOT;
$cppcontent .= <<<EOT
};
/* End of auto-generated definitions */
#endif
EOT;

/**
* @brief Get parameters of a function with defaults
* @param string $currentFunction Current function name
* @param array $parameters Parameter names
* @return string Parameter list
*/
function getFullParameters(string $currentFunction, array $parameters): string
{
global $header;
foreach ($header as $line) {
if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\((.*' . join('.*', $parameters) . '.*)command_completion_event_t\s*callback\s*/', $line, $matches)) {
return preg_replace('/,\s*$/', '', $matches[1]);
}
}
return '';
}

/**
* @brief Get the comment block of a function.
* Adds see/return doxygen tags
* @param string $currentFunction function name
* @param string $returnType Return type of function
* @param array $parameters Parameter names
* @return string Comment block
*/
function getComments(string $currentFunction, string $returnType, array $parameters): string
{
global $header;
/* First find the function */
foreach ($header as $i => $line) {
if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\(.*' . join('.*', $parameters) . '.*command_completion_event_t\s*callback\s*/', $line)) {
/* Backpeddle */
$lineIndex = 1;
for ($n = $i; $n != 0; --$n, $lineIndex++) {
$header[$n] = preg_replace('/^\t+/', '', $header[$n]);
$header[$n] = preg_replace('/@see (.+?)$/', '@see dpp::cluster::' . $currentFunction . "\n * @see \\1", $header[$n]);
$header[$n] = preg_replace('/@param callback .*$/', '@return ' . $returnType . ' returned object on completion', $header[$n]);
if (preg_match('/\s*\* On success /i', $header[$n])) {
$header[$n] = "";
}
if (preg_match('/\s*\/\*\*\s*$/', $header[$n])) {
$part = array_slice($header, $n, $lineIndex - 1);
array_splice($part, count($part) - 1, 0,
[
" * \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.", */
]
);
return str_replace("\n\n", "\n", join("\n", $part));
}
}
return '';
}
}
return '';
}

/* Finished parsing, output autogenerated files */
file_put_contents('include/dpp/cluster_coro_calls.h', $content);
/* file_put_contents('src/dpp/cluster_coro_calls.cpp', $cppcontent); */
3 changes: 2 additions & 1 deletion buildtools/make_sync_struct.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
/* 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');
if ($them <= $us) {
$thist = filemtime('buildtools/make_sync_struct.php');
if ($them <= $us && $thist <= $us) {
echo "-- No change required.\n";
exit(0);
}
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ struct DPP_EXPORT component_interaction {
/**
* @brief An auto complete interaction
*/
struct DPP_EXPORT autocomplete_interaction {
struct DPP_EXPORT autocomplete_interaction : public command_interaction {
};

/**
Expand Down
4 changes: 2 additions & 2 deletions include/dpp/auditlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ class DPP_EXPORT auditlog : public json_interface<auditlog> {
std::vector<audit_entry> entries; //!< Audit log entries

/** Constructor */
auditlog();
auditlog() = default;

/** Destructor */
~auditlog();
virtual ~auditlog() = default;

/** Read class values from json object
* @param j A json object to read from
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DPP_EXPORT ban : public json_interface<ban> {
ban();

/** Destructor */
~ban();
virtual ~ban() = default;

/** Read class values from json object
* @param j A json object to read from
Expand Down
Loading

0 comments on commit 8360952

Please sign in to comment.