<#--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<#-- Prevent freemarker from escaping stuff -->
<#outputformat "undefined">
<#-- Declare the name and type of variables passed in to the template -->
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.c.CLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.DataIoTypeDefinition" -->
${helper.getSourceDirectory()?replace(".", "/")}/${helper.camelCaseToSnakeCase(type.name)}.c
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <plc4c/data.h>
#include <plc4c/utils/list.h>
#include <plc4c/spi/evaluation_helper.h>
#include <plc4c/spi/types_private.h>
#include <plc4c/driver_${protocolName}_static.h>

#include "${helper.camelCaseToSnakeCase(type.name)}.h"

// Code generated by code-generation. DO NOT EDIT.

<#-- TODO: the code below implies that parserArguments will be null if not present... not pretty  -->
<#if type.parserArguments.isPresent()><#assign parserArguments=type.parserArguments.orElseThrow()></#if>
// Parse function.
plc4c_return_code ${helper.getCTypeName(type.name)}_parse(plc4c_spi_read_buffer* readBuffer, <#if parserArguments?has_content><#list parserArguments as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#if !parserArgument.type.isSimpleTypeReference() && !parserArgument.type.isEnumTypeReference()>*</#if> ${parserArgument.name}<#sep>, </#list>, </#if>plc4c_data** data_item) {
    uint16_t startPos = plc4c_spi_read_get_pos(readBuffer);
    uint16_t curPos;
    plc4c_return_code _res = OK;

    <#list type.switchField.orElseThrow().cases as case>
        <#if case.discriminatorValueTerms?has_content>if(<#list case.discriminatorValueTerms as discriminatorValueTerm><#if case.discriminatorValueTerms?size &gt; 1>(</#if><#if helper.discriminatorValueNeedsStringEqualityCheck(type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index])>strcmp(${helper.toParseExpression(type, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)}, ${helper.toParseExpression(null, null, discriminatorValueTerm, parserArguments)}) == 0<#else>${helper.toParseExpression(type, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} == <#if parserArguments[discriminatorValueTerm?index].type.isNonSimpleTypeReference()><#if parserArguments[discriminatorValueTerm?index].type.isEnumTypeReference()>${helper.getLanguageTypeNameForTypeReference(parserArguments[discriminatorValueTerm?index].type)}_${discriminatorValueTerm.asLiteral().orElseThrow().asVariableLiteral().orElseThrow().name}<#else>${helper.toParseExpression(type, null, discriminatorValueTerm, parserArguments)}</#if><#else>${helper.toParseExpression(type, null, discriminatorValueTerm, parserArguments)}</#if></#if><#if case.discriminatorValueTerms?size &gt; 1>)</#if><#sep> && </#sep></#list>) </#if>{ /* ${case.name} */
        <#assign skipReturn=false>
        <#list case.fields as field>
            <#switch field.typeName>
                <#case "array">
                    <#assign arrayField = field.asArrayField().orElseThrow()>
                    <#assign elementTypeReference=arrayField.type.elementTypeReference>

        // Array field (${arrayField.name})
                <#-- Only update curPos if the length expression uses it -->
                    <#if arrayField.loopExpression.contains("curPos")>
        curPos = plc4c_spi_read_get_pos(readBuffer) - startPos;
                    </#if>
                <#-- If this is a count array, we can directly initialize an array with the given size -->
                    <#if arrayField.isCountArrayField()>
        // Count array
        plc4c_list* ${arrayField.name};
        plc4c_utils_list_create(&${arrayField.name});
        int itemCount = (int) ${helper.toParseExpression(type, arrayField, arrayField.loopExpression, parserArguments)};
        for(int curItem = 0; curItem < itemCount; curItem++) {
            ${helper.getLanguageTypeNameForTypeReference(elementTypeReference)}* _val = malloc(sizeof(${helper.getLanguageTypeNameForTypeReference(elementTypeReference)}) * 1);
            _res = <#if elementTypeReference.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), "_val", arrayField)}<#else>${elementTypeReference.asNonSimpleTypeReference().orElseThrow().name}IO.staticParse(readBuffer<#if arrayField.params.isPresent()>, <#list arrayField.params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(elementTypeReference, parserArgument?index))}) (${helper.toParseExpression(type, arrayField, parserArgument, parserArguments)})<#sep>, </#sep></#list></#if>)</#if>;
            if(_res != OK) {
                return _res;
            }
            plc4c_data* _item = plc4c_data_create_${helper.getDataIoTypeNameForTypeReference(elementTypeReference)}_data(*_val);
            plc4c_utils_list_insert_head_value(${arrayField.name}, _item);
        }
                    <#-- In all other cases do we have to work with a list, that is later converted to an array -->
                    <#else>
                    <#-- For a length array, we read data till the read position of the buffer reaches a given position -->
                        <#if arrayField.isLengthArrayField()>
        // Length array
        int _${arrayField.name}Length = ${helper.toParseExpression(type, arrayField, arrayField.loopExpression, parserArguments)};
        plc4c_list* ${arrayField.name};
        plc4c_utils_list_create(&${arrayField.name});
        int ${arrayField.name}EndPos = plc4c_spi_read_get_pos(readBuffer) + _${arrayField.name}Length;
        while(plc4c_spi_read_get_pos(readBuffer) < ${arrayField.name}EndPos) {
            plc4c_data* _item =<#if elementTypeReference.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), null, arrayField)}<#else>${elementTypeReference.asNonSimpleTypeReference().orElseThrow().name}IO.staticParse(readBuffer<#if field.params.isPresent()>, <#list field.params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(elementTypeReference, parserArgument?index))}) (${helper.toParseExpression(type, arrayField, parserArgument, parserArguments)})<#sep>, </#sep></#list></#if>)</#if>;
            plc4c_utils_list_insert_head_value(${arrayField.name}, _item);
                        <#-- After parsing, update the current position, but only if it's needed -->
                            <#if arrayField.loopExpression.contains("curPos")>
            curPos = plc4c_spi_read_get_pos(readBuffer) - startPos;
                            </#if>
        }
                        <#-- A terminated array keeps on reading data as long as the termination expression evaluates to false -->
                        <#elseif arrayField.isTerminatedArrayField()>
        // Terminated array
        plc4c_list* ${arrayField.name};
        plc4c_utils_list_create(&${arrayField.name});
        while(!((boolean) (${helper.toParseExpression(type, arrayField, arrayField.loopExpression, parserArguments)}))) {
            plc4c_data* _item =<#if elementTypeReference.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), null, arrayField)}<#else>${elementTypeReference.asNonSimpleTypeReference().orElseThrow().name}IO.staticParse(readBuffer<#if field.params.isPresent()>, <#list field.params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(elementTypeReference, parserArgument?index))}) (${helper.toParseExpression(type, arrayField, parserArgument, parserArguments)})<#sep>, </#sep></#list></#if>)</#if>;
            plc4c_utils_list_insert_head_value(${arrayField.name}, _item);

                        <#-- After parsing, update the current position, but only if it's needed -->
                            <#if arrayField.loopExpression.contains("curPos")>
            curPos = plc4c_spi_read_get_pos(readBuffer) - startPos;
                            </#if>
        }
                        </#if>
                    <#--
                        Convert the list into an array. However if the array is of a primitive
                        type we have to iterate over it's elements and explicitly cast them.
                        Otherwise a simple toArray call is fine.
                    -->
                        <#--if elementTypeReference.isSimpleTypeReference()>
        ${helper.getLanguageTypeNameForField(arrayField)}[] ${arrayField.name} = malloc(sizeof(${helper.getLanguageTypeNameForField(arrayField)}) * _${arrayField.name}List.size()];
        for(int i = 0; i < _${arrayField.name}List.size(); i++) {
            ${arrayField.name}[i] = (${helper.getLanguageTypeNameForField(arrayField)}) _${arrayField.name}List.get(i);
        }
                        <#else>
        ${helper.getLanguageTypeNameForField(arrayField)}[] ${arrayField.name} = _${arrayField.name}List.toArray(new ${helper.getLanguageTypeNameForField(arrayField)}[0]);
                        </#if-->
                    </#if>
        *data_item = plc4c_data_create_list_data(${arrayField.name});

                    <#break>
                <#case "const">
                    <#assign constField =  field.asConstField().orElseThrow()>

                // Const Field (${constField.name})
                ${helper.getLanguageTypeNameForField(constField)} ${constField.name} = ${helper.getReadBufferReadMethodCall(constField.type.asSimpleTypeReference().orElseThrow())};
                if(${constField.name} != ${type.name}.${constField.name?upper_case}) {
                    throw new ParseException("Expected constant value " + ${type.name}.${constField.name?upper_case} + " but got " + ${constField.name});
                }
                    <#break>
                <#case "enum">
                    <#assign enumField =  field.asEnumField().orElseThrow()>

                // Enum field (${enumField.name})
                ${helper.getLanguageTypeNameForField(enumField)} ${enumField.name} = ${helper.getLanguageTypeNameForField(enumField)}.valueOf(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(enumField.type))});
                    <#break>
                <#case "manual">
                    <#assign manualField =  field.asManualField().orElseThrow()>

                // Manual Field (${manualField.name})
                ${helper.getLanguageTypeNameForField(manualField)} ${manualField.name} = (${helper.getLanguageTypeNameForField(manualField)}) (${helper.toParseExpression(type, manualField, manualField.parseExpression, parserArguments)});

                    <#switch case.name>
                        <#--#case "Time">
                return new PlcTime(${field.name});
                            <#break>
                        <#case "Date">
                return new PlcDate(${field.name});
                            <#break>
                        <#case "DateTime">
                return new PlcDateTime(${field.name});
                            <#break>
                        <#case "Struct">
                return new PlcStruct(${field.name});
                            <#break-->
                        <#case "STRING">
                *data_item = plc4c_data_create_string_data(stringLength, ${manualField.name});
                            <#break>
                        <#default>
                *data_item = plc4c_data_create_${case.name?lower_case}_data(${manualField.name});
                    </#switch>
                    <#assign skipReturn=true>

                    <#break>
                <#case "reserved">
                    <#assign reservedField=field.asReservedField().orElseThrow()>

                // Reserved Field (Compartmentalized so the "reserved" variable can't leak)
                {
                    ${helper.getLanguageTypeNameForField(reservedField)} _reserved = ${helper.getNullValueForTypeReference(reservedField.type)};
                    _res = ${helper.getReadBufferReadMethodCall(reservedField.type.asSimpleTypeReference().orElseThrow(), "&_reserved", reservedField)};
                    if(_res != OK) {
                        return _res;
                    }
                    if(_reserved != ${reservedField.referenceValue}) {
                      printf("Expected constant value '%d' but got '%d' for reserved field.", ${reservedField.referenceValue}, _reserved);
                    }
                }
                    <#break>
                <#case "simple">
                    <#assign simpleField=field.asSimpleField().orElseThrow()>

                // Simple Field (${simpleField.name})
                <#-- Inizialize a local variable with the simple type (Intentionally keeping the java-style names so they can be used in expressions) -->
                <#if simpleField.type.isSimpleTypeReference()>
                ${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = ${helper.getNullValueForTypeReference(simpleField.type)};
                _res = ${helper.getReadBufferReadMethodCall(simpleField.type.asSimpleTypeReference().orElseThrow(), "&" + simpleField.name, simpleField)};
                <#else>
                <#-- Inizialize a local variable with the complex type (Intentionally keeping the java-style names so they can be used in expressions) -->
                ${helper.getLanguageTypeNameForField(simpleField)}* ${simpleField.name};
                _res = ${helper.getCTypeName(simpleField.type.asNonSimpleTypeReference().orElseThrow().name)}_parse(readBuffer<#if simpleField.params.isPresent()>, <#list simpleField.params.orElseThrow() as parserTerm>${helper.toParseExpression(type, simpleField, parserTerm, parserArguments)}<#sep>, </#sep></#list></#if>, (void*) &${simpleField.name});
                </#if>
                if(_res != OK) {
                    return _res;
                }

                <#if case.name == "STRING">
                    <#if simpleField.type.isVstringTypeReference()>
                *data_item = plc4c_data_create_string_data(stringLength, ${simpleField.name});
                    <#else>
                *data_item = plc4c_data_create_string_data(${simpleField.type.asStringTypeReference().orElseThrow().sizeInBits}, ${simpleField.name});
                    </#if>
                <#else>
                *data_item = plc4c_data_create_${case.name?lower_case}_data(${simpleField.name});
                </#if>

                    <#break>
            </#switch>
        </#list>
    }<#sep> else </#sep></#list>

  return OK;
}

plc4c_return_code ${helper.getCTypeName(type.name)}_serialize(plc4c_spi_write_buffer* writeBuffer, <#if parserArguments?has_content><#list parserArguments as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#if !parserArgument.type.isSimpleTypeReference() && !parserArgument.type.isEnumTypeReference()>*</#if> ${parserArgument.name}<#sep>, </#list>, </#if>plc4c_data** data_item) {
  plc4c_return_code _res = OK;
    <#list type.switchField.orElseThrow().cases as case>
        <#if case.discriminatorValueTerms?has_content>if(<#list case.discriminatorValueTerms as discriminatorValueTerm><#if case.discriminatorValueTerms?size &gt; 1>(</#if><#if helper.discriminatorValueNeedsStringEqualityCheck(type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index])>strcmp(${helper.toParseExpression(type, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)}, ${helper.toParseExpression(null, null, discriminatorValueTerm, parserArguments)}) == 0<#else>${helper.toParseExpression(type, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} == <#if parserArguments[discriminatorValueTerm?index].type.isNonSimpleTypeReference()><#if parserArguments[discriminatorValueTerm?index].type.isEnumTypeReference()>${helper.getLanguageTypeNameForTypeReference(parserArguments[discriminatorValueTerm?index].type)}_${discriminatorValueTerm.asLiteral().orElseThrow().asVariableLiteral().orElseThrow().name}<#else>${helper.toParseExpression(type, null, discriminatorValueTerm, parserArguments)}</#if><#else>${helper.toParseExpression(type, null, discriminatorValueTerm, parserArguments)}</#if></#if><#if case.discriminatorValueTerms?size &gt; 1>)</#if><#sep> && </#sep></#list>) </#if>{ /* ${case.name} */
        <#list case.fields as field>
            <#switch field.typeName>
                <#case "array">
                    <#assign arrayField = field.asArrayField().orElseThrow()>
                    <#assign elementTypeReference=arrayField.type.elementTypeReference>

                    // Array field
                    <#break>
                <#case "checksum">
                    <#assign checksumField = field.asChecksumField().orElseThrow()>
                    <#assign simpleTypeReference = checksumField.type>

                    // Checksum Field (checksum)
                    <#break>
                <#case "const">
                    <#assign constField =  field.asConstField().orElseThrow()>
                    <#assign simpleTypeReference = constField.type.asSimpleTypeReference().orElseThrow()>

                    // Const Field (${constField.name})
                    <#break>
                <#case "discriminator">
                    <#assign discriminatorField = field.asDiscriminatorField().orElseThrow()>

                    // Discriminator Field (${discriminatorField.name})
                    <#break>
                <#case "enum">
                    <#assign enumField =  field.asEnumField().orElseThrow()>

                    // Enum Field (${enumField.name})
                    <#break>
                <#case "implicit">
                    <#assign implicitField = field.asImplicitField().orElseThrow()>
                    <#assign simpleTypeReference = implicitField.type.asSimpleTypeReference().orElseThrow()>

                    // Implicit Field (${implicitField.name})
                    <#break>
                <#case "manualArray">
                    <#assign manualArrayField = field.asManualArrayField().orElseThrow()>

                    // Manual Array Field (${manualArrayField.name})
                    <#break>
                <#case "manual">
                    <#assign manualField =  field.asManualField().orElseThrow()>

                    // Manual Field (${manualField.name})
                    <#break>
                <#case "optional">
                    <#assign optionalField = field.asOptionalField().orElseThrow()>

                    // Optional Field (${optionalField.name})
                    <#break>
                <#case "padding">
                    <#assign paddingField = field.asPaddingField().orElseThrow()>
                    <#assign simpleTypeReference = paddingField.type.asSimpleTypeReference().orElseThrow()>

                    // Padding Field (padding)
                    <#break>
                <#case "reserved">
                    <#assign reservedField = field.asReservedField().orElseThrow()>
                    <#assign simpleTypeReference = reservedField.type.asSimpleTypeReference().orElseThrow()>

                    // Reserved Field (reserved)
                    <#break>
                <#case "simple">
                    <#assign simpleField = field.asSimpleField().orElseThrow()>
                    <#assign simpleTypeReference = simpleField.type.asSimpleTypeReference().orElseThrow()>

                    // Simple field (${simpleField.name})
                    _res = ${helper.getWriteBufferWriteMethodCall(simpleTypeReference, "(*data_item)->data." + case.name?lower_case + "_value", simpleField)};
                    if(_res != OK) {
                        return _res;
                    }
                    <#break>
                <#case "virtual">
                    <#assign virtualField = field.asVirtualField().orElseThrow()>

                    // Virtual field (${simpleField.name})
                    <#break>
            </#switch>
        </#list>
        }<#sep> else </#sep></#list>
  return OK;
}

uint16_t ${helper.getCTypeName(type.name)}_length_in_bytes(plc4c_data* data_item<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)} ${helper.camelCaseToSnakeCase(parserArgument.name)}<#sep>, </#sep></#list></#if>) {
  return ${helper.getCTypeName(type.name)}_length_in_bits(data_item<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.camelCaseToSnakeCase(parserArgument.name)}<#sep>, </#sep></#list></#if>) / 8;
}

uint16_t ${helper.getCTypeName(type.name)}_length_in_bits(plc4c_data* data_item<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#if !parserArgument.type.isSimpleTypeReference() && !parserArgument.type.isEnumTypeReference()>*</#if> ${parserArgument.name}<#sep>, </#list></#if>) {
  uint16_t lengthInBits = 0;
    <#list type.switchField.orElseThrow().cases as case>
    <#if case.discriminatorValueTerms?has_content>if(<#list case.discriminatorValueTerms as discriminatorValueTerm><#if case.discriminatorValueTerms?size &gt; 1>(</#if><#if helper.discriminatorValueNeedsStringEqualityCheck(type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index])>strcmp(${helper.toParseExpression(type, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)}, ${helper.toParseExpression(null, null, discriminatorValueTerm, parserArguments)}) == 0<#else>${helper.toParseExpression(type, null, type.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} == <#if parserArguments[discriminatorValueTerm?index].type.isNonSimpleTypeReference()><#if parserArguments[discriminatorValueTerm?index].type.isEnumTypeReference()>${helper.getLanguageTypeNameForTypeReference(parserArguments[discriminatorValueTerm?index].type)}_${discriminatorValueTerm.asLiteral().orElseThrow().asVariableLiteral().orElseThrow().name}<#else>${helper.toParseExpression(type, null, discriminatorValueTerm, parserArguments)}</#if><#else>${helper.toParseExpression(type, null, discriminatorValueTerm, parserArguments)}</#if></#if><#if case.discriminatorValueTerms?size &gt; 1>)</#if><#sep> && </#sep></#list>) </#if>{ /* ${case.name} */
        <#list case.fields as field>
            <#switch field.typeName>
                <#case "array">
                    <#assign arrayField = field.asArrayField().orElseThrow()>
                    <#assign elementTypeReference=arrayField.type.elementTypeReference>

        // Array field
                    <#if elementTypeReference.isSimpleTypeReference()>
                        <#assign simpleTypeReference = elementTypeReference.asSimpleTypeReference().orElseThrow()>
        lengthInBits += ${simpleTypeReference.sizeInBits} * plc4c_utils_list_size(data_item->data.list_value);
                    <#else>
        if(data_item->data.list_value != NULL) {
            plc4c_list_element* curElement = data_item->data.list_value->tail;
            while (curElement != NULL) {
                lengthInBits += ${helper.getLengthInBitsFunctionNameForComplexTypedField(arrayField)}((${helper.getLanguageTypeNameForTypeReference(elementTypeReference)}*) curElement->value);
                curElement = curElement->next;
            }
        }
                    </#if>
                    <#break>
                <#case "checksum">
                    <#assign checksumField = field.asChecksumField().orElseThrow()>
                    <#assign simpleTypeReference = checksumField.type>

        // Checksum Field (checksum)
        lengthInBits += ${simpleTypeReference.sizeInBits};
                    <#break>
                <#case "const">
                    <#assign constField =  field.asConstField().orElseThrow()>
                    <#assign simpleTypeReference = constField.type.asSimpleTypeReference().orElseThrow()>

        // Const Field (${constField.name})
        lengthInBits += ${simpleTypeReference.sizeInBits};
                    <#break>
                <#case "discriminator">
                    <#assign discriminatorField = field.asDiscriminatorField().orElseThrow()>

        // Discriminator Field (${discriminatorField.name})
                    <#if discriminatorField.type.isSimpleTypeReference()>
                        <#assign simpleTypeReference = discriminatorField.type.asSimpleTypeReference().orElseThrow()>
                        <#if simpleTypeReference.isVstringTypeReference()>
                            <#assign vstringTypeReference = simpleTypeReference.asVstringTypeReference().orElseThrow()>
        lengthInBits +=  ${helper.toParseExpression(type, discriminatorField, vstringTypeReference.lengthExpression, parserArguments)};
                        <#else>
        lengthInBits += ${simpleTypeReference.sizeInBits};
                        </#if>
                    <#elseif helper.isEnumField(field)>
                        <#assign enumTypeReference = discriminatorField.type.asEnumTypeReference().orElseThrow()>
        lengthInBits += ${enumTypeReference.baseTypeReference.orElseThrow().sizeInBits};
                    <#else>
        lengthInBits += ${discriminatorField.name}.getLengthInBits();
                    </#if>
                    <#break>
                <#case "enum">
                    <#assign enumField =  field.asEnumField().orElseThrow()>

        // Enum Field (${enumField.name})
        lengthInBits += ${helper.getEnumBaseTypeReference(enumField.type).sizeInBits};
                <#break>
                <#case "implicit">
                    <#assign implicitField = field.asImplicitField().orElseThrow()>
                    <#assign simpleTypeReference = implicitField.type.asSimpleTypeReference().orElseThrow()>

        // Implicit Field (${implicitField.name})
        lengthInBits += ${simpleTypeReference.sizeInBits};
                    <#break>
                <#case "manualArray">
                    <#assign manualArrayField = field.asManualArrayField().orElseThrow()>

        // Manual Array Field (${manualArrayField.name})
        lengthInBits += ${helper.toSerializationExpression(type, manualArrayField, manualArrayField.lengthExpression, parserArguments)};
                    <#break>
                <#case "manual">
                    <#assign manualField =  field.asManualField().orElseThrow()>

        // Manual Field (${manualField.name})
        <#-- if the expression uses the "_value", we need to locally define it so we can access it in the expression -->
        <#if manualField.lengthExpression.contains("_value")>
        {
            ${helper.getLanguageTypeNameForField(manualField)} _value = data_item->data.${case.name?lower_case}_value;
            lengthInBits += ${helper.toSerializationExpression(type, manualField, manualField.lengthExpression, parserArguments)};
        }
        <#else>
        lengthInBits += ${helper.toSerializationExpression(type, manualField, manualField.lengthExpression, parserArguments)};
        </#if>
                    <#break>
                <#case "optional">
                    <#assign optionalField = field.asOptionalField().orElseThrow()>

        // Optional Field (${optionalField.name})
        if(data_item->${helper.getFieldName(type, optionalField)} != NULL) {
                    <#if optionalField.type.isSimpleTypeReference()>
                        <#assign simpleTypeReference = optionalField.type.asSimpleTypeReference().orElseThrow()>
            lengthInBits += ${simpleTypeReference.sizeInBits};
                    <#else>
            lengthInBits += ${helper.getLengthInBitsFunctionNameForComplexTypedField(optionalField)}(data_item->${helper.getFieldName(type, optionalField)});
                    </#if>
        }
                    <#break>
                <#case "padding">
                    <#assign paddingField = field.asPaddingField().orElseThrow()>
                    <#assign simpleTypeReference = paddingField.type.asSimpleTypeReference().orElseThrow()>

        // Padding Field (padding)
                <#-- We're replacing the "lastItem" with 'false' here as the item itself can't know if it is the last -->
        int _needsPadding = (int) (${helper.toSerializationExpression(type, paddingField, paddingField.paddingCondition, parserArguments)?replace("lastItem", "false")});
        while(_needsPadding-- > 0) {
            lengthInBits += ${simpleTypeReference.sizeInBits};
        }
                    <#break>
                <#case "reserved">
                    <#assign reservedField = field.asReservedField().orElseThrow()>
                    <#assign simpleTypeReference = reservedField.type.asSimpleTypeReference().orElseThrow()>

        // Reserved Field (reserved)
        lengthInBits += ${simpleTypeReference.sizeInBits};
                    <#break>
                <#case "simple">
                    <#assign simpleField = field.asSimpleField().orElseThrow()>

        // Simple field (${simpleField.name})
                    <#if simpleField.type.isSimpleTypeReference()>
                        <#assign simpleTypeReference = simpleField.type.asSimpleTypeReference().orElseThrow()>
                        <#if simpleTypeReference.isVstringTypeReference()>
                            <#assign vstringTypeReference = simpleTypeReference.asVstringTypeReference().orElseThrow()>
        lengthInBits +=  ${helper.toSerializationExpression(type, simpleField, vstringTypeReference.lengthExpression, parserArguments)};
                        <#else>
        lengthInBits += ${simpleTypeReference.sizeInBits};
                        </#if>
                    <#else>
        lengthInBits += ${helper.getLengthInBitsFunctionNameForComplexTypedField(simpleField)}(<#if simpleField.type.isEnumTypeReference()>&</#if>data_item->${helper.getFieldName(type, simpleField)});
                    </#if>
                    <#break>
                <#case "virtual">
                    <#assign virtualField = field.asVirtualField().orElseThrow()>

        // A virtual field doesn't have any in- or output.
                    <#break>
            </#switch>
        </#list>
    }<#sep> else </#sep></#list>
  return lengthInBits;
}

</#outputformat>