Skip to content

Commit

Permalink
structure data initializer, fix optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Oct 21, 2024
1 parent f27daa5 commit a7519ea
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 34 deletions.
48 changes: 32 additions & 16 deletions extension/freemarker/CompoundField.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
</#if>
</#macro>

<#macro field_data_member_type_name field>
<#if field.optional??>
::zserio::Optional<<@field_data_type_name field/>><#t>
<#else>
<@field_data_type_name field/><#t>
</#if>
</#macro>

<#macro field_data_arg_name field>
${field.name}_<#t>
</#macro>
Expand All @@ -14,25 +22,24 @@
${field.name}<#t>
</#macro>

<#macro field_view_type_name field>
<#if field.typeInfo.isSimple>
${field.typeInfo.typeFullName}<#t>
<#elseif field.typeInfo.isString>
::std::string_view<#t>
<#elseif field.typeInfo.isExtern>
const ${types.bitBuffer.name}&<#t>
<#elseif field.typeInfo.isBytes>
::zserio::Span<const uint8_t><#t>
<#else>
View<${field.typeInfo.typeFullName}><#t>
</#if>
</#macro>

<#macro field_view_getter_type_name field>
<#local typeName>
<#if field.typeInfo.isSimple>
${field.typeInfo.typeFullName}<#t>
<#elseif field.typeInfo.isString>
::std::string_view<#t>
<#elseif field.typeInfo.isExtern>
const ${types.bitBuffer.name}&<#t>
<#elseif field.typeInfo.isBytes>
::zserio::Span<const uint8_t><#t>
<#else>
View<${field.typeInfo.typeFullName}><#t>
</#if>
</#local>
<#if field.optional??>
::zserio::Optional<<@field_view_type_name field/>><#t>
::zserio::Optional<${typeName}><#t>
<#else>
<@field_view_type_name field/><#t>
${typeName}<#t>
</#if>
</#macro>

Expand All @@ -52,6 +59,15 @@
CHOICE_${field.name}<#t>
</#macro>

<#function has_optional_field fieldList>
<#list fieldList as field>
<#if field.optional??>
<#return true>
</#if>
</#list>
<#return false>
</#function>

<#function needs_allocator fieldList>
<#list fieldList as field>
<#if field.needsAllocator>
Expand Down
34 changes: 21 additions & 13 deletions extension/freemarker/Structure.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ ${name}::${name}() noexcept :
${name}::${name}(const allocator_type&<#if needs_allocator(fieldList)> allocator</#if>) noexcept<#rt>
<#list fieldList>
<#lt> :
<#items as field>
<#items as field>
<#if field.initializer??>
${field.name}(${field.initializer}<#if field.needsAllocator>, allocator</#if>)<#sep>,</#sep>
<#else>
${field.name}(<#if field.needsAllocator>allocator</#if>)<#sep>,</#sep>
</#items>
</#if>
</#items>
<#else>

</#list>
Expand Down Expand Up @@ -169,28 +173,32 @@ bool operator==(const View<${fullName}>&<#if fieldList?has_content || parameterL
</#if>
}

<#macro structure_view_field_less_than lhs rhs indent>
<#macro structure_view_field_less_than field indent>
<#local I>${""?left_pad(indent * 4)}</#local>
${I}if (${lhs} != ${rhs})
${I}if (<#if field.optional??>*</#if>lhs.${field.getterName}() != <#if field.optional??>*</#if>rhs.${field.getterName}())
${I}{
${I} return ${lhs} < ${rhs};
<#if field.typeInfo.isBoolean>
<#-- TODO[Mi-L@]: Remove once operator< for zserio::Bool is implemented in runtime! -->
${I} return static_cast<int>(<#if field.optional??>*</#if>lhs.${field.getterName}()) < <#rt>
<#lt>static_cast<int>(<#if field.optional??>*</#if>rhs.${field.getterName}());
<#else>
${I} return <#if field.optional??>*</#if>lhs.${field.getterName}() < <#if field.optional??>*</#if>rhs.${field.getterName}();
</#if>
${I}}
</#macro>
<#macro structure_view_field_less_than_optional field indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if field.optional??>
${I}if (${field.getterName}() && other.${field.getterName}())
${I}if (lhs.${field.getterName}() && rhs.${field.getterName}())
${I}{
<@structure_view_field_less_than "*lhs." + field.getterName + "()", "*rhs." + field.getterName + "()",
indent+1/>
<@structure_view_field_less_than field, indent+1/>
${I}}
${I}else if (${field.getterName}() != other.${field.getterName}())
${I}else if (lhs.${field.getterName}() != rhs.${field.getterName}())
${I}{
${I} return !${field.getterName}();
${I} return !lhs.${field.getterName}();
${I}}
<#else>
<@structure_view_field_less_than "lhs." + field.getterName + "()", "rhs." + field.getterName + "()",
indent/>
<@structure_view_field_less_than field indent/>
</#if>
</#macro>
bool operator<(const View<${fullName}>&<#if fieldList?has_content || parameterList?has_content> lhs</#if>, <#rt>
Expand Down Expand Up @@ -271,7 +279,7 @@ ${I}in.alignTo(${field.alignmentValue});
${I}in.alignTo(8);
</#if>
<#local compoundArguments><@field_view_parameters field/></#local>
${I}(void)::zserio::detail::read(reader, data.<@field_data_member_name field/><#rt>
${I}(void)::zserio::detail::read(reader, <#if field.optional??>*</#if>data.<@field_data_member_name field/><#rt>
<#lt><#if compoundArguments?has_content>, ${compoundArguments}</#if>);
</#macro>
View<${fullName}> read(::zserio::BitStreamReader&<#if fieldList?has_content> reader</#if>, <#rt>
Expand Down
5 changes: 4 additions & 1 deletion extension/freemarker/Structure.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include <memory>
#include <zserio/View.h>
<#if has_optional_field(fieldList)>
#include <zserio/Optional.h>
</#if>
<@system_includes headerSystemIncludes/>
<@user_includes headerUserIncludes/>
<@namespace_begin package.path/>
Expand All @@ -32,7 +35,7 @@ struct ${name}
</#list>

<#list fieldList as field>
<@field_data_type_name field/> <@field_data_member_name field/>;
<@field_data_member_type_name field/> <@field_data_member_name field/>;
</#list>
};

Expand Down
4 changes: 2 additions & 2 deletions runtime/src/zserio/Bitmasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace detail
{

template <typename T>
std::enable_if_t<is_bitmask_v<T>, BitSize> bitSizeOf(T value)
std::enable_if_t<is_bitmask_v<T>, BitSize> bitSizeOf(T value, BitSize bitPosition = 0)
{
return bitSizeOf(value.getValue());
return bitSizeOf(value.getValue(), bitPosition);
}

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/zserio/Enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ namespace detail
{

template <typename T>
inline std::enable_if_t<std::is_enum_v<T>, BitSize> bitSizeOf(T value)
inline std::enable_if_t<std::is_enum_v<T>, BitSize> bitSizeOf(T value, BitSize bitPosition = 0)
{
return bitSizeOf(enumToValue(value));
return bitSizeOf(enumToValue(value), bitPosition);
}

template <typename T>
Expand Down
27 changes: 27 additions & 0 deletions runtime/src/zserio/HashCodeUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ inline uint32_t calcHashCode(uint32_t seedValue, detail::IntWrapper<VALUE_TYPE,
return calcHashCode(seedValue, static_cast<VALUE_TYPE>(value));
}

/**
* Calculates hash code of the given bool wrapper value using the given seed value.
*
* \param seedValue Seed value (current hash code).
* \param value Value for which to calculate the hash code.
*
* \return Calculated hash code.
*/
inline uint32_t calcHashCode(uint32_t seedValue, Bool value)
{
return calcHashCode(seedValue, static_cast<Bool::value_type>(value));
}

/**
* Calculates hash code of the given floating-point wrapper value using the given seed value.
*
* \param seedValue Seed value (current hash code).
* \param value Value for which to calculate the hash code.
*
* \return Calculated hash code.
*/
template <typename VALUE_TYPE, detail::FloatType FLOAT_TYPE>
inline uint32_t calcHashCode(uint32_t seedValue, detail::FloatWrapper<VALUE_TYPE, FLOAT_TYPE> value)
{
return calcHashCode(seedValue, static_cast<VALUE_TYPE>(value));
}

// TODO[Mi-L@]: support all zserio wrappers

/**
Expand Down
24 changes: 24 additions & 0 deletions test/language/default_values/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
add_library(default_values_zs STATIC ${TEST_ZS_ROOT}/default_values.zs)
zserio_generate_cpp(
TARGET default_values_zs
SRC_DIR ${TEST_ZS_ROOT}
GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen
EXTRA_ARGS ${ZSERIO_EXTRA_ARGS}
GENERATED_SOURCES_VAR GENERATED_SOURCES
OUTPUT_VAR ZSERIO_LOG
ERROR_VAR ZSERIO_LOG
)
target_link_libraries(default_values_zs PUBLIC ZserioCpp17Runtime)
if (ZSERIO_LOG)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log.txt "${ZSERIO_LOG}")
check_zserio_warnings("${ZSERIO_LOG}" 0)
endif ()

add_custom_test(default_values
DEPENDS
default_values_zs
SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cpp/StructureDefaultValuesTest.cpp
GENERATED_SOURCES
${GENERATED_SOURCES}
)
9 changes: 9 additions & 0 deletions test/language/default_values/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
google-explicit-constructor:gen/default_values/structure_default_values/Permission.h

#hicpp-signed-bitwise:gen/default_values/structure_default_values/Permission.h

#readability-make-member-function-const:gen/default_values/structure_default_values/StructureDefaultValues.cpp

readability-uppercase-literal-suffix:gen/default_values/structure_default_values/StructureDefaultValues.cpp

readability-simplify-boolean-expr:gen/default_values/structure_default_values/StructureDefaultValues.cpp
98 changes: 98 additions & 0 deletions test/language/default_values/cpp/StructureDefaultValuesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "default_values/structure_default_values/BasicColor.h"
#include "default_values/structure_default_values/Permission.h"
#include "default_values/structure_default_values/StructureDefaultValues.h"
#include "gtest/gtest.h"
#include "zserio/BitStreamReader.h"
#include "zserio/CppRuntimeException.h"

namespace default_values
{
namespace structure_default_values
{

TEST(StructureDefaultValuesDataTest, checkDefaultBoolValue)
{
StructureDefaultValues structureDefaultValues;
ASSERT_EQ(true, structureDefaultValues.boolValue);
}

TEST(StructureDefaultValuesDataTest, checkDefaultBit4Value)
{
StructureDefaultValues structureDefaultValues;
ASSERT_EQ(0x0F, structureDefaultValues.bit4Value);
}

TEST(StructureDefaultValuesDataTest, checkDefaultInt16Value)
{
StructureDefaultValues structureDefaultValues;
ASSERT_EQ(0x0BEE, structureDefaultValues.int16Value);
}

TEST(StructureDefaultValuesDataTest, checkDefaultFloat16Value)
{
StructureDefaultValues structureDefaultValues;
float diff = 1.23F - structureDefaultValues.float16Value;
if (diff < 0.0F)
{
diff = -diff;
}
ASSERT_TRUE(diff <= std::numeric_limits<float>::epsilon());
}

TEST(StructureDefaultValuesDataTest, checkDefaultFloat32Value)
{
StructureDefaultValues structureDefaultValues;
float diff = 1.234F - structureDefaultValues.float32Value;
if (diff < 0.0F)
{
diff = -diff;
}
ASSERT_TRUE(diff <= std::numeric_limits<float>::epsilon());
}

TEST(StructureDefaultValuesDataTest, checkDefaultFloat64Value)
{
StructureDefaultValues structureDefaultValues;
double diff = 1.2345 - structureDefaultValues.float64Value;
if (diff < 0.0)
{
diff = -diff;
}
ASSERT_TRUE(diff <= std::numeric_limits<double>::epsilon());
}

TEST(StructureDefaultValuesDataTest, checkDefaultStringValue)
{
StructureDefaultValues structureDefaultValues;
ASSERT_EQ("string", structureDefaultValues.stringValue);
}

TEST(StructureDefaultValuesDataTest, checkDefaultEnumValue)
{
StructureDefaultValues structureDefaultValues;
ASSERT_EQ(BasicColor::BLACK, structureDefaultValues.enumValue);
}

TEST(StructureDefaultValuesDataTest, checkDefaultBitmaskValue)
{
StructureDefaultValues structureDefaultValues;
ASSERT_EQ(Permission::Values::READ_WRITE, structureDefaultValues.bitmaskValue);
}

TEST(StructureDefaultValuesDataTest, operatorEquality)
{
StructureDefaultValues structureDefaultValues1;
StructureDefaultValues structureDefaultValues2;
ASSERT_EQ(structureDefaultValues1, structureDefaultValues2);
}

TEST(StructureDefaultValuesDataTest, operatorLessThan)
{
StructureDefaultValues structureDefaultValues1;
StructureDefaultValues structureDefaultValues2;
ASSERT_FALSE(structureDefaultValues1 < structureDefaultValues2);
ASSERT_FALSE(structureDefaultValues2 < structureDefaultValues1);
}

} // namespace structure_default_values
} // namespace default_values

0 comments on commit a7519ea

Please sign in to comment.