Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2630,12 +2630,19 @@ private void generateConstPropertyMethods(
values);

sb.append(String.format("\n" +
indent + " #if __cplusplus >= 201402L\n" +
indent + " SBE_NODISCARD static SBE_CONSTEXPR %1$s %2$s(const std::uint64_t index) SBE_NOEXCEPT\n" +
indent + " {\n" +
indent + " const std::uint8_t %2$sValues[] = { %3$s, 0 };\n\n" +
indent + " return (char)%2$sValues[index];\n" +
indent + " }\n" +
indent + " #else\n" +
indent + " SBE_NODISCARD %1$s %2$s(const std::uint64_t index) const\n" +
indent + " {\n" +
indent + " static const std::uint8_t %2$sValues[] = { %3$s, 0 };\n\n" +

indent + " return (char)%2$sValues[index];\n" +
indent + " }\n",
indent + " }\n" +
indent + " #endif\n",
cppTypeName,
propertyName,
values));
Expand Down
2 changes: 2 additions & 0 deletions sbe-tool/src/test/c/CodeGenTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ class CodeGenTest : public testing::Test
output << static_cast<char>(CGT(car_discountedModel(&car))) << ';';

char code_buf[4];
memset(code_buf, 0, sizeof(code_buf));
CGT(engine) engine = {};
if (CGT(car_engine)(&car, &engine))
{
Expand Down Expand Up @@ -354,6 +355,7 @@ class CodeGenTest : public testing::Test
}

char code_buf[4];
memset(code_buf, 0, sizeof(code_buf));
CGT(engine) engine = {};
if (CGT(car_engine)(&car, &engine))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ void shouldUseConstexprWhenInitializingSemanticVersion() throws Exception
}
}

@Test
void shouldGenerateConstexprConstantCharAccessorOnlyForCpp14AndLater() throws Exception
{
try (InputStream in = Tests.getLocalResource("code-generation-schema.xml"))
{
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
final MessageSchema schema = parse(in, options);
final IrGenerator irg = new IrGenerator();
final Ir ir = irg.generate(schema);
final StringWriterOutputManager outputManager = new StringWriterOutputManager();
outputManager.setPackageName(ir.applicableNamespace());

final CppGenerator generator = new CppGenerator(ir, false, outputManager);
generator.generate();

final String source = outputManager.getSource("code.generation.test.Engine").toString();
assertThat(source, containsString("#if __cplusplus >= 201402L"));
assertThat(source, containsString(
"SBE_NODISCARD static SBE_CONSTEXPR char fuel(const std::uint64_t index) SBE_NOEXCEPT"));
assertThat(source, containsString("const std::uint8_t fuelValues[] = { 80, 101, 116, 114, 111, 108, 0 };"));
assertThat(source, containsString("#else"));
assertThat(source, containsString("SBE_NODISCARD char fuel(const std::uint64_t index) const"));
assertThat(source, containsString(
"static const std::uint8_t fuelValues[] = { 80, 101, 116, 114, 111, 108, 0 };"));
}
}

@Test
void dtosShouldReferenceTypesInDifferentPackages() throws Exception
{
Expand Down
Loading