Skip to content

Commit

Permalink
[C++] Generate DTOs for non-perf-sensitive usecases.
Browse files Browse the repository at this point in the history
In some applications performance is not cricital. Some users would like
to use SBE across their whole "estate", but don't want the "sharp edges"
associated with using flyweight codecs, e.g., accidental escape.

In this commit, I've added a first cut of DTO generation for C++ and a
simple test based on the Car Example.

The DTOs support encoding and decoding via the generated codecs using
`DtoT::encode(CodecT& codec, const DtoT& dto)` and
`DtoT::decode(CodecT& codec, Dto& dto)` methods.

Generation can be enabled specifying the target code generator class,
`uk.co.real_logic.sbe.generation.cpp.CppDtos`, or by passing a
system property `-Dsbe.cpp.generate.dtos=true`.
  • Loading branch information
ZachBray committed Nov 2, 2023
1 parent a14edd4 commit 1a9385d
Show file tree
Hide file tree
Showing 9 changed files with 2,249 additions and 62 deletions.
5 changes: 5 additions & 0 deletions sbe-tool/src/main/java/uk/co/real_logic/sbe/SbeTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ public class SbeTool
*/
public static final String DECODE_UNKNOWN_ENUM_VALUES = "sbe.decode.unknown.enum.values";

/**
* Should generate C++ DTOs. Defaults to false.
*/
public static final String GENERATE_CPP_DTOS = "sbe.cpp.generate.dtos";

/**
* Configuration option used to manage sinceVersion based transformations. When set, parsed schemas will be
* transformed to discard messages and types higher than the specified version. This can be useful when needing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package uk.co.real_logic.sbe.generation;

import uk.co.real_logic.sbe.generation.cpp.CppDtoGenerator;
import uk.co.real_logic.sbe.generation.java.JavaOutputManager;
import uk.co.real_logic.sbe.generation.c.CGenerator;
import uk.co.real_logic.sbe.generation.c.COutputManager;
Expand Down Expand Up @@ -81,10 +82,20 @@ public CodeGenerator newInstance(final Ir ir, final String outputDir)
*/
public CodeGenerator newInstance(final Ir ir, final String outputDir)
{
return new CppGenerator(
ir,
"true".equals(System.getProperty(DECODE_UNKNOWN_ENUM_VALUES)),
new NamespaceOutputManager(outputDir, ir.applicableNamespace()));
final NamespaceOutputManager outputManager = new NamespaceOutputManager(
outputDir, ir.applicableNamespace());
final boolean decodeUnknownEnumValues = "true".equals(System.getProperty(DECODE_UNKNOWN_ENUM_VALUES));

final CodeGenerator codecGenerator = new CppGenerator(ir, decodeUnknownEnumValues, outputManager);
final CodeGenerator dtoGenerator = new CppDtoGenerator(ir, outputManager);
final CodeGenerator combinedGenerator = () ->
{
codecGenerator.generate();
dtoGenerator.generate();
};

final boolean generateDtos = "true".equals(System.getProperty(GENERATE_CPP_DTOS));
return generateDtos ? combinedGenerator : codecGenerator;
}
},

Expand Down
Loading

0 comments on commit 1a9385d

Please sign in to comment.