Skip to content

Commit

Permalink
Added support for appropriate code generation when "validCond" proper…
Browse files Browse the repository at this point in the history
…ty is used for <bundle>.
  • Loading branch information
arobenko committed Dec 16, 2024
1 parent 0c58c00 commit cd9c885
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
30 changes: 30 additions & 0 deletions app/commsdsl2comms/src/CommsBundleField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CommsBundleField.h"

#include "CommsGenerator.h"
#include "CommsOptionalField.h"

#include "commsdsl/gen/comms.h"
#include "commsdsl/gen/util.h"
Expand Down Expand Up @@ -333,6 +334,35 @@ std::string CommsBundleField::commsDefRefreshFuncBodyImpl() const
return util::processTemplate(Templ, repl);
}

std::string CommsBundleField::commsDefValidFuncBodyImpl() const
{
auto validCond = bundleDslObj().validCond();
if (!validCond.valid()) {
return strings::emptyString();
}

auto& gen = CommsGenerator::cast(generator());
auto str = CommsOptionalField::commsDslCondToString(gen, m_members, validCond, true);

if (str.empty()) {
return strings::emptyString();
}

static const std::string Templ =
"if (!Base::valid()) {\n"
" return false;\n"
"}\n\n"
"return\n"
" #^#CODE#$#;\n"
;

util::ReplacementMap repl = {
{"CODE", std::move(str)},
};

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

bool CommsBundleField::commsPrepareInternal()
{
m_members = commsTransformFieldsList(members());
Expand Down
1 change: 1 addition & 0 deletions app/commsdsl2comms/src/CommsBundleField.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CommsBundleField final : public commsdsl::gen::BundleField, public CommsFi
virtual std::string commsDefPrivateCodeImpl() const override;
virtual std::string commsDefReadFuncBodyImpl() const override;
virtual std::string commsDefRefreshFuncBodyImpl() const override;
virtual std::string commsDefValidFuncBodyImpl() const override;
virtual bool commsIsVersionDependentImpl() const override;
virtual std::string commsMembersCustomizationOptionsBodyImpl(FieldOptsFunc fieldOptsFunc) const override;
virtual std::size_t commsMinLengthImpl() const override;
Expand Down
4 changes: 4 additions & 0 deletions app/commsdsl2comms/src/CommsInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ bool CommsInterface::writeImpl() const
bool CommsInterface::copyCodeFromInternal()
{
auto obj = dslObj();
if (!obj.valid()) {
return true;
}

auto& copyFrom = obj.copyCodeFrom();
if (copyFrom.empty()) {
return true;
Expand Down
10 changes: 4 additions & 6 deletions app/commsdsl2comms/test/test21/Schema.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema name="test21"
id="1"
endian="big"
version="5">
<schema name="test21" endian="big" version="5">
<fields>
<enum name="MsgId" type="uint8" semanticType="messageId" >
<validValue name="M1" val="1" />
Expand Down Expand Up @@ -56,7 +53,8 @@
<field>
<int name="O1" type="uint32" />
</field>
</optional>
</optional>

</fields>

<message name="Msg1" id="MsgId.M1" readOverride="extend" writeOverride="replace" lengthOverride="any" validOverride="replace">
Expand Down Expand Up @@ -84,7 +82,7 @@
<validValue name="V5" val="0xffffffffffffffff" />
</enum>
</message>

<frame name="Frame">
<id name="ID" field="MsgId" />
<payload name="Data" />
Expand Down
23 changes: 19 additions & 4 deletions app/commsdsl2comms/test/test30/Schema.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema name="test30"
id="1"
endian="big"
version="5">
<schema name="test30" endian="big" version="5">
<fields>
<enum name="MsgId" type="uint8" semanticType="messageId" >
<validValue name="M1" val="1" />
<validValue name="M2" val="2" />
<validValue name="M3" val="3" />
</enum>

<bundle name="B1">
Expand All @@ -17,6 +15,19 @@
</lengthPrefix>
</string>
</bundle>

<bundle name="B2" failOnInvalid="true">
<members>
<int name="M1" type="uint8" />
<int name="M2" type="uint8" />
</members>
<validCond>
<or>
<validCond value="$M1 = 0" />
<validCond value="$M2 != 0" />
</or>
</validCond>
</bundle>
</fields>

<message name="Msg1" id="MsgId.M1">
Expand All @@ -28,6 +39,10 @@
<bundle name="F1" reuse="B1" />
<int name="F2" type="uint16" />
</message>

<message name="Msg3" id="MsgId.M3">
<ref field="B2" name="F1" failOnInvalid="true"/>
</message>

<frame name="Frame">
<id name="ID" field="MsgId" />
Expand Down
12 changes: 12 additions & 0 deletions app/commsdsl2comms/test/test30/test30Test.th
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TestSuite : public CxxTest::TestSuite
public:
void test1();
void test2();
void test3();

using Interface =
test30::Message<
Expand Down Expand Up @@ -118,3 +119,14 @@ void TestSuite::test2()
TS_ASSERT(std::equal(std::begin(ExpBuf), std::end(ExpBuf), outBuf.begin()));
}

void TestSuite::test3()
{
Msg3 msg;
TS_ASSERT(msg.valid());

msg.field_f1().field_m1().setValue(1);
TS_ASSERT(!msg.valid());
msg.field_f1().field_m2().setValue(1);
TS_ASSERT(msg.valid());
}

0 comments on commit cd9c885

Please sign in to comment.