Skip to content

Commit

Permalink
feat: coroutine rewrite: dpp::awaitable<T> and dpp::co_task<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Jul 14, 2023
1 parent dd879ee commit c7aeb05
Show file tree
Hide file tree
Showing 6 changed files with 1,441 additions and 642 deletions.
18 changes: 10 additions & 8 deletions buildtools/classes/Generator/CoroGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function generateHeaderStart(): string
public function generateCppStart(): string
{
return $this->generateHeaderStart() . <<<EOT
#ifdef DPP_CORO
#include <dpp/export.h>
#include <dpp/snowflake.h>
Expand All @@ -80,6 +81,7 @@ public function checkForChanges(): bool
}

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

Expand All @@ -88,19 +90,15 @@ public function checkForChanges(): bool
*/
public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterTypes, string $parameterNames): string
{
$parameterNames = preg_replace('/^, /', '', $parameterNames);
if (!empty($parameterNames)) {
$parameterNames .= ', ';
}
return "auto inline co_{$currentFunction}($noDefaults) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->$currentFunction({$parameterNames}cc); }); \n}\n\n";
return "awaitable<confirmation_callback_t> co_{$currentFunction}($parameters);\n\n";
}

/**
* @inheritDoc
*/
public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterTypes, string $parameterNames): string
{
return '';
return "awaitable<confirmation_callback_t> cluster::co_${currentFunction}($noDefaults) {\n\treturn {this, static_cast<void (cluster::*)($parameterTypes". (!empty($parameterTypes) ? ", " : "") . "command_completion_event_t)>(&cluster::$currentFunction)$parameterNames};\n}\n\n";
}

/**
Expand All @@ -116,7 +114,7 @@ public function getCommentArray(): array
*/
public function saveHeader(string $content): void
{
$content .= "auto inline 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 = {}) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->request(url, method, cc, mimetype, headers); }); \n}\n\n";
$content .= "awaitable<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 = {});\n\n";
file_put_contents('include/dpp/cluster_coro_calls.h', $content);
}

Expand All @@ -125,7 +123,11 @@ public function saveHeader(string $content): void
*/
public function saveCpp(string $cppcontent): void
{
/* No cpp file to save, code is all inline */
$cppcontent .= "dpp::awaitable<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) {\n\treturn awaitable<http_request_completion_t>{[&](auto &&cc) { this->request(url, method, cc, postdata, mimetype, headers); }};\n}
#endif
";
file_put_contents('src/dpp/cluster_coro_calls.cpp', $cppcontent);
}

}
Loading

0 comments on commit c7aeb05

Please sign in to comment.