Skip to content

Commit c8741fc

Browse files
committed
Added std::string_view accessor
Added non-const raw access for array like fields
1 parent 5e27967 commit c8741fc

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,17 @@ private CharSequence generateArrayProperty(
11831183
token.arrayLength()));
11841184

11851185
sb.append(String.format("\n" +
1186-
indent + " const char *%1$s() const\n" +
1186+
indent + " const char *%1$s() const SBE_NOEXCEPT\n" +
1187+
indent + " {\n" +
1188+
"%2$s" +
1189+
indent + " return (m_buffer + m_offset + %3$d);\n" +
1190+
indent + " }\n",
1191+
propertyName,
1192+
generateTypeFieldNotPresentCondition(token.version(), indent),
1193+
offset));
1194+
1195+
sb.append(String.format("\n" +
1196+
indent + " char *%1$s() SBE_NOEXCEPT\n" +
11871197
indent + " {\n" +
11881198
"%2$s" +
11891199
indent + " return (m_buffer + m_offset + %3$d);\n" +
@@ -1199,7 +1209,7 @@ private CharSequence generateArrayProperty(
11991209
indent);
12001210

12011211
sb.append(String.format("\n" +
1202-
indent + " %1$s %2$s(const std::uint64_t index) const\n" +
1212+
indent + " %1$s %2$s(std::uint64_t index) const\n" +
12031213
indent + " {\n" +
12041214
indent + " if (index >= %3$d)\n" +
12051215
indent + " {\n" +
@@ -1222,7 +1232,7 @@ private CharSequence generateArrayProperty(
12221232
indent);
12231233

12241234
sb.append(String.format("\n" +
1225-
indent + " %1$s %2$s(const std::uint64_t index, const %3$s value)\n" +
1235+
indent + " %1$s %2$s(std::uint64_t index, %3$s value)\n" +
12261236
indent + " {\n" +
12271237
indent + " if (index >= %4$d)\n" +
12281238
indent + " {\n" +
@@ -1238,7 +1248,7 @@ private CharSequence generateArrayProperty(
12381248
storeValue));
12391249

12401250
sb.append(String.format("\n" +
1241-
indent + " std::uint64_t get%1$s(char *dst, const std::uint64_t length) const\n" +
1251+
indent + " std::uint64_t get%1$s(char *dst, std::uint64_t length) const\n" +
12421252
indent + " {\n" +
12431253
indent + " if (length > %2$d)\n" +
12441254
indent + " {\n" +
@@ -1255,7 +1265,7 @@ private CharSequence generateArrayProperty(
12551265
cppTypeName));
12561266

12571267
sb.append(String.format("\n" +
1258-
indent + " %1$s &put%2$s(const char *src)\n" +
1268+
indent + " %1$s &put%2$s(const char *src) SBE_NOEXCEPT\n" +
12591269
indent + " {\n" +
12601270
indent + " std::memcpy(m_buffer + m_offset + %3$d, src, sizeof(%4$s) * %5$d);\n" +
12611271
indent + " return *this;\n" +
@@ -1279,11 +1289,31 @@ private CharSequence generateArrayProperty(
12791289
token.arrayLength()));
12801290

12811291
sb.append(String.format("\n" +
1282-
indent + " %1$s &put%2$s(const std::string& str)\n" +
1292+
indent + " #if __cplusplus >= 201703L\n" +
1293+
indent + " std::string_view get%1$sAsStringView() const SBE_NOEXCEPT\n" +
1294+
indent + " {\n" +
1295+
indent + " std::string_view result(m_buffer + m_offset + %2$d, %3$d);\n" +
1296+
indent + " return result;\n" +
1297+
indent + " }\n" +
1298+
indent + " #endif\n",
1299+
toUpperFirstChar(propertyName),
1300+
offset,
1301+
token.arrayLength()));
1302+
1303+
sb.append(String.format("\n" +
1304+
indent + " #if __cplusplus >= 201703L\n" +
1305+
indent + " %1$s &put%2$s(std::string_view str) SBE_NOEXCEPT\n" +
12831306
indent + " {\n" +
12841307
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" +
12851308
indent + " return *this;\n" +
1286-
indent + " }\n",
1309+
indent + " }\n" +
1310+
indent + " #else\n" +
1311+
indent + " %1$s &put%2$s(const std::string& str) SBE_NOEXCEPT\n" +
1312+
indent + " {\n" +
1313+
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" +
1314+
indent + " return *this;\n" +
1315+
indent + " }\n" +
1316+
indent + " #endif\n",
12871317
containingClassName,
12881318
toUpperFirstChar(propertyName),
12891319
offset,

0 commit comments

Comments
 (0)