Skip to content

Commit cb95084

Browse files
authored
[C++] Use generated literal as the constant one to be shifted in the bitset code. (#829)
* [C++] Use generated literal as the constant one to be shifted in the bitset code. * [C++] Add dedicated test. * [C++] Simplfiy test.
1 parent 18222bc commit cb95084

File tree

3 files changed

+90
-12
lines changed

3 files changed

+90
-12
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -822,60 +822,65 @@ private CharSequence generateChoices(final String bitsetClassName, final List<To
822822
.forEach((token) ->
823823
{
824824
final String choiceName = formatPropertyName(token.name());
825-
final String typeName = cppTypeName(token.encoding().primitiveType());
825+
final PrimitiveType type = token.encoding().primitiveType();
826+
final String typeName = cppTypeName(type);
826827
final String choiceBitPosition = token.encoding().constValue().toString();
827-
final String byteOrderStr = formatByteOrderEncoding(
828-
token.encoding().byteOrder(), token.encoding().primitiveType());
828+
final String byteOrderStr = formatByteOrderEncoding(token.encoding().byteOrder(), type);
829+
final CharSequence constantOne = generateLiteral(type, "1");
829830

830831
new Formatter(sb).format("\n" +
831832
" static bool %1$s(const %2$s bits)\n" +
832833
" {\n" +
833-
" return (bits & (1u << %3$su)) != 0;\n" +
834+
" return (bits & (%4$s << %3$su)) != 0;\n" +
834835
" }\n",
835836
choiceName,
836837
typeName,
837-
choiceBitPosition);
838+
choiceBitPosition,
839+
constantOne);
838840

839841
new Formatter(sb).format("\n" +
840842
" static %2$s %1$s(const %2$s bits, const bool value)\n" +
841843
" {\n" +
842844
" return value ?" +
843-
" static_cast<%2$s>(bits | (1u << %3$su)) : static_cast<%2$s>(bits & ~(1u << %3$su));\n" +
845+
" static_cast<%2$s>(bits | (%4$s << %3$su)) : static_cast<%2$s>(bits & ~(%4$s << %3$su));\n" +
844846
" }\n",
845847
choiceName,
846848
typeName,
847-
choiceBitPosition);
849+
choiceBitPosition,
850+
constantOne);
848851

849852
new Formatter(sb).format("\n" +
850853
" SBE_NODISCARD bool %1$s() const\n" +
851854
" {\n" +
852855
"%2$s" +
853856
" %4$s val;\n" +
854857
" std::memcpy(&val, m_buffer + m_offset, sizeof(%4$s));\n" +
855-
" return (%3$s(val) & (1u << %5$su)) != 0;\n" +
858+
" return (%3$s(val) & (%6$s << %5$su)) != 0;\n" +
856859
" }\n",
857860
choiceName,
858861
generateChoiceNotPresentCondition(token.version()),
859862
byteOrderStr,
860863
typeName,
861-
choiceBitPosition);
864+
choiceBitPosition,
865+
constantOne);
862866

863867
new Formatter(sb).format("\n" +
864868
" %1$s &%2$s(const bool value)\n" +
865869
" {\n" +
866870
" %3$s bits;\n" +
867871
" std::memcpy(&bits, m_buffer + m_offset, sizeof(%3$s));\n" +
868872
" bits = %4$s(value ?" +
869-
" static_cast<%3$s>(%4$s(bits) | (1u << %5$su)) " +
870-
": static_cast<%3$s>(%4$s(bits) & ~(1u << %5$su)));\n" +
873+
" static_cast<%3$s>(%4$s(bits) | (%6$s << %5$su)) " +
874+
": static_cast<%3$s>(%4$s(bits) & ~(%6$s << %5$su)));\n" +
871875
" std::memcpy(m_buffer + m_offset, &bits, sizeof(%3$s));\n" +
872876
" return *this;\n" +
873877
" }\n",
874878
bitsetClassName,
875879
choiceName,
876880
typeName,
877881
byteOrderStr,
878-
choiceBitPosition);
882+
choiceBitPosition,
883+
constantOne);
879884
});
880885

881886
return sb;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2013-2020 Real Logic Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package uk.co.real_logic.sbe.generation.cpp;
17+
18+
import org.agrona.generation.StringWriterOutputManager;
19+
import org.junit.jupiter.api.Test;
20+
import uk.co.real_logic.sbe.Tests;
21+
import uk.co.real_logic.sbe.ir.Ir;
22+
import uk.co.real_logic.sbe.xml.IrGenerator;
23+
import uk.co.real_logic.sbe.xml.MessageSchema;
24+
import uk.co.real_logic.sbe.xml.ParserOptions;
25+
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;
29+
30+
public class CppGeneratorTest
31+
{
32+
@Test
33+
public void shouldUseGeneratedLiteralForConstantOneWhenGeneratingBitsetCode() throws Exception
34+
{
35+
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
36+
final MessageSchema schema = parse(Tests.getLocalResource("issue827.xml"), options);
37+
final IrGenerator irg = new IrGenerator();
38+
final Ir ir = irg.generate(schema);
39+
final StringWriterOutputManager outputManager = new StringWriterOutputManager();
40+
outputManager.setPackageName(ir.applicableNamespace());
41+
42+
final CppGenerator generator = new CppGenerator(ir, outputManager);
43+
generator.generate();
44+
45+
final String source = outputManager.getSource("issue827.FlagsSet").toString();
46+
assertFalse(source.contains("1u << "));
47+
assertTrue(source.contains("UINT64_C(0x1) << "));
48+
}
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
3+
package="issue827"
4+
id="827"
5+
version="1"
6+
semanticVersion="1.0"
7+
description="issue 661 test case"
8+
byteOrder="bigEndian">
9+
<types>
10+
<set name="FlagsSet" encodingType="uint64">
11+
<choice name="Bit0" description="Bit 0">0</choice>
12+
<choice name="Bit35" description="Bit 35">35</choice>
13+
</set>
14+
<composite name="messageHeader" description="Message identifiers and length of message root">
15+
<type name="blockLength" primitiveType="uint16"/>
16+
<type name="templateId" primitiveType="uint16"/>
17+
<type name="schemaId" primitiveType="uint16"/>
18+
<type name="version" primitiveType="uint16"/>
19+
</composite>
20+
</types>
21+
<sbe:message name="test" id="1" description="issue 827 test">
22+
<field name="set0" type="FlagsSet" id="1"/>
23+
</sbe:message>
24+
</sbe:messageSchema>

0 commit comments

Comments
 (0)