Skip to content

Commit

Permalink
Added support for ".x_body" file extensions for code injections in co…
Browse files Browse the repository at this point in the history
…mmsdsl2comms.
  • Loading branch information
arobenko committed Dec 24, 2024
1 parent 943e307 commit 20f8bca
Show file tree
Hide file tree
Showing 22 changed files with 394 additions and 75 deletions.
152 changes: 143 additions & 9 deletions app/commsdsl2comms/src/CommsField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ bool CommsField::commsPrepare()
auto& obj = m_field.dslObj();
bool overrides =
commsPrepareOverrideInternal(obj.valueOverride(), codePathPrefix, strings::valueFileSuffixStr(), m_customCode.m_value, "value") &&
commsPrepareOverrideInternal(obj.readOverride(), codePathPrefix, strings::readFileSuffixStr(), m_customCode.m_read, "read") &&
commsPrepareOverrideInternal(obj.writeOverride(), codePathPrefix, strings::writeFileSuffixStr(), m_customCode.m_write, "write") &&
commsPrepareOverrideInternal(obj.refreshOverride(), codePathPrefix, strings::refreshFileSuffixStr(), m_customCode.m_refresh, "refresh") &&
commsPrepareOverrideInternal(obj.lengthOverride(), codePathPrefix, strings::lengthFileSuffixStr(), m_customCode.m_length, "length") &&
commsPrepareOverrideInternal(obj.validOverride(), codePathPrefix, strings::validFileSuffixStr(), m_customCode.m_valid, "valid") &&
commsPrepareOverrideInternal(obj.nameOverride(), codePathPrefix, strings::nameFileSuffixStr(), m_customCode.m_name, "name");
commsPrepareOverrideInternal(obj.readOverride(), codePathPrefix, strings::readFileSuffixStr(), m_customCode.m_read, "read", &CommsField::commsPrepareCustomReadFromBodyInternal) &&
commsPrepareOverrideInternal(obj.writeOverride(), codePathPrefix, strings::writeFileSuffixStr(), m_customCode.m_write, "write", &CommsField::commsPrepareCustomWriteFromBodyInternal) &&
commsPrepareOverrideInternal(obj.refreshOverride(), codePathPrefix, strings::refreshFileSuffixStr(), m_customCode.m_refresh, "refresh", &CommsField::commsPrepareCustomRefreshFromBodyInternal) &&
commsPrepareOverrideInternal(obj.lengthOverride(), codePathPrefix, strings::lengthFileSuffixStr(), m_customCode.m_length, "length", &CommsField::commsPrepareCustomLengthFromBodyInternal) &&
commsPrepareOverrideInternal(obj.validOverride(), codePathPrefix, strings::validFileSuffixStr(), m_customCode.m_valid, "valid", &CommsField::commsPrepareCustomValidFromBodyInternal) &&
commsPrepareOverrideInternal(obj.nameOverride(), codePathPrefix, strings::nameFileSuffixStr(), m_customCode.m_name, "name", &CommsField::commsPrepareCustomNameFromBodyInternal);

if (!overrides) {
return false;
Expand Down Expand Up @@ -860,7 +860,8 @@ bool CommsField::commsPrepareOverrideInternal(
std::string& codePathPrefix,
const std::string& suffix,
std::string& customCode,
const std::string& name)
const std::string& name,
BodyCustomCodeFunc bodyFunc)
{
if (isOverrideCodeRequired(type) && (!comms::isGlobalField(m_field))) {
m_field.generator().logger().error(
Expand All @@ -876,11 +877,16 @@ bool CommsField::commsPrepareOverrideInternal(
}

auto contents = util::readFileContents(codePathPrefix + suffix);
if (contents.empty()) {
if (!contents.empty()) {
customCode = std::move(contents);
break;
}

customCode = std::move(contents);
if (bodyFunc == nullptr) {
break;
}

customCode = bodyFunc(codePathPrefix);
} while (false);

if (customCode.empty() && isOverrideCodeRequired(type)) {
Expand All @@ -893,6 +899,134 @@ bool CommsField::commsPrepareOverrideInternal(
return true;
}

std::string CommsField::commsPrepareCustomReadFromBodyInternal(const std::string& codePathPrefix)
{
auto contents = util::readFileContents(codePathPrefix + strings::readBodyFileSuffixStr());
if (contents.empty()) {
return std::string();
}

static const std::string Templ =
"/// @brief Custom read functionality\n"
"template <typename TIter>\n"
"comms::ErrorStatus read(TIter& iter, std::size_t len)\n"
"{\n"
" #^#BODY#$#\n"
"}\n";

util::ReplacementMap repl = {
{"BODY", std::move(contents)},
};

return util::processTemplate(Templ, repl);
}

std::string CommsField::commsPrepareCustomWriteFromBodyInternal(const std::string& codePathPrefix)
{
auto contents = util::readFileContents(codePathPrefix + strings::writeBodyFileSuffixStr());
if (contents.empty()) {
return std::string();
}

static const std::string Templ =
"/// @brief Custom write functionality\n"
"template <typename TIter>\n"
"comms::ErrorStatus write(TIter& iter, std::size_t len) const\n"
"{\n"
" #^#BODY#$#\n"
"}\n";

util::ReplacementMap repl = {
{"BODY", std::move(contents)},
};

return util::processTemplate(Templ, repl);
}

std::string CommsField::commsPrepareCustomRefreshFromBodyInternal(const std::string& codePathPrefix)
{
auto contents = util::readFileContents(codePathPrefix + strings::refreshBodyFileSuffixStr());
if (contents.empty()) {
return std::string();
}

static const std::string Templ =
"/// @brief Custom refresh functionality\n"
"bool refresh()\n"
"{\n"
" #^#BODY#$#\n"
"}\n";

util::ReplacementMap repl = {
{"BODY", std::move(contents)},
};

return util::processTemplate(Templ, repl);
}

std::string CommsField::commsPrepareCustomLengthFromBodyInternal(const std::string& codePathPrefix)
{
auto contents = util::readFileContents(codePathPrefix + strings::lengthBodyFileSuffixStr());
if (contents.empty()) {
return std::string();
}

static const std::string Templ =
"/// @brief Custom length calculation functionality\n"
"std::size_t length() const\n"
"{\n"
" #^#BODY#$#\n"
"}\n";

util::ReplacementMap repl = {
{"BODY", std::move(contents)},
};

return util::processTemplate(Templ, repl);
}

std::string CommsField::commsPrepareCustomValidFromBodyInternal(const std::string& codePathPrefix)
{
auto contents = util::readFileContents(codePathPrefix + strings::validBodyFileSuffixStr());
if (contents.empty()) {
return std::string();
}

static const std::string Templ =
"/// @brief Custom validity check functionality\n"
"bool valid() const\n"
"{\n"
" #^#BODY#$#\n"
"}\n";

util::ReplacementMap repl = {
{"BODY", std::move(contents)},
};

return util::processTemplate(Templ, repl);
}

std::string CommsField::commsPrepareCustomNameFromBodyInternal(const std::string& codePathPrefix)
{
auto contents = util::readFileContents(codePathPrefix + strings::nameBodyFileSuffixStr());
if (contents.empty()) {
return std::string();
}

static const std::string Templ =
"/// @brief Name of the field.\n"
"static const char* name()\n"
"{\n"
" #^#BODY#$#\n"
"}\n";

util::ReplacementMap repl = {
{"BODY", std::move(contents)},
};

return util::processTemplate(Templ, repl);
}

bool CommsField::commsWriteCommonInternal() const
{
auto& generator = m_field.generator();
Expand Down
11 changes: 10 additions & 1 deletion app/commsdsl2comms/src/CommsField.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,22 @@ class CommsField
std::string m_append;
};

using BodyCustomCodeFunc = std::string (*)(const std::string& codePathPrefix);

bool copyCodeFromInternal();
bool commsPrepareOverrideInternal(
commsdsl::parse::OverrideType type,
std::string& codePathPrefix,
const std::string& suffix,
std::string& customCode,
const std::string& name);
const std::string& name,
BodyCustomCodeFunc bodyFunc = nullptr);
static std::string commsPrepareCustomReadFromBodyInternal(const std::string& codePathPrefix);
static std::string commsPrepareCustomWriteFromBodyInternal(const std::string& codePathPrefix);
static std::string commsPrepareCustomRefreshFromBodyInternal(const std::string& codePathPrefix);
static std::string commsPrepareCustomLengthFromBodyInternal(const std::string& codePathPrefix);
static std::string commsPrepareCustomValidFromBodyInternal(const std::string& codePathPrefix);
static std::string commsPrepareCustomNameFromBodyInternal(const std::string& codePathPrefix);
bool commsWriteCommonInternal() const;
bool commsWriteDefInternal() const;
std::string commsFieldDefCodeInternal() const;
Expand Down
5 changes: 5 additions & 0 deletions app/commsdsl2comms/src/CommsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,15 @@ bool CommsGenerator::commsWriteExtraFilesInternal() const
strings::privateFileSuffixStr(),
strings::valueFileSuffixStr(),
strings::readFileSuffixStr(),
strings::readBodyFileSuffixStr(),
strings::writeFileSuffixStr(),
strings::writeBodyFileSuffixStr(),
strings::lengthFileSuffixStr(),
strings::lengthBodyFileSuffixStr(),
strings::validFileSuffixStr(),
strings::validBodyFileSuffixStr(),
strings::refreshFileSuffixStr(),
strings::refreshBodyFileSuffixStr(),
strings::nameFileSuffixStr(),
strings::incFileSuffixStr(),
strings::appendFileSuffixStr(),
Expand Down
Loading

0 comments on commit 20f8bca

Please sign in to comment.