Skip to content

[C++] Use generated literal as the constant one to be shifted in the bitset code. #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2020
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 @@ -822,60 +822,65 @@ private CharSequence generateChoices(final String bitsetClassName, final List<To
.forEach((token) ->
{
final String choiceName = formatPropertyName(token.name());
final String typeName = cppTypeName(token.encoding().primitiveType());
final PrimitiveType type = token.encoding().primitiveType();
final String typeName = cppTypeName(type);
final String choiceBitPosition = token.encoding().constValue().toString();
final String byteOrderStr = formatByteOrderEncoding(
token.encoding().byteOrder(), token.encoding().primitiveType());
final String byteOrderStr = formatByteOrderEncoding(token.encoding().byteOrder(), type);
final CharSequence constantOne = generateLiteral(type, "1");

new Formatter(sb).format("\n" +
" static bool %1$s(const %2$s bits)\n" +
" {\n" +
" return (bits & (1u << %3$su)) != 0;\n" +
" return (bits & (%4$s << %3$su)) != 0;\n" +
" }\n",
choiceName,
typeName,
choiceBitPosition);
choiceBitPosition,
constantOne);

new Formatter(sb).format("\n" +
" static %2$s %1$s(const %2$s bits, const bool value)\n" +
" {\n" +
" return value ?" +
" static_cast<%2$s>(bits | (1u << %3$su)) : static_cast<%2$s>(bits & ~(1u << %3$su));\n" +
" static_cast<%2$s>(bits | (%4$s << %3$su)) : static_cast<%2$s>(bits & ~(%4$s << %3$su));\n" +
" }\n",
choiceName,
typeName,
choiceBitPosition);
choiceBitPosition,
constantOne);

new Formatter(sb).format("\n" +
" SBE_NODISCARD bool %1$s() const\n" +
" {\n" +
"%2$s" +
" %4$s val;\n" +
" std::memcpy(&val, m_buffer + m_offset, sizeof(%4$s));\n" +
" return (%3$s(val) & (1u << %5$su)) != 0;\n" +
" return (%3$s(val) & (%6$s << %5$su)) != 0;\n" +
" }\n",
choiceName,
generateChoiceNotPresentCondition(token.version()),
byteOrderStr,
typeName,
choiceBitPosition);
choiceBitPosition,
constantOne);

new Formatter(sb).format("\n" +
" %1$s &%2$s(const bool value)\n" +
" {\n" +
" %3$s bits;\n" +
" std::memcpy(&bits, m_buffer + m_offset, sizeof(%3$s));\n" +
" bits = %4$s(value ?" +
" static_cast<%3$s>(%4$s(bits) | (1u << %5$su)) " +
": static_cast<%3$s>(%4$s(bits) & ~(1u << %5$su)));\n" +
" static_cast<%3$s>(%4$s(bits) | (%6$s << %5$su)) " +
": static_cast<%3$s>(%4$s(bits) & ~(%6$s << %5$su)));\n" +
" std::memcpy(m_buffer + m_offset, &bits, sizeof(%3$s));\n" +
" return *this;\n" +
" }\n",
bitsetClassName,
choiceName,
typeName,
byteOrderStr,
choiceBitPosition);
choiceBitPosition,
constantOne);
});

return sb;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2013-2020 Real Logic Limited.
*
* Licensed 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.
*/
package uk.co.real_logic.sbe.generation.cpp;

import org.agrona.generation.StringWriterOutputManager;
import org.junit.jupiter.api.Test;
import uk.co.real_logic.sbe.Tests;
import uk.co.real_logic.sbe.ir.Ir;
import uk.co.real_logic.sbe.xml.IrGenerator;
import uk.co.real_logic.sbe.xml.MessageSchema;
import uk.co.real_logic.sbe.xml.ParserOptions;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;

public class CppGeneratorTest
{
@Test
public void shouldUseGeneratedLiteralForConstantOneWhenGeneratingBitsetCode() throws Exception
{
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
final MessageSchema schema = parse(Tests.getLocalResource("issue827.xml"), 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, outputManager);
generator.generate();

final String source = outputManager.getSource("issue827.FlagsSet").toString();
assertFalse(source.contains("1u << "));
assertTrue(source.contains("UINT64_C(0x1) << "));
}
}
24 changes: 24 additions & 0 deletions sbe-tool/src/test/resources/issue827.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
package="issue827"
id="827"
version="1"
semanticVersion="1.0"
description="issue 661 test case"
byteOrder="bigEndian">
<types>
<set name="FlagsSet" encodingType="uint64">
<choice name="Bit0" description="Bit 0">0</choice>
<choice name="Bit35" description="Bit 35">35</choice>
</set>
<composite name="messageHeader" description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
</types>
<sbe:message name="test" id="1" description="issue 827 test">
<field name="set0" type="FlagsSet" id="1"/>
</sbe:message>
</sbe:messageSchema>