Skip to content

Commit

Permalink
structure, choice tests compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 26, 2024
1 parent b95abcc commit 3a84b59
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 83 deletions.
3 changes: 2 additions & 1 deletion cmake/zserio_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ function(zserio_add_library TARGET_NAME)

# add a custom target for the generation step
add_custom_target(${TARGET_NAME}_generate
DEPENDS ${TARGET_NAME}_ALWAYS_GENERATE)
DEPENDS ${TARGET_NAME}_ALWAYS_GENERATE
SOURCES ${ALL_SOURCES})

# add to custom "gen" target
if (NOT TARGET gen)
Expand Down
76 changes: 63 additions & 13 deletions compiler/extensions/cpp/freemarker/Choice.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
m_objectChoice(allocator)
</#macro>
<#assign emptyConstructorInitMacroName><#if fieldList?has_content>empty_constructor_field_initialization</#if></#assign>
<@compound_constructor_definition compoundConstructorsData emptyConstructorInitMacroName/>
<@compound_constructor_definition compoundConstructorsData emptyConstructorInitMacroName/>

<#assign initializer=[]/>
<#if fieldList?has_content><#assign initializer=["m_objectChoice(allocator)"]/></#if>
<@compound_noinit_constructor_definition compoundConstructorsData initializer/>

</#if>
<#macro read_constructor_field_initialization packed>
m_objectChoice(readObject(<#if packed>context, </#if>in, allocator), allocator)
</#macro>
<#assign readConstructorInitMacroName><#if fieldList?has_content>read_constructor_field_initialization</#if></#assign>
<@compound_read_constructor_definition compoundConstructorsData, readConstructorInitMacroName/>
<@compound_read_constructor_definition compoundConstructorsData/>
<#if isPackable && usedInPackedArray>

<@compound_read_constructor_definition compoundConstructorsData, readConstructorInitMacroName, true/>
<@compound_read_constructor_definition compoundConstructorsData, true/>
</#if>

<#if needs_compound_initialization(compoundConstructorsData) || has_field_with_initialization(fieldList)>
Expand Down Expand Up @@ -84,10 +84,12 @@
</#list>
</#if>
</#macro>
<#-- -->
<#macro choice_no_match name indent>
<#local I>${""?left_pad(indent * 4)}</#local>
${I}throw ::zserio::CppRuntimeException("No match in choice ${name}!");
</#macro>
<#-- -->
<#macro choice_switch memberActionMacroName noMatchMacroName selectorExpression indent packed=false>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if canUseNativeSwitch>
Expand Down Expand Up @@ -133,6 +135,7 @@ ${I}}
</#if>
</#if>
</#macro>
<#-- -->
<#if withTypeInfoCode>
const ${types.typeInfo.name}& ${name}::typeInfo()
{
Expand Down Expand Up @@ -542,6 +545,53 @@ uint32_t ${name}::hashCode() const

return result;
}

<#macro choice_read_member member packed indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if fieldList?has_content>
<#if member.compoundField??>
<@compound_read_field member.compoundField, name, indent, packed/>
<#else>
${I}m_objectChoice.reset();
</#if>
</#if>
<#if canUseNativeSwitch><#rt>
${I}break;</#if>
</#macro>
<#-- -->
void ${name}::read(::zserio::BitStreamReader& in<#if parameterArgs(true)?has_content>, ${parameterArgs(true)}</#if>)
{
<#if needs_compound_initialization(compoundConstructorsData)>
m_isInitialized = true;
<#elseif has_field_with_initialization(compoundConstructorsData.fieldList)>
m_areChildrenInitialized = true;
</#if>
<#list compoundParametersData.list as param>
${param.cppName} = <#if !param.typeInfo.isSimple>&</#if>${param.name}_;
<#if !param?has_next>

</#if>
</#list>
<@choice_switch "choice_read_member", "choice_no_match", selectorExpression, 1, false/>
}
<#if usedInPackedArray && usedInPackedArray>

void ${name}::read(ZserioPackingContext& context, ::zserio::BitStreamReader& in<#if parameterArgs(true)?has_content>, ${parameterArgs(true)}</#if>)
{
<#if needs_compound_initialization(compoundConstructorsData)>
m_isInitialized = true;
<#elseif has_field_with_initialization(compoundConstructorsData.fieldList)>
m_areChildrenInitialized = true;
</#if>
<#list compoundParametersData.list as param>
${param.cppName} = <#if !param.typeInfo.isSimple>&</#if>${param.name}_;
<#if !param?has_next>

</#if>
</#list>
<@choice_switch "choice_read_member", "choice_no_match", selectorExpression, 1, true/>
}
</#if>
<#if withWriterCode>

<#macro choice_write_member member packed indent>
Expand Down Expand Up @@ -573,7 +623,7 @@ void ${name}::write(${name}::ZserioPackingContext&<#if uses_packing_context(fiel
<#if fieldList?has_content>

<@inner_classes_definition name, fieldList/>
<#macro choice_read_member member packed indent>
<#-- macro choice_read_member member packed indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if member.compoundField??>
<#if needs_field_read_local_variable(member.compoundField)>
Expand All @@ -587,18 +637,18 @@ ${I}}
${I}return ${types.anyHolder.name}(allocator);
</#if>
</#macro>
${types.anyHolder.name} ${name}::readObject(::zserio::BitStreamReader& in, const allocator_type& allocator)
/*${types.anyHolder.name} ${name}::readObject(::zserio::BitStreamReader& in, const allocator_type& allocator)
{
<@choice_switch "choice_read_member", "choice_no_match", selectorExpression, 1/>
}
}*/
<#if isPackable && usedInPackedArray>
${types.anyHolder.name} ${name}::readObject(${name}::ZserioPackingContext&<#if uses_packing_context(fieldList)> context</#if>,
/*${types.anyHolder.name} ${name}::readObject(${name}::ZserioPackingContext&<#if uses_packing_context(fieldList)> context</#if>,
::zserio::BitStreamReader& in, const allocator_type& allocator)
{
<@choice_switch "choice_read_member", "choice_no_match", selectorExpression, 1, true/>
}
</#if>
}*/
</#if-->

<#macro choice_copy_object member packed indent>
<#local I>${""?left_pad(indent * 4)}</#local>
Expand Down
26 changes: 24 additions & 2 deletions compiler/extensions/cpp/freemarker/Choice.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public:
<@compound_constructor_declaration compoundConstructorsData/>
</#if>

<@compound_noinit_constructor_declaration compoundConstructorsData/>

<@compound_read_constructor_declaration compoundConstructorsData/>
<#if isPackable && usedInPackedArray>
<#if withCodeComments>

</#if>
<@compound_read_constructor_declaration compoundConstructorsData, true/>
</#if>

Expand Down Expand Up @@ -303,6 +303,28 @@ public:
*/
</#if>
uint32_t hashCode() const;

<#if withCodeComments>
/**
* Reads zserio object from the bit stream.
*
* \param in Bit stream reader from where to read this Zserio object.
*/
</#if>
void read(::zserio::BitStreamReader& in<#if parameterArgs(true)?has_content>, ${parameterArgs(true)}</#if>);
<#if isPackable && usedInPackedArray>
<#if withCodeComments>

/**
* Reads zserio object from the bit stream.
*
* \param context Context for packed arrays.
* \param in Bit stream reader from where to read this Zserio object.
*/
</#if>
void read(ZserioPackingContext& context, ::zserio::BitStreamReader& in<#if parameterArgs(true)?has_content>, ${parameterArgs(true)}</#if>);
</#if>
<#-- -->
<#if withWriterCode>

<#if withCodeComments>
Expand Down
66 changes: 33 additions & 33 deletions compiler/extensions/cpp/freemarker/CompoundConstructor.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam
}
</#macro>

<#macro compound_noinit_constructor_declaration compoundConstructorsData>
<#if withCodeComments>
/**
* NoInit constructor to be called from read operation.
*
* \param allocator Allocator to construct from.
*/
</#if>
${compoundConstructorsData.compoundName}(::zserio::NoInitT, const allocator_type& allocator) noexcept;
</#macro>

<#macro compound_noinit_constructor_definition compoundConstructorsData initializers=noInitInitializers()>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}<#rt>
(::zserio::NoInitT, const allocator_type& allocator) noexcept<#if initializers?has_content> :</#if>
<#list initializers as init>
${init}<#if init?has_next>,</#if>
</#list>
{
}
</#macro>

<#macro compound_read_constructor_declaration compoundConstructorsData packed=false>
<#if withCodeComments>
/**
Expand All @@ -70,50 +91,29 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam
<#nt><#rt><#-- trim only newline -->
</#if>
::zserio::BitStreamReader& in<#t>
<#if constructorArgumentTypeList?has_content>
<#if parameterArgs(true)?has_content>
<#lt>,
${constructorArgumentTypeList}<#t>
${parameterArgs(true)}<#rt>
</#if>
<#lt>, const allocator_type& allocator = allocator_type());
</#macro>

<#macro compound_read_constructor_definition compoundConstructorsData memberInitializationMacroName packed=false>
<#local constructorArgumentTypeList><@compound_constructor_argument_type_list compoundConstructorsData, 2/></#local>
<#local hasInitializers=constructorArgumentTypeList?has_content ||
needs_compound_initialization(compoundConstructorsData) ||
has_field_with_initialization(compoundConstructorsData.fieldList) ||
memberInitializationMacroName != ""/>
<#macro compound_read_constructor_definition compoundConstructorsData packed=false>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#if packed>
${compoundConstructorsData.compoundName}::ZserioPackingContext& context, <#t>
</#if>
::zserio::BitStreamReader&<#if compoundConstructorsData.fieldList?has_content> in</#if><#t>
<#if constructorArgumentTypeList?has_content>
<#lt>,
${constructorArgumentTypeList}<#t>
</#if>
, const allocator_type&<#if read_constructor_needs_allocator(compoundConstructorsData.fieldList)> allocator</#if><#t>
<#if hasInitializers>
<#lt>) :
<#else>
<#lt>)
</#if>
<#if constructorArgumentTypeList?has_content>
<@compound_parameter_constructor_initializers compoundConstructorsData.compoundParametersData, 2,
needs_compound_initialization(compoundConstructorsData)/>
</#if>
<#if needs_compound_initialization(compoundConstructorsData)>
m_isInitialized(true)<#if memberInitializationMacroName != "">,</#if>
<#elseif has_field_with_initialization(compoundConstructorsData.fieldList)>
m_areChildrenInitialized(true)<#if memberInitializationMacroName != "">,</#if>
</#if>
<#if memberInitializationMacroName != "">
<#if packed>ZserioPackingContext& context, </#if><#rt>
::zserio::BitStreamReader& in, <#rt>
<#if parameterArgs(true)?has_content>${parameterArgs(true)}, </#if><#rt>
const allocator_type& allocator<#rt>
) :
${compoundConstructorsData.compoundName}(::zserio::NoInit, allocator)
<#-- #if memberInitializationMacroName != "">
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(0),
</#if>
<@.vars[memberInitializationMacroName] packed/>
</#if>
</#if-->
{
read(<#if packed>context, </#if>in<#if parameterArgs(false)?has_content>, ${parameterArgs(false)}</#if>);
}
</#macro>

Expand Down
6 changes: 4 additions & 2 deletions compiler/extensions/cpp/freemarker/CompoundField.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ ${I}if (${field.optional.clause})
${I}if (in.readBool())
</#if>
${I}{
<@compound_read_field_inner field, compoundName, indent+1, packed/>
${I} ${field.readFieldInner(packed)}
/*<@compound_read_field_inner field, compoundName, indent+1, packed/>*/
${I}}

${I}return <@field_member_type_name field/>(::zserio::NullOpt<#if field.holderNeedsAllocator>, allocator</#if>);
<#else>
<@compound_read_field_inner field, compoundName, indent, packed/>
${I}${field.readFieldInner(packed)}
/*<@compound_read_field_inner field, compoundName, indent, packed/>*/
</#if>
</#macro>

Expand Down
Loading

0 comments on commit 3a84b59

Please sign in to comment.