Skip to content

Commit fd81387

Browse files
committed
[Java] add #define for template id in C
1 parent cf61f3a commit fd81387

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/c/CGenerator.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,8 @@ private CharSequence generateMessageFlyweightFunctions(
19601960
" return %2$s;\n" +
19611961
"}\n\n" +
19621962

1963+
"#define %13$s_SBE_TEMPLATE_ID %4$s\n\n" +
1964+
19631965
"SBE_ONE_DEF %3$s %10$s_sbe_template_id(void)\n" +
19641966
"{\n" +
19651967
" return %4$s;\n" +
@@ -2100,7 +2102,23 @@ private CharSequence generateMessageFlyweightFunctions(
21002102
semanticType,
21012103
structName,
21022104
messageHeaderStruct,
2103-
semanticVersion);
2105+
semanticVersion,
2106+
toUppercaseWithUnderscores(structName));
2107+
}
2108+
2109+
private String toUppercaseWithUnderscores(final String camelCaseString)
2110+
{
2111+
final StringBuilder sb = new StringBuilder();
2112+
for (int i = 0; i < camelCaseString.length(); i++)
2113+
{
2114+
final char theChar = camelCaseString.charAt(i);
2115+
if (Character.isUpperCase(theChar))
2116+
{
2117+
sb.append("_");
2118+
}
2119+
sb.append(Character.toUpperCase(theChar));
2120+
}
2121+
return sb.toString();
21042122
}
21052123

21062124
private CharSequence generateFieldFunctions(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2013-2024 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 java.io.InputStream;
27+
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.hamcrest.Matchers.containsString;
30+
import static org.hamcrest.Matchers.not;
31+
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;
32+
33+
class CppGeneratorTest
34+
{
35+
@Test
36+
void shouldUseGeneratedLiteralForConstantOneWhenGeneratingBitsetCode() throws Exception
37+
{
38+
try (InputStream in = Tests.getLocalResource("issue827.xml"))
39+
{
40+
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
41+
final MessageSchema schema = parse(in, options);
42+
final IrGenerator irg = new IrGenerator();
43+
final Ir ir = irg.generate(schema);
44+
final StringWriterOutputManager outputManager = new StringWriterOutputManager();
45+
outputManager.setPackageName(ir.applicableNamespace());
46+
47+
final CppGenerator generator = new CppGenerator(ir, false, outputManager);
48+
generator.generate();
49+
50+
final String source = outputManager.getSource("issue827.FlagsSet").toString();
51+
assertThat(source, not(containsString("1u << ")));
52+
assertThat(source, containsString("UINT64_C(0x1) << "));
53+
}
54+
}
55+
56+
@Test
57+
void shouldUseConstexprWhenInitializingSemanticVersion() throws Exception
58+
{
59+
try (InputStream in = Tests.getLocalResource("code-generation-schema.xml"))
60+
{
61+
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
62+
final MessageSchema schema = parse(in, options);
63+
final IrGenerator irg = new IrGenerator();
64+
final Ir ir = irg.generate(schema);
65+
final StringWriterOutputManager outputManager = new StringWriterOutputManager();
66+
outputManager.setPackageName(ir.applicableNamespace());
67+
68+
final CppGenerator generator = new CppGenerator(ir, false, outputManager);
69+
generator.generate();
70+
71+
final String source = outputManager.getSource("code.generation.test.Car").toString();
72+
assertThat(source, containsString("static constexpr const char* SBE_SEMANTIC_VERSION = \"5.2\""));
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)