Skip to content

Commit

Permalink
[Java] Provide raw access to field containing a bitset via a getRaw()…
Browse files Browse the repository at this point in the history
… method on the generated bitset decoder. Issue #893.
  • Loading branch information
mjpt777 committed Jul 10, 2022
1 parent b0f91d5 commit 8aa6cae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,16 @@ private void generateVarDataDescriptors(

private void generateChoiceSet(final List<Token> tokens) throws IOException
{
final String bitSetName = formatClassName(tokens.get(0).applicableTypeName());
final Token token = tokens.get(0);
final String bitSetName = formatClassName(token.applicableTypeName());

try (Writer out = outputManager.createOutput(bitSetName))
{
out.append(generateFileHeader(ir.namespaces(), bitSetName, null));
out.append(generateClassDeclaration(bitSetName));
out.append(generateFixedFlyweightCode(bitSetName, tokens.get(0).encodedLength()));
out.append(generateFixedFlyweightCode(bitSetName, token.encodedLength()));

final Encoding encoding = token.encoding();
new Formatter(out).format("\n" +
" %1$s &clear()\n" +
" {\n" +
Expand All @@ -722,7 +724,7 @@ private void generateChoiceSet(final List<Token> tokens) throws IOException
" return *this;\n" +
" }\n",
bitSetName,
cppTypeName(tokens.get(0).encoding().primitiveType()));
cppTypeName(encoding.primitiveType()));

new Formatter(out).format("\n" +
" SBE_NODISCARD bool isEmpty() const\n" +
Expand All @@ -731,7 +733,7 @@ private void generateChoiceSet(final List<Token> tokens) throws IOException
" std::memcpy(&val, m_buffer + m_offset, sizeof(%1$s));\n" +
" return 0 == val;\n" +
" }\n",
cppTypeName(tokens.get(0).encoding().primitiveType()));
cppTypeName(encoding.primitiveType()));

new Formatter(out).format("\n" +
" SBE_NODISCARD %1$s rawValue() const\n" +
Expand All @@ -740,7 +742,7 @@ private void generateChoiceSet(final List<Token> tokens) throws IOException
" std::memcpy(&val, m_buffer + m_offset, sizeof(%1$s));\n" +
" return val;\n" +
" }\n",
cppTypeName(tokens.get(0).encoding().primitiveType()));
cppTypeName(encoding.primitiveType()));

new Formatter(out).format("\n" +
" %1$s &rawValue(%2$s value)\n" +
Expand All @@ -749,7 +751,7 @@ private void generateChoiceSet(final List<Token> tokens) throws IOException
" return *this;\n" +
" }\n",
bitSetName,
cppTypeName(tokens.get(0).encoding().primitiveType()));
cppTypeName(encoding.primitiveType()));

out.append(generateChoices(bitSetName, tokens.subList(1, tokens.size() - 1)));
out.append(generateChoicesDisplay(bitSetName, tokens.subList(1, tokens.size() - 1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,23 +1185,34 @@ private void generateBitSet(final List<Token> tokens) throws IOException
final String bitSetName = token.applicableTypeName();
final String decoderName = decoderName(bitSetName);
final String encoderName = encoderName(bitSetName);
final List<Token> messageBody = getMessageBody(tokens);
final List<Token> choiceList = tokens.subList(1, tokens.size() - 1);
final String implementsString = implementsInterface(Flyweight.class.getSimpleName());

try (Writer out = outputManager.createOutput(decoderName))
{
final Encoding encoding = token.encoding();
generateFixedFlyweightHeader(out, token, decoderName, implementsString, readOnlyBuffer, fqReadOnlyBuffer);
out.append(generateChoiceIsEmpty(token.encoding().primitiveType()));
generateChoiceDecoders(out, messageBody);
out.append(generateChoiceDisplay(messageBody));
out.append(generateChoiceIsEmpty(encoding.primitiveType()));

new Formatter(out).format(
"\n" +
" public %s getRaw()\n" +
" {\n" +
" return %s;\n" +
" }\n",
primitiveTypeName(token),
generateGet(encoding.primitiveType(), "offset", byteOrderString(encoding)));

generateChoiceDecoders(out, choiceList);
out.append(generateChoiceDisplay(choiceList));
out.append("}\n");
}

try (Writer out = outputManager.createOutput(encoderName))
{
generateFixedFlyweightHeader(out, token, encoderName, implementsString, mutableBuffer, fqMutableBuffer);
generateChoiceClear(out, encoderName, token);
generateChoiceEncoders(out, encoderName, messageBody);
generateChoiceEncoders(out, encoderName, choiceList);
out.append("}\n");
}
}
Expand Down Expand Up @@ -1244,10 +1255,11 @@ private void generateEnum(final List<Token> tokens) throws IOException
out.append(generateEnumFileHeader(ir.applicableNamespace()));
out.append(generateEnumDeclaration(enumName, enumToken));

out.append(generateEnumValues(getMessageBody(tokens), generateLiteral(encoding.primitiveType(), nullVal)));
final List<Token> valuesList = tokens.subList(1, tokens.size() - 1);
out.append(generateEnumValues(valuesList, generateLiteral(encoding.primitiveType(), nullVal)));
out.append(generateEnumBody(enumToken, enumName));

out.append(generateEnumLookupMethod(getMessageBody(tokens), enumName, nullVal));
out.append(generateEnumLookupMethod(valuesList, enumName, nullVal));

out.append("}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import static uk.co.real_logic.sbe.ir.GenerationUtil.collectFields;
import static uk.co.real_logic.sbe.ir.GenerationUtil.collectGroups;
import static uk.co.real_logic.sbe.ir.GenerationUtil.collectVarData;
import static uk.co.real_logic.sbe.ir.GenerationUtil.getMessageBody;
import static uk.co.real_logic.sbe.ir.Signal.BEGIN_ENUM;
import static uk.co.real_logic.sbe.ir.Signal.BEGIN_SET;

Expand Down Expand Up @@ -137,7 +136,7 @@ public void generate() throws IOException
{
final Token msgToken = tokens.get(0);
final String codecModName = codecModName(msgToken.name());
final List<Token> messageBody = getMessageBody(tokens);
final List<Token> messageBody = tokens.subList(1, tokens.size() - 1);

int i = 0;
final List<Token> fields = new ArrayList<>();
Expand Down Expand Up @@ -1175,7 +1174,7 @@ private static void generateEnum(
{
final String originalEnumName = enumTokens.get(0).applicableTypeName();
final String enumRustName = formatStructName(originalEnumName);
final List<Token> messageBody = getMessageBody(enumTokens);
final List<Token> messageBody = enumTokens.subList(1, enumTokens.size() - 1);
if (messageBody.isEmpty())
{
throw new IllegalArgumentException("No valid values provided for enum " + originalEnumName);
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.25.4-SNAPSHOT
1.26.0-SNAPSHOT

0 comments on commit 8aa6cae

Please sign in to comment.