Skip to content

Commit

Permalink
Added support for the "fixedValue" property of fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Dec 8, 2024
1 parent 08d9861 commit ea4db42
Show file tree
Hide file tree
Showing 35 changed files with 240 additions and 109 deletions.
10 changes: 8 additions & 2 deletions app/commsdsl2comms/src/CommsField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ std::string CommsField::commsFieldBaseParams(commsdsl::parse::Endian endian) con
return comms::dslEndianToOpt(endian);
}

void CommsField::commsAddFieldDefOptions(commsdsl::gen::util::StringsList& opts) const
void CommsField::commsAddFieldDefOptions(commsdsl::gen::util::StringsList& opts, bool tempFieldObj) const
{
if (comms::isGlobalField(m_field)) {
opts.push_back("TExtraOpts...");
Expand All @@ -755,7 +755,9 @@ void CommsField::commsAddFieldDefOptions(commsdsl::gen::util::StringsList& opts)
opts.push_back("typename TOpt::" + comms::scopeFor(m_field, m_field.generator(), gen.commsHasMainNamespaceInOptions(), true));
}

util::addToStrList("comms::option::def::HasName", opts);
if (!tempFieldObj) {
util::addToStrList("comms::option::def::HasName", opts);
}

do {
auto checkFieldTypeFunc =
Expand Down Expand Up @@ -795,6 +797,10 @@ void CommsField::commsAddFieldDefOptions(commsdsl::gen::util::StringsList& opts)
if (m_forcedPseudo || m_field.dslObj().isPseudo()) {
util::addToStrList("comms::option::def::EmptySerialization", opts);
}

if (m_field.dslObj().isFixedValue()) {
util::addToStrList("comms::option::def::FixedValue", opts);
}
}

void CommsField::commsAddFieldTypeOption(commsdsl::gen::util::StringsList& opts) const
Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/src/CommsField.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class CommsField

std::string commsCommonNameFuncCode() const;
std::string commsFieldBaseParams(commsdsl::parse::Endian endian) const;
void commsAddFieldDefOptions(commsdsl::gen::util::StringsList& opts) const;
void commsAddFieldDefOptions(commsdsl::gen::util::StringsList& opts, bool tempFieldObj = false) const;
void commsAddFieldTypeOption(commsdsl::gen::util::StringsList& opts) const;
bool commsIsExtended() const;

Expand Down
5 changes: 3 additions & 2 deletions app/commsdsl2comms/src/CommsIntField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ std::string CommsIntField::commsDefFieldOptsInternal(bool variantPropKey) const
{
util::StringsList opts;

commsAddFieldDefOptions(opts);
commsAddFieldDefOptions(opts, variantPropKey);
commsAddLengthOptInternal(opts);
commsAddSerOffsetOptInternal(opts);
commsAddDisplayOffsetOptInternal(opts);
Expand Down Expand Up @@ -1085,7 +1085,8 @@ void CommsIntField::commsAddAvailableLengthLimitOptInternal(StringsList& opts) c

bool CommsIntField::commsRequiresFailOnInvalidRefreshInternal() const
{
if (!dslObj().isFailOnInvalid()) {
if ((!dslObj().isFailOnInvalid()) ||
(dslObj().isFixedValue())) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/src/CommsVariantField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ std::string CommsVariantField::commsDefCopyCodeInternal() const
" if (!Base::currentFieldValid()) {\n\n"
" return true;\n"
" }\n\n"
" if (Base::currentField() != other.currentField()) {\n\n"
" if (Base::currentField() != other.currentField()) {\n"
" return false;\n"
" }\n\n"
" switch(Base::currentField()) {\n"
Expand Down
6 changes: 3 additions & 3 deletions app/commsdsl2comms/test/test28/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
</enum>
<variant name="Variant1">
<bundle name="P1">
<int name="type" type="uint8" validValue="0" failOnInvalid="true" />
<int name="type" type="uint8" validValue="0" failOnInvalid="true" fixedValue="true" />
<int name="val" type="uint32" />
</bundle>
<bundle name="P2">
<int name="type" type="uint8" defaultValidValue="1" failOnInvalid="true" />
<int name="type" type="uint8" defaultValidValue="1" failOnInvalid="true" fixedValue="true" />
<int name="val" type="uint8" />
</bundle>
</variant>

<variant name="Variant2" reuse="Variant1" defaultMember="P1" />

<int name="VariantType" type="uint8" failOnInvalid="true" />
<int name="VariantType" type="uint8" failOnInvalid="true" fixedValue="true" />
<variant name="Variant3">
<description>
Tests having more members than COMMS library can handle
Expand Down
4 changes: 2 additions & 2 deletions app/commsdsl2comms/test/test32/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<validValue name="P3" val="2" />
<validValue name="P4" val="3" />
</enum>
<int name="PropType" type="uint8" failOnInvalid="true" />
<int name="PropType" type="uint8" failOnInvalid="true" fixedValue="true" />
<variant name="Variant1">
<bundle name="P1">
<int name="Type" reuse="PropType" defaultValidValue="Prop.P1"/>
Expand Down Expand Up @@ -44,7 +44,7 @@
</set>
</bundle>
<bundle name="Any">
<int name="Type" type="uint8"/>
<int name="Type" type="uint8" />
<int name="Length" type="uint8" semanticType="length" />
<data name="Value" />
</bundle>
Expand Down
11 changes: 6 additions & 5 deletions app/commsdsl2comms/test/test32/test32Test.th
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public:
template <std::size_t TIdx, typename TField>
void operator()(TField& field)
{
TS_ASSERT(field.field_type().value() == m_type);
TS_ASSERT(field.field_type().getValue() == m_type);
}

private:
Expand Down Expand Up @@ -127,10 +127,11 @@ void TestSuite::test4()
TS_ASSERT(msg1.valid());

using PropKeyType = Msg1::Field_f1::Field_p1::Field_type::ValueType;
TS_ASSERT_EQUALS(f.field_type().value(), static_cast<PropKeyType>(0));
f.field_type().value() = static_cast<PropKeyType>(1);
TS_ASSERT(msg1.doRefresh());
TS_ASSERT_EQUALS(f.field_type().value(), static_cast<PropKeyType>(0));
TS_ASSERT_EQUALS(f.field_type().getValue(), static_cast<PropKeyType>(0));
TS_ASSERT(f.field_type().hasFixedValue());
// f.field_type().value() = static_cast<PropKeyType>(1);
// TS_ASSERT(msg1.doRefresh());
// TS_ASSERT_EQUALS(f.field_type().getValue(), static_cast<PropKeyType>(0));
}

void TestSuite::test5()
Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/test/test33/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<validValue name="P2" val="1" />
</enum>

<int name="PropType" type="uint8" failOnInvalid="true" />
<int name="PropType" type="uint8" failOnInvalid="true" fixedValue="true" />
<int name="PropLength" type="uint8" displayName="Length" semanticType="length"/>

<variant name="Variant1">
Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/test/test43/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<validValue name="K6" val="25" />
</enum>

<int name="PropKeyCommon" type="uint8" failOnInvalid="true"/>
<int name="PropKeyCommon" type="uint8" failOnInvalid="true" />

<variant name="KeyValueProp">
<bundle name="Prop1">
Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/test/test44/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<validValue name="M3" val="3" />
</enum>

<int name="VariantType" type="uint8" failOnInvalid="true" />
<int name="VariantType" type="uint8" failOnInvalid="true" fixedValue="true" />

<variant name="Variant1">
<description>
Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/test/test45/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<validValue name="M0" val="0" />
</enum>

<int name="VariantType" type="uint8" failOnInvalid="true" />
<int name="VariantType" type="uint8" failOnInvalid="true" fixedValue="true" />

<variant name="Variant1">
<bundle name="P0">
Expand Down
2 changes: 1 addition & 1 deletion app/commsdsl2comms/test/test48/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</enum>
</bitfield>

<int name="VariantType" type="uint8" failOnInvalid="true" />
<int name="VariantType" type="uint8" failOnInvalid="true" fixedValue="true" />

<variant name="Variant1">
<bundle name="P0">
Expand Down
4 changes: 2 additions & 2 deletions app/commsdsl2comms/test/test6/Schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@
</optional>
<variant name="m6">
<bundle name="p1">
<int name="key" type="uint8" validValue="0" failOnInvalid="true" />
<int name="key" type="uint8" validValue="0" failOnInvalid="true" fixedValue="true" />
<enum name="val" type="uint8">
<validValue name="v1" val="0" />
<validValue name="v2" val="1" />
</enum>
</bundle>
<bundle name="p2">
<int name="key" type="uint8" defaultValue="1" validValue="1" failOnInvalid="true" />
<int name="key" type="uint8" defaultValue="1" validValue="1" failOnInvalid="true" fixedValue="true" />
<enum name="val" type="uint8" defaultValue="v1">
<validValue name="v1" val="5" />
<validValue name="v2" val="6" />
Expand Down
30 changes: 22 additions & 8 deletions app/commsdsl2emscripten/src/EmscriptenEnumField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,44 @@ std::string EmscriptenEnumField::emscriptenHeaderExtraPublicFuncsImpl() const
"typename std::underlying_type<ValueType>::type getValueConstant() const\n"
"{\n"
" return asConstant(getValue());\n"
"}\n\n"
"}\n"
;

if (field().dslObj().isFixedValue()) {
return Templ;
}

static const std::string SetTempl =
"\n"
"void setValueConstant(typename std::underlying_type<ValueType>::type val)\n"
"{\n"
" return setValue(static_cast<ValueType>(val));\n"
"}\n"
;
return Templ;
"}\n";

return Templ + SetTempl;
}

std::string EmscriptenEnumField::emscriptenSourceBindFuncsImpl() const
{
static const std::string Templ =
std::string templ =
".class_function(\"valueNameOf\", &#^#CLASS_NAME#$#::valueNameOf)\n"
".function(\"valueName\", &#^#CLASS_NAME#$#::valueName)\n"
".class_function(\"asConstant\", &#^#CLASS_NAME#$#::asConstant)\n"
".function(\"getValueConstant\", &#^#CLASS_NAME#$#::getValueConstant)\n"
".function(\"setValueConstant\", &#^#CLASS_NAME#$#::setValueConstant)"
".function(\"getValueConstant\", &#^#CLASS_NAME#$#::getValueConstant)"
;

if (!field().dslObj().isFixedValue()) {
templ +=
"\n"
".function(\"setValueConstant\", &#^#CLASS_NAME#$#::setValueConstant)"
;
}

util::ReplacementMap repl = {
{"CLASS_NAME", emscriptenBindClassName()}
};

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

std::string EmscriptenEnumField::emscriptenSourceBindExtraImpl() const
Expand Down
Loading

0 comments on commit ea4db42

Please sign in to comment.