From c8741fcf1156580f6af99bdc605f066063ba1982 Mon Sep 17 00:00:00 2001 From: Sergey Kovalevich Date: Wed, 30 Jan 2019 10:52:28 +0300 Subject: [PATCH] Added std::string_view accessor Added non-const raw access for array like fields --- .../sbe/generation/cpp/CppGenerator.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java index 56b71446bf..4913dd7829 100755 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java @@ -1183,7 +1183,17 @@ private CharSequence generateArrayProperty( token.arrayLength())); sb.append(String.format("\n" + - indent + " const char *%1$s() const\n" + + indent + " const char *%1$s() const SBE_NOEXCEPT\n" + + indent + " {\n" + + "%2$s" + + indent + " return (m_buffer + m_offset + %3$d);\n" + + indent + " }\n", + propertyName, + generateTypeFieldNotPresentCondition(token.version(), indent), + offset)); + + sb.append(String.format("\n" + + indent + " char *%1$s() SBE_NOEXCEPT\n" + indent + " {\n" + "%2$s" + indent + " return (m_buffer + m_offset + %3$d);\n" + @@ -1199,7 +1209,7 @@ private CharSequence generateArrayProperty( indent); sb.append(String.format("\n" + - indent + " %1$s %2$s(const std::uint64_t index) const\n" + + indent + " %1$s %2$s(std::uint64_t index) const\n" + indent + " {\n" + indent + " if (index >= %3$d)\n" + indent + " {\n" + @@ -1222,7 +1232,7 @@ private CharSequence generateArrayProperty( indent); sb.append(String.format("\n" + - indent + " %1$s %2$s(const std::uint64_t index, const %3$s value)\n" + + indent + " %1$s %2$s(std::uint64_t index, %3$s value)\n" + indent + " {\n" + indent + " if (index >= %4$d)\n" + indent + " {\n" + @@ -1238,7 +1248,7 @@ private CharSequence generateArrayProperty( storeValue)); sb.append(String.format("\n" + - indent + " std::uint64_t get%1$s(char *dst, const std::uint64_t length) const\n" + + indent + " std::uint64_t get%1$s(char *dst, std::uint64_t length) const\n" + indent + " {\n" + indent + " if (length > %2$d)\n" + indent + " {\n" + @@ -1255,7 +1265,7 @@ private CharSequence generateArrayProperty( cppTypeName)); sb.append(String.format("\n" + - indent + " %1$s &put%2$s(const char *src)\n" + + indent + " %1$s &put%2$s(const char *src) SBE_NOEXCEPT\n" + indent + " {\n" + indent + " std::memcpy(m_buffer + m_offset + %3$d, src, sizeof(%4$s) * %5$d);\n" + indent + " return *this;\n" + @@ -1279,11 +1289,31 @@ private CharSequence generateArrayProperty( token.arrayLength())); sb.append(String.format("\n" + - indent + " %1$s &put%2$s(const std::string& str)\n" + + indent + " #if __cplusplus >= 201703L\n" + + indent + " std::string_view get%1$sAsStringView() const SBE_NOEXCEPT\n" + + indent + " {\n" + + indent + " std::string_view result(m_buffer + m_offset + %2$d, %3$d);\n" + + indent + " return result;\n" + + indent + " }\n" + + indent + " #endif\n", + toUpperFirstChar(propertyName), + offset, + token.arrayLength())); + + sb.append(String.format("\n" + + indent + " #if __cplusplus >= 201703L\n" + + indent + " %1$s &put%2$s(std::string_view str) SBE_NOEXCEPT\n" + indent + " {\n" + indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" + indent + " return *this;\n" + - indent + " }\n", + indent + " }\n" + + indent + " #else\n" + + indent + " %1$s &put%2$s(const std::string& str) SBE_NOEXCEPT\n" + + indent + " {\n" + + indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" + + indent + " return *this;\n" + + indent + " }\n" + + indent + " #endif\n", containingClassName, toUpperFirstChar(propertyName), offset,