Skip to content

Commit 9e67177

Browse files
committed
[Java] Generate minimal set of cases in switch for attribute metadata in C++ codec.
1 parent 6852773 commit 9e67177

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package uk.co.real_logic.sbe.generation.cpp;
1717

18+
import org.agrona.Strings;
1819
import org.agrona.Verify;
1920
import org.agrona.generation.OutputManager;
2021
import uk.co.real_logic.sbe.PrimitiveType;
@@ -2113,25 +2114,38 @@ private static void generateFieldMetaAttributeMethod(
21132114
final String timeUnit = encoding.timeUnit() == null ? "" : encoding.timeUnit();
21142115
final String semanticType = encoding.semanticType() == null ? "" : encoding.semanticType();
21152116

2116-
new Formatter(sb).format("\n" +
2117-
indent + " SBE_NODISCARD static const char * %sMetaAttribute(const MetaAttribute metaAttribute)" +
2118-
" SBE_NOEXCEPT\n" +
2119-
indent + " {\n" +
2120-
indent + " switch (metaAttribute)\n" +
2121-
indent + " {\n" +
2122-
indent + " case MetaAttribute::EPOCH: return \"%s\";\n" +
2123-
indent + " case MetaAttribute::TIME_UNIT: return \"%s\";\n" +
2124-
indent + " case MetaAttribute::SEMANTIC_TYPE: return \"%s\";\n" +
2125-
indent + " case MetaAttribute::PRESENCE: return \"%s\";\n" +
2126-
indent + " }\n\n" +
2117+
sb.append("\n")
2118+
.append(indent).append(" SBE_NODISCARD static const char * ")
2119+
.append(token.name()).append("MetaAttribute(const MetaAttribute metaAttribute) SBE_NOEXCEPT\n")
2120+
.append(indent).append(" {\n")
2121+
.append(indent).append(" switch (metaAttribute)\n")
2122+
.append(indent).append(" {\n");
21272123

2128-
indent + " return \"\";\n" +
2129-
indent + " }\n",
2130-
token.name(),
2131-
epoch,
2132-
timeUnit,
2133-
semanticType,
2134-
encoding.presence().toString().toLowerCase());
2124+
if (!Strings.isEmpty(epoch))
2125+
{
2126+
sb.append(indent)
2127+
.append(" case MetaAttribute::EPOCH: return \"").append(epoch).append("\";\n");
2128+
}
2129+
2130+
if (!Strings.isEmpty(timeUnit))
2131+
{
2132+
sb.append(indent)
2133+
.append(" case MetaAttribute::TIME_UNIT: return \"").append(timeUnit).append("\";\n");
2134+
}
2135+
2136+
if (!Strings.isEmpty(semanticType))
2137+
{
2138+
sb.append(indent)
2139+
.append(" case MetaAttribute::SEMANTIC_TYPE: return \"").append(semanticType)
2140+
.append("\";\n");
2141+
}
2142+
2143+
sb
2144+
.append(indent).append(" case MetaAttribute::PRESENCE: return \"")
2145+
.append(encoding.presence().toString().toLowerCase()).append("\";\n")
2146+
.append(indent).append(" default: return \"\";\n")
2147+
.append(indent).append(" }\n")
2148+
.append(indent).append(" }\n");
21352149
}
21362150

21372151
private static CharSequence generateEnumFieldNotPresentCondition(

0 commit comments

Comments
 (0)