Skip to content

[C++] Allow customizing header extension #1054

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -55,6 +55,7 @@ public class CppGenerator implements CodeGenerator
private static final String INDENT = " ";
private static final String TWO_INDENT = INDENT + INDENT;
private static final String THREE_INDENT = TWO_INDENT + INDENT;
private static final String DEFAULT_FILE_EXTENSION = ".h";

private final Ir ir;
private final OutputManager outputManager;
Expand Down Expand Up @@ -109,6 +110,15 @@ public CppGenerator(
this.outputManager = outputManager;
}

private String getFileExtension()
{
if (outputManager instanceof WithFileExtension)
{
return ((WithFileExtension)outputManager).getFileExtension();
}
return DEFAULT_FILE_EXTENSION;
}

/**
* Generate the composites for dealing with the message header.
*
Expand Down Expand Up @@ -1818,7 +1828,7 @@ private CharSequence generateFileHeader(
sb.append("\n");
for (final String incName : typesToInclude)
{
sb.append("#include \"").append(toUpperFirstChar(incName)).append(".h\"\n");
sb.append("#include \"").append(toUpperFirstChar(incName)).append(getFileExtension()).append("\"\n");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
* {@link OutputManager} for managing the creation of C++11 source files as the target of code generation.
* The character encoding for the {@link java.io.Writer} is UTF-8.
*/
public class NamespaceOutputManager implements OutputManager
public class NamespaceOutputManager implements OutputManager, WithFileExtension
{
private String fileExt;
private final File outputDir;

/**
Expand All @@ -51,6 +52,9 @@ public NamespaceOutputManager(final String baseDirName, final String namespaceNa
{
throw new IllegalStateException("Unable to create directory: " + packageDirName);
}

// Defaults to .h to maintain backward compatibility
fileExt = System.getProperty("sbe.cpp.file.extension", ".h");
}

/**
Expand All @@ -65,7 +69,19 @@ public NamespaceOutputManager(final String baseDirName, final String namespaceNa
*/
public Writer createOutput(final String name) throws IOException
{
final File targetFile = new File(outputDir, name + ".h");
final File targetFile = new File(outputDir, name + fileExt);
return Files.newBufferedWriter(targetFile.toPath(), StandardCharsets.UTF_8);
}

@Override
public void withFileExtension(final String fileExtension)
{
this.fileExt = fileExtension;
}

@Override
public String getFileExtension()
{
return this.fileExt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2013-2025 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;

/**
* Abstraction to provide file extension configuration and retrieval.
*/
public interface WithFileExtension
{

/**
* Sets the file extension.
*
* @param fileExtension A valid file extension (e.g. ".hpp")
*/
void withFileExtension(String fileExtension);

/**
* Gets a file extension.
*
* @return the configured file extension.
*/
String getFileExtension();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package uk.co.real_logic.sbe.generation.cpp;

import org.agrona.generation.OutputManager;
import org.agrona.generation.StringWriterOutputManager;
import org.junit.jupiter.api.Test;
import uk.co.real_logic.sbe.Tests;
Expand All @@ -24,11 +25,14 @@
import uk.co.real_logic.sbe.xml.MessageSchema;
import uk.co.real_logic.sbe.xml.ParserOptions;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;

Expand Down Expand Up @@ -140,4 +144,48 @@ void shouldUseConstexprWhenDefiningPrecedenceChecksLookupTables() throws Excepti
assertThat(source, containsString("static constexpr const char *STATE_TRANSITIONS_LOOKUP[] ="));
}
}

@Test
void shouldAllowFileExtensionConfiguration() throws Exception
{
class OutputManagerWithExt extends StringWriterOutputManager implements WithFileExtension {
private String fileExtension;

@Override
public String getFileExtension() {
return fileExtension;
}

@Override
public void withFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}

}

try (InputStream in = Tests.getLocalResource("code-generation-schema.xml"))
{
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
final MessageSchema schema = parse(in, options);
final IrGenerator irg = new IrGenerator();
final Ir ir = irg.generate(schema);
final OutputManagerWithExt outputManager = new OutputManagerWithExt();
outputManager.setPackageName(ir.applicableNamespace());
outputManager.withFileExtension(".hpp");

assertEquals(outputManager.getFileExtension(), ".hpp");

final CppGenerator generator = new CppGenerator(
ir,
false,
PrecedenceChecks.newInstance(new PrecedenceChecks.Context().shouldGeneratePrecedenceChecks(true)),
false,
outputManager);
generator.generate();

final String source = outputManager.getSource("code.generation.test.Car").toString();
assertThat(source, containsString("#include \"MessageHeader.hpp\""));
assertThat(source,containsString("#include \"BoostType.hpp\""));
}
}
}