Skip to content

Commit 1808341

Browse files
authored
Merge pull request #631 from ksergey/cpp-simple-type-gen
[c++] Improved field generation
2 parents 9a289d6 + c8741fc commit 1808341

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
@@ -1180,7 +1180,17 @@ private CharSequence generateArrayProperty(
11801180
token.arrayLength()));
11811181

11821182
sb.append(String.format("\n" +
1183-
indent + " const char *%1$s() const\n" +
1183+
indent + " const char *%1$s() const SBE_NOEXCEPT\n" +
1184+
indent + " {\n" +
1185+
"%2$s" +
1186+
indent + " return (m_buffer + m_offset + %3$d);\n" +
1187+
indent + " }\n",
1188+
propertyName,
1189+
generateTypeFieldNotPresentCondition(token.version(), indent),
1190+
offset));
1191+
1192+
sb.append(String.format("\n" +
1193+
indent + " char *%1$s() SBE_NOEXCEPT\n" +
11841194
indent + " {\n" +
11851195
"%2$s" +
11861196
indent + " return (m_buffer + m_offset + %3$d);\n" +
@@ -1196,7 +1206,7 @@ private CharSequence generateArrayProperty(
11961206
indent);
11971207

11981208
sb.append(String.format("\n" +
1199-
indent + " %1$s %2$s(const std::uint64_t index) const\n" +
1209+
indent + " %1$s %2$s(std::uint64_t index) const\n" +
12001210
indent + " {\n" +
12011211
indent + " if (index >= %3$d)\n" +
12021212
indent + " {\n" +
@@ -1218,7 +1228,7 @@ private CharSequence generateArrayProperty(
12181228
indent);
12191229

12201230
sb.append(String.format("\n" +
1221-
indent + " %1$s %2$s(const std::uint64_t index, const %3$s value)\n" +
1231+
indent + " %1$s %2$s(std::uint64_t index, %3$s value)\n" +
12221232
indent + " {\n" +
12231233
indent + " if (index >= %4$d)\n" +
12241234
indent + " {\n" +
@@ -1234,7 +1244,7 @@ private CharSequence generateArrayProperty(
12341244
storeValue));
12351245

12361246
sb.append(String.format("\n" +
1237-
indent + " std::uint64_t get%1$s(char *dst, const std::uint64_t length) const\n" +
1247+
indent + " std::uint64_t get%1$s(char *dst, std::uint64_t length) const\n" +
12381248
indent + " {\n" +
12391249
indent + " if (length > %2$d)\n" +
12401250
indent + " {\n" +
@@ -1251,7 +1261,7 @@ private CharSequence generateArrayProperty(
12511261
cppTypeName));
12521262

12531263
sb.append(String.format("\n" +
1254-
indent + " %1$s &put%2$s(const char *src)\n" +
1264+
indent + " %1$s &put%2$s(const char *src) SBE_NOEXCEPT\n" +
12551265
indent + " {\n" +
12561266
indent + " std::memcpy(m_buffer + m_offset + %3$d, src, sizeof(%4$s) * %5$d);\n" +
12571267
indent + " return *this;\n" +
@@ -1275,11 +1285,31 @@ private CharSequence generateArrayProperty(
12751285
token.arrayLength()));
12761286

12771287
sb.append(String.format("\n" +
1278-
indent + " %1$s &put%2$s(const std::string& str)\n" +
1288+
indent + " #if __cplusplus >= 201703L\n" +
1289+
indent + " std::string_view get%1$sAsStringView() const SBE_NOEXCEPT\n" +
1290+
indent + " {\n" +
1291+
indent + " std::string_view result(m_buffer + m_offset + %2$d, %3$d);\n" +
1292+
indent + " return result;\n" +
1293+
indent + " }\n" +
1294+
indent + " #endif\n",
1295+
toUpperFirstChar(propertyName),
1296+
offset,
1297+
token.arrayLength()));
1298+
1299+
sb.append(String.format("\n" +
1300+
indent + " #if __cplusplus >= 201703L\n" +
1301+
indent + " %1$s &put%2$s(std::string_view str) SBE_NOEXCEPT\n" +
12791302
indent + " {\n" +
12801303
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" +
12811304
indent + " return *this;\n" +
1282-
indent + " }\n",
1305+
indent + " }\n" +
1306+
indent + " #else\n" +
1307+
indent + " %1$s &put%2$s(const std::string& str) SBE_NOEXCEPT\n" +
1308+
indent + " {\n" +
1309+
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" +
1310+
indent + " return *this;\n" +
1311+
indent + " }\n" +
1312+
indent + " #endif\n",
12831313
containingClassName,
12841314
toUpperFirstChar(propertyName),
12851315
offset,

0 commit comments

Comments
 (0)