cemplate

Simple C++ template engine
git clone git://git.dimitrijedobrota.com/cemplate.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

commit36b5321d23281086549dbb857fdbcc39dbcf57fd
parent8af41f111c5fd88053b3276b7c6de4e8da9fa40e
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateSat, 22 Feb 2025 22:25:11 +0100

Function declarations, return statement

Diffstat:
MCMakeLists.txt|+-
Minclude/cemplate/cemplate.hpp|++++-
Msource/cemplate.cpp|+++++++++++++++-----

3 files changed, 20 insertions(+), 7 deletions(-)


diff --git a/CMakeLists.txt b/CMakeLists.txt

@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)

project(
cemplate
VERSION 0.1.2
VERSION 0.1.3
DESCRIPTION "Simple C++ template engine"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/cemplate.git"
LANGUAGES CXX

diff --git a/include/cemplate/cemplate.hpp b/include/cemplate/cemplate.hpp

@@ -14,6 +14,8 @@ std::string nspace(const std::string& name);

std::string nspace_close(const std::string& name);
std::string ret(const std::string& val);
struct param_t
{
std::string type;

@@ -22,7 +24,8 @@ struct param_t

std::string func(const std::string& ret,
const std::string& name,
std::vector<param_t> params);
std::vector<param_t> params,
bool delc = false);
std::string func_close();

diff --git a/source/cemplate.cpp b/source/cemplate.cpp

@@ -18,7 +18,7 @@ std::string include(const std::string& header, bool local)

std::string nspace(const std::string& name)
{
return std::format("namespace {}\n{{\n\n\n", name);
return std::format("namespace {}\n{{\n\n", name);
}
std::string nspace_close(const std::string& name)

@@ -26,21 +26,31 @@ std::string nspace_close(const std::string& name)

return std::format("\n}} // namespace {}\n\n", name);
}
std::string ret(const std::string& val)
{
return std::format("return {};\n", val);
}
std::string func(const std::string& ret,
const std::string& name,
std::vector<param_t> params)
std::vector<param_t> params,
bool decl)
{
static const auto format = [](const param_t& param)
{ return param.name.empty() ? param.type : param.type + ' ' + param.name; };
std::string res;
res += ret + ' ';
res += name + '(';
if (!params.empty()) {
res += params[0].type + ' ' + params[0].name;
res += format(params[0]);
for (std::size_t i = 1; i < params.size(); i++) {
res += ", " + params[i].type + ' ' + params[i].name;
res += ", " + format(params[i]);
}
}
res += ") {\n";
res += ')';
res += decl ? ";\n" : " {\n";
return res;
}